|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { test, vi } from 'vitest'; |
| 5 | +import type { CliFlags } from '@agent-device/contracts/command'; |
| 6 | +import { getFlagDefinitionsForKey } from './flag-registry.ts'; |
| 7 | +import type { FlagDefinition } from './flag-types.ts'; |
| 8 | +import { resolveConfigBackedFlagDefaults } from '../../cli-schema/cli-config.ts'; |
| 9 | +import { recordActionEntry } from '../../daemon/session-action-recorder.ts'; |
| 10 | +import { makeIosSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 11 | +import { makeTempWorkspace } from '../../__tests__/cli-config-fixtures.ts'; |
| 12 | + |
| 13 | +/** |
| 14 | + * These are planted-divergence tests, not membership snapshots. A snapshot would |
| 15 | + * re-list the admitted/recorded keys and drift exactly like the two hand-maintained |
| 16 | + * allowlists it replaced; these plant a divergence in the ONE declaration and require |
| 17 | + * the production derivation to follow it, which a second list could not do. |
| 18 | + * |
| 19 | + * Like #2421's `command-input-option-field.test.ts`, the plant lands BEFORE the surface |
| 20 | + * is built. `cli-config.ts` freezes `PROJECT_CONFIG_FLAG_KEYS` and the recorder freezes |
| 21 | + * `RECORDED_FLAG_KEYS` at their own module load, so each test resets the graph, writes |
| 22 | + * the divergence onto the live declaration, and only then imports the consumer — so what |
| 23 | + * it observes is the real derivation running over the planted declaration. |
| 24 | + */ |
| 25 | + |
| 26 | +const OPERATOR_CLI_FLAGS: CliFlags = { json: false, help: false, version: false }; |
| 27 | + |
| 28 | +function writeProjectConfig(project: string, entries: Record<string, unknown>): void { |
| 29 | + fs.writeFileSync(path.join(project, 'agent-device.json'), JSON.stringify(entries), 'utf8'); |
| 30 | +} |
| 31 | + |
| 32 | +function declarationFor(key: 'daemonBaseUrl' | 'overlayRefs'): FlagDefinition { |
| 33 | + const declaration = getFlagDefinitionsForKey(key)[0]; |
| 34 | + assert.ok(declaration, `expected a declaration for ${key}`); |
| 35 | + return declaration; |
| 36 | +} |
| 37 | + |
| 38 | +test('project-config admission follows the declaration: an undeclared key is refused, a planted declaration is admitted', async () => { |
| 39 | + // Shipped: the declaration declines project config, so the same file the shipped |
| 40 | + // derivation reads is rejected. |
| 41 | + assert.equal(declarationFor('daemonBaseUrl').projectConfig, false); |
| 42 | + const shipped = makeTempWorkspace(); |
| 43 | + try { |
| 44 | + writeProjectConfig(shipped.project, { daemonBaseUrl: 'https://daemon.example.test' }); |
| 45 | + assert.throws( |
| 46 | + () => |
| 47 | + resolveConfigBackedFlagDefaults({ |
| 48 | + command: 'devices', |
| 49 | + cwd: shipped.project, |
| 50 | + cliFlags: OPERATOR_CLI_FLAGS, |
| 51 | + env: { HOME: shipped.home }, |
| 52 | + }), |
| 53 | + /not allowed in project config file/, |
| 54 | + ); |
| 55 | + } finally { |
| 56 | + fs.rmSync(shipped.root, { recursive: true, force: true }); |
| 57 | + } |
| 58 | + |
| 59 | + // Planted: flip the declaration before `cli-config` builds, and the identical file is |
| 60 | + // admitted. A hand-maintained allowlist in `cli-config.ts` could not follow this. |
| 61 | + vi.resetModules(); |
| 62 | + const registry = await import('./flag-registry.ts'); |
| 63 | + const planted = getInRegistry(registry, 'daemonBaseUrl'); |
| 64 | + Object.assign(planted, { projectConfig: true }); |
| 65 | + const { resolveConfigBackedFlagDefaults: derivePlanted } = |
| 66 | + await import('../../cli-schema/cli-config.ts'); |
| 67 | + const workspace = makeTempWorkspace(); |
| 68 | + try { |
| 69 | + writeProjectConfig(workspace.project, { daemonBaseUrl: 'https://daemon.example.test' }); |
| 70 | + const defaults = derivePlanted({ |
| 71 | + command: 'devices', |
| 72 | + cwd: workspace.project, |
| 73 | + cliFlags: OPERATOR_CLI_FLAGS, |
| 74 | + env: { HOME: workspace.home }, |
| 75 | + }); |
| 76 | + assert.equal(defaults.daemonBaseUrl, 'https://daemon.example.test'); |
| 77 | + } finally { |
| 78 | + fs.rmSync(workspace.root, { recursive: true, force: true }); |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | +test('recorder sanitization follows the declaration: an undeclared key is dropped, a planted declaration is recorded', async () => { |
| 83 | + // Shipped: `overlayRefs` declines recording so it is dropped; `fps` (recorded) is the |
| 84 | + // control that proves the copy itself works. |
| 85 | + assert.equal(declarationFor('overlayRefs').recorded, false); |
| 86 | + const shipped = recordActionEntry(makeIosSession('default'), { |
| 87 | + command: 'screenshot', |
| 88 | + positionals: [], |
| 89 | + flags: { overlayRefs: true, fps: 5 }, |
| 90 | + result: {}, |
| 91 | + }); |
| 92 | + assert.equal(shipped?.flags.overlayRefs, undefined, 'overlayRefs is undeclared for recording'); |
| 93 | + assert.equal(shipped?.flags.fps, 5, 'fps is declared for recording'); |
| 94 | + |
| 95 | + // Planted: flip the declaration before the recorder builds, and the production |
| 96 | + // `sanitizeFlags` now copies the same value. |
| 97 | + vi.resetModules(); |
| 98 | + const registry = await import('./flag-registry.ts'); |
| 99 | + const planted = getInRegistry(registry, 'overlayRefs'); |
| 100 | + Object.assign(planted, { recorded: true }); |
| 101 | + const { recordActionEntry: recordPlanted } = |
| 102 | + await import('../../daemon/session-action-recorder.ts'); |
| 103 | + const recorded = recordPlanted(makeIosSession('default'), { |
| 104 | + command: 'screenshot', |
| 105 | + positionals: [], |
| 106 | + flags: { overlayRefs: true }, |
| 107 | + result: {}, |
| 108 | + }); |
| 109 | + assert.equal(recorded?.flags.overlayRefs, true, 'a planted declaration is copied'); |
| 110 | +}); |
| 111 | + |
| 112 | +test('a flag declaration without both admission fields does not compile', () => { |
| 113 | + // @ts-expect-error projectConfig and recorded are required; omission is the fail-closed gate. |
| 114 | + const declaration: FlagDefinition = { key: 'json', names: ['--json'], type: 'boolean' }; |
| 115 | + assert.ok(declaration); |
| 116 | +}); |
| 117 | + |
| 118 | +function getInRegistry( |
| 119 | + registry: typeof import('./flag-registry.ts'), |
| 120 | + key: 'daemonBaseUrl' | 'overlayRefs', |
| 121 | +): FlagDefinition { |
| 122 | + const declaration = registry.getFlagDefinitionsForKey(key)[0]; |
| 123 | + assert.ok(declaration, `expected a declaration for ${key}`); |
| 124 | + return declaration; |
| 125 | +} |
0 commit comments