|
| 1 | +import { createTestDeviceInventoryGateways } from '@agent-device/device-selection/inventory-gateways-fixtures'; |
| 2 | +import { test, expect } from 'vitest'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { fakeCreateCommandSurface } from '../../__tests__/support/command-surface-fake.ts'; |
| 5 | +import { |
| 6 | + createRequestHandler, |
| 7 | + lifecycleDeviceRuntimeGateway, |
| 8 | +} from '../../__tests__/support/test-device-runtime-gateway.ts'; |
| 9 | +import type { DaemonRequest } from '../../core/daemon-request.ts'; |
| 10 | +import { LeaseRegistry } from '../../resources/lease-registry.ts'; |
| 11 | +import { makeSessionStore } from '../../__tests__/support/store-factory.ts'; |
| 12 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 13 | +import { PRE_DISPATCH_REFUSAL_REASONS } from '@agent-device/kernel/errors'; |
| 14 | +import { mkdtempForTestSync } from '@agent-device/test-kit'; |
| 15 | + |
| 16 | +// #3674: a run whose daemon was replaced mid-test keeps sending commands to the session it opened. |
| 17 | +// The answer has to name the session that went missing, and must not depend on how many devices |
| 18 | +// happen to be booted on the host. |
| 19 | + |
| 20 | +const LOST_SESSION = 'e2e-ios-0'; |
| 21 | + |
| 22 | +function bootedSimulator(id: string, name: string): DeviceInfo { |
| 23 | + return { |
| 24 | + platform: 'apple', |
| 25 | + target: 'mobile', |
| 26 | + id, |
| 27 | + name, |
| 28 | + kind: 'simulator', |
| 29 | + booted: true, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +const BOOTED_SIMULATORS: DeviceInfo[] = [ |
| 34 | + bootedSimulator('SIM-A', 'TinyConcepts E2E'), |
| 35 | + bootedSimulator('SIM-B', 'Perf iPhone 17'), |
| 36 | +]; |
| 37 | + |
| 38 | +function makeHandler(listings: { count: number }) { |
| 39 | + const sessionStore = makeSessionStore('agent-device-router-named-session-'); |
| 40 | + const handler = createRequestHandler({ |
| 41 | + createCommandSurface: fakeCreateCommandSurface, |
| 42 | + logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'), |
| 43 | + token: 'test-token', |
| 44 | + sessionStore, |
| 45 | + leaseRegistry: new LeaseRegistry(), |
| 46 | + deviceRuntimeGateway: lifecycleDeviceRuntimeGateway, |
| 47 | + deviceInventoryGateways: createTestDeviceInventoryGateways({ |
| 48 | + local: async () => { |
| 49 | + listings.count += 1; |
| 50 | + return BOOTED_SIMULATORS; |
| 51 | + }, |
| 52 | + }), |
| 53 | + trackDownloadableArtifact: () => 'artifact-id', |
| 54 | + }); |
| 55 | + return { handler, sessionStore }; |
| 56 | +} |
| 57 | + |
| 58 | +function request(overrides: Partial<DaemonRequest> = {}): DaemonRequest { |
| 59 | + return { |
| 60 | + token: 'test-token', |
| 61 | + session: LOST_SESSION, |
| 62 | + command: 'snapshot', |
| 63 | + positionals: [], |
| 64 | + flags: {}, |
| 65 | + meta: { requestId: 'named-session-request', sessionExplicit: true }, |
| 66 | + ...overrides, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +test('a command naming a session this daemon does not hold answers on the session, not the device', async () => { |
| 71 | + const { handler } = makeHandler({ count: 0 }); |
| 72 | + |
| 73 | + const response = await handler(request()); |
| 74 | + |
| 75 | + expect(response.ok).toBe(false); |
| 76 | + if (response.ok) return; |
| 77 | + expect(response.error.code).toBe('SESSION_NOT_FOUND'); |
| 78 | + expect(response.error.message).toContain(`No active session "${LOST_SESSION}"`); |
| 79 | + expect(response.error.details?.reason).toBe(PRE_DISPATCH_REFUSAL_REASONS.namedSessionNotFound); |
| 80 | + expect(response.error.details?.dispatched).toBe('no'); |
| 81 | + expect(response.error.details?.session).toBe(LOST_SESSION); |
| 82 | +}); |
| 83 | + |
| 84 | +test('the refusal answers without listing the host devices at all', async () => { |
| 85 | + const listings = { count: 0 }; |
| 86 | + const { handler } = makeHandler(listings); |
| 87 | + |
| 88 | + await handler(request()); |
| 89 | + |
| 90 | + expect(listings.count).toBe(0); |
| 91 | +}); |
| 92 | + |
| 93 | +test('the same command with no session named still resolves a device sessionlessly', async () => { |
| 94 | + // The closest negative: same daemon, same two booted simulators, same command. What changes is |
| 95 | + // only whether the caller named a session, and that is what the answer must turn on. |
| 96 | + const listings = { count: 0 }; |
| 97 | + const { handler } = makeHandler(listings); |
| 98 | + |
| 99 | + const response = await handler( |
| 100 | + request({ session: 'default', meta: { requestId: 'sessionless-request' } }), |
| 101 | + ); |
| 102 | + |
| 103 | + expect(response.ok).toBe(false); |
| 104 | + if (response.ok) return; |
| 105 | + expect(response.error.code).toBe('AMBIGUOUS_MATCH'); |
| 106 | + expect(listings.count).toBeGreaterThan(0); |
| 107 | +}); |
| 108 | + |
| 109 | +test('a named session beside a device selector keeps honouring the device it names', async () => { |
| 110 | + // The other closest negative: the same missing session, but the request states its target |
| 111 | + // outright. Then the session is not what is missing and the refusal must stay out of the way. |
| 112 | + const listings = { count: 0 }; |
| 113 | + const { handler } = makeHandler(listings); |
| 114 | + |
| 115 | + const response = await handler(request({ flags: { platform: 'ios', udid: 'SIM-A' } })); |
| 116 | + |
| 117 | + if (!response.ok) { |
| 118 | + expect(response.error.details?.reason).not.toBe( |
| 119 | + PRE_DISPATCH_REFUSAL_REASONS.namedSessionNotFound, |
| 120 | + ); |
| 121 | + } |
| 122 | + expect(listings.count).toBeGreaterThan(0); |
| 123 | +}); |
| 124 | + |
| 125 | +test('a sessionless close naming a missing session answers on the session', async () => { |
| 126 | + const { handler } = makeHandler({ count: 0 }); |
| 127 | + |
| 128 | + const response = await handler(request({ command: 'close', positionals: ['com.example.app'] })); |
| 129 | + |
| 130 | + expect(response.ok).toBe(false); |
| 131 | + if (response.ok) return; |
| 132 | + expect(response.error.code).toBe('SESSION_NOT_FOUND'); |
| 133 | + expect(response.error.message).toContain(`No active session "${LOST_SESSION}"`); |
| 134 | +}); |
0 commit comments