Skip to content

Commit 33f46f8

Browse files
thymikeeclaude
andcommitted
refactor: migrate leaf modules from test-only DI to vitest mocking
Drop optional DI parameters from 6 production modules that existed solely as test seams. Replace with vi.mock() in new vitest test files. Production files simplified: - recording-telemetry.ts: remove writeTelemetry? - find.ts: remove dispatch?, use dispatchCommand directly - install-source.ts: remove entire deps? object (7 params) - session-runtime-command.ts: remove clearRuntimeHints? - android-system-dialog.ts: remove 4 DI params, simplify internals - record-trace-recording.ts: remove writeRecordingTelemetry from deps Closes #281 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent c393ffe commit 33f46f8

20 files changed

Lines changed: 939 additions & 348 deletions

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dist/
55
*.log
66
test/screenshots/*.png
77
test/artifacts/
8+
*.gesture-telemetry.json
89
.build/
910
.swiftpm/
1011
DerivedData/

‎package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"prepack": "pnpm build:all",
3636
"typecheck": "tsc -p tsconfig.json",
3737
"test": "node --test",
38-
"test:unit": "node --test src/__tests__/*.test.ts src/core/__tests__/*.test.ts src/daemon/__tests__/*.test.ts src/daemon/handlers/__tests__/*.test.ts src/platforms/**/__tests__/*.test.ts src/utils/**/__tests__/*.test.ts",
38+
"test:unit": "node --test $(find src -path '*/__tests__/*.test.ts' ! -name '*.vitest.test.ts' | sort) && vitest run",
3939
"test:smoke": "node --test test/integration/smoke-*.test.ts",
4040
"test:integration": "node --test test/integration/*.test.ts"
4141
},
@@ -78,6 +78,7 @@
7878
"@types/node": "^22.0.0",
7979
"@types/pngjs": "^6.0.5",
8080
"prettier": "^3.3.3",
81-
"typescript": "^5.9.3"
81+
"typescript": "^5.9.3",
82+
"vitest": "^4.1.2"
8283
}
8384
}

‎pnpm-lock.yaml‎

Lines changed: 620 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/daemon/__tests__/request-router-android-modal.test.ts renamed to src/daemon/__tests__/request-router-android-modal.vitest.test.ts

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import test from 'node:test';
2-
import assert from 'node:assert/strict';
1+
import { test, expect, vi } from 'vitest';
32
import fs from 'node:fs';
43
import os from 'node:os';
54
import path from 'node:path';
@@ -8,6 +7,48 @@ import { SessionStore } from '../session-store.ts';
87
import type { SessionState } from '../types.ts';
98
import { LeaseRegistry } from '../lease-registry.ts';
109

10+
let snapshotCalls = 0;
11+
12+
vi.mock('../../platforms/android/index.ts', async (importOriginal) => {
13+
const actual = await importOriginal<typeof import('../../platforms/android/index.ts')>();
14+
return {
15+
...actual,
16+
snapshotAndroid: vi.fn(async () => {
17+
snapshotCalls += 1;
18+
if (snapshotCalls === 1) {
19+
return {
20+
nodes: [
21+
{
22+
index: 0,
23+
type: 'android.widget.TextView',
24+
label: 'Process system is not responding',
25+
rect: { x: 50, y: 400, width: 500, height: 80 },
26+
},
27+
{
28+
index: 1,
29+
type: 'android.widget.Button',
30+
label: 'Close app',
31+
rect: { x: 100, y: 600, width: 220, height: 80 },
32+
},
33+
],
34+
};
35+
}
36+
return { nodes: [] };
37+
}),
38+
openAndroidApp: vi.fn(async () => {}),
39+
getAndroidAppState: vi.fn(async () => ({ package: 'com.android.settings' })),
40+
};
41+
});
42+
43+
const execCalls: string[][] = [];
44+
45+
vi.mock('../../utils/exec.ts', () => ({
46+
runCmd: vi.fn(async (_cmd: string, args: string[]) => {
47+
execCalls.push(args);
48+
return { stdout: '', stderr: '', exitCode: 0 };
49+
}),
50+
}));
51+
1152
function makeStore(): SessionStore {
1253
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-router-android-modal-'));
1354
return new SessionStore(path.join(tempRoot, 'sessions'));
@@ -40,12 +81,14 @@ function makeAndroidSession(name: string): SessionState {
4081
}
4182

4283
test('generic Android gesture commands dismiss blocking system dialogs during recording', async () => {
84+
snapshotCalls = 0;
85+
execCalls.length = 0;
86+
4387
const sessionStore = makeStore();
4488
sessionStore.set('default', makeAndroidSession('default'));
4589
const dispatchCalls: string[][] = [];
46-
const execCalls: string[][] = [];
47-
const reopenedApps: string[] = [];
48-
let snapshotCalls = 0;
90+
91+
const { openAndroidApp } = await import('../../platforms/android/index.ts');
4992

5093
const handler = createRequestHandler({
5194
logPath: path.join(os.tmpdir(), 'daemon.log'),
@@ -57,36 +100,6 @@ test('generic Android gesture commands dismiss blocking system dialogs during re
57100
dispatchCalls.push([command, ...positionals]);
58101
return {};
59102
},
60-
snapshotAndroidUi: async () => {
61-
snapshotCalls += 1;
62-
if (snapshotCalls === 1) {
63-
return {
64-
nodes: [
65-
{
66-
index: 0,
67-
type: 'android.widget.TextView',
68-
label: 'Process system is not responding',
69-
rect: { x: 50, y: 400, width: 500, height: 80 },
70-
},
71-
{
72-
index: 1,
73-
type: 'android.widget.Button',
74-
label: 'Close app',
75-
rect: { x: 100, y: 600, width: 220, height: 80 },
76-
},
77-
],
78-
};
79-
}
80-
return { nodes: [] };
81-
},
82-
reopenAndroidApp: async (_device, app) => {
83-
reopenedApps.push(app);
84-
},
85-
readAndroidAppState: async () => ({ package: 'com.android.settings' }),
86-
execCommand: async (_cmd, args) => {
87-
execCalls.push(args);
88-
return { stdout: '', stderr: '', exitCode: 0 };
89-
},
90103
});
91104

92105
const response = await handler({
@@ -97,9 +110,12 @@ test('generic Android gesture commands dismiss blocking system dialogs during re
97110
meta: { requestId: 'req-android-modal' },
98111
});
99112

100-
assert.equal(response.ok, true);
101-
assert.deepEqual(dispatchCalls, [['scroll', 'down', '0.55']]);
102-
assert.deepEqual(execCalls, [['-s', 'emulator-5554', 'shell', 'input', 'tap', '210', '640']]);
103-
assert.deepEqual(reopenedApps, ['com.android.settings']);
104-
assert.equal(snapshotCalls, 2);
113+
expect(response.ok).toBe(true);
114+
expect(dispatchCalls).toEqual([['scroll', 'down', '0.55']]);
115+
expect(execCalls).toEqual([['-s', 'emulator-5554', 'shell', 'input', 'tap', '210', '640']]);
116+
expect(openAndroidApp).toHaveBeenCalledWith(
117+
expect.objectContaining({ id: 'emulator-5554' }),
118+
'com.android.settings',
119+
);
120+
expect(snapshotCalls).toBe(2);
105121
});

‎src/daemon/android-system-dialog.ts‎

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,22 @@ export type AndroidBlockingDialogRecoveryResult = 'absent' | 'recovered' | 'fail
1515

1616
export async function recoverAndroidBlockingSystemDialog(params: {
1717
session: SessionState;
18-
snapshotAndroidUi?: typeof snapshotAndroid;
19-
reopenAndroidApp?: typeof openAndroidApp;
20-
readAndroidAppState?: typeof getAndroidAppState;
21-
execCommand?: typeof runCmd;
2218
}): Promise<AndroidBlockingDialogRecoveryResult> {
23-
const {
24-
session,
25-
snapshotAndroidUi = snapshotAndroid,
26-
reopenAndroidApp = openAndroidApp,
27-
readAndroidAppState = getAndroidAppState,
28-
execCommand = runCmd,
29-
} = params;
19+
const { session } = params;
3020

3121
if (session.device.platform !== 'android' || !session.recording) {
3222
return 'absent';
3323
}
3424

3525
try {
36-
const nodes = await readAndroidSnapshotNodes(session, snapshotAndroidUi);
26+
const nodes = await readAndroidSnapshotNodes(session);
3727
const closeAppButton = findCloseAppButton(nodes);
3828
if (!closeAppButton?.rect) {
3929
return 'absent';
4030
}
4131

4232
const { x, y } = centerOfRect(closeAppButton.rect);
43-
const tapResult = await execCommand(
33+
const tapResult = await runCmd(
4434
'adb',
4535
adbArgs(session.device, [
4636
'shell',
@@ -66,7 +56,7 @@ export async function recoverAndroidBlockingSystemDialog(params: {
6656
return 'failed';
6757
}
6858

69-
const dismissed = await waitForBlockingDialogToDismiss(session, snapshotAndroidUi);
59+
const dismissed = await waitForBlockingDialogToDismiss(session);
7060
if (!dismissed) {
7161
emitDiagnostic({
7262
level: 'warn',
@@ -80,12 +70,8 @@ export async function recoverAndroidBlockingSystemDialog(params: {
8070
}
8171

8272
if (session.appBundleId) {
83-
await reopenAndroidApp(session.device, session.appBundleId);
84-
const focused = await waitForFocusedAndroidApp(
85-
session,
86-
session.appBundleId,
87-
readAndroidAppState,
88-
);
73+
await openAndroidApp(session.device, session.appBundleId);
74+
const focused = await waitForFocusedAndroidApp(session, session.appBundleId);
8975
if (!focused) {
9076
emitDiagnostic({
9177
level: 'warn',
@@ -126,11 +112,8 @@ export async function recoverAndroidBlockingSystemDialog(params: {
126112
}
127113
}
128114

129-
async function readAndroidSnapshotNodes(
130-
session: SessionState,
131-
snapshotAndroidUi: typeof snapshotAndroid,
132-
): Promise<SnapshotNode[]> {
133-
const rawSnapshot = await snapshotAndroidUi(session.device, {
115+
async function readAndroidSnapshotNodes(session: SessionState): Promise<SnapshotNode[]> {
116+
const rawSnapshot = await snapshotAndroid(session.device, {
134117
interactiveOnly: false,
135118
compact: false,
136119
});
@@ -147,34 +130,30 @@ function findCloseAppButton(nodes: SnapshotNode[]): SnapshotNode | undefined {
147130
});
148131
}
149132

150-
async function waitForBlockingDialogToDismiss(
151-
session: SessionState,
152-
snapshotAndroidUi: typeof snapshotAndroid,
153-
): Promise<boolean> {
133+
async function waitForBlockingDialogToDismiss(session: SessionState): Promise<boolean> {
154134
for (let attempt = 0; attempt < ANDROID_MODAL_POLL_ATTEMPTS; attempt += 1) {
155-
const nodes = await readAndroidSnapshotNodes(session, snapshotAndroidUi);
135+
const nodes = await readAndroidSnapshotNodes(session);
156136
if (!containsBlockingDialog(nodes)) {
157137
return true;
158138
}
159139
await sleep(ANDROID_MODAL_POLL_MS);
160140
}
161-
const nodes = await readAndroidSnapshotNodes(session, snapshotAndroidUi);
141+
const nodes = await readAndroidSnapshotNodes(session);
162142
return !containsBlockingDialog(nodes);
163143
}
164144

165145
async function waitForFocusedAndroidApp(
166146
session: SessionState,
167147
appBundleId: string,
168-
readAndroidAppState: typeof getAndroidAppState,
169148
): Promise<boolean> {
170149
for (let attempt = 0; attempt < ANDROID_MODAL_POLL_ATTEMPTS; attempt += 1) {
171-
const state = await readAndroidAppState(session.device);
150+
const state = await getAndroidAppState(session.device);
172151
if (state.package === appBundleId) {
173152
return true;
174153
}
175154
await sleep(ANDROID_MODAL_POLL_MS);
176155
}
177-
const state = await readAndroidAppState(session.device);
156+
const state = await getAndroidAppState(session.device);
178157
return state.package === appBundleId;
179158
}
180159

0 commit comments

Comments
 (0)