1+ import { once } from 'node:events' ;
12import { PassThrough , Writable } from 'node:stream' ;
23import { alternateBuffer , cursor , settings } from '@bomb.sh/tty' ;
3- import { describe , expect , test } from 'vitest' ;
4+ import { describe , expect , test , vi } from 'vitest' ;
45import { createUI } from './ui.ts' ;
56
67const terminalSettings = settings ( cursor ( false ) , alternateBuffer ( ) ) ;
78const apply = Buffer . from ( terminalSettings . apply ) . toString ( 'utf8' ) ;
89const restore = Buffer . from ( terminalSettings . revert ) . toString ( 'utf8' ) ;
910
1011describe ( 'UI lifecycle' , ( ) => {
11- test ( 'restores terminal settings when main was never called' , async ( ) => {
12+ test ( 'does not change terminal settings when main was never called' , async ( ) => {
1213 using io = setup ( ) ;
13- const { writes } = io ;
14- {
15- await using _ui = await createUI ( io ) ;
16- expect ( writes ) . toEqual ( [ apply ] ) ;
17- }
14+ const controller = new AbortController ( ) ;
15+ await createUI ( { ...io , signal : controller . signal } ) ;
16+ expect ( io . writes ) . toEqual ( [ ] ) ;
1817
19- expect ( writes ) . toEqual ( [ apply , restore ] ) ;
18+ controller . abort ( ) ;
19+ await nextTurn ( ) ;
20+ expect ( io . writes ) . toEqual ( [ ] ) ;
21+ } ) ;
22+
23+ test ( 'does not start when the signal is already aborted' , async ( ) => {
24+ using io = setup ( ) ;
25+ const controller = new AbortController ( ) ;
26+ controller . abort ( ) ;
27+ const ui = await createUI ( { ...io , signal : controller . signal } ) ;
28+ ui . document . documentElement . appendChild ( ui . document . createTextNode ( 'unused' ) ) ;
29+
30+ await ui . main ( ) ;
31+ await ui . main ( ) ;
32+ expect ( io . writes ) . toEqual ( [ ] ) ;
2033 } ) ;
2134
2235 test ( 'repeated main calls return the same running task' , async ( ) => {
2336 using io = setup ( ) ;
2437 const { writes } = io ;
25- {
26- await using resource = await createUI ( io ) ;
27- expect ( resource . main ( ) ) . toBe ( resource . main ( ) ) ;
38+ const controller = new AbortController ( ) ;
39+ const ui = await createUI ( { ...io , signal : controller . signal } ) ;
40+ const running = ui . main ( ) ;
41+ try {
42+ expect ( ui . main ( ) ) . toBe ( running ) ;
43+ controller . abort ( ) ;
44+ expect ( ui . main ( ) ) . toBe ( running ) ;
45+ } finally {
46+ controller . abort ( ) ;
47+ await running ;
2848 }
2949
50+ expect ( ui . main ( ) ) . toBe ( running ) ;
3051 expect ( writes ) . toEqual ( [ apply , restore ] ) ;
3152 } ) ;
3253
33- test ( 'disposal waits for a second output write before restoring the terminal ' , async ( ) => {
54+ test ( 'main waits for pending output and terminal restoration after abort ' , async ( ) => {
3455 const output = Promise . withResolvers < void > ( ) ;
56+ const restoration = Promise . withResolvers < void > ( ) ;
3557 let frames = 0 ;
3658 using io = setup ( ( chunk , complete ) => {
37- if ( chunk === apply || chunk === restore ) {
59+ if ( chunk === apply ) {
3860 complete ( ) ;
61+ } else if ( chunk === restore ) {
62+ void restoration . promise . then ( ( ) => complete ( ) ) ;
3963 } else if ( ++ frames === 2 ) {
4064 void output . promise . then ( ( ) => complete ( ) ) ;
4165 } else {
4266 complete ( ) ;
4367 }
4468 } ) ;
4569 const { writes } = io ;
46- const ui = await createUI ( io ) ;
47- let disposing : Promise < void > | undefined ;
70+ const controller = new AbortController ( ) ;
71+ const ui = await createUI ( { ...io , signal : controller . signal } ) ;
72+ const running = ui . main ( ) ;
73+ let settled = false ;
74+ void running . then (
75+ ( ) => {
76+ settled = true ;
77+ } ,
78+ ( ) => {
79+ settled = true ;
80+ } ,
81+ ) ;
4882
4983 try {
50- void ui . main ( ) ;
5184 const { document } = ui ;
5285 document . documentElement . appendChild ( document . createTextNode ( 'first' ) ) ;
5386 await nextTurn ( ) ;
@@ -57,56 +90,76 @@ describe('UI lifecycle', () => {
5790 await nextTurn ( ) ;
5891 expect ( frames ) . toBe ( 2 ) ;
5992
60- disposing = Promise . resolve ( ui [ Symbol . asyncDispose ] ( ) ) ;
61- let settled = false ;
62- void disposing . then (
63- ( ) => {
64- settled = true ;
65- } ,
66- ( ) => {
67- settled = true ;
68- } ,
69- ) ;
93+ controller . abort ( ) ;
94+ expect ( ui . main ( ) ) . toBe ( running ) ;
7095 await nextTurn ( ) ;
7196 expect ( settled ) . toBe ( false ) ;
7297 expect ( writes ) . not . toContain ( restore ) ;
7398
7499 output . resolve ( ) ;
75- await disposing ;
100+ await nextTurn ( ) ;
76101 expect ( writes . at ( - 1 ) ) . toBe ( restore ) ;
102+ expect ( settled ) . toBe ( false ) ;
103+
104+ restoration . resolve ( ) ;
105+ await running ;
106+ expect ( ui . main ( ) ) . toBe ( running ) ;
77107 expect ( frames ) . toBe ( 2 ) ;
78108 } finally {
109+ controller . abort ( ) ;
79110 output . resolve ( ) ;
80- await ( disposing ?? ui [ Symbol . asyncDispose ] ( ) ) ;
111+ restoration . resolve ( ) ;
112+ await running ;
81113 }
82114 } ) ;
83115
84- test ( 'disposal propagates a render failure after restoring terminal settings' , async ( ) => {
116+ test ( 'main propagates a render failure after restoring terminal settings' , async ( ) => {
85117 using io = setup ( ) ;
86118 const { writes } = io ;
87- const ui = await createUI ( io ) ;
119+ const controller = new AbortController ( ) ;
120+ const ui = await createUI ( { ...io , signal : controller . signal } ) ;
88121 const failure = new TestRenderError ( ) ;
89122 const text = ui . document . createTextNode ( 'unreadable' ) ;
90123 Object . defineProperty ( text , 'data' , {
91124 get ( ) {
92125 throw failure ;
93126 } ,
94127 } ) ;
95- let disposing : Promise < void > | undefined ;
128+ const running = ui . main ( ) ;
96129
97130 try {
98- const failed = expect ( ui . main ( ) ) . rejects . toBe ( failure ) ;
131+ const failed = expect ( running ) . rejects . toBe ( failure ) ;
99132 ui . document . documentElement . appendChild ( text ) ;
100133 await failed ;
101134
102- disposing = Promise . resolve ( ui [ Symbol . asyncDispose ] ( ) ) ;
103- await expect ( disposing ) . rejects . toBe ( failure ) ;
135+ controller . abort ( ) ;
136+ expect ( ui . main ( ) ) . toBe ( running ) ;
137+ await expect ( ui . main ( ) ) . rejects . toBe ( failure ) ;
104138 expect ( writes ) . toEqual ( [ apply , restore ] ) ;
105139 } finally {
106- // Manually own the expected failure; never dispose the resource twice.
107- if ( ! disposing ) {
108- await Promise . resolve ( ui [ Symbol . asyncDispose ] ( ) ) . catch ( ( ) => { } ) ;
109- }
140+ controller . abort ( ) ;
141+ await running . catch ( ( ) => { } ) ;
142+ }
143+ } ) ;
144+
145+ test ( 'rejects setup failure without attempting terminal restoration' , async ( ) => {
146+ const failure = new Error ( 'setup failed' ) ;
147+ using io = setup ( ( _chunk , complete ) => complete ( failure ) ) ;
148+ const controller = new AbortController ( ) ;
149+ const ui = await createUI ( { ...io , signal : controller . signal } ) ;
150+ const write = vi . spyOn ( io . output , 'write' ) ;
151+ const outputError = once ( io . output , 'error' ) ;
152+ const running = ui . main ( ) ;
153+
154+ try {
155+ await expect ( running ) . rejects . toBe ( failure ) ;
156+ await expect ( outputError ) . resolves . toEqual ( [ failure ] ) ;
157+ expect ( write ) . toHaveBeenCalledTimes ( 1 ) ;
158+ expect ( io . writes ) . toEqual ( [ apply ] ) ;
159+ } finally {
160+ controller . abort ( ) ;
161+ write . mockRestore ( ) ;
162+ await running . catch ( ( ) => { } ) ;
110163 }
111164 } ) ;
112165} ) ;
0 commit comments