@@ -36,6 +36,7 @@ import { verifyGoogleChatRequest } from "./auth.js";
3636
3737afterEach ( ( ) => {
3838 vi . restoreAllMocks ( ) ;
39+ vi . useRealTimers ( ) ;
3940 vi . unstubAllGlobals ( ) ;
4041} ) ;
4142
@@ -177,21 +178,29 @@ async function verifyOutcomeWithin(
177178 | { status : "rejected" ; error : unknown }
178179 | { status : "pending" }
179180> {
180- return await Promise . race ( [
181- promise . then (
182- ( value ) => ( { status : "resolved" as const , value } ) ,
183- ( error : unknown ) => ( { status : "rejected" as const , error } ) ,
184- ) ,
185- new Promise < { status : "pending" } > ( ( resolve ) => {
186- setTimeout ( ( ) => resolve ( { status : "pending" as const } ) , timeoutMs ) ;
187- } ) ,
188- ] ) ;
181+ let timeout : ReturnType < typeof setTimeout > | undefined ;
182+ try {
183+ return await Promise . race ( [
184+ promise . then (
185+ ( value ) => ( { status : "resolved" as const , value } ) ,
186+ ( error : unknown ) => ( { status : "rejected" as const , error } ) ,
187+ ) ,
188+ new Promise < { status : "pending" } > ( ( resolve ) => {
189+ timeout = setTimeout ( ( ) => resolve ( { status : "pending" as const } ) , timeoutMs ) ;
190+ } ) ,
191+ ] ) ;
192+ } finally {
193+ if ( timeout !== undefined ) {
194+ clearTimeout ( timeout ) ;
195+ }
196+ }
189197}
190198
191199describe ( "googlechat cert-fetch — real guard proof" , ( ) => {
192200 it ( "times out a stalled cert fetch through the real production guard" , async ( ) => {
193201 const realFetch = globalThis . fetch ;
194202 const server = await startHangingLoopbackServer ( ) ;
203+ vi . useFakeTimers ( { toFake : [ "setTimeout" ] } ) ;
195204 const certFetchTimeout = captureCertFetchTimeout ( ) ;
196205 let pendingVerify : Promise < { ok : boolean ; reason ?: string } > | undefined ;
197206
@@ -219,10 +228,19 @@ describe("googlechat cert-fetch — real guard proof", () => {
219228 expect ( certFetchTimeout . scheduled ( ) ) . toBe ( true ) ;
220229 expect ( fetchMock . mock . calls [ 0 ] ?. [ 1 ] ?. signal ) . toBeInstanceOf ( AbortSignal ) ;
221230
222- // Mutation control: without firing the deadline the verification stays
223- // pending, proving the rejection below comes from the real timeout.
224- const beforeFire = await verifyOutcomeWithin ( pendingVerify , 500 ) ;
225- expect ( beforeFire . status ) . toBe ( "pending" ) ;
231+ // Mutation control: advance the original 500ms pending window without
232+ // sleeping, then prove only the production deadline can settle it.
233+ let verifySettled = false ;
234+ void pendingVerify . then (
235+ ( ) => {
236+ verifySettled = true ;
237+ } ,
238+ ( ) => {
239+ verifySettled = true ;
240+ } ,
241+ ) ;
242+ await vi . advanceTimersByTimeAsync ( 500 ) ;
243+ expect ( verifySettled ) . toBe ( false ) ;
226244
227245 // Fire the captured production deadline; the composed signal aborts the
228246 // in-flight loopback request and verifyGoogleChatRequest returns ok:false.
0 commit comments