|
| 1 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + PURESTACK_SKILL_PATH, |
| 7 | + readPureStackSkill, |
| 8 | + updatePureStackSkill, |
| 9 | +} from './agent-skill' |
| 10 | + |
| 11 | +const packageRoot = path.resolve(import.meta.dirname, '../..') |
| 12 | +let temporary: string |
| 13 | + |
| 14 | +beforeEach(async () => { |
| 15 | + temporary = await mkdtemp(path.join(tmpdir(), 'purestack-skill-test-')) |
| 16 | +}) |
| 17 | +afterEach(async () => { |
| 18 | + await rm(temporary, { recursive: true, force: true }) |
| 19 | +}) |
| 20 | + |
| 21 | +async function site(dependencies: Record<string, string> = { purestack: '1' }) { |
| 22 | + await writeFile( |
| 23 | + path.join(temporary, 'package.json'), |
| 24 | + JSON.stringify({ name: 'site', dependencies }), |
| 25 | + ) |
| 26 | + return path.join(temporary, PURESTACK_SKILL_PATH) |
| 27 | +} |
| 28 | + |
| 29 | +describe('PureStack agent skill', () => { |
| 30 | + it('marks the shipped skill as managed without breaking its frontmatter', async () => { |
| 31 | + const skill = await readPureStackSkill(packageRoot, '9.8.7') |
| 32 | + const [frontmatter, rest] = skill.split(/(?<=\n---\n)/, 2) |
| 33 | + expect(frontmatter).toMatch(/^---\nname: purestack-development\n/) |
| 34 | + expect(rest).toMatch( |
| 35 | + /^\n<!-- Managed by PureStack 9\.8\.7\. Run `purestack skills update`/, |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it('rejects a shipped skill without frontmatter', async () => { |
| 40 | + const file = path.join(temporary, PURESTACK_SKILL_PATH) |
| 41 | + await mkdir(path.dirname(file), { recursive: true }) |
| 42 | + await writeFile(file, '# No frontmatter\n') |
| 43 | + await expect(readPureStackSkill(temporary, '1.0.0')).rejects.toThrow( |
| 44 | + 'skills/purestack-development/SKILL.md must start with frontmatter.', |
| 45 | + ) |
| 46 | + }) |
| 47 | + |
| 48 | + it('replaces a stale or edited copy and reports when nothing changed', async () => { |
| 49 | + const target = await site() |
| 50 | + await mkdir(path.dirname(target), { recursive: true }) |
| 51 | + await writeFile(target, 'locally edited') |
| 52 | + |
| 53 | + expect(await updatePureStackSkill(temporary, packageRoot, '2.0.0')).toBe( |
| 54 | + true, |
| 55 | + ) |
| 56 | + expect(await readFile(target, 'utf8')).toBe( |
| 57 | + await readPureStackSkill(packageRoot, '2.0.0'), |
| 58 | + ) |
| 59 | + expect(await updatePureStackSkill(temporary, packageRoot, '2.0.0')).toBe( |
| 60 | + false, |
| 61 | + ) |
| 62 | + }) |
| 63 | + |
| 64 | + it('also recognizes PureStack as a development dependency', async () => { |
| 65 | + await writeFile( |
| 66 | + path.join(temporary, 'package.json'), |
| 67 | + JSON.stringify({ devDependencies: { purestack: '1' } }), |
| 68 | + ) |
| 69 | + expect(await updatePureStackSkill(temporary, packageRoot, '2.0.0')).toBe( |
| 70 | + true, |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + it.each([ |
| 75 | + ['without a package.json', undefined], |
| 76 | + ['without a PureStack dependency', { regor: '1' }], |
| 77 | + ])('refuses folders %s', async (_case, dependencies) => { |
| 78 | + if (dependencies) await site(dependencies) |
| 79 | + await expect( |
| 80 | + updatePureStackSkill(temporary, packageRoot, '2.0.0'), |
| 81 | + ).rejects.toThrow( |
| 82 | + 'Run purestack skills update in the root folder of a PureStack site.', |
| 83 | + ) |
| 84 | + }) |
| 85 | +}) |
0 commit comments