|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync, existsSync, readdirSync, unlinkSync } from 'fs'; |
| 3 | +import { tmpdir, homedir } from 'os'; |
| 4 | +import { join } from 'path'; |
| 5 | + |
| 6 | +// We can't easily redirect homedir() at runtime, so insights.test seeds |
| 7 | +// a unique-named history file in the REAL ~/.codeep/history/ and cleans |
| 8 | +// it up afterwards. This keeps the test honest (exercises real disk + |
| 9 | +// real paths) without colliding with the user's actual history. |
| 10 | + |
| 11 | +const HISTORY_DIR = join(homedir(), '.codeep', 'history'); |
| 12 | +const TEST_ID_PREFIX = '_insights_test_'; |
| 13 | + |
| 14 | +import { formatInsights } from './insights'; |
| 15 | + |
| 16 | +interface TestRun { |
| 17 | + id: string; |
| 18 | + startTime: number; |
| 19 | + endTime: number; |
| 20 | + prompt: string; |
| 21 | + projectRoot: string; |
| 22 | + actions: { id: string; timestamp: number; type: string; path?: string }[]; |
| 23 | +} |
| 24 | + |
| 25 | +function writeTestRun(run: TestRun): string { |
| 26 | + if (!existsSync(HISTORY_DIR)) mkdirSync(HISTORY_DIR, { recursive: true }); |
| 27 | + const filename = `${TEST_ID_PREFIX}${run.id}.json`; |
| 28 | + writeFileSync(join(HISTORY_DIR, filename), JSON.stringify(run)); |
| 29 | + return filename; |
| 30 | +} |
| 31 | + |
| 32 | +function cleanupTestRuns(): void { |
| 33 | + if (!existsSync(HISTORY_DIR)) return; |
| 34 | + for (const f of readdirSync(HISTORY_DIR)) { |
| 35 | + if (f.startsWith(TEST_ID_PREFIX)) { |
| 36 | + try { unlinkSync(join(HISTORY_DIR, f)); } catch { /* best-effort */ } |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +describe('insights', () => { |
| 42 | + beforeEach(() => cleanupTestRuns()); |
| 43 | + afterEach(() => cleanupTestRuns()); |
| 44 | + |
| 45 | + it('renders empty state with hint when no runs in window', () => { |
| 46 | + const out = formatInsights({ days: 7 }); |
| 47 | + // Real history dir may have other runs from the user; only assert |
| 48 | + // shape (headline + "_No agent runs_" OR aggregate metrics line). |
| 49 | + expect(out).toMatch(/^## Activity — last 7 days/); |
| 50 | + }); |
| 51 | + |
| 52 | + it('aggregates runs across project, tools, and files', () => { |
| 53 | + const now = Date.now(); |
| 54 | + const oneHourAgo = now - 3_600_000; |
| 55 | + writeTestRun({ |
| 56 | + id: `${TEST_ID_PREFIX}a`, |
| 57 | + startTime: oneHourAgo, |
| 58 | + endTime: oneHourAgo + 120_000, |
| 59 | + prompt: 'add a new endpoint', |
| 60 | + projectRoot: '/Users/test/proj-a', |
| 61 | + actions: [ |
| 62 | + { id: 'x1', timestamp: oneHourAgo + 10_000, type: 'read', path: '/Users/test/proj-a/api.ts' }, |
| 63 | + { id: 'x2', timestamp: oneHourAgo + 20_000, type: 'write', path: '/Users/test/proj-a/api.ts' }, |
| 64 | + { id: 'x3', timestamp: oneHourAgo + 30_000, type: 'write', path: '/Users/test/proj-a/api.test.ts' }, |
| 65 | + ], |
| 66 | + }); |
| 67 | + writeTestRun({ |
| 68 | + id: `${TEST_ID_PREFIX}b`, |
| 69 | + startTime: oneHourAgo + 1_000, |
| 70 | + endTime: oneHourAgo + 60_000, |
| 71 | + prompt: 'fix the failing test', |
| 72 | + projectRoot: '/Users/test/proj-b', |
| 73 | + actions: [ |
| 74 | + { id: 'y1', timestamp: oneHourAgo + 5_000, type: 'execute', path: undefined }, |
| 75 | + ], |
| 76 | + }); |
| 77 | + |
| 78 | + const out = formatInsights({ days: 1 }); |
| 79 | + // Headline tally includes our 2 runs (real user runs may also be |
| 80 | + // present, so assert ≥ not exact). |
| 81 | + expect(out).toMatch(/\*\*\d+\*\* runs?/); |
| 82 | + // Per-project section names our buckets (basename of projectRoot). |
| 83 | + expect(out).toContain('proj-a'); |
| 84 | + expect(out).toContain('proj-b'); |
| 85 | + // Top tools section surfaces the action types we wrote. |
| 86 | + expect(out).toContain('`write`'); |
| 87 | + expect(out).toContain('`read`'); |
| 88 | + expect(out).toContain('`execute`'); |
| 89 | + // Most-touched files section abbreviates HOME prefix. |
| 90 | + expect(out).toContain('api.ts'); |
| 91 | + // Recent runs section quotes the user prompt. |
| 92 | + expect(out).toContain('add a new endpoint'); |
| 93 | + expect(out).toContain('fix the failing test'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('clamps --days to [1, 365]', () => { |
| 97 | + expect(formatInsights({ days: 0 })).toMatch(/last 1 day\b/); |
| 98 | + expect(formatInsights({ days: -10 })).toMatch(/last 1 day\b/); |
| 99 | + expect(formatInsights({ days: 9999 })).toMatch(/last 365 days/); |
| 100 | + }); |
| 101 | +}); |
0 commit comments