@@ -37,18 +37,27 @@ function callback(authorizationUrl: string) {
3737}
3838
3939describe ( "desktop Codex callback helper" , ( ) => {
40- it ( "binds before opening sign-in, ignores a foreign response, and returns only the code callback" , async ( ) => {
40+ it ( "binds before opening sign-in, ignores a foreign response, and returns only the code callback" , async ( {
41+ onTestFinished,
42+ } ) => {
4143 const authorizationUrl = request ( await freePort ( ) ) ;
4244 const expected = callback ( authorizationUrl ) ;
43- const received = await receiveCodexAuthCallback ( authorizationUrl , async ( ) => {
45+ const opening = Promise . withResolvers < boolean > ( ) ;
46+ onTestFinished ( ( ) => opening . resolve ( true ) ) ;
47+ const checkResponse = async ( ) => {
4448 const invalid = new URL ( expected ) ;
4549 invalid . searchParams . set ( "state" , "foreign" ) ;
4650 expect ( ( await fetch ( invalid ) ) . status ) . toBe ( 400 ) ;
4751 const response = await fetch ( expected ) ;
4852 expect ( response . headers . get ( "cache-control" ) ) . toBe ( "no-store" ) ;
4953 expect ( await response . text ( ) ) . not . toContain ( "test-code" ) ;
50- return true ;
54+ } ;
55+ let responseChecked ! : Promise < void > ;
56+ const received = await receiveCodexAuthCallback ( authorizationUrl , ( ) => {
57+ responseChecked = checkResponse ( ) ;
58+ return responseChecked . then ( ( ) => opening . promise ) ;
5159 } ) ;
60+ await responseChecked ;
5261 expect ( received ) . toBe ( expected ) ;
5362 } ) ;
5463 it ( "returns hosted web to the exact instance and environment without putting the code in its query" , async ( ) => {
@@ -61,34 +70,52 @@ describe("desktop Codex callback helper", () => {
6170 instanceId : ProviderInstanceId . make ( "work" ) ,
6271 flowId : "flow-one" ,
6372 } ;
73+ const openBrowser = async ( ) => {
74+ const response = await fetch ( expected , { redirect : "manual" } ) ;
75+ expect ( response . status ) . toBe ( 303 ) ;
76+ const delivery = response . headers . get ( "location" ) ! ;
77+ expect ( new URL ( delivery ) . searchParams . has ( "code" ) ) . toBe ( false ) ;
78+ expect ( readCodexAuthDelivery ( delivery ) ?. callbackUrl ) . toBe ( expected ) ;
79+ expect ( readCodexAuthDelivery ( delivery ) ?. returnUrl ) . toBe ( input . returnUrl ) ;
80+ return true ;
81+ } ;
82+ let opening ! : Promise < boolean > ;
6483 await receiveCodexAuthCallback (
6584 authorizationUrl ,
66- async ( ) => {
67- const response = await fetch ( expected , { redirect : "manual" } ) ;
68- expect ( response . status ) . toBe ( 303 ) ;
69- const delivery = response . headers . get ( "location" ) ! ;
70- expect ( new URL ( delivery ) . searchParams . has ( "code" ) ) . toBe ( false ) ;
71- expect ( readCodexAuthDelivery ( delivery ) ?. callbackUrl ) . toBe ( expected ) ;
72- expect ( readCodexAuthDelivery ( delivery ) ?. returnUrl ) . toBe ( input . returnUrl ) ;
73- return true ;
74- } ,
85+ ( ) => ( opening = openBrowser ( ) ) ,
7586 ( url ) => codexAuthDeliveryUrl ( input , url ) ,
7687 ) ;
88+ await opening ;
7789 } ) ;
78- it ( "cancels and releases its listener so exact-port reauthorization can run again" , async ( ) => {
79- const authorizationUrl = request ( await freePort ( ) ) ;
80- await expect (
81- receiveCodexAuthCallback ( authorizationUrl , async ( ) => {
82- cancelCodexAuthCallback ( authorizationUrl ) ;
83- return true ;
84- } ) ,
85- ) . rejects . toThrow ( "cancelled" ) ;
86- expect (
87- await receiveCodexAuthCallback ( authorizationUrl , async ( ) => {
88- await fetch ( callback ( authorizationUrl ) ) ;
89- return true ;
90- } ) ,
91- ) . toBe ( callback ( authorizationUrl ) ) ;
90+ it ( "cancels and releases its listener so exact-port reauthorization can run again" , async ( {
91+ onTestFinished,
92+ signal,
93+ } ) => {
94+ for ( const external of [ false , true ] ) {
95+ const authorizationUrl = request ( await freePort ( ) ) ;
96+ const abort = new AbortController ( ) ;
97+ const opening = Promise . withResolvers < boolean > ( ) ;
98+ onTestFinished ( ( ) => opening . resolve ( true ) ) ;
99+ await expect (
100+ receiveCodexAuthCallback (
101+ authorizationUrl ,
102+ async ( ) => {
103+ if ( external ) abort . abort ( ) ;
104+ else cancelCodexAuthCallback ( authorizationUrl ) ;
105+ return opening . promise ;
106+ } ,
107+ undefined ,
108+ abort . signal ,
109+ ) ,
110+ ) . rejects . toThrow ( "cancelled" ) ;
111+ if ( signal . aborted ) return ;
112+ expect (
113+ await receiveCodexAuthCallback ( authorizationUrl , async ( ) => {
114+ await fetch ( callback ( authorizationUrl ) ) ;
115+ return true ;
116+ } ) ,
117+ ) . toBe ( callback ( authorizationUrl ) ) ;
118+ }
92119 } ) ;
93120 it ( "allows two accounts to complete independently" , async ( ) => {
94121 const a = request ( await freePort ( ) , "a" . repeat ( 43 ) ) ;
0 commit comments