|
| 1 | +import { describe, test, expect } from 'bun:test'; |
| 2 | +import { logInvocation } from '../src/log'; |
| 3 | +import type { InjectionEntry } from '../src/inject'; |
| 4 | +import type { LorebookEntry } from '../src/resolve'; |
| 5 | +import { mkdtempSync, readFileSync } from 'fs'; |
| 6 | +import { join } from 'path'; |
| 7 | +import { tmpdir } from 'os'; |
| 8 | + |
| 9 | +function makeMatch(name: string, filePath: string, matchedKeys: string[]): InjectionEntry { |
| 10 | + return { |
| 11 | + entry: { |
| 12 | + name, |
| 13 | + keys: matchedKeys, |
| 14 | + excludeKeys: [], |
| 15 | + priority: 0, |
| 16 | + enabled: true, |
| 17 | + description: '', |
| 18 | + content: 'test content', |
| 19 | + source: 'project', |
| 20 | + filePath, |
| 21 | + } satisfies LorebookEntry, |
| 22 | + matchedKeys, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +describe('logInvocation', () => { |
| 27 | + test('appends JSONL line to log file', () => { |
| 28 | + const tmp = mkdtempSync(join(tmpdir(), 'lorebook-log-')); |
| 29 | + const logPath = join(tmp, 'lorebook.log'); |
| 30 | + |
| 31 | + logInvocation('tell me about git', [makeMatch('git-policy', '.claude/lorebook/git-policy.md', ['git'])], logPath); |
| 32 | + |
| 33 | + const content = readFileSync(logPath, 'utf-8').trim(); |
| 34 | + const parsed = JSON.parse(content); |
| 35 | + expect(parsed.prompt).toBe('tell me about git'); |
| 36 | + expect(parsed.timestamp).toBeDefined(); |
| 37 | + expect(parsed.entries).toHaveLength(1); |
| 38 | + expect(parsed.entries[0].name).toBe('git-policy'); |
| 39 | + expect(parsed.entries[0].source).toBe('.claude/lorebook/git-policy.md'); |
| 40 | + expect(parsed.entries[0].keywords).toEqual(['git']); |
| 41 | + }); |
| 42 | + |
| 43 | + test('appends multiple invocations as separate lines', () => { |
| 44 | + const tmp = mkdtempSync(join(tmpdir(), 'lorebook-log-')); |
| 45 | + const logPath = join(tmp, 'lorebook.log'); |
| 46 | + |
| 47 | + logInvocation('prompt one', [makeMatch('a', '.claude/lorebook/a.md', ['alpha'])], logPath); |
| 48 | + logInvocation('prompt two', [makeMatch('b', '~/.claude/lorebook/b.md', ['beta'])], logPath); |
| 49 | + |
| 50 | + const lines = readFileSync(logPath, 'utf-8').trim().split('\n'); |
| 51 | + expect(lines).toHaveLength(2); |
| 52 | + expect(JSON.parse(lines[0]!).prompt).toBe('prompt one'); |
| 53 | + expect(JSON.parse(lines[1]!).prompt).toBe('prompt two'); |
| 54 | + }); |
| 55 | + |
| 56 | + test('logs multiple matched entries in a single invocation', () => { |
| 57 | + const tmp = mkdtempSync(join(tmpdir(), 'lorebook-log-')); |
| 58 | + const logPath = join(tmp, 'lorebook.log'); |
| 59 | + |
| 60 | + logInvocation( |
| 61 | + 'git push to gpu', |
| 62 | + [ |
| 63 | + makeMatch('git-policy', '.claude/lorebook/git-policy.md', ['git', 'push']), |
| 64 | + makeMatch('gpu-transfers', '~/.claude/lorebook/gpu-transfers.md', ['gpu']), |
| 65 | + ], |
| 66 | + logPath |
| 67 | + ); |
| 68 | + |
| 69 | + const parsed = JSON.parse(readFileSync(logPath, 'utf-8').trim()); |
| 70 | + expect(parsed.entries).toHaveLength(2); |
| 71 | + expect(parsed.entries[0].name).toBe('git-policy'); |
| 72 | + expect(parsed.entries[1].name).toBe('gpu-transfers'); |
| 73 | + }); |
| 74 | + |
| 75 | + test('does not throw on write failure', () => { |
| 76 | + // Attempt to write to an invalid path — should silently fail |
| 77 | + expect(() => logInvocation('test', [makeMatch('a', 'a.md', ['x'])], '/dev/null/impossible/path.log')).not.toThrow(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments