|
1 | 1 | import { test } from 'vitest'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | | -import { createAgentDeviceClient, type AgentDeviceClientConfig } from '../client/client.ts'; |
| 3 | +import { |
| 4 | + createAgentDeviceClient, |
| 5 | + type AgentDeviceClient, |
| 6 | + type AgentDeviceClientConfig, |
| 7 | + type PrepareCommandResult, |
| 8 | + type PushCommandResult, |
| 9 | + type TriggerAppEventCommandResult, |
| 10 | + type WaitCommandResult, |
| 11 | +} from '../client/client.ts'; |
4 | 12 | import { runCommand } from '../commands/command-surface.ts'; |
| 13 | +import type { CommandResult } from '../core/command-descriptor/command-result.ts'; |
5 | 14 | import type { DaemonRequest, DaemonResponse, DaemonResponseData } from '../kernel/contracts.ts'; |
6 | 15 | import { AppError } from '../kernel/errors.ts'; |
7 | 16 |
|
| 17 | +type Equal<A, B> = |
| 18 | + (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false; |
| 19 | + |
| 20 | +const closedProjectionResponses: Record<string, DaemonResponseData> = { |
| 21 | + wait: { waitedMs: 25, text: 'Ready' }, |
| 22 | + prepare: { |
| 23 | + action: 'ios-runner', |
| 24 | + platform: 'ios', |
| 25 | + deviceId: 'SIM-001', |
| 26 | + deviceName: 'iPhone 16', |
| 27 | + kind: 'simulator', |
| 28 | + durationMs: 30, |
| 29 | + runner: { uptimeMs: 42 }, |
| 30 | + cache: 'exact', |
| 31 | + artifact: 'valid', |
| 32 | + buildMs: 10, |
| 33 | + connectMs: 20, |
| 34 | + healthCheckMs: 10, |
| 35 | + xctestrunPath: '/tmp/AgentDevice.xctestrun', |
| 36 | + timing: { |
| 37 | + totalMs: 30, |
| 38 | + additiveParts: { buildMs: 10, connectAfterBuildMs: 10, healthCheckMs: 10 }, |
| 39 | + containment: { connectMs: ['buildMs'], healthCheckMs: [] }, |
| 40 | + note: 'Use additiveParts.', |
| 41 | + }, |
| 42 | + message: 'Prepared Apple runner: iPhone 16', |
| 43 | + }, |
| 44 | + push: { |
| 45 | + platform: 'android', |
| 46 | + package: 'com.example.demo', |
| 47 | + action: 'com.example.demo.TEST_PUSH', |
| 48 | + extrasCount: 1, |
| 49 | + message: 'Pushed notification to com.example.demo', |
| 50 | + }, |
| 51 | + 'trigger-app-event': { |
| 52 | + event: 'screenshot_taken', |
| 53 | + eventUrl: 'demo://agent-device/event?name=screenshot_taken', |
| 54 | + transport: 'deep-link', |
| 55 | + message: 'Triggered app event: screenshot_taken', |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
8 | 59 | function createTransport( |
9 | 60 | handler: (req: Omit<DaemonRequest, 'token'>) => Promise<DaemonResponse> | DaemonResponse, |
10 | 61 | ): { |
@@ -35,6 +86,43 @@ function createTransport( |
35 | 86 | }; |
36 | 87 | } |
37 | 88 |
|
| 89 | +test('client exposes narrowed result types for closed daemon projections', async () => { |
| 90 | + const setup = createTransport(async (req) => closedProjectionResponse(req.command)); |
| 91 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 92 | + |
| 93 | + const waitResult = await client.command.wait({ durationMs: 25 }); |
| 94 | + const prepareResult = await client.command.prepare({ action: 'ios-runner' }); |
| 95 | + const pushResult = await client.apps.push({ |
| 96 | + app: 'com.example.demo', |
| 97 | + payload: { extras: { source: 'test' } }, |
| 98 | + }); |
| 99 | + const triggerResult = await client.apps.triggerEvent({ event: 'screenshot_taken' }); |
| 100 | + |
| 101 | + const waitType: Equal<typeof waitResult, WaitCommandResult> = true; |
| 102 | + const prepareType: Equal<typeof prepareResult, PrepareCommandResult> = true; |
| 103 | + const pushType: Equal<typeof pushResult, PushCommandResult> = true; |
| 104 | + const triggerType: Equal<typeof triggerResult, TriggerAppEventCommandResult> = true; |
| 105 | + const clientWaitType: Equal< |
| 106 | + Awaited<ReturnType<AgentDeviceClient['command']['wait']>>, |
| 107 | + CommandResult<'wait'> |
| 108 | + > = true; |
| 109 | + |
| 110 | + assert.deepEqual( |
| 111 | + [waitType, prepareType, pushType, triggerType, clientWaitType], |
| 112 | + [true, true, true, true, true], |
| 113 | + ); |
| 114 | + assert.deepEqual(waitResult, { waitedMs: 25, text: 'Ready' }); |
| 115 | + assert.equal(prepareResult.timing.additiveParts.connectAfterBuildMs, 10); |
| 116 | + assert.equal(pushResult.platform, 'android'); |
| 117 | + assert.equal(triggerResult.transport, 'deep-link'); |
| 118 | +}); |
| 119 | + |
| 120 | +function closedProjectionResponse(command: string): DaemonResponse { |
| 121 | + const data = closedProjectionResponses[command]; |
| 122 | + if (!data) throw new Error(`Unexpected command: ${command}`); |
| 123 | + return { ok: true, data }; |
| 124 | +} |
| 125 | + |
38 | 126 | test('apps.open resolves session device identifiers from open response', async () => { |
39 | 127 | const setup = createTransport(async (req) => { |
40 | 128 | if (req.command === 'open') { |
|
0 commit comments