|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as os from 'os'; |
| 5 | +import { execFileSync } from 'child_process'; |
| 6 | + |
| 7 | +describe('theory-capture hook', () => { |
| 8 | + let tmpDir: string; |
| 9 | + let theoryPath: string; |
| 10 | + let hookScript: string; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'theory-capture-test-')); |
| 14 | + theoryPath = path.join(tmpDir, 'THEORY.MD'); |
| 15 | + // Copy hook script to tmpDir so it runs outside ESM project scope |
| 16 | + // (project has "type": "module" which breaks require() in .js files) |
| 17 | + const srcScript = path.resolve( |
| 18 | + __dirname, |
| 19 | + '..', |
| 20 | + '..', |
| 21 | + '..', |
| 22 | + 'templates', |
| 23 | + 'claude-hooks', |
| 24 | + 'theory-capture.js' |
| 25 | + ); |
| 26 | + hookScript = path.join(tmpDir, 'theory-capture.js'); |
| 27 | + fs.copyFileSync(srcScript, hookScript); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 32 | + }); |
| 33 | + |
| 34 | + function runHook(input: Record<string, unknown>): string { |
| 35 | + try { |
| 36 | + return execFileSync('node', [hookScript], { |
| 37 | + input: JSON.stringify(input), |
| 38 | + cwd: tmpDir, |
| 39 | + encoding: 'utf-8', |
| 40 | + timeout: 5000, |
| 41 | + env: { ...process.env, PATH: process.env['PATH'] }, |
| 42 | + }); |
| 43 | + } catch (e: unknown) { |
| 44 | + // Hook should never throw, but capture output if it does |
| 45 | + return (e as { stdout?: string }).stdout || ''; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + it('writes theory-cache.json on Edit to THEORY.MD', () => { |
| 50 | + fs.writeFileSync(theoryPath, '# My Theory\n\nSome content\n'); |
| 51 | + |
| 52 | + runHook({ |
| 53 | + tool_name: 'Edit', |
| 54 | + tool_input: { file_path: theoryPath }, |
| 55 | + }); |
| 56 | + |
| 57 | + const cachePath = path.join(tmpDir, '.stackmemory', 'theory-cache.json'); |
| 58 | + expect(fs.existsSync(cachePath)).toBe(true); |
| 59 | + |
| 60 | + const cache = JSON.parse(fs.readFileSync(cachePath, 'utf-8')); |
| 61 | + expect(cache.path).toBe(theoryPath); |
| 62 | + expect(cache.lineCount).toBe(4); |
| 63 | + expect(cache.hash).toBeTruthy(); |
| 64 | + expect(cache.timestamp).toBeTruthy(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('writes theory-cache.json on Write to THEORY.MD', () => { |
| 68 | + fs.writeFileSync(theoryPath, 'line1\nline2\n'); |
| 69 | + |
| 70 | + runHook({ |
| 71 | + tool_name: 'Write', |
| 72 | + tool_input: { file_path: theoryPath }, |
| 73 | + }); |
| 74 | + |
| 75 | + const cachePath = path.join(tmpDir, '.stackmemory', 'theory-cache.json'); |
| 76 | + expect(fs.existsSync(cachePath)).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it('ignores Edit to non-THEORY.MD files', () => { |
| 80 | + const otherFile = path.join(tmpDir, 'README.md'); |
| 81 | + fs.writeFileSync(otherFile, 'hello'); |
| 82 | + |
| 83 | + runHook({ |
| 84 | + tool_name: 'Edit', |
| 85 | + tool_input: { file_path: otherFile }, |
| 86 | + }); |
| 87 | + |
| 88 | + const cachePath = path.join(tmpDir, '.stackmemory', 'theory-cache.json'); |
| 89 | + expect(fs.existsSync(cachePath)).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('ignores Read tool even for THEORY.MD', () => { |
| 93 | + fs.writeFileSync(theoryPath, 'content'); |
| 94 | + |
| 95 | + runHook({ |
| 96 | + tool_name: 'Read', |
| 97 | + tool_input: { file_path: theoryPath }, |
| 98 | + }); |
| 99 | + |
| 100 | + const cachePath = path.join(tmpDir, '.stackmemory', 'theory-cache.json'); |
| 101 | + expect(fs.existsSync(cachePath)).toBe(false); |
| 102 | + }); |
| 103 | + |
| 104 | + it('handles case-insensitive THEORY.MD path', () => { |
| 105 | + // Create lowercase variant |
| 106 | + const lowerPath = path.join(tmpDir, 'theory.md'); |
| 107 | + fs.writeFileSync(lowerPath, 'content\n'); |
| 108 | + |
| 109 | + runHook({ |
| 110 | + tool_name: 'Edit', |
| 111 | + tool_input: { file_path: lowerPath }, |
| 112 | + }); |
| 113 | + |
| 114 | + const cachePath = path.join(tmpDir, '.stackmemory', 'theory-cache.json'); |
| 115 | + // isTheoryPath checks lowercase so this should match |
| 116 | + expect(fs.existsSync(cachePath)).toBe(true); |
| 117 | + }); |
| 118 | + |
| 119 | + it('handles missing file gracefully', () => { |
| 120 | + // Don't create the file — hook should not crash |
| 121 | + const missingPath = path.join(tmpDir, 'THEORY.MD'); |
| 122 | + |
| 123 | + runHook({ |
| 124 | + tool_name: 'Edit', |
| 125 | + tool_input: { file_path: missingPath }, |
| 126 | + }); |
| 127 | + |
| 128 | + const cachePath = path.join(tmpDir, '.stackmemory', 'theory-cache.json'); |
| 129 | + expect(fs.existsSync(cachePath)).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + it('handles missing tool_input gracefully', () => { |
| 133 | + // Should not crash |
| 134 | + runHook({ tool_name: 'Edit' }); |
| 135 | + |
| 136 | + const cachePath = path.join(tmpDir, '.stackmemory', 'theory-cache.json'); |
| 137 | + expect(fs.existsSync(cachePath)).toBe(false); |
| 138 | + }); |
| 139 | +}); |
0 commit comments