/** * Skills 2.0 gap E — promote_skill tool. * * Tests the one-step promotion personal → workspace, the refusal of team and * enterprise scopes in this edition, frontmatter rewrite with promoted_from * history and file move. */ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import * as fs from 'node:fs'; import * as path from 'node:path'; import * as os from 'node:os'; import { createSkillTools, getSkillDirForScope } from '../src/skill-tools.js'; import { parseSkillFrontmatter } from '../src/skill-frontmatter.js'; function seedSkill(dir: string, name: string, scope: string | null, body = 'body content'): void { fs.mkdirSync(dir, { recursive: true }); const scopeLine = scope ? `scope: ${scope}\n` : ''; const content = `---\nname: ${name}\ndescription: seeded test skill\n${scopeLine}---\n${body}`; fs.writeFileSync(path.join(dir, `${name}.md`), content, 'utf-8'); } describe('promote_skill — gap E', () => { let tmpDir: string; let reloadCount: number; function createTools(overrides: Partial[0]> = {}) { return createSkillTools({ waggleHome: tmpDir, onSkillsChanged: () => { reloadCount++; }, getWorkspaceId: () => 'ws-alpha', getTeamId: () => 'team-bravo', ...overrides, }); } function run(tools: ReturnType, name: string, args: Record = {}) { const tool = tools.find(t => t.name === name); if (!tool) throw new Error(`Tool ${name} not found`); return tool.execute(args); } beforeEach(() => { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-promote-test-')); reloadCount = 0; }); afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); }); describe('happy path', () => { it('promotes personal → workspace, moves file, appends history', async () => { const personalDir = getSkillDirForScope(tmpDir, 'personal', {})!; seedSkill(personalDir, 'deploy-helper', 'personal'); const tools = createTools(); const out = await run(tools, 'promote_skill', { skill_name: 'deploy-helper' }); expect(out).toContain('personal → workspace'); // File moved expect(fs.existsSync(path.join(personalDir, 'deploy-helper.md'))).toBe(false); const wsDir = getSkillDirForScope(tmpDir, 'workspace', { workspaceId: 'ws-alpha' })!; const newPath = path.join(wsDir, 'deploy-helper.md'); expect(fs.existsSync(newPath)).toBe(true); // Frontmatter updated with scope + promoted_from history const parsed = parseSkillFrontmatter(fs.readFileSync(newPath, 'utf-8')); expect(parsed.frontmatter.scope).toBe('workspace'); expect(parsed.frontmatter.promoted_from).toEqual(['personal']); // onSkillsChanged fired expect(reloadCount).toBe(1); }); }); describe('validation', () => { it('errors when skill is not found', async () => { const out = await run(createTools(), 'promote_skill', { skill_name: 'ghost' }); expect(out).toContain('not found in any accessible scope'); }); it('errors when already at enterprise (top)', async () => { const entDir = getSkillDirForScope(tmpDir, 'enterprise', {})!; seedSkill(entDir, 'top', 'enterprise'); const out = await run(createTools(), 'promote_skill', { skill_name: 'top' }); expect(out).toContain('already at the top scope'); }); it('errors when explicit target_scope is not exactly one rung above', async () => { const personalDir = getSkillDirForScope(tmpDir, 'personal', {})!; seedSkill(personalDir, 'jumpy', 'personal'); const out = await run(createTools(), 'promote_skill', { skill_name: 'jumpy', target_scope: 'team', // skip workspace — disallowed }); expect(out).toContain('not exactly one rung up'); }); it('treats a legacy skill without frontmatter scope as its dir-derived scope', async () => { const personalDir = getSkillDirForScope(tmpDir, 'personal', {})!; seedSkill(personalDir, 'legacy', null); // no scope field const out = await run(createTools(), 'promote_skill', { skill_name: 'legacy' }); expect(out).toContain('personal → workspace'); }); }); describe('edition limits', () => { it('refuses team promotion and leaves the skill in place', async () => { const wsDir = getSkillDirForScope(tmpDir, 'workspace', { workspaceId: 'ws-alpha' })!; seedSkill(wsDir, 'shared-helper', 'workspace'); const out = await run(createTools(), 'promote_skill', { skill_name: 'shared-helper' }); expect(out).toContain('not available in this edition'); expect(fs.existsSync(path.join(wsDir, 'shared-helper.md'))).toBe(true); }); it('refuses enterprise promotion', async () => { const teamDir = getSkillDirForScope(tmpDir, 'team', { teamId: 'team-bravo' })!; seedSkill(teamDir, 'hot-skill', 'team'); const out = await run(createTools(), 'promote_skill', { skill_name: 'hot-skill' }); expect(out).toContain('Cannot promote to enterprise scope'); expect(out).toContain('not available in this edition'); }); it('denies workspace promotion when no workspace is active', async () => { const personalDir = getSkillDirForScope(tmpDir, 'personal', {})!; seedSkill(personalDir, 'orphan', 'personal'); const tools = createTools({ getWorkspaceId: () => null }); const out = await run(tools, 'promote_skill', { skill_name: 'orphan' }); expect(out).toContain('no active workspace'); }); }); describe('safety', () => { it('refuses to overwrite an existing skill at the target scope', async () => { const personalDir = getSkillDirForScope(tmpDir, 'personal', {})!; const wsDir = getSkillDirForScope(tmpDir, 'workspace', { workspaceId: 'ws-alpha' })!; seedSkill(personalDir, 'collision', 'personal'); seedSkill(wsDir, 'collision', 'workspace'); // already exists at target const out = await run(createTools(), 'promote_skill', { skill_name: 'collision' }); expect(out).toContain('already exists at scope'); // Source file must NOT have been deleted expect(fs.existsSync(path.join(personalDir, 'collision.md'))).toBe(true); }); }); });