11import { describe , test , expect } from 'bun:test'
2+ import { withTimeoutOr } from '@codebuff/common/util/promise'
23
34import {
4- withTimeout ,
55 getGlobalOscTimeout ,
66 getQueryOscTimeout ,
77} from '../terminal-color-detection'
@@ -13,14 +13,14 @@ import {
1313
1414describe ( 'OSC Timeout Protection Scenarios' , ( ) => {
1515 describe ( 'Scenario 1: Terminal ignores OSC queries entirely' , ( ) => {
16- test ( 'withTimeout returns fallback value when promise never resolves' , async ( ) => {
16+ test ( 'withTimeoutOr returns fallback value when promise never resolves' , async ( ) => {
1717 // Simulate a terminal that ignores the OSC query completely
1818 const neverResolvingPromise = new Promise < string > ( ( ) => {
1919 // This promise never resolves, simulating a non-responsive terminal
2020 } )
2121
2222 const startTime = Date . now ( )
23- const result = await withTimeout ( neverResolvingPromise , 100 , 'fallback' )
23+ const result = await withTimeoutOr ( neverResolvingPromise , 100 , 'fallback' )
2424 const elapsed = Date . now ( ) - startTime
2525
2626 expect ( result ) . toBe ( 'fallback' )
@@ -31,27 +31,27 @@ describe('OSC Timeout Protection Scenarios', () => {
3131 test ( 'returns null theme when detection hangs' , async ( ) => {
3232 const hangingDetection = new Promise < 'dark' | 'light' | null > ( ( ) => { } )
3333
34- const result = await withTimeout ( hangingDetection , 50 , null )
34+ const result = await withTimeoutOr ( hangingDetection , 50 , null )
3535 expect ( result ) . toBeNull ( )
3636 } )
3737 } )
3838
3939 describe ( 'Scenario 2: Terminal responds very slowly' , ( ) => {
40- test ( 'withTimeout returns fallback for slow responses' , async ( ) => {
40+ test ( 'withTimeoutOr returns fallback for slow responses' , async ( ) => {
4141 const slowPromise = new Promise < string > ( ( resolve ) => {
4242 setTimeout ( ( ) => resolve ( 'slow-response' ) , 500 )
4343 } )
4444
45- const result = await withTimeout ( slowPromise , 100 , 'timeout-fallback' )
45+ const result = await withTimeoutOr ( slowPromise , 100 , 'timeout-fallback' )
4646 expect ( result ) . toBe ( 'timeout-fallback' )
4747 } )
4848
49- test ( 'withTimeout returns actual value for responses within timeout' , async ( ) => {
49+ test ( 'withTimeoutOr returns actual value for responses within timeout' , async ( ) => {
5050 const fastPromise = new Promise < string > ( ( resolve ) => {
5151 setTimeout ( ( ) => resolve ( 'fast-response' ) , 10 )
5252 } )
5353
54- const result = await withTimeout ( fastPromise , 100 , 'timeout-fallback' )
54+ const result = await withTimeoutOr ( fastPromise , 100 , 'timeout-fallback' )
5555 expect ( result ) . toBe ( 'fast-response' )
5656 } )
5757 } )
@@ -71,7 +71,7 @@ describe('OSC Timeout Protection Scenarios', () => {
7171 test ( 'multiple timeouts racing dont cause issues' , async ( ) => {
7272 // Create multiple promises that might complete at similar times
7373 const promises = Array . from ( { length : 10 } , ( _ , i ) =>
74- withTimeout (
74+ withTimeoutOr (
7575 new Promise < number > ( ( resolve ) =>
7676 setTimeout ( ( ) => resolve ( i ) , Math . random ( ) * 100 ) ,
7777 ) ,
@@ -89,17 +89,17 @@ describe('OSC Timeout Protection Scenarios', () => {
8989 } )
9090
9191 test ( 'cleanup happens even on timeout' , async ( ) => {
92- // This test verifies that withTimeout properly cleans up its own timeout
92+ // This test verifies that withTimeoutOr properly cleans up its own timeout
9393 // even when the underlying promise doesn't resolve
9494 const neverResolves = new Promise < string > ( ( ) => {
9595 // This promise intentionally never resolves
9696 } )
9797
9898 // This should timeout and return the fallback value
99- const result = await withTimeout ( neverResolves , 50 , 'timeout' )
99+ const result = await withTimeoutOr ( neverResolves , 50 , 'timeout' )
100100 expect ( result ) . toBe ( 'timeout' )
101101
102- // Note: withTimeout cleans up its own setTimeout via the finally block
102+ // Note: withTimeoutOr cleans up its own setTimeout via the finally block
103103 // The underlying promise's resources would need to be cleaned up separately
104104 } )
105105 } )
@@ -125,34 +125,34 @@ describe('OSC Timeout Protection Scenarios', () => {
125125 } )
126126
127127 describe ( 'Scenario 6: Error handling' , ( ) => {
128- test ( 'rejected promises propagate through withTimeout ' , async ( ) => {
128+ test ( 'rejected promises propagate through withTimeoutOr ' , async ( ) => {
129129 const rejectingPromise = Promise . reject ( new Error ( 'test error' ) )
130130
131131 await expect (
132- withTimeout ( rejectingPromise , 100 , 'fallback' ) ,
132+ withTimeoutOr ( rejectingPromise , 100 , 'fallback' ) ,
133133 ) . rejects . toThrow ( 'test error' )
134134 } )
135135
136136 test ( 'timeout value can be any type' , async ( ) => {
137137 const hangingPromise = new Promise < { theme : string } > ( ( ) => { } )
138138
139- const objectResult = await withTimeout ( hangingPromise , 50 , {
139+ const objectResult = await withTimeoutOr ( hangingPromise , 50 , {
140140 theme : 'default' ,
141141 } )
142142 expect ( objectResult ) . toEqual ( { theme : 'default' } )
143143
144144 const hangingPromise2 = new Promise < number [ ] > ( ( ) => { } )
145- const arrayResult = await withTimeout ( hangingPromise2 , 50 , [ 1 , 2 , 3 ] )
145+ const arrayResult = await withTimeoutOr ( hangingPromise2 , 50 , [ 1 , 2 , 3 ] )
146146 expect ( arrayResult ) . toEqual ( [ 1 , 2 , 3 ] )
147147 } )
148148 } )
149149
150150 describe ( 'Scenario 7: Stacked timeouts (defense in depth)' , ( ) => {
151151 test ( 'inner timeout fires before outer timeout' , async ( ) => {
152152 const innerPromise = new Promise < string > ( ( ) => { } )
153- const innerResult = withTimeout ( innerPromise , 30 , 'inner-timeout' )
153+ const innerResult = withTimeoutOr ( innerPromise , 30 , 'inner-timeout' )
154154
155- const outerPromise = withTimeout ( innerResult , 100 , 'outer-timeout' )
155+ const outerPromise = withTimeoutOr ( innerResult , 100 , 'outer-timeout' )
156156
157157 const result = await outerPromise
158158 // Inner timeout should fire first
@@ -165,7 +165,7 @@ describe('OSC Timeout Protection Scenarios', () => {
165165 // This never resolves, simulating broken inner timeout
166166 } )
167167
168- const result = await withTimeout ( brokenInnerPromise , 50 , 'outer-saved-us' )
168+ const result = await withTimeoutOr ( brokenInnerPromise , 50 , 'outer-saved-us' )
169169 expect ( result ) . toBe ( 'outer-saved-us' )
170170 } )
171171 } )
0 commit comments