@@ -40,8 +40,20 @@ function guardResponse(body: unknown, status = 200, url = DEVICE_CODE_URL) {
4040afterEach ( ( ) => {
4141 mocks . fetchWithSsrFGuard . mockReset ( ) ;
4242 vi . restoreAllMocks ( ) ;
43+ vi . useRealTimers ( ) ;
4344} ) ;
4445
46+ function runDeviceFlowAfterFirstPoll (
47+ io : Parameters < typeof runGitHubCopilotDeviceFlow > [ 0 ] ,
48+ domain ?: string ,
49+ ) {
50+ vi . useFakeTimers ( ) ;
51+ const result = runGitHubCopilotDeviceFlow ( io , domain ) ;
52+ return Promise . all ( [ result , vi . advanceTimersByTimeAsync ( 5_000 ) ] ) . then (
53+ ( [ flowResult ] ) => flowResult ,
54+ ) ;
55+ }
56+
4557describe ( "runGitHubCopilotDeviceFlow — normal flow" , ( ) => {
4658 it ( "bounds requests and returns authorized status and access token on successful flow" , async ( ) => {
4759 let callIdx = 0 ;
@@ -64,7 +76,7 @@ describe("runGitHubCopilotDeviceFlow — normal flow", () => {
6476 } ) ;
6577
6678 const showCode = vi . fn ( async ( ) => { } ) ;
67- const result = await runGitHubCopilotDeviceFlow ( { showCode, signal : controller . signal } ) ;
79+ const result = await runDeviceFlowAfterFirstPoll ( { showCode, signal : controller . signal } ) ;
6880
6981 expect ( result ) . toEqual ( { status : "authorized" , accessToken : "ghu_tok_xyz" } ) ;
7082 expect ( showCode ) . toHaveBeenCalledWith ( {
@@ -86,7 +98,7 @@ describe("runGitHubCopilotDeviceFlow — normal flow", () => {
8698 return guardResponse ( { error : "access_denied" } , 200 , ACCESS_TOKEN_URL ) ;
8799 } ) ;
88100
89- const result = await runGitHubCopilotDeviceFlow ( {
101+ const result = await runDeviceFlowAfterFirstPoll ( {
90102 showCode : vi . fn ( async ( ) => { } ) ,
91103 } ) ;
92104 expect ( result ) . toEqual ( { status : "access_denied" } ) ;
@@ -102,7 +114,7 @@ describe("runGitHubCopilotDeviceFlow — normal flow", () => {
102114 return guardResponse ( { error : "expired_token" } , 200 , ACCESS_TOKEN_URL ) ;
103115 } ) ;
104116
105- const result = await runGitHubCopilotDeviceFlow ( {
117+ const result = await runDeviceFlowAfterFirstPoll ( {
106118 showCode : vi . fn ( async ( ) => { } ) ,
107119 } ) ;
108120 expect ( result ) . toEqual ( { status : "expired" } ) ;
@@ -128,10 +140,25 @@ describe("runGitHubCopilotDeviceFlow — HTTP error propagation", () => {
128140 return guardResponse ( { } , 500 , ACCESS_TOKEN_URL ) ;
129141 } ) ;
130142
131- await expect ( runGitHubCopilotDeviceFlow ( { showCode : vi . fn ( async ( ) => { } ) } ) ) . rejects . toThrow (
143+ await expect ( runDeviceFlowAfterFirstPoll ( { showCode : vi . fn ( async ( ) => { } ) } ) ) . rejects . toThrow (
132144 "GitHub device token failed: HTTP 500" ,
133145 ) ;
134146 } ) ;
147+
148+ it ( "rejects a malformed access token response" , async ( ) => {
149+ let callIdx = 0 ;
150+ mocks . fetchWithSsrFGuard . mockImplementation ( async ( ) => {
151+ callIdx += 1 ;
152+ if ( callIdx === 1 ) {
153+ return guardResponse ( VALID_DEVICE_CODE_BODY ) ;
154+ }
155+ return guardResponse ( { access_token : null , token_type : "bearer" } , 200 , ACCESS_TOKEN_URL ) ;
156+ } ) ;
157+
158+ await expect ( runDeviceFlowAfterFirstPoll ( { showCode : vi . fn ( async ( ) => { } ) } ) ) . rejects . toThrow (
159+ "GitHub device flow returned an invalid access token" ,
160+ ) ;
161+ } ) ;
135162} ) ;
136163
137164describe ( "postGitHubDeviceFlowForm — response size bound" , ( ) => {
@@ -208,7 +235,7 @@ describe("postGitHubDeviceFlowForm — response size bound", () => {
208235 } ;
209236 } ) ;
210237
211- await expect ( runGitHubCopilotDeviceFlow ( { showCode : vi . fn ( async ( ) => { } ) } ) ) . rejects . toThrow (
238+ await expect ( runDeviceFlowAfterFirstPoll ( { showCode : vi . fn ( async ( ) => { } ) } ) ) . rejects . toThrow (
212239 "github-copilot.device-flow" ,
213240 ) ;
214241
@@ -246,7 +273,7 @@ describe("runGitHubCopilotDeviceFlow — data-residency GitHub Enterprise", () =
246273 } ) ;
247274
248275 const showCode = vi . fn ( async ( ) => { } ) ;
249- const result = await runGitHubCopilotDeviceFlow ( { showCode } , GHE_DOMAIN ) ;
276+ const result = await runDeviceFlowAfterFirstPoll ( { showCode } , GHE_DOMAIN ) ;
250277
251278 expect ( result ) . toEqual ( { status : "authorized" , accessToken : "ghu_ghe_tok" } ) ;
252279 expect ( urls ) . toEqual ( [ gheDeviceCodeUrl , gheAccessTokenUrl ] ) ;
@@ -271,3 +298,82 @@ describe("runGitHubCopilotDeviceFlow — data-residency GitHub Enterprise", () =
271298 ) . rejects . toThrow ( "unexpected verification URL" ) ;
272299 } ) ;
273300} ) ;
301+
302+ describe ( "runGitHubCopilotDeviceFlow — polling intervals" , ( ) => {
303+ it ( "waits before the first poll and keeps cumulative slow_down increases" , async ( ) => {
304+ vi . useFakeTimers ( ) ;
305+ vi . setSystemTime ( new Date ( "2026-07-16T00:00:00Z" ) ) ;
306+ const startedAt = Date . now ( ) ;
307+ const pollTimes : number [ ] = [ ] ;
308+ const pollResponses = [
309+ { error : "authorization_pending" } ,
310+ { error : "slow_down" } ,
311+ { error : "slow_down" } ,
312+ { access_token : "test-access-token" , token_type : "bearer" } ,
313+ ] ;
314+ mocks . fetchWithSsrFGuard . mockImplementation ( async ( params ) => {
315+ if ( params . url === DEVICE_CODE_URL ) {
316+ const { interval : _interval , ...withoutInterval } = VALID_DEVICE_CODE_BODY ;
317+ return guardResponse ( withoutInterval ) ;
318+ }
319+ pollTimes . push ( Date . now ( ) ) ;
320+ return guardResponse ( pollResponses . shift ( ) , 200 , ACCESS_TOKEN_URL ) ;
321+ } ) ;
322+
323+ const result = runGitHubCopilotDeviceFlow ( { showCode : vi . fn ( async ( ) => { } ) } ) ;
324+
325+ await vi . advanceTimersByTimeAsync ( 4_999 ) ;
326+ expect ( pollTimes ) . toEqual ( [ ] ) ;
327+ await vi . advanceTimersByTimeAsync ( 1 ) ;
328+ expect ( pollTimes ) . toEqual ( [ startedAt + 5_000 ] ) ;
329+
330+ await vi . advanceTimersByTimeAsync ( 5_000 ) ;
331+ expect ( pollTimes ) . toEqual ( [ startedAt + 5_000 , startedAt + 10_000 ] ) ;
332+ await vi . advanceTimersByTimeAsync ( 10_000 ) ;
333+ expect ( pollTimes ) . toEqual ( [ startedAt + 5_000 , startedAt + 10_000 , startedAt + 20_000 ] ) ;
334+ await vi . advanceTimersByTimeAsync ( 15_000 ) ;
335+
336+ await expect ( result ) . resolves . toEqual ( {
337+ status : "authorized" ,
338+ accessToken : "test-access-token" ,
339+ } ) ;
340+ expect ( pollTimes ) . toEqual ( [
341+ startedAt + 5_000 ,
342+ startedAt + 10_000 ,
343+ startedAt + 20_000 ,
344+ startedAt + 35_000 ,
345+ ] ) ;
346+ } ) ;
347+
348+ it ( "uses the interval returned with slow_down" , async ( ) => {
349+ vi . useFakeTimers ( ) ;
350+ vi . setSystemTime ( new Date ( "2026-07-16T00:00:00Z" ) ) ;
351+ const startedAt = Date . now ( ) ;
352+ const pollTimes : number [ ] = [ ] ;
353+ const pollResponses = [
354+ { error : "slow_down" , interval : 7 } ,
355+ { access_token : "test-access-token" , token_type : "bearer" } ,
356+ ] ;
357+ mocks . fetchWithSsrFGuard . mockImplementation ( async ( params ) => {
358+ if ( params . url === DEVICE_CODE_URL ) {
359+ return guardResponse ( { ...VALID_DEVICE_CODE_BODY , interval : 2 } ) ;
360+ }
361+ pollTimes . push ( Date . now ( ) ) ;
362+ return guardResponse ( pollResponses . shift ( ) , 200 , ACCESS_TOKEN_URL ) ;
363+ } ) ;
364+
365+ const result = runGitHubCopilotDeviceFlow ( { showCode : vi . fn ( async ( ) => { } ) } ) ;
366+
367+ await vi . advanceTimersByTimeAsync ( 2_000 ) ;
368+ expect ( pollTimes ) . toEqual ( [ startedAt + 2_000 ] ) ;
369+ await vi . advanceTimersByTimeAsync ( 6_999 ) ;
370+ expect ( pollTimes ) . toHaveLength ( 1 ) ;
371+ await vi . advanceTimersByTimeAsync ( 1 ) ;
372+
373+ await expect ( result ) . resolves . toEqual ( {
374+ status : "authorized" ,
375+ accessToken : "test-access-token" ,
376+ } ) ;
377+ expect ( pollTimes ) . toEqual ( [ startedAt + 2_000 , startedAt + 9_000 ] ) ;
378+ } ) ;
379+ } ) ;
0 commit comments