Skip to content

Commit 12bab7a

Browse files
amankansal-ltclaude
andcommitted
test(provider-webdriver): exercise in-flight deadlines and aborts for real
The BrowserStack session-details timeout test threw a prebuilt TimeoutError from the fetch stub, so it passed even with the deadline removed. The fetch now stays pending until the signal the lookup armed aborts, and the test checks that signal is the 15 second deadline. The TestMu AI upload cancellation test aborted before the upload reached fetch, so it never covered a request in flight. It now waits for the stub to start before aborting. The connect suites shared a copied connectWithGeneratedProviderProfile helper; it now lives in the shared test utilities. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 402370e commit 12bab7a

5 files changed

Lines changed: 68 additions & 63 deletions

File tree

‎packages/provider-webdriver/src/browserstack.test.ts‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
22
import { promises as fs } from 'node:fs';
33

44
import path from 'node:path';
5-
import { afterEach, test } from 'vitest';
5+
import { afterEach, test, vi } from 'vitest';
66
import { AppError } from '@agent-device/kernel/errors';
77
import {
88
listBrowserStackCloudArtifacts,
@@ -109,22 +109,45 @@ test('BrowserStack passes bs:// ids and URLs to the hub and uploads only local p
109109
test('BrowserStack session details lookup has a deadline and fails typed', async () => {
110110
const lookup = async () =>
111111
await listBrowserStackCloudArtifacts('browserstack', 'SESSION1', upload);
112-
const timeout = new DOMException('The operation was aborted due to timeout', 'TimeoutError');
113-
const transportFailures: unknown[] = [timeout, new TypeError('fetch failed')];
114-
for (const failure of transportFailures) {
115-
globalThis.fetch = async (_input, init) => {
116-
assert.ok(init?.signal instanceof AbortSignal);
117-
throw failure;
118-
};
119-
await assert.rejects(lookup(), (error: unknown) => {
112+
// The deadline is the only thing that ends a hung lookup: the fetch stays pending until the
113+
// signal the lookup armed for 15 s aborts, so removing the deadline would hang this test.
114+
const deadline = new AbortController();
115+
const timeoutSpy = vi.spyOn(AbortSignal, 'timeout').mockImplementation(() => deadline.signal);
116+
let started: () => void = () => {};
117+
const fetchStarted = new Promise<void>((resolve) => {
118+
started = resolve;
119+
});
120+
globalThis.fetch = async (_input, init) =>
121+
await new Promise<Response>((_resolve, reject) => {
122+
init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), { once: true });
123+
started();
124+
});
125+
try {
126+
const pending = lookup();
127+
await fetchStarted;
128+
assert.deepEqual(timeoutSpy.mock.calls, [[15_000]]);
129+
const timeout = new DOMException('The operation was aborted due to timeout', 'TimeoutError');
130+
deadline.abort(timeout);
131+
await assert.rejects(pending, (error: unknown) => {
120132
assert.ok(error instanceof AppError);
121133
assert.equal(error.code, 'COMMAND_FAILED');
122134
assert.equal(error.message, 'BrowserStack session details lookup failed.');
123-
assert.equal(error.cause, failure);
135+
assert.equal(error.cause, timeout);
124136
return true;
125137
});
138+
} finally {
139+
timeoutSpy.mockRestore();
126140
}
127141

142+
const networkFailure = new TypeError('fetch failed');
143+
globalThis.fetch = async () => {
144+
throw networkFailure;
145+
};
146+
await assert.rejects(
147+
lookup(),
148+
(error: unknown) => error instanceof AppError && error.cause === networkFailure,
149+
);
150+
128151
for (const body of ['<html>gateway</html>', '[]']) {
129152
globalThis.fetch = async () => new Response(body, { status: 200 });
130153
await assert.rejects(lookup(), (error: unknown) => {

‎packages/provider-webdriver/src/testmu.test.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,19 @@ test('TestMu upload reads the lt:// reference and aborts while the request is in
197197
const abortReason = new Error('request cancelled during TestMu AI upload');
198198
try {
199199
await fs.writeFile(appPath, 'placeholder');
200+
let started: () => void = () => {};
201+
const fetchStarted = new Promise<void>((resolve) => {
202+
started = resolve;
203+
});
200204
globalThis.fetch = async (_input, init) =>
201205
await new Promise<Response>((_resolve, reject) => {
202206
assert.equal(init?.signal, controller.signal);
203-
if (init?.signal?.aborted) {
204-
reject(init.signal.reason);
205-
return;
206-
}
207207
init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), { once: true });
208+
started();
208209
});
209210

210211
const pending = uploadTestMuApp(appPath, auth, controller.signal);
211-
await Promise.resolve();
212+
await fetchStarted;
212213
controller.abort(abortReason);
213214
await assert.rejects(pending, (error: unknown) => error === abortReason);
214215

‎src/__tests__/cloud-connect-profile.test.ts‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { AppError } from '@agent-device/kernel/errors';
1717
import { verifyLimrunConnection } from '@agent-device/provider-limrun';
1818
import { providerWebDriver } from '../provider-webdriver.ts';
1919
import { mkdtempForTestSync } from './test-utils/tmp-dir.ts';
20+
import { connectWithGeneratedProviderProfile } from './test-utils/connect-command.ts';
2021

2122
vi.mock('../cli/auth-session.ts', async (importOriginal) => ({
2223
...(await importOriginal<typeof import('../cli/auth-session.ts')>()),
@@ -857,29 +858,6 @@ async function captureConnectStdout(task: () => Promise<void>): Promise<void> {
857858
}
858859
}
859860

860-
async function connectWithGeneratedProviderProfile(options: {
861-
stateDir: string;
862-
positionals: string[];
863-
flags: Partial<Parameters<typeof connectCommand>[0]['flags']>;
864-
}): Promise<void> {
865-
const stdoutWrite = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
866-
try {
867-
await connectCommand({
868-
positionals: options.positionals,
869-
flags: {
870-
json: true,
871-
help: false,
872-
version: false,
873-
stateDir: options.stateDir,
874-
...options.flags,
875-
},
876-
client: {} as AgentDeviceClient,
877-
});
878-
} finally {
879-
stdoutWrite.mockRestore();
880-
}
881-
}
882-
883861
function readGeneratedConfig(configPath: string): {
884862
tenant?: string;
885863
leaseProvider?: string;

‎src/__tests__/cloud-connect-testmu.test.ts‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import { afterEach, beforeEach, test, vi } from 'vitest';
22
import assert from 'node:assert/strict';
33
import fs from 'node:fs';
44
import path from 'node:path';
5-
import { connectCommand } from '../cli/commands/connection.ts';
65
import { runCliCapture } from './cli-capture.ts';
76
import {
87
readActiveConnectionState,
98
type RemoteConnectionState,
109
} from '../remote/remote-connection-state.ts';
11-
import type { AgentDeviceClient } from '../agent-device-client.ts';
1210
import { resolveCloudWebDriverConnectProfile } from '../cli/connection/cloud-webdriver-profile.ts';
1311
import { AppError } from '@agent-device/kernel/errors';
1412
import { providerWebDriver } from '../provider-webdriver.ts';
1513
import { mkdtempForTestSync } from './test-utils/tmp-dir.ts';
14+
import { connectWithGeneratedProviderProfile } from './test-utils/connect-command.ts';
1615

1716
vi.mock('../provider-webdriver.ts', () => ({
1817
providerWebDriver: { verifyConnection: vi.fn() },
@@ -313,29 +312,6 @@ test('connect limrun refuses profile fields Limrun does not read', async () => {
313312
);
314313
});
315314

316-
async function connectWithGeneratedProviderProfile(options: {
317-
stateDir: string;
318-
positionals: string[];
319-
flags: Partial<Parameters<typeof connectCommand>[0]['flags']>;
320-
}): Promise<void> {
321-
const stdoutWrite = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
322-
try {
323-
await connectCommand({
324-
positionals: options.positionals,
325-
flags: {
326-
json: true,
327-
help: false,
328-
version: false,
329-
stateDir: options.stateDir,
330-
...options.flags,
331-
},
332-
client: {} as AgentDeviceClient,
333-
});
334-
} finally {
335-
stdoutWrite.mockRestore();
336-
}
337-
}
338-
339315
function readGeneratedConfig(configPath: string): {
340316
providerApp?: string;
341317
providerOsVersion?: string;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { vi } from 'vitest';
2+
import { connectCommand } from '../../cli/commands/connection.ts';
3+
import type { AgentDeviceClient } from '../../agent-device-client.ts';
4+
5+
/** Runs `connect` the way the CLI does, writing its generated profile under `stateDir`. */
6+
export async function connectWithGeneratedProviderProfile(options: {
7+
stateDir: string;
8+
positionals: string[];
9+
flags: Partial<Parameters<typeof connectCommand>[0]['flags']>;
10+
}): Promise<void> {
11+
const stdoutWrite = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
12+
try {
13+
await connectCommand({
14+
positionals: options.positionals,
15+
flags: {
16+
json: true,
17+
help: false,
18+
version: false,
19+
stateDir: options.stateDir,
20+
...options.flags,
21+
},
22+
client: {} as AgentDeviceClient,
23+
});
24+
} finally {
25+
stdoutWrite.mockRestore();
26+
}
27+
}

0 commit comments

Comments
 (0)