|
| 1 | +import { test, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +vi.mock('../../core/dispatch.ts', async (importOriginal) => { |
| 6 | + const actual = await importOriginal<typeof import('../../core/dispatch.ts')>(); |
| 7 | + return { ...actual, dispatchCommand: vi.fn(async () => ({})) }; |
| 8 | +}); |
| 9 | + |
| 10 | +vi.mock('../../platforms/ios/runner-client.ts', async (importOriginal) => { |
| 11 | + const actual = await importOriginal<typeof import('../../platforms/ios/runner-client.ts')>(); |
| 12 | + return { ...actual, stopIosRunnerSession: vi.fn(async () => {}) }; |
| 13 | +}); |
| 14 | + |
| 15 | +vi.mock('../device-ready.ts', () => ({ ensureDeviceReady: vi.fn(async () => {}) })); |
| 16 | + |
| 17 | +import { dispatchCommand } from '../../core/dispatch.ts'; |
| 18 | +import { createRequestHandler } from '../request-router.ts'; |
| 19 | +import type { DaemonRequest, SessionState } from '../types.ts'; |
| 20 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 21 | +import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
| 22 | +import { AppError, retriableForErrorCode } from '../../utils/errors.ts'; |
| 23 | +import { supportedPlatformsForCommand } from '../../core/capabilities.ts'; |
| 24 | + |
| 25 | +const mockDispatch = vi.mocked(dispatchCommand); |
| 26 | + |
| 27 | +function makeIosSession(name: string): SessionState { |
| 28 | + return { |
| 29 | + name, |
| 30 | + createdAt: 1_700_000_000_000, |
| 31 | + actions: [], |
| 32 | + device: { |
| 33 | + platform: 'ios', |
| 34 | + target: 'mobile', |
| 35 | + id: 'SIM-001', |
| 36 | + name: 'iPhone 16', |
| 37 | + kind: 'simulator', |
| 38 | + booted: true, |
| 39 | + simulatorSetPath: '/tmp/tenant-a/set', |
| 40 | + }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function makeHandler(sessionStore = makeSessionStore('agent-device-router-typed-error-')) { |
| 45 | + return { |
| 46 | + sessionStore, |
| 47 | + handler: createRequestHandler({ |
| 48 | + logPath: path.join(os.tmpdir(), 'daemon.log'), |
| 49 | + token: 'test-token', |
| 50 | + sessionStore, |
| 51 | + leaseRegistry: new LeaseRegistry(), |
| 52 | + trackDownloadableArtifact: () => 'artifact-id', |
| 53 | + }), |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function request(command: string, overrides: Partial<DaemonRequest> = {}): DaemonRequest { |
| 58 | + return { |
| 59 | + token: 'test-token', |
| 60 | + session: 'typed-error', |
| 61 | + command, |
| 62 | + positionals: [], |
| 63 | + flags: {}, |
| 64 | + ...overrides, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +beforeEach(() => { |
| 69 | + mockDispatch.mockReset(); |
| 70 | +}); |
| 71 | + |
| 72 | +test('retriableForErrorCode is a conservative policy: transient => true, others => undefined', () => { |
| 73 | + expect(retriableForErrorCode('DEVICE_IN_USE')).toBe(true); |
| 74 | + expect(retriableForErrorCode('INVALID_ARGS')).toBeUndefined(); |
| 75 | + expect(retriableForErrorCode('UNSUPPORTED_OPERATION')).toBeUndefined(); |
| 76 | + expect(retriableForErrorCode('COMMAND_FAILED')).toBeUndefined(); |
| 77 | +}); |
| 78 | + |
| 79 | +test('UNSUPPORTED_OPERATION errors carry supportedOn derived from the capability matrix', async () => { |
| 80 | + const { sessionStore, handler } = makeHandler(); |
| 81 | + sessionStore.set('typed-error', makeIosSession('typed-error')); |
| 82 | + mockDispatch.mockRejectedValue(new AppError('UNSUPPORTED_OPERATION', 'nope on this platform')); |
| 83 | + |
| 84 | + // `home` routes through the (mocked) generic dispatch and is platform-restricted. |
| 85 | + const response = await handler(request('home')); |
| 86 | + |
| 87 | + expect(response.ok).toBe(false); |
| 88 | + if (response.ok) return; |
| 89 | + const expected = supportedPlatformsForCommand('home'); |
| 90 | + expect(expected.length).toBeGreaterThan(0); // home is a platform-restricted command |
| 91 | + expect(response.error.supportedOn).toBe(expected.join(', ')); |
| 92 | +}); |
| 93 | + |
| 94 | +test('DEVICE_IN_USE errors are flagged retriable; supportedOn stays absent', async () => { |
| 95 | + const { sessionStore, handler } = makeHandler(); |
| 96 | + sessionStore.set('typed-error', makeIosSession('typed-error')); |
| 97 | + mockDispatch.mockRejectedValue(new AppError('DEVICE_IN_USE', 'device busy')); |
| 98 | + |
| 99 | + const response = await handler(request('home')); |
| 100 | + |
| 101 | + expect(response.ok).toBe(false); |
| 102 | + if (response.ok) return; |
| 103 | + expect(response.error.retriable).toBe(true); |
| 104 | + expect('supportedOn' in response.error).toBe(false); |
| 105 | +}); |
| 106 | + |
| 107 | +test('deterministic errors (INVALID_ARGS) are returned with the default shape — no typed-error fields', async () => { |
| 108 | + const { sessionStore, handler } = makeHandler(); |
| 109 | + sessionStore.set('typed-error', makeIosSession('typed-error')); |
| 110 | + |
| 111 | + // Conflicting explicit selector under a reject lock policy fails with INVALID_ARGS |
| 112 | + // before dispatch — a deterministic error. |
| 113 | + const response = await handler( |
| 114 | + request('home', { flags: { udid: 'SIM-999' }, meta: { lockPolicy: 'reject' } }), |
| 115 | + ); |
| 116 | + |
| 117 | + expect(response.ok).toBe(false); |
| 118 | + if (response.ok) return; |
| 119 | + expect(response.error.code).toBe('INVALID_ARGS'); |
| 120 | + expect('retriable' in response.error).toBe(false); |
| 121 | + expect('supportedOn' in response.error).toBe(false); |
| 122 | + expect(mockDispatch).not.toHaveBeenCalled(); |
| 123 | +}); |
0 commit comments