|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import type { AgentDeviceBackend, BackendCommandContext } from '../backend.ts'; |
| 4 | +import type { ArtifactAdapter } from '../io.ts'; |
| 5 | +import { |
| 6 | + createAgentDevice, |
| 7 | + createMemorySessionStore, |
| 8 | + restrictedCommandPolicy, |
| 9 | +} from '../runtime.ts'; |
| 10 | +import { createCommandRouter } from '../commands/index.ts'; |
| 11 | + |
| 12 | +const artifacts = { |
| 13 | + resolveInput: async () => ({ path: '/tmp/input' }), |
| 14 | + reserveOutput: async (_ref, options) => ({ |
| 15 | + path: `/tmp/${options.field}${options.ext}`, |
| 16 | + visibility: options.visibility ?? 'client-visible', |
| 17 | + publish: async () => undefined, |
| 18 | + }), |
| 19 | + createTempFile: async (options) => ({ |
| 20 | + path: `/tmp/${options.prefix}${options.ext}`, |
| 21 | + visibility: 'internal', |
| 22 | + cleanup: async () => {}, |
| 23 | + }), |
| 24 | +} satisfies ArtifactAdapter; |
| 25 | + |
| 26 | +test('diagnostics runtime commands call typed backend primitives and redact sensitive data', async () => { |
| 27 | + const contexts: BackendCommandContext[] = []; |
| 28 | + const device = createAgentDevice({ |
| 29 | + backend: createDiagnosticsBackend(contexts), |
| 30 | + artifacts, |
| 31 | + sessions: createMemorySessionStore([ |
| 32 | + { name: 'default', appId: 'app-1', appBundleId: 'com.example.app' }, |
| 33 | + ]), |
| 34 | + policy: restrictedCommandPolicy(), |
| 35 | + }); |
| 36 | + |
| 37 | + const logs = await device.observability.logs({ |
| 38 | + session: 'default', |
| 39 | + limit: 10, |
| 40 | + levels: ['info'], |
| 41 | + search: 'ready', |
| 42 | + }); |
| 43 | + assert.equal(logs.kind, 'diagnosticsLogs'); |
| 44 | + assert.equal(logs.redacted, true); |
| 45 | + assert.match(logs.entries[0]?.message ?? '', /token=\[REDACTED\]/); |
| 46 | + assert.equal(logs.entries[0]?.metadata?.authorization, '[REDACTED]'); |
| 47 | + |
| 48 | + const network = await device.observability.network({ |
| 49 | + session: 'default', |
| 50 | + include: 'all', |
| 51 | + limit: 5, |
| 52 | + }); |
| 53 | + assert.equal(network.kind, 'diagnosticsNetwork'); |
| 54 | + assert.equal(network.redacted, true); |
| 55 | + assert.match(network.entries[0]?.url ?? '', /token=%5BREDACTED%5D/); |
| 56 | + assert.equal(network.entries[0]?.requestHeaders?.Authorization, '[REDACTED]'); |
| 57 | + assert.match(network.entries[0]?.requestBody ?? '', /password=\[REDACTED\]/); |
| 58 | + |
| 59 | + const perf = await device.observability.perf({ session: 'default', sampleMs: 100 }); |
| 60 | + assert.equal(perf.kind, 'diagnosticsPerf'); |
| 61 | + assert.equal(perf.redacted, false); |
| 62 | + assert.equal(perf.metrics[0]?.name, 'cpu'); |
| 63 | + |
| 64 | + assert.deepEqual( |
| 65 | + contexts.map((context) => ({ appId: context.appId, appBundleId: context.appBundleId })), |
| 66 | + [ |
| 67 | + { appId: 'app-1', appBundleId: 'com.example.app' }, |
| 68 | + { appId: 'app-1', appBundleId: 'com.example.app' }, |
| 69 | + { appId: 'app-1', appBundleId: 'com.example.app' }, |
| 70 | + ], |
| 71 | + ); |
| 72 | +}); |
| 73 | + |
| 74 | +test('diagnostics commands validate bounded windows and router dispatches diagnostics namespace', async () => { |
| 75 | + const router = createCommandRouter({ |
| 76 | + createRuntime: () => |
| 77 | + createAgentDevice({ |
| 78 | + backend: createDiagnosticsBackend([]), |
| 79 | + artifacts, |
| 80 | + policy: restrictedCommandPolicy(), |
| 81 | + }), |
| 82 | + }); |
| 83 | + |
| 84 | + const ok = await router.dispatch({ |
| 85 | + command: 'diagnostics.network', |
| 86 | + options: { limit: 1, include: 'summary' }, |
| 87 | + }); |
| 88 | + assert.equal(ok.ok, true); |
| 89 | + assert.equal(ok.ok && 'kind' in ok.data ? ok.data.kind : undefined, 'diagnosticsNetwork'); |
| 90 | + const data = |
| 91 | + ok.ok && 'kind' in ok.data && ok.data.kind === 'diagnosticsNetwork' ? ok.data : undefined; |
| 92 | + assert.equal(data?.entries[0]?.requestHeaders, undefined); |
| 93 | + |
| 94 | + const invalid = await router.dispatch({ |
| 95 | + command: 'diagnostics.logs', |
| 96 | + options: { limit: 501 }, |
| 97 | + }); |
| 98 | + assert.equal(invalid.ok, false); |
| 99 | + assert.equal(invalid.ok ? undefined : invalid.error.code, 'INVALID_ARGS'); |
| 100 | +}); |
| 101 | + |
| 102 | +function createDiagnosticsBackend(contexts: BackendCommandContext[]): AgentDeviceBackend { |
| 103 | + return { |
| 104 | + platform: 'ios', |
| 105 | + readLogs: async (context) => { |
| 106 | + contexts.push(context); |
| 107 | + return { |
| 108 | + backend: 'fixture', |
| 109 | + redacted: false, |
| 110 | + entries: [ |
| 111 | + { |
| 112 | + timestamp: '2026-04-16T00:00:00.000Z', |
| 113 | + level: 'info', |
| 114 | + message: 'ready token=secret', |
| 115 | + metadata: { authorization: 'Bearer secret' }, |
| 116 | + }, |
| 117 | + ], |
| 118 | + }; |
| 119 | + }, |
| 120 | + dumpNetwork: async (context) => { |
| 121 | + contexts.push(context); |
| 122 | + return { |
| 123 | + backend: 'fixture', |
| 124 | + entries: [ |
| 125 | + { |
| 126 | + method: 'POST', |
| 127 | + url: 'https://example.test/path?token=secret', |
| 128 | + status: 200, |
| 129 | + requestHeaders: { Authorization: 'Bearer secret' }, |
| 130 | + responseHeaders: { 'content-type': 'application/json' }, |
| 131 | + requestBody: 'password=secret', |
| 132 | + responseBody: '{"ok":true}', |
| 133 | + }, |
| 134 | + ], |
| 135 | + }; |
| 136 | + }, |
| 137 | + measurePerf: async (context) => { |
| 138 | + contexts.push(context); |
| 139 | + return { |
| 140 | + backend: 'fixture', |
| 141 | + metrics: [{ name: 'cpu', value: 12.5, unit: '%' }], |
| 142 | + }; |
| 143 | + }, |
| 144 | + }; |
| 145 | +} |
0 commit comments