Skip to content

Commit 9a02da5

Browse files
thymikeeclaude
andcommitted
refactor: migrate session sub-handlers + request-router from test-only DI to vitest mocking
Remove ~35 DI parameters from session-close, session-open, session-deploy, session-device-utils, session-runtime, record-trace-recording, and request-router. All sub-handlers now use direct imports instead of threaded typeof params. Tests rewritten from node:test to vitest with vi.mock() replacing manual dependency injection. Closes #283 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 73298e5 commit 9a02da5

27 files changed

Lines changed: 3426 additions & 3530 deletions

‎src/daemon/__tests__/request-router-android-modal.vitest.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
import { test, expect, vi } from 'vitest';
1+
import { test, expect, vi, beforeEach } from 'vitest';
22
import fs from 'node:fs';
33
import os from 'node:os';
44
import path from 'node:path';
5+
6+
let snapshotCalls = 0;
7+
const dispatchCalls: string[][] = [];
8+
9+
vi.mock('../../core/dispatch.ts', async (importOriginal) => {
10+
const actual = await importOriginal<typeof import('../../core/dispatch.ts')>();
11+
return {
12+
...actual,
13+
dispatchCommand: vi.fn(async (_device: unknown, command: string, positionals: string[]) => {
14+
dispatchCalls.push([command, ...positionals]);
15+
return {};
16+
}),
17+
};
18+
});
19+
520
import { createRequestHandler } from '../request-router.ts';
621
import { SessionStore } from '../session-store.ts';
722
import type { SessionState } from '../types.ts';
823
import { LeaseRegistry } from '../lease-registry.ts';
924

