|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, test } from "vitest"; |
| 5 | +import { runInitCommand } from "../../src/commands/init"; |
| 6 | + |
| 7 | +const tempDirs: string[] = []; |
| 8 | + |
| 9 | +function makeTempDir(prefix: string): string { |
| 10 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 11 | + tempDirs.push(dir); |
| 12 | + return dir; |
| 13 | +} |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + for (const dir of tempDirs.splice(0)) { |
| 17 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 18 | + } |
| 19 | +}); |
| 20 | + |
| 21 | +describe("runInitCommand", () => { |
| 22 | + test("creates SKILL.md in current directory", () => { |
| 23 | + const root = makeTempDir("skillet-init-"); |
| 24 | + const cwd = path.join(root, "alpha-skill"); |
| 25 | + fs.mkdirSync(cwd, { recursive: true }); |
| 26 | + |
| 27 | + const output: string[] = []; |
| 28 | + const exitCode = runInitCommand([], { |
| 29 | + cwd, |
| 30 | + yes: false, |
| 31 | + stdout: (line) => output.push(line), |
| 32 | + stderr: () => undefined, |
| 33 | + }); |
| 34 | + |
| 35 | + const skillFile = path.join(cwd, "SKILL.md"); |
| 36 | + expect(exitCode).toBe(0); |
| 37 | + expect(fs.existsSync(skillFile)).toBe(true); |
| 38 | + expect(fs.readFileSync(skillFile, "utf8")).toContain("name: alpha-skill"); |
| 39 | + expect(output[0]).toContain("Created"); |
| 40 | + }); |
| 41 | + |
| 42 | + test("creates SKILL.md in specified directory", () => { |
| 43 | + const root = makeTempDir("skillet-init-"); |
| 44 | + const cwd = root; |
| 45 | + |
| 46 | + const output: string[] = []; |
| 47 | + const exitCode = runInitCommand(["beta-skill"], { |
| 48 | + cwd, |
| 49 | + yes: false, |
| 50 | + stdout: (line) => output.push(line), |
| 51 | + stderr: () => undefined, |
| 52 | + }); |
| 53 | + |
| 54 | + const skillFile = path.join(cwd, "beta-skill", "SKILL.md"); |
| 55 | + expect(exitCode).toBe(0); |
| 56 | + expect(fs.readFileSync(skillFile, "utf8")).toContain("name: beta-skill"); |
| 57 | + expect(fs.readFileSync(skillFile, "utf8")).toContain("description:"); |
| 58 | + }); |
| 59 | + |
| 60 | + test("does not overwrite existing SKILL.md without confirmation", () => { |
| 61 | + const root = makeTempDir("skillet-init-"); |
| 62 | + const cwd = path.join(root, "gamma-skill"); |
| 63 | + fs.mkdirSync(cwd, { recursive: true }); |
| 64 | + const skillFile = path.join(cwd, "SKILL.md"); |
| 65 | + fs.writeFileSync(skillFile, "existing"); |
| 66 | + |
| 67 | + const errors: string[] = []; |
| 68 | + const exitCode = runInitCommand([], { |
| 69 | + cwd, |
| 70 | + yes: false, |
| 71 | + confirmOverwrite: () => false, |
| 72 | + stdout: () => undefined, |
| 73 | + stderr: (line) => errors.push(line), |
| 74 | + }); |
| 75 | + |
| 76 | + expect(exitCode).toBe(1); |
| 77 | + expect(fs.readFileSync(skillFile, "utf8")).toBe("existing"); |
| 78 | + expect(errors.join("\n")).toContain("already exists"); |
| 79 | + }); |
| 80 | + |
| 81 | + test("overwrites existing SKILL.md when --yes is set", () => { |
| 82 | + const root = makeTempDir("skillet-init-"); |
| 83 | + const cwd = path.join(root, "delta-skill"); |
| 84 | + fs.mkdirSync(cwd, { recursive: true }); |
| 85 | + const skillFile = path.join(cwd, "SKILL.md"); |
| 86 | + fs.writeFileSync(skillFile, "existing"); |
| 87 | + |
| 88 | + const exitCode = runInitCommand([], { |
| 89 | + cwd, |
| 90 | + yes: true, |
| 91 | + stdout: () => undefined, |
| 92 | + stderr: () => undefined, |
| 93 | + }); |
| 94 | + |
| 95 | + expect(exitCode).toBe(0); |
| 96 | + expect(fs.readFileSync(skillFile, "utf8")).toContain("name: delta-skill"); |
| 97 | + }); |
| 98 | + |
| 99 | + test("rejects invalid skill names", () => { |
| 100 | + const root = makeTempDir("skillet-init-"); |
| 101 | + const cwd = root; |
| 102 | + |
| 103 | + const errors: string[] = []; |
| 104 | + const exitCode = runInitCommand(["InvalidName"], { |
| 105 | + cwd, |
| 106 | + yes: false, |
| 107 | + stdout: () => undefined, |
| 108 | + stderr: (line) => errors.push(line), |
| 109 | + }); |
| 110 | + |
| 111 | + expect(exitCode).toBe(1); |
| 112 | + expect(errors.join("\n")).toContain("must be lowercase"); |
| 113 | + }); |
| 114 | +}); |
0 commit comments