Skip to content

Commit 5cd1d5c

Browse files
committed
fix(provider-webdriver): load the diagnostics emitter on demand so the eager closure holds
The static import of @agent-device/host-kit/diagnostics in the transport added two modules to provider-webdriver's eager import closure (51 vs 49 at the merge-base). The unsupported-route diagnostic is a rare path, so reportRouteUnsupported now imports the emitter inside the function; the classifier stays synchronous. The closure walker counts only static and top-level dynamic imports, so the budget holds. The host-kit dependency stays declared because the dynamic import still resolves the package. The diagnostic assertions wait for the event with vi.waitFor. Mutations checked: - remove the reportRouteUnsupported call: 6 tests fail (5 classifier rows and the terminate-app fallback test).
1 parent 33e3e2d commit 5cd1d5c

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

‎packages/provider-webdriver/src/webdriver-mutation-resend.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'node:assert/strict';
22
import fs from 'node:fs';
33
import path from 'node:path';
4-
import { afterEach, test } from 'vitest';
4+
import { afterEach, test, vi } from 'vitest';
55
import {
66
countDiagnosticEventsByPhase,
77
withDiagnosticsScope,
@@ -252,7 +252,9 @@ test('an unsupported app-termination route falls back to the sibling route once'
252252

253253
await withDiagnosticsScope({ command: 'close' }, async () => {
254254
await client.terminateApp('com.example.app');
255-
assert.equal(countDiagnosticEventsByPhase(['webdriver_route_unsupported']), 1);
255+
await vi.waitFor(() =>
256+
assert.equal(countDiagnosticEventsByPhase(['webdriver_route_unsupported']), 1),
257+
);
256258
});
257259

258260
assert.equal(sendsTo('POST /session/:id/appium/device/terminate_app'), 1);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'node:assert/strict';
22
import http from 'node:http';
33
import net, { type AddressInfo } from 'node:net';
4-
import { afterEach, test } from 'vitest';
4+
import { afterEach, test, vi } from 'vitest';
55
import {
66
countDiagnosticEventsByPhase,
77
withDiagnosticsScope,
@@ -298,9 +298,11 @@ for (const { status, body, unsupported } of ROUTE_ANSWERS) {
298298
return true;
299299
},
300300
);
301-
assert.equal(
302-
countDiagnosticEventsByPhase(['webdriver_route_unsupported']),
303-
unsupported ? 1 : 0,
301+
await vi.waitFor(() =>
302+
assert.equal(
303+
countDiagnosticEventsByPhase(['webdriver_route_unsupported']),
304+
unsupported ? 1 : 0,
305+
),
304306
);
305307
});
306308
});

‎packages/provider-webdriver/src/webdriver-transport.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { setTimeout as sleep } from 'node:timers/promises';
2-
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
32
import { AppError } from '@agent-device/kernel/errors';
43
import { agentDeviceRequestHeaders } from './request-headers.ts';
54
import { basicAuthHeader, trimLeadingSlash, withTrailingSlash } from './webdriver-utils.ts';
@@ -203,9 +202,7 @@ export class WebDriverTransport {
203202
const payload = text ? parseJsonResponse(text) : {};
204203
if (ok) return readWebDriverValue(payload);
205204
const error = webdriverError(status, payload);
206-
if (isWebDriverRouteUnsupported(error)) {
207-
emitDiagnostic({ phase: WEBDRIVER_ROUTE_UNSUPPORTED_REASON, data: { method, path, status } });
208-
}
205+
if (isWebDriverRouteUnsupported(error)) reportRouteUnsupported({ method, path, status });
209206
throw error;
210207
}
211208

@@ -371,6 +368,18 @@ function isUnsupportedRouteAnswer(status: number, payload: unknown): boolean {
371368
return UNSUPPORTED_ROUTE_STATUSES.has(status);
372369
}
373370

371+
/**
372+
* Records the answer in the request diagnostics. The emitter loads on demand because this path is
373+
* rare and the transport sits in the provider's eager import closure.
374+
*/
375+
function reportRouteUnsupported(data: { method: string; path: string; status: number }): void {
376+
void import('@agent-device/host-kit/diagnostics')
377+
.then(({ emitDiagnostic }) =>
378+
emitDiagnostic({ phase: WEBDRIVER_ROUTE_UNSUPPORTED_REASON, data }),
379+
)
380+
.catch(() => undefined);
381+
}
382+
374383
/**
375384
* The driver answered that it does not implement the route, so nothing was dispatched and a
376385
* sibling route for the same action may be tried. A timeout or 5xx is not this answer: the first

0 commit comments

Comments
 (0)