|
| 1 | +// auto-orchestrate.test.cjs — UserPromptSubmit soft-gate hook contract. |
| 2 | +// |
| 3 | +// auto-orchestrate.cjs intercepts UserPromptSubmit, detects "new |
| 4 | +// implementation" intents (implement feature / build / wire up / etc.), |
| 5 | +// and injects 3-fronta orchestration guidance + research cache state + |
| 6 | +// budget into the turn. |
| 7 | +// |
| 8 | +// Contract (per source: shared/hooks/auto-orchestrate.cjs): |
| 9 | +// - Input on stdin: { prompt, session_id, cwd, ... } (UserPromptSubmit) |
| 10 | +// - No trigger match → stdout: { continue: true } (silent pass-through) |
| 11 | +// - Trigger match → stdout: { continue: true, hookSpecificOutput: { |
| 12 | +// hookEventName: 'UserPromptSubmit', |
| 13 | +// additionalContext: <guidance string> } } |
| 14 | +// - Always exit 0 (fail-open contract) |
| 15 | +// - NEVER blocks the turn |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const { test, describe } = require('node:test'); |
| 20 | +const assert = require('node:assert/strict'); |
| 21 | +const { runHook, parseHookOutput } = require('../../_helpers/run-hook.cjs'); |
| 22 | + |
| 23 | +function userPrompt(prompt, extra = {}) { |
| 24 | + return { prompt, session_id: 'test-session', cwd: process.cwd(), ...extra }; |
| 25 | +} |
| 26 | + |
| 27 | +function expectSilentPassthrough(result) { |
| 28 | + assert.equal(result.exitCode, 0, `auto-orchestrate exited ${result.exitCode}; stderr: ${result.stderr}`); |
| 29 | + const parsed = parseHookOutput(result.stdout); |
| 30 | + assert.ok(parsed, `expected JSON output; got: ${result.stdout}`); |
| 31 | + assert.equal(parsed.continue, true); |
| 32 | + assert.equal(parsed.hookSpecificOutput, undefined, |
| 33 | + `silent pass-through should not emit hookSpecificOutput; got: ${JSON.stringify(parsed.hookSpecificOutput)}`); |
| 34 | +} |
| 35 | + |
| 36 | +function expectGuidanceInjected(result) { |
| 37 | + assert.equal(result.exitCode, 0); |
| 38 | + const parsed = parseHookOutput(result.stdout); |
| 39 | + assert.ok(parsed); |
| 40 | + assert.equal(parsed.continue, true); |
| 41 | + assert.ok(parsed.hookSpecificOutput, 'expected hookSpecificOutput on trigger match'); |
| 42 | + assert.equal(parsed.hookSpecificOutput.hookEventName, 'UserPromptSubmit'); |
| 43 | + assert.equal(typeof parsed.hookSpecificOutput.additionalContext, 'string'); |
| 44 | + assert.ok(parsed.hookSpecificOutput.additionalContext.length > 10, |
| 45 | + 'guidance string should be non-trivial'); |
| 46 | + return parsed.hookSpecificOutput.additionalContext; |
| 47 | +} |
| 48 | + |
| 49 | +describe('auto-orchestrate: trigger patterns', () => { |
| 50 | + test('triggers on Czech "implementuj novou feature"', () => { |
| 51 | + const r = runHook('auto-orchestrate', userPrompt('implementuj novou feature pro správu uživatelů')); |
| 52 | + expectGuidanceInjected(r); |
| 53 | + }); |
| 54 | + |
| 55 | + test('triggers on English "implement a new endpoint"', () => { |
| 56 | + const r = runHook('auto-orchestrate', userPrompt('implement a new endpoint for the users API')); |
| 57 | + expectGuidanceInjected(r); |
| 58 | + }); |
| 59 | + |
| 60 | + test('triggers on "build new component"', () => { |
| 61 | + const r = runHook('auto-orchestrate', userPrompt('build a new component for the dashboard')); |
| 62 | + expectGuidanceInjected(r); |
| 63 | + }); |
| 64 | + |
| 65 | + test('triggers on "wire up payments"', () => { |
| 66 | + const r = runHook('auto-orchestrate', userPrompt('wire up payments via Stripe')); |
| 67 | + expectGuidanceInjected(r); |
| 68 | + }); |
| 69 | + |
| 70 | + test('triggers on "vytvoř novou stránku"', () => { |
| 71 | + const r = runHook('auto-orchestrate', userPrompt('vytvoř novou stránku pro registraci')); |
| 72 | + expectGuidanceInjected(r); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('auto-orchestrate: skip patterns', () => { |
| 77 | + test('skips "fix typo"', () => { |
| 78 | + const r = runHook('auto-orchestrate', userPrompt('fix typo in README — implement → implements')); |
| 79 | + expectSilentPassthrough(r); |
| 80 | + }); |
| 81 | + |
| 82 | + test('skips "quick fix"', () => { |
| 83 | + const r = runHook('auto-orchestrate', userPrompt('quick fix the broken link')); |
| 84 | + expectSilentPassthrough(r); |
| 85 | + }); |
| 86 | + |
| 87 | + test('skips "skip research"', () => { |
| 88 | + const r = runHook('auto-orchestrate', userPrompt('skip research and just implement the feature')); |
| 89 | + expectSilentPassthrough(r); |
| 90 | + }); |
| 91 | + |
| 92 | + test('silent on simple greeting', () => { |
| 93 | + const r = runHook('auto-orchestrate', userPrompt('ahoj, jak se máš?')); |
| 94 | + expectSilentPassthrough(r); |
| 95 | + }); |
| 96 | + |
| 97 | + test('silent on simple question', () => { |
| 98 | + const r = runHook('auto-orchestrate', userPrompt('what does this function do?')); |
| 99 | + expectSilentPassthrough(r); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('auto-orchestrate: fail-open contract', () => { |
| 104 | + test('exit 0 on malformed JSON input', () => { |
| 105 | + const r = runHook('auto-orchestrate', '{ malformed'); |
| 106 | + assert.equal(r.exitCode, 0); |
| 107 | + const parsed = parseHookOutput(r.stdout); |
| 108 | + assert.ok(parsed); |
| 109 | + assert.equal(parsed.continue, true); |
| 110 | + }); |
| 111 | + |
| 112 | + test('exit 0 on empty stdin', () => { |
| 113 | + const r = runHook('auto-orchestrate', ''); |
| 114 | + assert.equal(r.exitCode, 0); |
| 115 | + const parsed = parseHookOutput(r.stdout); |
| 116 | + assert.ok(parsed); |
| 117 | + assert.equal(parsed.continue, true); |
| 118 | + }); |
| 119 | + |
| 120 | + test('exit 0 on missing prompt field', () => { |
| 121 | + const r = runHook('auto-orchestrate', { session_id: 'test', cwd: '/tmp' }); |
| 122 | + assert.equal(r.exitCode, 0); |
| 123 | + expectSilentPassthrough(r); |
| 124 | + }); |
| 125 | + |
| 126 | + test('respects 5s timeout', () => { |
| 127 | + const r = runHook('auto-orchestrate', userPrompt('hello'), { timeout: 5000 }); |
| 128 | + assert.equal(r.timedOut, false); |
| 129 | + }); |
| 130 | + |
| 131 | + test('CORTEX_BUDGET_DISABLED suppresses budget guidance', () => { |
| 132 | + // When budget is disabled, guidance should still emit but without USD figures |
| 133 | + const r = runHook( |
| 134 | + 'auto-orchestrate', |
| 135 | + userPrompt('implement a new feature'), |
| 136 | + { env: { CORTEX_BUDGET_DISABLED: '1' } } |
| 137 | + ); |
| 138 | + const ctx = expectGuidanceInjected(r); |
| 139 | + assert.ok(!/\$\d+\.\d{2}/.test(ctx), |
| 140 | + `with CORTEX_BUDGET_DISABLED=1, guidance should not contain dollar figures; got: ${ctx.slice(0, 300)}`); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('auto-orchestrate: PII / Dave-path leak guard', () => { |
| 145 | + test('output does not leak Dave-specific paths', () => { |
| 146 | + const r = runHook('auto-orchestrate', userPrompt('implement a new feature')); |
| 147 | + const stdout = r.stdout.toLowerCase(); |
| 148 | + assert.ok( |
| 149 | + !stdout.includes('/c/users/david/') && !stdout.includes('c:\\users\\david\\'), |
| 150 | + `auto-orchestrate hook leaked Dave-specific path; output:\n${r.stdout}` |
| 151 | + ); |
| 152 | + }); |
| 153 | +}); |
0 commit comments