|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { handleSessionCommands } from '../session.ts'; |
| 7 | +import { SessionStore } from '../../session-store.ts'; |
| 8 | +import type { DaemonRequest, DaemonResponse, SessionState } from '../../types.ts'; |
| 9 | + |
| 10 | +function makeSessionStore(): SessionStore { |
| 11 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-session-handler-')); |
| 12 | + return new SessionStore(path.join(root, 'sessions')); |
| 13 | +} |
| 14 | + |
| 15 | +function makeSession(name: string, device: SessionState['device']): SessionState { |
| 16 | + return { |
| 17 | + name, |
| 18 | + device, |
| 19 | + createdAt: Date.now(), |
| 20 | + actions: [], |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +const noopInvoke = async (_req: DaemonRequest): Promise<DaemonResponse> => ({ ok: true, data: {} }); |
| 25 | + |
| 26 | +test('boot requires session or explicit selector', async () => { |
| 27 | + const sessionStore = makeSessionStore(); |
| 28 | + const response = await handleSessionCommands({ |
| 29 | + req: { |
| 30 | + token: 't', |
| 31 | + session: 'default', |
| 32 | + command: 'boot', |
| 33 | + positionals: [], |
| 34 | + flags: {}, |
| 35 | + }, |
| 36 | + sessionName: 'default', |
| 37 | + logPath: path.join(os.tmpdir(), 'daemon.log'), |
| 38 | + sessionStore, |
| 39 | + invoke: noopInvoke, |
| 40 | + ensureReady: async () => {}, |
| 41 | + }); |
| 42 | + assert.ok(response); |
| 43 | + assert.equal(response?.ok, false); |
| 44 | + if (response && !response.ok) { |
| 45 | + assert.equal(response.error.code, 'INVALID_ARGS'); |
| 46 | + } |
| 47 | +}); |
| 48 | + |
| 49 | +test('boot rejects unsupported iOS device kind', async () => { |
| 50 | + const sessionStore = makeSessionStore(); |
| 51 | + const sessionName = 'ios-device-session'; |
| 52 | + sessionStore.set( |
| 53 | + sessionName, |
| 54 | + makeSession(sessionName, { |
| 55 | + platform: 'ios', |
| 56 | + id: 'ios-device-1', |
| 57 | + name: 'iPhone Device', |
| 58 | + kind: 'device', |
| 59 | + booted: true, |
| 60 | + }), |
| 61 | + ); |
| 62 | + const response = await handleSessionCommands({ |
| 63 | + req: { |
| 64 | + token: 't', |
| 65 | + session: sessionName, |
| 66 | + command: 'boot', |
| 67 | + positionals: [], |
| 68 | + flags: {}, |
| 69 | + }, |
| 70 | + sessionName, |
| 71 | + logPath: path.join(os.tmpdir(), 'daemon.log'), |
| 72 | + sessionStore, |
| 73 | + invoke: noopInvoke, |
| 74 | + ensureReady: async () => { |
| 75 | + throw new Error('ensureReady should not be called for unsupported boot'); |
| 76 | + }, |
| 77 | + }); |
| 78 | + assert.ok(response); |
| 79 | + assert.equal(response?.ok, false); |
| 80 | + if (response && !response.ok) { |
| 81 | + assert.equal(response.error.code, 'UNSUPPORTED_OPERATION'); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +test('boot succeeds for supported device in session', async () => { |
| 86 | + const sessionStore = makeSessionStore(); |
| 87 | + const sessionName = 'android-session'; |
| 88 | + sessionStore.set( |
| 89 | + sessionName, |
| 90 | + makeSession(sessionName, { |
| 91 | + platform: 'android', |
| 92 | + id: 'emulator-5554', |
| 93 | + name: 'Pixel Emulator', |
| 94 | + kind: 'emulator', |
| 95 | + booted: true, |
| 96 | + }), |
| 97 | + ); |
| 98 | + let ensureCalls = 0; |
| 99 | + const response = await handleSessionCommands({ |
| 100 | + req: { |
| 101 | + token: 't', |
| 102 | + session: sessionName, |
| 103 | + command: 'boot', |
| 104 | + positionals: [], |
| 105 | + flags: {}, |
| 106 | + }, |
| 107 | + sessionName, |
| 108 | + logPath: path.join(os.tmpdir(), 'daemon.log'), |
| 109 | + sessionStore, |
| 110 | + invoke: noopInvoke, |
| 111 | + ensureReady: async () => { |
| 112 | + ensureCalls += 1; |
| 113 | + }, |
| 114 | + }); |
| 115 | + assert.ok(response); |
| 116 | + assert.equal(response?.ok, true); |
| 117 | + assert.equal(ensureCalls, 1); |
| 118 | + if (response && response.ok) { |
| 119 | + assert.equal(response.data?.platform, 'android'); |
| 120 | + assert.equal(response.data?.booted, true); |
| 121 | + } |
| 122 | +}); |
0 commit comments