|
| 1 | +import { daemonPlatformServicesFixture } from '../../__tests__/platform-services-fixture.ts'; |
| 2 | +import { expect, test } from 'vitest'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import { handleSessionCommands } from './session-command-harness.ts'; |
| 5 | +import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts'; |
| 6 | +import { mkdtempForTestSync } from '../../../__tests__/test-utils/tmp-dir.ts'; |
| 7 | +import { testCreateCommandSurface } from '../../__tests__/command-surface-fixture.ts'; |
| 8 | +import type { DaemonRequest } from '../../daemon-request.ts'; |
| 9 | + |
| 10 | +function request(command: string): DaemonRequest { |
| 11 | + return { token: 't', session: 'default', command, positionals: [], flags: {} }; |
| 12 | +} |
| 13 | + |
| 14 | +async function dispatch(command: string) { |
| 15 | + return await handleSessionCommands({ |
| 16 | + createCommandSurface: testCreateCommandSurface, |
| 17 | + platformServices: daemonPlatformServicesFixture(), |
| 18 | + req: request(command), |
| 19 | + sessionName: 'default', |
| 20 | + logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'), |
| 21 | + sessionStore: makeSessionStore('agent-device-session-router-'), |
| 22 | + invoke: async () => ({ ok: true, data: {} }), |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +test('the session router stays silent for commands its table does not carry', async () => { |
| 27 | + expect(await dispatch('not_a_command')).toBeNull(); |
| 28 | +}); |
| 29 | + |
| 30 | +test('the session router maps boot, shutdown and appstate straight to their handlers', async () => { |
| 31 | + // No session and no selector: each command reaches its own handler's guard rather than |
| 32 | + // the router's null, which is what distinguishes routing from silence. |
| 33 | + for (const command of ['boot', 'shutdown', 'appstate']) { |
| 34 | + const response = await dispatch(command); |
| 35 | + if (!response || response.ok) { |
| 36 | + throw new Error( |
| 37 | + `${command} should reach its handler's guard as an INVALID_ARGS refusal, got ${JSON.stringify(response)}`, |
| 38 | + ); |
| 39 | + } |
| 40 | + expect(response.error.code, command).toBe('INVALID_ARGS'); |
| 41 | + } |
| 42 | +}); |
0 commit comments