|
1 | 1 | import { resolveSnapshotRuntimePlan } from '@agent-device/contracts/platform'; |
| 2 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
2 | 3 | import { expect, test } from 'vitest'; |
3 | | -import { IOS_SIMULATOR } from '../../__tests__/test-utils/index.ts'; |
| 4 | +import { ANDROID_EMULATOR, IOS_SIMULATOR } from '../../__tests__/test-utils/index.ts'; |
4 | 5 | import { admitRuntimePlan } from '../handlers/session-runtime-admission.ts'; |
| 6 | +import type { BindDeviceRuntime } from '../request-runtime-binding.ts'; |
5 | 7 | import { bindSnapshotCaptureRuntime } from '../snapshot-runtime-binding.ts'; |
6 | 8 | import { snapshotRuntimeFixture } from './snapshot-runtime-fixture.ts'; |
7 | 9 |
|
8 | | -test('the snapshot binder takes an admission, never a bare plan', () => { |
| 10 | +test('the snapshot binder takes an admission, never a bare plan and never a separate device', async () => { |
| 11 | + const { inspectFacts } = snapshotRuntimeFixture(); |
9 | 12 | const plan = resolveSnapshotRuntimePlan({ customActions: false, hasActiveApp: true }); |
10 | | - // Type-level proof that admit-before-bind is enforced at the seam: the binder's parameter is |
11 | | - // AdmittedRuntimePlan<SnapshotRuntimePlan>, which only admitRuntimePlan produces. Widening it |
12 | | - // back to SnapshotRuntimePlan makes this directive unused and tsc fails. |
| 13 | + const admission = await admitRuntimePlan({ device: IOS_SIMULATOR, plan, inspectFacts }); |
| 14 | + // Type-level proof that admit-before-bind is enforced at the seam: the binder's only |
| 15 | + // input besides the request-scope binder is AdmittedRuntimePlan<SnapshotRuntimePlan>, which |
| 16 | + // only admitRuntimePlan produces and which names the device it was admitted for. Widening the |
| 17 | + // parameter back to a plan, or re-adding a device parameter, makes a directive below unused |
| 18 | + // and tsc fails. |
13 | 19 | // @ts-expect-error the facts-first binder requires the admission token, not the plan |
14 | | - const bindBarePlan = () => bindSnapshotCaptureRuntime(undefined, IOS_SIMULATOR, plan); |
15 | | - expect(bindBarePlan).toBeTypeOf('function'); |
| 20 | + const bindBarePlan = () => bindSnapshotCaptureRuntime(undefined, plan); |
| 21 | + // @ts-expect-error there is no separate device argument: the token's device is the bind target |
| 22 | + const bindOtherDevice = () => bindSnapshotCaptureRuntime(undefined, ANDROID_EMULATOR, admission); |
| 23 | + expect([bindBarePlan, bindOtherDevice].every((fn) => typeof fn === 'function')).toBe(true); |
16 | 24 | }); |
17 | 25 |
|
18 | | -test('binds the operation the admitted plan selected and nothing wider', async () => { |
| 26 | +test('binds exactly the device the facts were admitted for, and only the plan’s operation', async () => { |
19 | 27 | const { inspectFacts, bindDevice } = snapshotRuntimeFixture(); |
| 28 | + const boundDevices: DeviceInfo[] = []; |
| 29 | + const recordingBind: BindDeviceRuntime = async (device, use) => { |
| 30 | + boundDevices.push(device); |
| 31 | + return await bindDevice(device, use); |
| 32 | + }; |
20 | 33 | const plan = resolveSnapshotRuntimePlan({ customActions: false, hasActiveApp: true }); |
21 | 34 | const admission = await admitRuntimePlan({ device: IOS_SIMULATOR, plan, inspectFacts }); |
22 | 35 | if (!admission.admitted) throw new Error('fixture admits captureSnapshot on an iOS simulator'); |
23 | | - const runtime = await bindSnapshotCaptureRuntime(bindDevice, IOS_SIMULATOR, admission); |
| 36 | + |
| 37 | + const runtime = await bindSnapshotCaptureRuntime(recordingBind, admission); |
| 38 | + |
| 39 | + // Whatever a caller holds besides the token (a session, another device) is irrelevant to the |
| 40 | + // bind: the request-scope binder only ever sees the token's own device. |
| 41 | + expect(boundDevices).toEqual([IOS_SIMULATOR]); |
| 42 | + expect(admission.device).toBe(IOS_SIMULATOR); |
24 | 43 | expect(Object.keys(runtime)).toEqual(['captureSnapshot']); |
25 | 44 | }); |
0 commit comments