/** * Third-party agent skills vendored into the package (docs/agent/external) and installed by * `iapl-skills --external`. They are pinned, text-only and carry their licence; the installer * downloads nothing and leaves the project's own skill folders alone. */ import { execFileSync } from 'node:child_process' import { createHash } from 'node:crypto' import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' import { afterAll, describe, expect, test } from 'vitest' type VendoredSkill = { commit: string files: Record license: string name: string path: string publisher: string repo: string } const root = path.resolve(import.meta.dirname, '..') const extDir = path.join(root, 'docs/agent/external') const manifest = JSON.parse(readFileSync(path.join(extDir, 'manifest.json'), 'utf8')) as { skills: VendoredSkill[] } const read = (file: string) => readFileSync(file, 'utf8') function filesOf(dir: string, base = dir): string[] { return readdirSync(dir).flatMap((name) => { const full = path.join(dir, name) return statSync(full).isDirectory() ? filesOf(full, base) : [path.relative(base, full).split(path.sep).join('/')] }) } describe('vendored external skills', () => { test('every skill is pinned to a full commit SHA, never a branch', () => { expect(manifest.skills.length).toBeGreaterThan(0) for (const skill of manifest.skills) { expect(skill.commit, skill.name).toMatch(/^[0-9a-f]{40}$/) expect(skill.repo, skill.name).toMatch(/^https:\/\/github\.com\/[\w.-]+\/[\w.-]+$/) } // The vendoring script holds the same pins, and fetches by commit only. const script = read(path.join(root, 'scripts/vendor-external-skills.mjs')) const pins = [...script.matchAll(/commit: '([^']+)'/g)].map((m) => m[1]) expect(pins.sort()).toEqual(manifest.skills.map((s) => s.commit).sort()) expect(script).not.toMatch(/--branch|refs\/heads|\/main\b|\/master\b/) }) test('the installer downloads nothing', () => { const bin = read(path.join(root, 'bin/iapl-skills.mjs')) expect(bin).not.toMatch(/https?:\/\/|\bfetch\(|child_process|execFile|spawn/) }) test('the files match manifest.json, are text only, and carry a licence and the source', () => { for (const skill of manifest.skills) { const dir = path.join(extDir, skill.name) const files = filesOf(dir).sort() expect(files, skill.name).toEqual(Object.keys(skill.files).sort()) for (const file of files) { const full = path.join(dir, file) expect(createHash('sha256').update(readFileSync(full)).digest('hex'), `${skill.name}/${file}`).toBe(skill.files[file]) expect(file, `${skill.name}: text only`).toMatch(/\.(?:json|md|txt)$|^LICENSE$/) expect(statSync(full).mode & 0o111, `${skill.name}/${file} is executable`).toBe(0) } expect(files.some((f) => /^LICENSE(?:\.md|\.txt)?$/.test(f)), `${skill.name}: licence`).toBe(true) expect(read(path.join(dir, 'SOURCE.md'))).toContain(skill.commit) } }) test('each SKILL.md is renamed to its folder and starts with the project-rules override', () => { for (const skill of manifest.skills) { expect(skill.name).toMatch(/^ext-/) const text = read(path.join(extDir, skill.name, 'SKILL.md')) expect(text).toMatch(new RegExp(`^---\\nname: ${skill.name}\\n`)) const body = text.slice(text.indexOf('\n---', 3) + 4).trimStart() expect(body.startsWith('> **Project rules win.**'), skill.name).toBe(true) // No instruction to pull rules from a live URL at run time. expect(text).not.toMatch(/raw\.githubusercontent\.com/) } }) }) describe('iapl-skills --external', () => { const work = mkdtempSync(path.join(tmpdir(), 'ipal-ext-test-')) afterAll(() => rmSync(work, { force: true, recursive: true })) const skillsDir = path.join(work, '.agents/skills') const run = (...args: string[]) => execFileSync('node', [path.join(root, 'bin/iapl-skills.mjs'), '--no-pointer', ...args], { cwd: work, encoding: 'utf8' }) const names = () => readdirSync(skillsDir).sort() test('adds ext-* only on request and leaves the project\'s own folders alone', () => { mkdirSync(path.join(skillsDir, 'my-skill'), { recursive: true }) writeFileSync(path.join(skillsDir, 'my-skill/SKILL.md'), 'own') mkdirSync(path.join(skillsDir, 'ext-mine')) writeFileSync(path.join(skillsDir, 'ext-mine/SKILL.md'), 'own ext') run() expect(names().filter((n) => n.startsWith('ext-') && n !== 'ext-mine')).toEqual([]) run('--external') for (const skill of manifest.skills) {expect(existsSync(path.join(skillsDir, skill.name, 'SKILL.md')), skill.name).toBe(true)} expect(read(path.join(skillsDir, 'my-skill/SKILL.md'))).toBe('own') expect(read(path.join(skillsDir, 'ext-mine/SKILL.md'))).toBe('own ext') }) test('removes only retired ext-* folders that IAPL installed', () => { const retired = path.join(skillsDir, 'ext-retired') mkdirSync(retired) cpSync(path.join(extDir, manifest.skills[0].name, 'SOURCE.md'), path.join(retired, 'SOURCE.md')) run('--external', manifest.skills[0].name) expect(existsSync(retired)).toBe(false) expect(existsSync(path.join(skillsDir, 'ext-mine'))).toBe(true) }) test('an unknown name stops the run before anything is copied', () => { const empty = mkdtempSync(path.join(work, 'empty-')) expect(() => execFileSync('node', [path.join(root, 'bin/iapl-skills.mjs'), '--no-pointer', '--external', 'ext-nope'], { cwd: empty, stdio: 'pipe' }), ).toThrow() expect(readdirSync(empty)).toEqual([]) }) })