-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathrequest-router-lock-policy.test.ts
More file actions
200 lines (179 loc) · 5.66 KB
/
request-router-lock-policy.test.ts
File metadata and controls
200 lines (179 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { test, expect, vi, beforeEach } from 'vitest';
import os from 'node:os';
import path from 'node:path';
vi.mock('../../core/dispatch.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../core/dispatch.ts')>();
return { ...actual, dispatchCommand: vi.fn(async () => ({})) };
});
import { dispatchCommand } from '../../core/dispatch.ts';
import { createRequestHandler } from '../request-router.ts';
import type { SessionState } from '../types.ts';
import { LeaseRegistry } from '../lease-registry.ts';
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
const mockDispatch = vi.mocked(dispatchCommand);
function makeIosSession(name: string): SessionState {
return {
name,
createdAt: Date.now(),
actions: [],
device: {
platform: 'ios',
target: 'mobile',
id: 'SIM-001',
name: 'iPhone 16',
kind: 'simulator',
booted: true,
simulatorSetPath: '/tmp/tenant-a/set',
},
};
}
beforeEach(() => {
mockDispatch.mockReset();
mockDispatch.mockResolvedValue({});
});
test('direct daemon requests cannot bypass reject lock policy for existing sessions', async () => {
const sessionStore = makeSessionStore('agent-device-router-lock-');
sessionStore.set('qa-ios', makeIosSession('qa-ios'));
const handler = createRequestHandler({
logPath: path.join(os.tmpdir(), 'daemon.log'),
token: 'test-token',
sessionStore,
leaseRegistry: new LeaseRegistry(),
trackDownloadableArtifact: () => 'artifact-id',
});
const response = await handler({
token: 'test-token',
session: 'qa-ios',
command: 'home',
positionals: [],
flags: {
udid: 'SIM-999',
},
meta: {
lockPolicy: 'reject',
},
});
expect(mockDispatch).not.toHaveBeenCalled();
expect(response.ok).toBe(false);
if (!response.ok) {
expect(response.error.code).toBe('INVALID_ARGS');
expect(response.error.message).toMatch(/--udid=SIM-999/i);
expect(response.error.hint).toMatch(/agent-device session list/i);
expect(response.error.hint).toMatch(/agent-device close --session qa-ios/i);
}
});
test('batch steps cannot bypass reject lock policy on nested direct requests', async () => {
const sessionStore = makeSessionStore('agent-device-router-lock-');
sessionStore.set('qa-ios', makeIosSession('qa-ios'));
const handler = createRequestHandler({
logPath: path.join(os.tmpdir(), 'daemon.log'),
token: 'test-token',
sessionStore,
leaseRegistry: new LeaseRegistry(),
trackDownloadableArtifact: () => 'artifact-id',
});
const response = await handler({
token: 'test-token',
session: 'qa-ios',
command: 'batch',
positionals: [],
flags: {
batchSteps: [
{
command: 'home',
flags: {
serial: 'emulator-5554',
},
},
],
},
meta: {
lockPolicy: 'reject',
},
});
expect(mockDispatch).not.toHaveBeenCalled();
expect(response.ok).toBe(false);
if (!response.ok) {
expect(response.error.code).toBe('INVALID_ARGS');
expect(response.error.message).toMatch(/Batch failed at step 1/i);
expect(response.error.message).toMatch(/--serial=emulator-5554/i);
expect(response.error.hint).toMatch(/agent-device session list/i);
expect(response.error.hint).toMatch(/agent-device close --session qa-ios/i);
}
});
test('direct daemon requests apply strip lock policy for existing sessions before dispatch', async () => {
const sessionStore = makeSessionStore('agent-device-router-lock-');
sessionStore.set('qa-ios', makeIosSession('qa-ios'));
let dispatchCalls = 0;
mockDispatch.mockImplementation(async () => {
dispatchCalls += 1;
return {};
});
const handler = createRequestHandler({
logPath: path.join(os.tmpdir(), 'daemon.log'),
token: 'test-token',
sessionStore,
leaseRegistry: new LeaseRegistry(),
trackDownloadableArtifact: () => 'artifact-id',
});
const response = await handler({
token: 'test-token',
session: 'qa-ios',
command: 'home',
positionals: [],
flags: {
target: 'tv',
udid: 'SIM-999',
device: 'iPhone 16',
},
meta: {
lockPolicy: 'strip',
},
});
expect(dispatchCalls).toBe(1);
expect(response.ok).toBe(true);
const action = sessionStore.get('qa-ios')?.actions.at(-1);
expect(action?.flags.platform).toBe('ios');
expect(action?.flags.udid).toBe(undefined);
expect(action?.flags.target).toBe(undefined);
expect(action?.flags.device).toBe('iPhone 16');
});
test('batch preserves tenant-scoped session names across nested requests', async () => {
const sessionStore = makeSessionStore('agent-device-router-lock-');
sessionStore.set('tenant-a:default', makeIosSession('tenant-a:default'));
const leaseRegistry = new LeaseRegistry();
const lease = leaseRegistry.allocateLease({
tenantId: 'tenant-a',
runId: 'run-1',
});
let dispatchCalls = 0;
mockDispatch.mockImplementation(async () => {
dispatchCalls += 1;
return {};
});
const handler = createRequestHandler({
logPath: path.join(os.tmpdir(), 'daemon.log'),
token: 'test-token',
sessionStore,
leaseRegistry,
trackDownloadableArtifact: () => 'artifact-id',
});
const response = await handler({
token: 'test-token',
session: 'default',
command: 'batch',
positionals: [],
flags: {
batchSteps: [{ command: 'home' }],
},
meta: {
tenantId: 'tenant-a',
runId: 'run-1',
leaseId: lease.leaseId,
sessionIsolation: 'tenant',
},
});
expect(response.ok).toBe(true);
expect(dispatchCalls).toBe(1);
expect(sessionStore.get('tenant-a:default')?.actions.at(-1)?.command).toBe('home');
});