|
| 1 | +/** |
| 2 | + * Tests for skill install command |
| 3 | + */ |
| 4 | +import { describe, it, expect, beforeEach, afterEach, spyOn } from 'bun:test'; |
| 5 | +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs'; |
| 6 | +import { tmpdir } from 'os'; |
| 7 | +import { join } from 'path'; |
| 8 | +import { BUNDLED_SKILL_FILES } from '../bundled-skill'; |
| 9 | +import { copyArchonSkill, skillInstallCommand } from './skill'; |
| 10 | + |
| 11 | +describe('copyArchonSkill', () => { |
| 12 | + let tempDir: string; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + tempDir = mkdtempSync(join(tmpdir(), 'archon-skill-test-')); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + rmSync(tempDir, { recursive: true, force: true }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('writes every bundled skill file under .claude/skills/archon/', async () => { |
| 23 | + await copyArchonSkill(tempDir); |
| 24 | + |
| 25 | + const skillRoot = join(tempDir, '.claude', 'skills', 'archon'); |
| 26 | + for (const [relativePath, content] of Object.entries(BUNDLED_SKILL_FILES)) { |
| 27 | + const dest = join(skillRoot, relativePath); |
| 28 | + expect(existsSync(dest)).toBe(true); |
| 29 | + expect(readFileSync(dest, 'utf-8')).toBe(content); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it('overwrites pre-existing skill files with bundled content', async () => { |
| 34 | + const skillRoot = join(tempDir, '.claude', 'skills', 'archon'); |
| 35 | + const skillMdPath = join(skillRoot, 'SKILL.md'); |
| 36 | + |
| 37 | + // Pre-seed with stale content; copyArchonSkill must overwrite it. |
| 38 | + await copyArchonSkill(tempDir); |
| 39 | + writeFileSync(skillMdPath, 'STALE'); |
| 40 | + expect(readFileSync(skillMdPath, 'utf-8')).toBe('STALE'); |
| 41 | + |
| 42 | + await copyArchonSkill(tempDir); |
| 43 | + expect(readFileSync(skillMdPath, 'utf-8')).toBe(BUNDLED_SKILL_FILES['SKILL.md']); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('skillInstallCommand', () => { |
| 48 | + let tempDir: string; |
| 49 | + let logSpy: ReturnType<typeof spyOn>; |
| 50 | + let errSpy: ReturnType<typeof spyOn>; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + tempDir = mkdtempSync(join(tmpdir(), 'archon-skill-cmd-test-')); |
| 54 | + logSpy = spyOn(console, 'log').mockImplementation(() => {}); |
| 55 | + errSpy = spyOn(console, 'error').mockImplementation(() => {}); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + rmSync(tempDir, { recursive: true, force: true }); |
| 60 | + logSpy.mockRestore(); |
| 61 | + errSpy.mockRestore(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns 0 and installs the skill into the target directory', async () => { |
| 65 | + const exitCode = await skillInstallCommand(tempDir); |
| 66 | + |
| 67 | + expect(exitCode).toBe(0); |
| 68 | + expect(existsSync(join(tempDir, '.claude', 'skills', 'archon', 'SKILL.md'))).toBe(true); |
| 69 | + // Final log line should mention restarting Claude Code |
| 70 | + const lastLog = logSpy.mock.calls.at(-1)?.[0] as string | undefined; |
| 71 | + expect(lastLog).toContain('Restart Claude Code'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns 1 and prints an error when the target directory does not exist', async () => { |
| 75 | + const missing = join(tempDir, 'does-not-exist'); |
| 76 | + const exitCode = await skillInstallCommand(missing); |
| 77 | + |
| 78 | + expect(exitCode).toBe(1); |
| 79 | + expect(errSpy).toHaveBeenCalled(); |
| 80 | + const firstError = errSpy.mock.calls[0][0] as string; |
| 81 | + expect(firstError).toContain('Directory does not exist'); |
| 82 | + // Nothing should have been written |
| 83 | + expect(existsSync(join(missing, '.claude'))).toBe(false); |
| 84 | + }); |
| 85 | +}); |
0 commit comments