|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 5 | +import { createIntentFsCache } from '../src/fs-cache.js' |
| 6 | + |
| 7 | +let root: string |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + root = mkdtempSync(join(tmpdir(), 'intent-fs-cache-test-')) |
| 11 | +}) |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + rmSync(root, { recursive: true, force: true }) |
| 15 | +}) |
| 16 | + |
| 17 | +describe('createIntentFsCache', () => { |
| 18 | + it('caches package.json reads', () => { |
| 19 | + writeFileSync( |
| 20 | + join(root, 'package.json'), |
| 21 | + JSON.stringify({ name: 'test-package' }), |
| 22 | + ) |
| 23 | + const cache = createIntentFsCache() |
| 24 | + |
| 25 | + expect(cache.readPackageJson(root)?.name).toBe('test-package') |
| 26 | + expect(cache.readPackageJson(root)?.name).toBe('test-package') |
| 27 | + |
| 28 | + expect(cache.getStats()).toEqual( |
| 29 | + expect.objectContaining({ |
| 30 | + packageJsonReadCount: 1, |
| 31 | + packageJsonCacheHits: 1, |
| 32 | + }), |
| 33 | + ) |
| 34 | + }) |
| 35 | + |
| 36 | + it('caches skill file discovery without exposing cached arrays', () => { |
| 37 | + const skillDir = join(root, 'skills', 'core') |
| 38 | + mkdirSync(skillDir, { recursive: true }) |
| 39 | + writeFileSync(join(skillDir, 'SKILL.md'), '---\nname: core\n---\n') |
| 40 | + const cache = createIntentFsCache() |
| 41 | + |
| 42 | + const first = cache.findSkillFiles(join(root, 'skills')) |
| 43 | + first.push('mutated') |
| 44 | + const laterSkillDir = join(root, 'skills', 'later') |
| 45 | + mkdirSync(laterSkillDir, { recursive: true }) |
| 46 | + writeFileSync(join(laterSkillDir, 'SKILL.md'), '---\nname: later\n---\n') |
| 47 | + const second = cache.findSkillFiles(join(root, 'skills')) |
| 48 | + |
| 49 | + expect(second).toHaveLength(1) |
| 50 | + expect(second[0]).toBe(join(skillDir, 'SKILL.md')) |
| 51 | + }) |
| 52 | +}) |
0 commit comments