-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathrequest-execution-scope.test.ts
More file actions
191 lines (175 loc) · 5.99 KB
/
request-execution-scope.test.ts
File metadata and controls
191 lines (175 loc) · 5.99 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
import { afterAll, test, expect } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { withDiagnosticsScope } from '../../utils/diagnostics.ts';
import {
makeAndroidSession,
makeIosSession,
} from '../../__tests__/test-utils/session-factories.ts';
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
import { LeaseRegistry } from '../lease-registry.ts';
import { clearRequestCanceled, markRequestCanceled } from '../request-cancel.ts';
import {
createRequestExecutionScope,
prepareLockedRequestScope,
} from '../request-execution-scope.ts';
import type { DaemonRequest } from '../types.ts';
const TEST_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-request-execution-scope-'));
const LOG_PATH = path.join(TEST_ROOT, 'diagnostics.log');
afterAll(() => {
fs.rmSync(TEST_ROOT, { recursive: true, force: true });
});
test('createRequestExecutionScope applies tenant scoping and lease admission', async () => {
const sessionStore = makeSessionStore('agent-device-request-scope-');
const leaseRegistry = new LeaseRegistry();
const lease = leaseRegistry.allocateLease({ tenantId: 'tenant-a', runId: 'run-1' });
const scope = await createRequestExecutionScope({
req: makeRequest({
session: 'default',
command: 'snapshot',
meta: {
tenantId: 'tenant-a',
runId: 'run-1',
leaseId: lease.leaseId,
sessionIsolation: 'tenant',
},
}),
sessionStore,
leaseRegistry,
});
expect(scope.req.session).toBe('tenant-a:default');
expect(scope.req.meta?.tenantId).toBe('tenant-a');
expect(scope.sessionName).toBe('tenant-a:default');
});
test('createRequestExecutionScope rejects tenant requests without an active lease', async () => {
await expect(
createRequestExecutionScope({
req: makeRequest({
session: 'default',
command: 'snapshot',
meta: {
tenantId: 'tenant-a',
runId: 'run-1',
leaseId: '0'.repeat(32),
sessionIsolation: 'tenant',
},
}),
sessionStore: makeSessionStore('agent-device-request-scope-'),
leaseRegistry: new LeaseRegistry(),
}),
).rejects.toThrow(/Lease is not active/);
});
test('prepareLockedRequestScope preserves existing-session selector validation', async () => {
const sessionStore = makeSessionStore('agent-device-request-scope-');
sessionStore.set('default', makeAndroidSession('default'));
const scope = await createRequestExecutionScope({
req: makeRequest({
command: 'snapshot',
flags: {
platform: 'ios',
},
}),
sessionStore,
leaseRegistry: new LeaseRegistry(),
});
expect(() =>
prepareLockedRequestScope({
scope,
logPath: LOG_PATH,
sessionStore,
trackDownloadableArtifact: () => 'artifact-id',
}),
).toThrow(/already bound to android device "Pixel" \(emulator-5554\).*--platform=ios/i);
});
test('prepareLockedRequestScope blocks commands for invalidated recordings before handlers run', async () => {
const sessionStore = makeSessionStore('agent-device-request-scope-');
sessionStore.set(
'default',
makeIosSession('default', {
recording: {
platform: 'ios-device-runner',
outPath: '/tmp/recording.mp4',
remotePath: '/tmp/remote.mp4',
startedAt: Date.now(),
showTouches: true,
gestureEvents: [],
invalidatedReason: 'iOS runner session restarted during recording',
},
}),
);
const scope = await createRequestExecutionScope({
req: makeRequest({ command: 'snapshot' }),
sessionStore,
leaseRegistry: new LeaseRegistry(),
});
const result = await withDiagnosticsScope({ command: 'snapshot', logPath: LOG_PATH }, async () =>
prepareLockedRequestScope({
scope,
logPath: LOG_PATH,
sessionStore,
trackDownloadableArtifact: () => 'artifact-id',
}),
);
expect(result.type).toBe('response');
if (result.type === 'response') {
expect(result.response.ok).toBe(false);
if (!result.response.ok) {
expect(result.response.error.message).toBe('iOS runner session restarted during recording');
}
}
});
test('runLocked rejects a canceled request before executing work', async () => {
const requestId = 'request-scope-canceled-before-lock';
const scope = await createRequestExecutionScope({
req: makeRequest({ meta: { requestId } }),
sessionStore: makeSessionStore('agent-device-request-scope-'),
leaseRegistry: new LeaseRegistry(),
});
markRequestCanceled(requestId);
try {
await expect(scope.runLocked(async () => 'ran')).rejects.toThrow(/request canceled/);
} finally {
clearRequestCanceled(requestId);
}
});
test('runLocked rejects a request canceled while waiting for its execution lock', async () => {
const requestId = 'request-scope-canceled-after-lock';
const sessionStore = makeSessionStore('agent-device-request-scope-');
sessionStore.set('default', makeIosSession('default'));
const leaseRegistry = new LeaseRegistry();
const first = await createRequestExecutionScope({
req: makeRequest({ command: 'click' }),
sessionStore,
leaseRegistry,
});
const second = await createRequestExecutionScope({
req: makeRequest({ command: 'click', meta: { requestId } }),
sessionStore,
leaseRegistry,
});
let releaseLock: () => void = () => {};
const lockReleased = new Promise<void>((resolve) => {
releaseLock = resolve;
});
const firstRun = first.runLocked(async () => await lockReleased);
const secondRun = second.runLocked(async () => 'ran');
const secondExpectation = expect(secondRun).rejects.toThrow(/request canceled/);
markRequestCanceled(requestId);
releaseLock();
try {
await firstRun;
await secondExpectation;
} finally {
clearRequestCanceled(requestId);
}
});
function makeRequest(overrides: Partial<DaemonRequest> = {}): DaemonRequest {
return {
token: 'test-token',
session: 'default',
command: 'snapshot',
positionals: [],
...overrides,
};
}