|
| 1 | +import { test } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import { spawnSync } from '@socketsecurity/lib-stable/process/spawn/child' |
| 4 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 5 | +import os from 'node:os' |
| 6 | +import path from 'node:path' |
| 7 | +import { fileURLToPath } from 'node:url' |
| 8 | + |
| 9 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 10 | +const HOOK_PATH = path.join(__dirname, '..', 'index.mts') |
| 11 | + |
| 12 | +// Build a transcript whose most-recent assistant event uses `model`, plus an |
| 13 | +// optional user line (for bypass-phrase tests). |
| 14 | +function makeTranscript(model: string, userText?: string): string { |
| 15 | + const dir = mkdtempSync(path.join(os.tmpdir(), 'tokenspend-')) |
| 16 | + const p = path.join(dir, 'session.jsonl') |
| 17 | + const lines = [] |
| 18 | + if (userText) { |
| 19 | + lines.push(JSON.stringify({ role: 'user', content: userText })) |
| 20 | + } |
| 21 | + lines.push(JSON.stringify({ type: 'assistant', model, content: [] })) |
| 22 | + writeFileSync(p, lines.join('\n')) |
| 23 | + return p |
| 24 | +} |
| 25 | + |
| 26 | +function runHook( |
| 27 | + command: string, |
| 28 | + transcriptPath: string, |
| 29 | + effort: string, |
| 30 | + extraEnv: Record<string, string> = {}, |
| 31 | +): { stderr: string; exitCode: number } { |
| 32 | + const result = spawnSync('node', [HOOK_PATH], { |
| 33 | + input: JSON.stringify({ |
| 34 | + tool_name: 'Bash', |
| 35 | + tool_input: { command }, |
| 36 | + transcript_path: transcriptPath, |
| 37 | + }), |
| 38 | + env: { ...process.env, CLAUDE_EFFORT: effort, ...extraEnv }, |
| 39 | + }) |
| 40 | + return { stderr: String(result.stderr), exitCode: result.status ?? -1 } |
| 41 | +} |
| 42 | + |
| 43 | +const CASCADE = 'pnpm run sync --target . --fix' |
| 44 | + |
| 45 | +test('REMINDS on mechanical command + premium model (opus)', () => { |
| 46 | + const t = makeTranscript('claude-opus-4-8') |
| 47 | + const { stderr, exitCode } = runHook(CASCADE, t, 'low') |
| 48 | + assert.equal(exitCode, 2) |
| 49 | + assert.match(stderr, /token-spend-guard/) |
| 50 | + assert.match(stderr, /premium/) |
| 51 | + assert.match(stderr, /opus/) |
| 52 | +}) |
| 53 | + |
| 54 | +test('REMINDS on mechanical command + premium effort (high)', () => { |
| 55 | + const t = makeTranscript('claude-sonnet-4-6') |
| 56 | + const { stderr, exitCode } = runHook(CASCADE, t, 'high') |
| 57 | + assert.equal(exitCode, 2) |
| 58 | + assert.match(stderr, /effort/) |
| 59 | +}) |
| 60 | + |
| 61 | +test('ALLOWS mechanical command on cheap model + low effort', () => { |
| 62 | + const t = makeTranscript('claude-sonnet-4-6') |
| 63 | + const { exitCode } = runHook(CASCADE, t, 'low') |
| 64 | + assert.equal(exitCode, 0) |
| 65 | +}) |
| 66 | + |
| 67 | +test('ALLOWS a non-mechanical command even on premium model + high effort', () => { |
| 68 | + const t = makeTranscript('claude-opus-4-8') |
| 69 | + const { exitCode } = runHook('git status', t, 'high') |
| 70 | + assert.equal(exitCode, 0) |
| 71 | +}) |
| 72 | + |
| 73 | +test('model bypass silences the model flag (effort low → fully clears)', () => { |
| 74 | + const t = makeTranscript('claude-opus-4-8', 'Allow model bypass') |
| 75 | + const { exitCode } = runHook(CASCADE, t, 'low') |
| 76 | + assert.equal(exitCode, 0) |
| 77 | +}) |
| 78 | + |
| 79 | +test('effort bypass silences the effort flag (cheap model → fully clears)', () => { |
| 80 | + const t = makeTranscript('claude-sonnet-4-6', 'Allow effort bypass') |
| 81 | + const { exitCode } = runHook(CASCADE, t, 'max') |
| 82 | + assert.equal(exitCode, 0) |
| 83 | +}) |
| 84 | + |
| 85 | +test('one bypass does NOT silence the other dimension', () => { |
| 86 | + // opus + high, only model bypassed → effort still flags. |
| 87 | + const t = makeTranscript('claude-opus-4-8', 'Allow model bypass') |
| 88 | + const { stderr, exitCode } = runHook(CASCADE, t, 'high') |
| 89 | + assert.equal(exitCode, 2) |
| 90 | + assert.match(stderr, /effort/) |
| 91 | + assert.doesNotMatch(stderr, /Switch: \/model/) |
| 92 | +}) |
| 93 | + |
| 94 | +test('cascade commit subject triggers the guard', () => { |
| 95 | + const t = makeTranscript('claude-opus-4-8') |
| 96 | + const { exitCode } = runHook( |
| 97 | + 'git commit -m "chore(wheelhouse): cascade template@abc123"', |
| 98 | + t, |
| 99 | + 'low', |
| 100 | + ) |
| 101 | + assert.equal(exitCode, 2) |
| 102 | +}) |
| 103 | + |
| 104 | +test('disabled env var short-circuits', () => { |
| 105 | + const t = makeTranscript('claude-opus-4-8') |
| 106 | + const { exitCode } = runHook(CASCADE, t, 'high', { |
| 107 | + SOCKET_TOKEN_SPEND_GUARD_DISABLED: '1', |
| 108 | + }) |
| 109 | + assert.equal(exitCode, 0) |
| 110 | +}) |
| 111 | + |
| 112 | +test('IGNORES non-Bash tools', () => { |
| 113 | + const result = spawnSync('node', [HOOK_PATH], { |
| 114 | + input: JSON.stringify({ |
| 115 | + tool_name: 'Write', |
| 116 | + tool_input: { command: CASCADE }, |
| 117 | + }), |
| 118 | + env: { ...process.env, CLAUDE_EFFORT: 'high' }, |
| 119 | + }) |
| 120 | + assert.equal(result.status, 0) |
| 121 | +}) |
0 commit comments