|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { PUBLIC_COMMANDS } from '../../../command-catalog.ts'; |
| 5 | +import { makeAndroidSession, makeSessionStore } from '../../../__tests__/test-utils/index.ts'; |
| 6 | +import type { |
| 7 | + BindDeviceRuntime, |
| 8 | + InspectDeviceRuntimeFacts, |
| 9 | +} from '../../request-runtime-binding.ts'; |
| 10 | +import { |
| 11 | + createCapabilitiesAdmissionRuntime, |
| 12 | + legacyCapabilityUses, |
| 13 | +} from './session-capabilities.fixtures.ts'; |
| 14 | +import { handleSessionCommands } from './session-command-harness.ts'; |
| 15 | + |
| 16 | +test('capabilities projects the install family from exactly one facts inspection', async () => { |
| 17 | + const { sessionName, sessionStore } = createAndroidCapabilitiesSession('install-family'); |
| 18 | + const runtime = createCapabilitiesAdmissionRuntime({ |
| 19 | + appLogAvailable: true, |
| 20 | + networkAvailable: true, |
| 21 | + providerMode: 'local', |
| 22 | + deployAvailable: true, |
| 23 | + sourceAvailable: true, |
| 24 | + pushAvailable: true, |
| 25 | + readinessAvailable: true, |
| 26 | + }); |
| 27 | + |
| 28 | + const response = await dispatchCapabilities({ |
| 29 | + sessionName, |
| 30 | + sessionStore, |
| 31 | + bindDevice: runtime.bindDevice, |
| 32 | + inspectFacts: runtime.inspectFacts, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(response).toMatchObject({ ok: true }); |
| 36 | + if (!response?.ok) return; |
| 37 | + expect(response.data?.availableCommands).toEqual( |
| 38 | + expect.arrayContaining([ |
| 39 | + PUBLIC_COMMANDS.install, |
| 40 | + PUBLIC_COMMANDS.reinstall, |
| 41 | + PUBLIC_COMMANDS.installFromSource, |
| 42 | + PUBLIC_COMMANDS.push, |
| 43 | + ]), |
| 44 | + ); |
| 45 | + expect(runtime.inspections).toHaveLength(1); |
| 46 | + expect(runtime.uses).toEqual(legacyCapabilityUses); |
| 47 | +}); |
| 48 | + |
| 49 | +test('capabilities keeps legacy runtime commands outside the install-family facts projection', async () => { |
| 50 | + const { sessionName, sessionStore } = createAndroidCapabilitiesSession('legacy-runtime'); |
| 51 | + const runtime = createCapabilitiesAdmissionRuntime({ |
| 52 | + appLogAvailable: true, |
| 53 | + networkAvailable: true, |
| 54 | + providerMode: 'local', |
| 55 | + deployAvailable: true, |
| 56 | + sourceAvailable: true, |
| 57 | + pushAvailable: true, |
| 58 | + readinessAvailable: true, |
| 59 | + }); |
| 60 | + |
| 61 | + const response = await dispatchCapabilities({ |
| 62 | + sessionName, |
| 63 | + sessionStore, |
| 64 | + bindDevice: runtime.bindDevice, |
| 65 | + inspectFacts: runtime.inspectFacts, |
| 66 | + }); |
| 67 | + |
| 68 | + expect(response).toMatchObject({ ok: true }); |
| 69 | + if (!response?.ok) return; |
| 70 | + // `bootTarget` facts are unavailable in this fixture. A global descriptor projection |
| 71 | + // would hide boot; only this unit's install family is authorized to consume facts. |
| 72 | + expect(response.data?.availableCommands).toContain(PUBLIC_COMMANDS.boot); |
| 73 | + expect(runtime.inspections).toHaveLength(1); |
| 74 | + expect(runtime.uses).toEqual(legacyCapabilityUses); |
| 75 | +}); |
| 76 | + |
| 77 | +test('capabilities fails closed only for install-family projection when facts inspection fails', async () => { |
| 78 | + const { sessionName, sessionStore } = createAndroidCapabilitiesSession('facts-failure'); |
| 79 | + const runtime = createCapabilitiesAdmissionRuntime({ |
| 80 | + appLogAvailable: true, |
| 81 | + networkAvailable: true, |
| 82 | + providerMode: 'local', |
| 83 | + }); |
| 84 | + |
| 85 | + const response = await dispatchCapabilities({ |
| 86 | + sessionName, |
| 87 | + sessionStore, |
| 88 | + bindDevice: runtime.bindDevice, |
| 89 | + inspectFacts: async () => { |
| 90 | + throw new Error('provider facts unavailable'); |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(response).toMatchObject({ ok: true }); |
| 95 | + if (!response?.ok) return; |
| 96 | + expect(response.data?.availableCommands).toContain(PUBLIC_COMMANDS.boot); |
| 97 | + expect(response.data?.availableCommands).toContain(PUBLIC_COMMANDS.logs); |
| 98 | + expect(response.data?.availableCommands).not.toContain(PUBLIC_COMMANDS.install); |
| 99 | + expect(response.data?.availableCommands).not.toContain(PUBLIC_COMMANDS.reinstall); |
| 100 | + expect(response.data?.availableCommands).not.toContain(PUBLIC_COMMANDS.installFromSource); |
| 101 | + expect(response.data?.availableCommands).not.toContain(PUBLIC_COMMANDS.push); |
| 102 | + expect(runtime.uses).toEqual(legacyCapabilityUses); |
| 103 | +}); |
| 104 | + |
| 105 | +function createAndroidCapabilitiesSession(suffix: string) { |
| 106 | + const sessionName = `android-capabilities-${suffix}`; |
| 107 | + const sessionStore = makeSessionStore(`agent-device-capabilities-${suffix}-`); |
| 108 | + sessionStore.set(sessionName, makeAndroidSession(sessionName)); |
| 109 | + return { sessionName, sessionStore }; |
| 110 | +} |
| 111 | + |
| 112 | +async function dispatchCapabilities(params: { |
| 113 | + sessionName: string; |
| 114 | + sessionStore: ReturnType<typeof makeSessionStore>; |
| 115 | + bindDevice: BindDeviceRuntime; |
| 116 | + inspectFacts: InspectDeviceRuntimeFacts; |
| 117 | +}) { |
| 118 | + return await handleSessionCommands({ |
| 119 | + req: { |
| 120 | + token: 't', |
| 121 | + session: params.sessionName, |
| 122 | + command: PUBLIC_COMMANDS.capabilities, |
| 123 | + positionals: [], |
| 124 | + flags: {}, |
| 125 | + }, |
| 126 | + sessionName: params.sessionName, |
| 127 | + logPath: path.join(os.tmpdir(), 'daemon.log'), |
| 128 | + sessionStore: params.sessionStore, |
| 129 | + bindDevice: params.bindDevice, |
| 130 | + inspectFacts: params.inspectFacts, |
| 131 | + invoke: async () => ({ ok: true, data: {} }), |
| 132 | + }); |
| 133 | +} |
0 commit comments