|
| 1 | +/** |
| 2 | + * Tests for TheorySkill |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { execSync } from 'child_process'; |
| 9 | +import type { SkillContext } from '../claude-skills.js'; |
| 10 | + |
| 11 | +// We test against a real temp directory with git init |
| 12 | +let tmpDir: string; |
| 13 | +let originalCwd: string; |
| 14 | + |
| 15 | +// Mock logger |
| 16 | +vi.mock('../../core/monitoring/logger.js', () => ({ |
| 17 | + logger: { |
| 18 | + info: vi.fn(), |
| 19 | + debug: vi.fn(), |
| 20 | + warn: vi.fn(), |
| 21 | + error: vi.fn(), |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +function makeContext(overrides?: Partial<SkillContext>): SkillContext { |
| 26 | + return { |
| 27 | + projectId: 'test-project', |
| 28 | + userId: 'test-user', |
| 29 | + dualStackManager: {} as any, |
| 30 | + handoffManager: {} as any, |
| 31 | + contextRetriever: {} as any, |
| 32 | + database: {} as any, |
| 33 | + ...overrides, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe('TheorySkill', () => { |
| 38 | + beforeEach(() => { |
| 39 | + tmpDir = fs.mkdtempSync(path.join('/tmp', 'theory-test-')); |
| 40 | + originalCwd = process.cwd(); |
| 41 | + // Initialize a git repo so getGitRoot() works |
| 42 | + execSync('git init', { cwd: tmpDir, timeout: 5000 }); |
| 43 | + process.chdir(tmpDir); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + process.chdir(originalCwd); |
| 48 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + // Dynamic import to pick up mocks correctly |
| 52 | + async function getTheorySkill() { |
| 53 | + // Clear module cache to pick up fresh cwd |
| 54 | + const mod = await import('../theory-skill.js'); |
| 55 | + return mod.TheorySkill; |
| 56 | + } |
| 57 | + |
| 58 | + it('init creates THEORY.MD with sections', async () => { |
| 59 | + const TheorySkill = await getTheorySkill(); |
| 60 | + const skill = new TheorySkill(makeContext()); |
| 61 | + |
| 62 | + const result = skill.init('Build context-aware memory for AI agents'); |
| 63 | + |
| 64 | + expect(result.success).toBe(true); |
| 65 | + expect(result.message).toContain('Created THEORY.MD'); |
| 66 | + expect(result.data?.sections).toHaveLength(5); |
| 67 | + |
| 68 | + // File should exist |
| 69 | + const content = fs.readFileSync(path.join(tmpDir, 'THEORY.MD'), 'utf-8'); |
| 70 | + expect(content).toContain('## Problem'); |
| 71 | + expect(content).toContain('Build context-aware memory for AI agents'); |
| 72 | + expect(content).toContain('## Operating Theory'); |
| 73 | + expect(content).toContain('## Strategy'); |
| 74 | + expect(content).toContain('## Key Discoveries'); |
| 75 | + expect(content).toContain('## Open Questions'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('init rejects empty problem statement', async () => { |
| 79 | + const TheorySkill = await getTheorySkill(); |
| 80 | + const skill = new TheorySkill(makeContext()); |
| 81 | + |
| 82 | + const result = skill.init(''); |
| 83 | + expect(result.success).toBe(false); |
| 84 | + expect(result.message).toContain('problem statement is required'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('init refuses if THEORY.MD already exists', async () => { |
| 88 | + const TheorySkill = await getTheorySkill(); |
| 89 | + const skill = new TheorySkill(makeContext()); |
| 90 | + |
| 91 | + fs.writeFileSync(path.join(tmpDir, 'THEORY.MD'), '# existing'); |
| 92 | + |
| 93 | + const result = skill.init('New problem'); |
| 94 | + expect(result.success).toBe(false); |
| 95 | + expect(result.message).toContain('already exists'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('show returns content when THEORY.MD exists', async () => { |
| 99 | + const TheorySkill = await getTheorySkill(); |
| 100 | + const skill = new TheorySkill(makeContext()); |
| 101 | + |
| 102 | + const testContent = '# THEORY.MD\n\nSome theory content here.'; |
| 103 | + fs.writeFileSync(path.join(tmpDir, 'THEORY.MD'), testContent); |
| 104 | + |
| 105 | + const result = skill.show(); |
| 106 | + expect(result.success).toBe(true); |
| 107 | + expect(result.message).toBe(testContent); |
| 108 | + expect(result.data?.length).toBe(testContent.length); |
| 109 | + }); |
| 110 | + |
| 111 | + it('show returns error when no THEORY.MD', async () => { |
| 112 | + const TheorySkill = await getTheorySkill(); |
| 113 | + const skill = new TheorySkill(makeContext()); |
| 114 | + |
| 115 | + const result = skill.show(); |
| 116 | + expect(result.success).toBe(false); |
| 117 | + expect(result.message).toContain('No THEORY.MD found'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('update validates and writes content', async () => { |
| 121 | + const TheorySkill = await getTheorySkill(); |
| 122 | + const skill = new TheorySkill(makeContext()); |
| 123 | + |
| 124 | + // Content must be > 100 chars |
| 125 | + const content = |
| 126 | + '# THEORY.MD\n\n## Problem\n\nWe need to build a context-aware memory system that preserves agent state across sessions and enables efficient retrieval.'; |
| 127 | + |
| 128 | + const result = skill.update(content); |
| 129 | + expect(result.success).toBe(true); |
| 130 | + expect(result.message).toContain('Updated THEORY.MD'); |
| 131 | + |
| 132 | + const written = fs.readFileSync(path.join(tmpDir, 'THEORY.MD'), 'utf-8'); |
| 133 | + expect(written).toBe(content); |
| 134 | + }); |
| 135 | + |
| 136 | + it('update rejects too-short content', async () => { |
| 137 | + const TheorySkill = await getTheorySkill(); |
| 138 | + const skill = new TheorySkill(makeContext()); |
| 139 | + |
| 140 | + const result = skill.update('too short'); |
| 141 | + expect(result.success).toBe(false); |
| 142 | + expect(result.message).toContain('too short'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('update warns on checkboxes', async () => { |
| 146 | + const TheorySkill = await getTheorySkill(); |
| 147 | + const skill = new TheorySkill(makeContext()); |
| 148 | + |
| 149 | + const content = |
| 150 | + '# THEORY.MD\n\n## Problem\n\nBuild a thing.\n\n- [x] Done\n- [ ] Not done\n\nLots more text here to get past the minimum length requirement which is one hundred characters.'; |
| 151 | + |
| 152 | + const result = skill.update(content); |
| 153 | + expect(result.success).toBe(true); |
| 154 | + expect(result.data?.warnings).toContainEqual( |
| 155 | + expect.stringContaining('checkboxes') |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + it('update warns on dates', async () => { |
| 160 | + const TheorySkill = await getTheorySkill(); |
| 161 | + const skill = new TheorySkill(makeContext()); |
| 162 | + |
| 163 | + const content = |
| 164 | + '# THEORY.MD\n\n## Problem\n\nBuild a thing.\n\n2024-01-15: Added feature X\n\nLots more text here to get past the minimum length requirement which is one hundred characters exactly.'; |
| 165 | + |
| 166 | + const result = skill.update(content); |
| 167 | + expect(result.success).toBe(true); |
| 168 | + expect(result.data?.warnings).toContainEqual( |
| 169 | + expect.stringContaining('dates') |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('update records frame event when frameManager available', async () => { |
| 174 | + const TheorySkill = await getTheorySkill(); |
| 175 | + const mockFrameManager = { |
| 176 | + createFrame: vi.fn().mockReturnValue('frame-123'), |
| 177 | + addEvent: vi.fn().mockReturnValue('event-123'), |
| 178 | + closeFrame: vi.fn(), |
| 179 | + }; |
| 180 | + |
| 181 | + const skill = new TheorySkill( |
| 182 | + makeContext({ frameManager: mockFrameManager as any }) |
| 183 | + ); |
| 184 | + |
| 185 | + const content = |
| 186 | + '# THEORY.MD\n\n## Problem\n\nWe need context-aware memory for AI agents that persists across sessions and supports efficient semantic retrieval.'; |
| 187 | + |
| 188 | + skill.update(content); |
| 189 | + |
| 190 | + expect(mockFrameManager.createFrame).toHaveBeenCalledWith( |
| 191 | + 'write', |
| 192 | + 'theory-update', |
| 193 | + expect.objectContaining({ source: 'THEORY.MD' }) |
| 194 | + ); |
| 195 | + expect(mockFrameManager.addEvent).toHaveBeenCalledWith( |
| 196 | + 'artifact', |
| 197 | + expect.objectContaining({ type: 'theory-update' }), |
| 198 | + 'frame-123' |
| 199 | + ); |
| 200 | + expect(mockFrameManager.closeFrame).toHaveBeenCalledWith( |
| 201 | + 'frame-123', |
| 202 | + expect.objectContaining({ theory_updated: true }) |
| 203 | + ); |
| 204 | + }); |
| 205 | + |
| 206 | + it('status reports metadata when THEORY.MD exists', async () => { |
| 207 | + const TheorySkill = await getTheorySkill(); |
| 208 | + const skill = new TheorySkill(makeContext()); |
| 209 | + |
| 210 | + // Create via init |
| 211 | + skill.init('Test problem for status check'); |
| 212 | + |
| 213 | + const result = skill.status(); |
| 214 | + expect(result.success).toBe(true); |
| 215 | + expect(result.data?.exists).toBe(true); |
| 216 | + expect(result.data?.lineCount).toBeGreaterThan(0); |
| 217 | + expect(result.data?.sections).toContain('## Problem'); |
| 218 | + expect(result.data?.totalSections).toBe(5); |
| 219 | + expect(result.data?.lastModified).toBeDefined(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('status reports not found when no THEORY.MD', async () => { |
| 223 | + const TheorySkill = await getTheorySkill(); |
| 224 | + const skill = new TheorySkill(makeContext()); |
| 225 | + |
| 226 | + const result = skill.status(); |
| 227 | + expect(result.success).toBe(true); |
| 228 | + expect(result.data?.exists).toBe(false); |
| 229 | + }); |
| 230 | + |
| 231 | + it('git root detection works', async () => { |
| 232 | + const TheorySkill = await getTheorySkill(); |
| 233 | + const skill = new TheorySkill(makeContext()); |
| 234 | + |
| 235 | + // The skill should find the git root (our tmpDir) |
| 236 | + skill.init('Git root test'); |
| 237 | + |
| 238 | + expect(fs.existsSync(path.join(tmpDir, 'THEORY.MD'))).toBe(true); |
| 239 | + }); |
| 240 | +}); |
0 commit comments