1- import { describe , expect , spyOn , test } from "bun:test"
1+ import { afterEach , describe , expect , spyOn , test } from "bun:test"
22import {
33 pollCliAuthRequest ,
44 startCliAuthRequest ,
55 waitForCliAuthApproval ,
66} from "../../src/lib/cli-auth"
77
8+ let activeFetchSpy : ReturnType < typeof spyOn > | undefined
9+
10+ function spyOnFetch ( ) {
11+ const fetchSpy = spyOn ( globalThis , "fetch" )
12+ activeFetchSpy = fetchSpy
13+ return fetchSpy
14+ }
15+
16+ afterEach ( ( ) => {
17+ activeFetchSpy ?. mockRestore ( )
18+ activeFetchSpy = undefined
19+ } )
20+
821describe ( "CLI browser auth client" , ( ) => {
922 test ( "starts a CLI auth request against the platform API" , async ( ) => {
10- const fetchSpy = spyOn ( globalThis , "fetch" ) . mockResolvedValueOnce (
23+ const fetchSpy = spyOnFetch ( ) . mockResolvedValueOnce (
1124 new Response (
1225 JSON . stringify ( {
1326 requestId : "req_123" ,
@@ -27,12 +40,10 @@ describe("CLI browser auth client", () => {
2740 expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 )
2841 expect ( fetchSpy . mock . calls [ 0 ] ?. [ 0 ] ) . toBe ( "https://app.outlit.ai/api/cli-auth/start" )
2942 expect ( ( fetchSpy . mock . calls [ 0 ] ?. [ 1 ] as RequestInit ) . method ) . toBe ( "POST" )
30-
31- fetchSpy . mockRestore ( )
3243 } )
3344
3445 test ( "polls a CLI auth request with the terminal-only poll token" , async ( ) => {
35- const fetchSpy = spyOn ( globalThis , "fetch" ) . mockResolvedValueOnce (
46+ const fetchSpy = spyOnFetch ( ) . mockResolvedValueOnce (
3647 new Response ( JSON . stringify ( { status : "pending" , intervalSeconds : 2 } ) , { status : 200 } ) ,
3748 )
3849
@@ -49,12 +60,10 @@ describe("CLI browser auth client", () => {
4960 requestId : "req_123" ,
5061 pollToken : "poll_token_123" ,
5162 } )
52-
53- fetchSpy . mockRestore ( )
5463 } )
5564
5665 test ( "parses a failed CLI auth poll response" , async ( ) => {
57- const fetchSpy = spyOn ( globalThis , "fetch" ) . mockResolvedValueOnce (
66+ spyOnFetch ( ) . mockResolvedValueOnce (
5867 new Response ( JSON . stringify ( { status : "failed" , error : "Could not verify CLI key" } ) , {
5968 status : 200 ,
6069 } ) ,
@@ -69,12 +78,10 @@ describe("CLI browser auth client", () => {
6978 status : "failed" ,
7079 error : "Could not verify CLI key" ,
7180 } )
72-
73- fetchSpy . mockRestore ( )
7481 } )
7582
7683 test ( "returns invalid CLI auth poll credentials without treating them as transient" , async ( ) => {
77- const fetchSpy = spyOn ( globalThis , "fetch" ) . mockResolvedValueOnce (
84+ spyOnFetch ( ) . mockResolvedValueOnce (
7885 new Response (
7986 JSON . stringify ( {
8087 error : "Invalid CLI auth polling credentials" ,
@@ -90,13 +97,11 @@ describe("CLI browser auth client", () => {
9097 } )
9198
9299 expect ( result ) . toEqual ( { status : "invalid" } )
93-
94- fetchSpy . mockRestore ( )
95100 } )
96101
97102 test ( "waits until polling returns an approved API key" , async ( ) => {
98103 let callCount = 0
99- const fetchSpy = spyOn ( globalThis , "fetch" ) . mockImplementation ( ( async ( ) => {
104+ const fetchSpy = spyOnFetch ( ) . mockImplementation ( ( async ( ) => {
100105 callCount += 1
101106 return new Response (
102107 JSON . stringify (
@@ -132,7 +137,5 @@ describe("CLI browser auth client", () => {
132137 keyPrefix : "ok_abc" ,
133138 } )
134139 expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 )
135-
136- fetchSpy . mockRestore ( )
137140 } )
138141} )
0 commit comments