|
| 1 | +// prefer-async-spawn: streaming-stdio-required — spawns the hook subprocess and |
| 2 | +// pipes an Edit/Write payload on stdin, asserting on exit (always 0) + stderr. |
| 3 | +import { spawn } from '@socketsecurity/lib-stable/process/spawn/child' |
| 4 | +import { mkdtempSync, writeFileSync } from 'node:fs' |
| 5 | +import os from 'node:os' |
| 6 | +import path from 'node:path' |
| 7 | +import { fileURLToPath } from 'node:url' |
| 8 | +import { test } from 'node:test' |
| 9 | +import assert from 'node:assert/strict' |
| 10 | + |
| 11 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 12 | +const HOOK = path.resolve(__dirname, '..', 'index.mts') |
| 13 | + |
| 14 | +function changelogPath(): string { |
| 15 | + const dir = mkdtempSync(path.join(os.tmpdir(), 'changelog-shape-test-')) |
| 16 | + const p = path.join(dir, 'CHANGELOG.md') |
| 17 | + writeFileSync(p, '# Changelog\n') |
| 18 | + return p |
| 19 | +} |
| 20 | + |
| 21 | +function runWrite( |
| 22 | + filePath: string, |
| 23 | + content: string, |
| 24 | +): Promise<{ code: number; stderr: string }> { |
| 25 | + return new Promise((resolve, reject) => { |
| 26 | + const child = spawn(process.execPath, [HOOK], { |
| 27 | + stdio: ['pipe', 'ignore', 'pipe'], |
| 28 | + }) |
| 29 | + void child.catch(() => undefined) |
| 30 | + let stderr = '' |
| 31 | + child.process.stderr!.on('data', d => { |
| 32 | + stderr += d.toString() |
| 33 | + }) |
| 34 | + child.process.on('error', reject) |
| 35 | + child.process.on('exit', code => { |
| 36 | + resolve({ code: code ?? -1, stderr }) |
| 37 | + }) |
| 38 | + child.stdin!.end( |
| 39 | + JSON.stringify({ |
| 40 | + tool_name: 'Write', |
| 41 | + tool_input: { file_path: filePath, content }, |
| 42 | + }), |
| 43 | + ) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +test('nudges a bullet with no agents.md link (exit 0, warns)', async () => { |
| 48 | + const { code, stderr } = await runWrite( |
| 49 | + changelogPath(), |
| 50 | + '## 1.2.0\n\n- Added a new flag for verbose output\n', |
| 51 | + ) |
| 52 | + assert.equal(code, 0, 'always non-blocking') |
| 53 | + assert.match(stderr, /changelog-entry-shape-nudge/) |
| 54 | +}) |
| 55 | + |
| 56 | +test('quiet when the bullet links an agents.md doc', async () => { |
| 57 | + const { code, stderr } = await runWrite( |
| 58 | + changelogPath(), |
| 59 | + '## 1.2.0\n\n- Added a verbose flag ([`verbose`](docs/agents.md/repo/verbose.md))\n', |
| 60 | + ) |
| 61 | + assert.equal(code, 0) |
| 62 | + assert.doesNotMatch(stderr, /changelog-entry-shape-nudge/) |
| 63 | +}) |
| 64 | + |
| 65 | +test('ignores indented sub-bullets', async () => { |
| 66 | + const { code, stderr } = await runWrite( |
| 67 | + changelogPath(), |
| 68 | + '## 1.2.0\n\n- Feature ([`x`](docs/agents.md/fleet/x.md))\n - detail one\n - detail two\n', |
| 69 | + ) |
| 70 | + assert.equal(code, 0) |
| 71 | + assert.doesNotMatch(stderr, /changelog-entry-shape-nudge/) |
| 72 | +}) |
| 73 | + |
| 74 | +test('a non-CHANGELOG file is ignored', async () => { |
| 75 | + const dir = mkdtempSync(path.join(os.tmpdir(), 'changelog-shape-test-')) |
| 76 | + const p = path.join(dir, 'NOTES.md') |
| 77 | + writeFileSync(p, '# notes\n') |
| 78 | + const { code, stderr } = await runWrite(p, '- a bare bullet with no link\n') |
| 79 | + assert.equal(code, 0) |
| 80 | + assert.doesNotMatch(stderr, /changelog-entry-shape-nudge/) |
| 81 | +}) |
| 82 | + |
| 83 | +test('headings and blank lines do not trigger', async () => { |
| 84 | + const { code, stderr } = await runWrite( |
| 85 | + changelogPath(), |
| 86 | + '# Changelog\n\n## 2.0.0\n\n', |
| 87 | + ) |
| 88 | + assert.equal(code, 0) |
| 89 | + assert.doesNotMatch(stderr, /changelog-entry-shape-nudge/) |
| 90 | +}) |
| 91 | + |
| 92 | +test('non-Edit/Write tool passes silently', async () => { |
| 93 | + const child = spawn(process.execPath, [HOOK], { |
| 94 | + stdio: ['pipe', 'ignore', 'pipe'], |
| 95 | + }) |
| 96 | + void child.catch(() => undefined) |
| 97 | + const code = await new Promise<number>(resolve => { |
| 98 | + child.process.on('exit', c => resolve(c ?? -1)) |
| 99 | + child.stdin!.end( |
| 100 | + JSON.stringify({ tool_name: 'Bash', tool_input: { command: 'ls' } }), |
| 101 | + ) |
| 102 | + }) |
| 103 | + assert.equal(code, 0) |
| 104 | +}) |
| 105 | + |
| 106 | +test('malformed payload fails open (exit 0)', async () => { |
| 107 | + const child = spawn(process.execPath, [HOOK], { |
| 108 | + stdio: ['pipe', 'ignore', 'pipe'], |
| 109 | + }) |
| 110 | + void child.catch(() => undefined) |
| 111 | + const code = await new Promise<number>(resolve => { |
| 112 | + child.process.on('exit', c => resolve(c ?? -1)) |
| 113 | + child.stdin!.end('{ not json') |
| 114 | + }) |
| 115 | + assert.equal(code, 0) |
| 116 | +}) |
0 commit comments