11import assert from 'node:assert/strict' ;
2- import { test } from 'vitest' ;
2+ import { afterEach , test } from 'vitest' ;
33import type { DeviceLease } from '@agent-device/contracts/device' ;
44import { deviceFieldsFromPublicPlatform , type DeviceInfo } from '@agent-device/kernel/device' ;
55import { AppError } from '@agent-device/kernel/errors' ;
6- import { createCloudWebDriverRuntime } from './runtime.ts' ;
6+ import { createCloudWebDriverRuntime , type CloudWebDriverRuntimeOptions } from './runtime.ts' ;
7+
8+ const realFetch = globalThis . fetch ;
9+
10+ afterEach ( ( ) => {
11+ globalThis . fetch = realFetch ;
12+ } ) ;
713
814test ( 'session allocation preserves its primary failure when provider cleanup also fails' , async ( ) => {
9- const previousFetch = globalThis . fetch ;
1015 let cleanupCalled = false ;
11- globalThis . fetch = async ( ) =>
12- new Response ( JSON . stringify ( { value : { message : 'create session failed' } } ) , {
13- status : 500 ,
14- headers : { 'Content-Type' : 'application/json' } ,
15- } ) ;
16- const runtime = createCloudWebDriverRuntime ( {
17- clientVersion : 'test' ,
18- provider : 'webdriver-test' ,
19- endpoint : 'https://webdriver.test/wd/hub/' ,
20- platform : 'android' ,
21- deviceName : 'Test device' ,
22- requestPolicy : { retryAttempts : 0 } ,
16+ globalThis . fetch = async ( ) => jsonResponse ( { value : { message : 'create session failed' } } , 500 ) ;
17+ const runtime = makeRuntime ( {
2318 prepareSession : async ( { base } ) => ( {
2419 ...base ,
2520 cleanup : async ( ) => {
@@ -30,10 +25,8 @@ test('session allocation preserves its primary failure when provider cleanup als
3025 } ) ;
3126
3227 try {
33- const allocate = runtime . leaseLifecycle . allocate ;
34- assert . ok ( allocate ) ;
3528 await assert . rejects (
36- ( ) => allocate ( makeLease ( ) ) ,
29+ ( ) => runtime . leaseLifecycle . allocate ! ( makeLease ( ) ) ,
3730 ( error : unknown ) => {
3831 assert . ok ( error instanceof AppError ) ;
3932 assert . match ( error . message , / c r e a t e s e s s i o n f a i l e d / ) ;
@@ -44,33 +37,20 @@ test('session allocation preserves its primary failure when provider cleanup als
4437 assert . equal ( cleanupCalled , true ) ;
4538 } finally {
4639 await runtime . shutdown ( ) ;
47- globalThis . fetch = previousFetch ;
4840 }
4941} ) ;
5042
5143// #1774: a request already gone before the create is issued must create
5244// NOTHING. Prepared provider resources are cleaned up and no `POST /session`
5345// goes out, so there is no billed device session to leak.
5446test ( 'allocation canceled before create issues no session and cleans up prepared work' , async ( ) => {
55- const previousFetch = globalThis . fetch ;
5647 let sessionRequests = 0 ;
5748 let cleanupCalled = false ;
5849 globalThis . fetch = async ( input ) => {
59- if ( String ( input instanceof Request ? input . url : input ) . endsWith ( '/session' ) ) {
60- sessionRequests += 1 ;
61- }
62- return new Response ( JSON . stringify ( { value : { sessionId : 'wd-1' , capabilities : { } } } ) , {
63- status : 200 ,
64- headers : { 'Content-Type' : 'application/json' } ,
65- } ) ;
50+ if ( requestUrl ( input ) . endsWith ( '/session' ) ) sessionRequests += 1 ;
51+ return jsonResponse ( { value : { sessionId : 'wd-1' , capabilities : { } } } ) ;
6652 } ;
67- const runtime = createCloudWebDriverRuntime ( {
68- clientVersion : 'test' ,
69- provider : 'webdriver-test' ,
70- endpoint : 'https://webdriver.test/wd/hub/' ,
71- platform : 'android' ,
72- deviceName : 'Test device' ,
73- requestPolicy : { retryAttempts : 0 } ,
53+ const runtime = makeRuntime ( {
7454 prepareSession : async ( { base } ) => ( {
7555 ...base ,
7656 cleanup : async ( ) => {
@@ -83,10 +63,8 @@ test('allocation canceled before create issues no session and cleans up prepared
8363 controller . abort ( ) ;
8464
8565 try {
86- const allocate = runtime . leaseLifecycle . allocate ;
87- assert . ok ( allocate ) ;
8866 await assert . rejects (
89- ( ) => allocate ( makeLease ( ) , { signal : controller . signal } ) ,
67+ ( ) => runtime . leaseLifecycle . allocate ! ( makeLease ( ) , { signal : controller . signal } ) ,
9068 ( error : unknown ) => {
9169 assert . ok ( error instanceof AppError ) ;
9270 assert . equal ( error . details ?. reason , 'request_canceled' ) ;
@@ -97,7 +75,6 @@ test('allocation canceled before create issues no session and cleans up prepared
9775 assert . equal ( cleanupCalled , true ) ;
9876 } finally {
9977 await runtime . shutdown ( ) ;
100- globalThis . fetch = previousFetch ;
10178 }
10279} ) ;
10380
@@ -106,44 +83,28 @@ test('allocation canceled before create issues no session and cleans up prepared
10683// register that session (nobody is waiting on it) — it deletes it, holding the
10784// id it just learned, so the billed session is released instead of orphaned.
10885test ( 'a session that completes after cancellation is released, not registered' , async ( ) => {
109- const previousFetch = globalThis . fetch ;
11086 const controller = new AbortController ( ) ;
11187 let deletedSessionId : string | undefined ;
11288 globalThis . fetch = async ( input , init ) => {
113- const url = String ( input instanceof Request ? input . url : input ) ;
89+ const url = requestUrl ( input ) ;
11490 const method = init ?. method ?? 'GET' ;
11591 if ( url . endsWith ( '/session' ) && method === 'POST' ) {
11692 // The client disconnects mid-create; the provider finishes anyway.
11793 controller . abort ( ) ;
118- return new Response ( JSON . stringify ( { value : { sessionId : 'wd-live' , capabilities : { } } } ) , {
119- status : 200 ,
120- headers : { 'Content-Type' : 'application/json' } ,
121- } ) ;
94+ return jsonResponse ( { value : { sessionId : 'wd-live' , capabilities : { } } } ) ;
12295 }
12396 if ( url . endsWith ( '/session/wd-live' ) && method === 'DELETE' ) {
12497 deletedSessionId = 'wd-live' ;
125- return new Response ( JSON . stringify ( { value : null } ) , {
126- status : 200 ,
127- headers : { 'Content-Type' : 'application/json' } ,
128- } ) ;
98+ return jsonResponse ( { value : null } ) ;
12999 }
130100 throw new Error ( `unexpected ${ method } ${ url } ` ) ;
131101 } ;
132- const runtime = createCloudWebDriverRuntime ( {
133- clientVersion : 'test' ,
134- provider : 'webdriver-test' ,
135- endpoint : 'https://webdriver.test/wd/hub/' ,
136- platform : 'android' ,
137- deviceName : 'Test device' ,
138- requestPolicy : { retryAttempts : 0 } ,
139- } ) ;
102+ const runtime = makeRuntime ( ) ;
140103 const lease = makeLease ( ) ;
141104
142105 try {
143- const allocate = runtime . leaseLifecycle . allocate ;
144- assert . ok ( allocate ) ;
145106 await assert . rejects (
146- ( ) => allocate ( lease , { signal : controller . signal } ) ,
107+ ( ) => runtime . leaseLifecycle . allocate ! ( lease , { signal : controller . signal } ) ,
147108 ( error : unknown ) => {
148109 assert . ok ( error instanceof AppError ) ;
149110 assert . equal ( error . details ?. reason , 'request_canceled' ) ;
@@ -156,7 +117,6 @@ test('a session that completes after cancellation is released, not registered',
156117 assert . equal ( runtime . getInteractor ( makeDevice ( lease ) ) , undefined ) ;
157118 } finally {
158119 await runtime . shutdown ( ) ;
159- globalThis . fetch = previousFetch ;
160120 }
161121} ) ;
162122
@@ -165,30 +125,20 @@ test('a session that completes after cancellation is released, not registered',
165125// an operator can find and stop the maybe-orphaned billed session, rather than
166126// this process guessing at REST cleanup of a session it never owned.
167127test ( 'a create-timeout surfaces provider evidence for the maybe-leaked session' , async ( ) => {
168- const previousFetch = globalThis . fetch ;
169- globalThis . fetch = async ( input , init ) => {
170- if ( String ( input instanceof Request ? input . url : input ) . endsWith ( '/session' ) ) {
171- return await new Promise < Response > ( ( _resolve , reject ) => {
172- init ?. signal ?. addEventListener ( 'abort' , ( ) => reject ( init . signal ?. reason as Error ) ) ;
173- } ) ;
174- }
175- throw new Error ( 'unexpected request' ) ;
176- } ;
177- const runtime = createCloudWebDriverRuntime ( {
178- clientVersion : 'test' ,
128+ globalThis . fetch = async ( _input , init ) =>
129+ await new Promise < Response > ( ( _resolve , reject ) => {
130+ init ?. signal ?. addEventListener ( 'abort' , ( ) => reject ( init . signal ?. reason as Error ) ) ;
131+ } ) ;
132+ const runtime = makeRuntime ( {
179133 provider : 'browserstack' ,
180- endpoint : 'https://webdriver.test/wd/hub/' ,
181134 platform : 'ios' ,
182- deviceName : 'iPhone 17' ,
183135 requestPolicy : { retryAttempts : 0 , sessionCreateTimeoutMs : 40 } ,
184136 } ) ;
185137 const lease = { ...makeLease ( ) , leaseProvider : 'browserstack' } ;
186138
187139 try {
188- const allocate = runtime . leaseLifecycle . allocate ;
189- assert . ok ( allocate ) ;
190140 await assert . rejects (
191- ( ) => allocate ( lease ) ,
141+ ( ) => runtime . leaseLifecycle . allocate ! ( lease ) ,
192142 ( error : unknown ) => {
193143 assert . ok ( error instanceof AppError ) ;
194144 assert . equal ( error . details ?. reason , 'provider_session_create_timeout' ) ;
@@ -200,10 +150,32 @@ test('a create-timeout surfaces provider evidence for the maybe-leaked session',
200150 ) ;
201151 } finally {
202152 await runtime . shutdown ( ) ;
203- globalThis . fetch = previousFetch ;
204153 }
205154} ) ;
206155
156+ function makeRuntime ( overrides : Partial < CloudWebDriverRuntimeOptions > = { } ) {
157+ return createCloudWebDriverRuntime ( {
158+ clientVersion : 'test' ,
159+ provider : 'webdriver-test' ,
160+ endpoint : 'https://webdriver.test/wd/hub/' ,
161+ platform : 'android' ,
162+ deviceName : 'Test device' ,
163+ requestPolicy : { retryAttempts : 0 } ,
164+ ...overrides ,
165+ } ) ;
166+ }
167+
168+ function jsonResponse ( body : unknown , status = 200 ) : Response {
169+ return new Response ( JSON . stringify ( body ) , {
170+ status,
171+ headers : { 'Content-Type' : 'application/json' } ,
172+ } ) ;
173+ }
174+
175+ function requestUrl ( input : RequestInfo | URL ) : string {
176+ return String ( input instanceof Request ? input . url : input ) ;
177+ }
178+
207179function makeDevice ( lease : DeviceLease ) : DeviceInfo {
208180 return {
209181 ...deviceFieldsFromPublicPlatform ( 'android' ) ,
0 commit comments