10-
let snapshotCalls = 0;
11-
1225
vi.mock('../../platforms/android/index.ts', async (importOriginal) => {
1326
const actual = await importOriginal<typeof import('../../platforms/android/index.ts')>();
1427
return {
@@ -83,10 +96,10 @@ function makeAndroidSession(name: string): SessionState {
8396
test('generic Android gesture commands dismiss blocking system dialogs during recording', async () => {
8497
snapshotCalls = 0;
8598
execCalls.length = 0;
99+
dispatchCalls.length = 0;
86100

87101
const sessionStore = makeStore();
88102
sessionStore.set('default', makeAndroidSession('default'));
89-
const dispatchCalls: string[][] = [];
90103

91104
const { openAndroidApp } = await import('../../platforms/android/index.ts');
92105

@@ -96,10 +109,6 @@ test('generic Android gesture commands dismiss blocking system dialogs during re
96109
sessionStore,
97110
leaseRegistry: new LeaseRegistry(),
98111
trackDownloadableArtifact: () => 'artifact-id',
99-
dispatchCommand: async (_device, command, positionals) => {
100-
dispatchCalls.push([command, ...positionals]);
101-
return {};
102-
},
103112
});
104113

105114
const response = await handler({

src/daemon/__tests__/request-router-lock-policy.test.ts renamed to src/daemon/__tests__/request-router-lock-policy.vitest.ts

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import test from 'node:test';
2-
import assert from 'node:assert/strict';
1+
import { test, expect, vi, beforeEach } from 'vitest';
32
import fs from 'node:fs';
43
import os from 'node:os';
54
import path from 'node:path';
5+
6+
vi.mock('../../core/dispatch.ts', async (importOriginal) => {
7+
const actual = await importOriginal<typeof import('../../core/dispatch.ts')>();
8+
return { ...actual, dispatchCommand: vi.fn(async () => ({})) };
9+
});
10+
11+
import { dispatchCommand } from '../../core/dispatch.ts';
612
import { createRequestHandler } from '../request-router.ts';
713
import { SessionStore } from '../session-store.ts';
814
import type { SessionState } from '../types.ts';
915
import { LeaseRegistry } from '../lease-registry.ts';
1016

17+
const mockDispatch = vi.mocked(dispatchCommand);
18+
1119
function makeStore(): SessionStore {
1220
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-router-lock-'));
1321
return new SessionStore(path.join(tempRoot, 'sessions'));
@@ -30,21 +38,21 @@ function makeIosSession(name: string): SessionState {
3038
};
3139
}
3240

41+
beforeEach(() => {
42+
mockDispatch.mockReset();
43+
mockDispatch.mockResolvedValue({});
44+
});
45+
3346
test('direct daemon requests cannot bypass reject lock policy for existing sessions', async () => {
3447
const sessionStore = makeStore();
3548
sessionStore.set('qa-ios', makeIosSession('qa-ios'));
36-
let dispatchCalls = 0;
3749

3850
const handler = createRequestHandler({
3951
logPath: path.join(os.tmpdir(), 'daemon.log'),
4052
token: 'test-token',
4153
sessionStore,
4254
leaseRegistry: new LeaseRegistry(),
4355
trackDownloadableArtifact: () => 'artifact-id',
44-
dispatchCommand: async () => {
45-
dispatchCalls += 1;
46-
return {};
47-
},
4856
});
4957

5058
const response = await handler({
@@ -60,29 +68,24 @@ test('direct daemon requests cannot bypass reject lock policy for existing sessi
6068
},
6169
});
6270

63-
assert.equal(dispatchCalls, 0);
64-
assert.equal(response.ok, false);
71+
expect(mockDispatch).not.toHaveBeenCalled();
72+
expect(response.ok).toBe(false);
6573
if (!response.ok) {
66-
assert.equal(response.error.code, 'INVALID_ARGS');
67-
assert.match(response.error.message, /--udid=SIM-999/i);
74+
expect(response.error.code).toBe('INVALID_ARGS');
75+
expect(response.error.message).toMatch(/--udid=SIM-999/i);
6876
}
6977
});
7078

7179
test('batch steps cannot bypass reject lock policy on nested direct requests', async () => {
7280
const sessionStore = makeStore();
7381
sessionStore.set('qa-ios', makeIosSession('qa-ios'));
74-
let dispatchCalls = 0;
7582

7683
const handler = createRequestHandler({
7784
logPath: path.join(os.tmpdir(), 'daemon.log'),
7885
token: 'test-token',
7986
sessionStore,
8087
leaseRegistry: new LeaseRegistry(),
8188
trackDownloadableArtifact: () => 'artifact-id',
82-
dispatchCommand: async () => {
83-
dispatchCalls += 1;
84-
return {};
85-
},
8689
});
8790

8891
const response = await handler({
@@ -105,30 +108,30 @@ test('batch steps cannot bypass reject lock policy on nested direct requests', a
105108
},
106109
});
107110

108-
assert.equal(dispatchCalls, 0);
109-
assert.equal(response.ok, false);
111+
expect(mockDispatch).not.toHaveBeenCalled();
112+
expect(response.ok).toBe(false);
110113
if (!response.ok) {
111-
assert.equal(response.error.code, 'INVALID_ARGS');
112-
assert.match(response.error.message, /Batch failed at step 1/i);
113-
assert.match(response.error.message, /--serial=emulator-5554/i);
114+
expect(response.error.code).toBe('INVALID_ARGS');
115+
expect(response.error.message).toMatch(/Batch failed at step 1/i);
116+
expect(response.error.message).toMatch(/--serial=emulator-5554/i);
114117
}
115118
});
116119

117120
test('direct daemon requests apply strip lock policy for existing sessions before dispatch', async () => {
118121
const sessionStore = makeStore();
119122
sessionStore.set('qa-ios', makeIosSession('qa-ios'));
120123
let dispatchCalls = 0;
124+
mockDispatch.mockImplementation(async () => {
125+
dispatchCalls += 1;
126+
return {};
127+
});
121128

122129
const handler = createRequestHandler({
123130
logPath: path.join(os.tmpdir(), 'daemon.log'),
124131
token: 'test-token',
125132
sessionStore,
126133
leaseRegistry: new LeaseRegistry(),
127134
trackDownloadableArtifact: () => 'artifact-id',
128-
dispatchCommand: async () => {
129-
dispatchCalls += 1;
130-
return {};
131-
},
132135
});
133136

134137
const response = await handler({
@@ -146,13 +149,13 @@ test('direct daemon requests apply strip lock policy for existing sessions befor
146149
},
147150
});
148151

149-
assert.equal(dispatchCalls, 1);
150-
assert.equal(response.ok, true);
152+
expect(dispatchCalls).toBe(1);
153+
expect(response.ok).toBe(true);
151154
const action = sessionStore.get('qa-ios')?.actions.at(-1);
152-
assert.equal(action?.flags.platform, 'ios');
153-
assert.equal(action?.flags.udid, undefined);
154-
assert.equal(action?.flags.target, undefined);
155-
assert.equal(action?.flags.device, 'iPhone 16');
155+
expect(action?.flags.platform).toBe('ios');
156+
expect(action?.flags.udid).toBe(undefined);
157+
expect(action?.flags.target).toBe(undefined);
158+
expect(action?.flags.device).toBe('iPhone 16');
156159
});
157160

158161
test('batch preserves tenant-scoped session names across nested requests', async () => {
@@ -164,17 +167,17 @@ test('batch preserves tenant-scoped session names across nested requests', async
164167
runId: 'run-1',
165168
});
166169
let dispatchCalls = 0;
170+
mockDispatch.mockImplementation(async () => {
171+
dispatchCalls += 1;
172+
return {};
173+
});
167174

168175
const handler = createRequestHandler({
169176
logPath: path.join(os.tmpdir(), 'daemon.log'),
170177
token: 'test-token',
171178
sessionStore,
172179
leaseRegistry,
173180
trackDownloadableArtifact: () => 'artifact-id',
174-
dispatchCommand: async () => {
175-
dispatchCalls += 1;
176-
return {};
177-
},
178181
});
179182

180183
const response = await handler({
@@ -193,7 +196,7 @@ test('batch preserves tenant-scoped session names across nested requests', async
193196
},
194197
});
195198

196-
assert.equal(response.ok, true);
197-
assert.equal(dispatchCalls, 1);
198-
assert.equal(sessionStore.get('tenant-a:default')?.actions.at(-1)?.command, 'home');
199+
expect(response.ok).toBe(true);
200+
expect(dispatchCalls).toBe(1);
201+
expect(sessionStore.get('tenant-a:default')?.actions.at(-1)?.command).toBe('home');
199202
});

src/daemon/__tests__/request-router-recording-health.test.ts renamed to src/daemon/__tests__/request-router-recording-health.vitest.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1-
import test from 'node:test';
2-
import assert from 'node:assert/strict';
1+
import { test, expect, vi, beforeEach } from 'vitest';
32
import fs from 'node:fs';
43
import os from 'node:os';
54
import path from 'node:path';
5+
6+
vi.mock('../../core/dispatch.ts', async (importOriginal) => {
7+
const actual = await importOriginal<typeof import('../../core/dispatch.ts')>();
8+
return { ...actual, dispatchCommand: vi.fn(async () => ({})) };
9+
});
10+
11+
import { dispatchCommand } from '../../core/dispatch.ts';
612
import { createRequestHandler } from '../request-router.ts';
713
import { SessionStore } from '../session-store.ts';
814
import type { SessionState } from '../types.ts';
915
import { LeaseRegistry } from '../lease-registry.ts';
1016

17+
const mockDispatch = vi.mocked(dispatchCommand);
18+
1119
function makeStore(): SessionStore {
1220
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-router-recording-health-'));
1321
return new SessionStore(path.join(tempRoot, 'sessions'));
1422
}
1523

24+
beforeEach(() => {
25+
mockDispatch.mockReset();
26+
mockDispatch.mockResolvedValue({});
27+
});
28+
1629
test('router blocks non-record commands when recording was invalidated', async () => {
1730
const sessionStore = makeStore();
1831
const session: SessionState = {
@@ -41,17 +54,12 @@ test('router blocks non-record commands when recording was invalidated', async (
4154
};
4255
sessionStore.set('default', session);
4356

44-
let dispatched = false;
4557
const handler = createRequestHandler({
4658
logPath: path.join(os.tmpdir(), 'daemon.log'),
4759
token: 'test-token',
4860
sessionStore,
4961
leaseRegistry: new LeaseRegistry(),
5062
trackDownloadableArtifact: () => 'artifact-id',
51-
dispatchCommand: async () => {
52-
dispatched = true;
53-
return {};
54-
},
5563
});
5664

5765
const response = await handler({
@@ -62,11 +70,11 @@ test('router blocks non-record commands when recording was invalidated', async (
6270
meta: { requestId: 'req-invalidated-recording' },
6371
});
6472

65-
assert.equal(response.ok, false);
73+
expect(response.ok).toBe(false);
6674
if (response.ok) {
6775
return;
6876
}
69-
assert.equal(response.error.code, 'COMMAND_FAILED');
70-
assert.equal(response.error.message, 'iOS runner session restarted during recording');
71-
assert.equal(dispatched, false);
77+
expect(response.error.code).toBe('COMMAND_FAILED');
78+
expect(response.error.message).toBe('iOS runner session restarted during recording');
79+
expect(mockDispatch).not.toHaveBeenCalled();
7280
});

0 commit comments

Comments
 (0)