|
| 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 { runFindCommand } from "../../src/commands/find"; |
| 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 | +function createSkill(baseDir: string, relativeDir: string, name: string, description: string): void { |
| 16 | + const skillDir = path.join(baseDir, relativeDir, name); |
| 17 | + fs.mkdirSync(skillDir, { recursive: true }); |
| 18 | + fs.writeFileSync( |
| 19 | + path.join(skillDir, "SKILL.md"), |
| 20 | + `---\nname: ${name}\ndescription: ${description}\n---\n\n# ${name}\n` |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + for (const dir of tempDirs.splice(0)) { |
| 26 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | +describe("runFindCommand", () => { |
| 31 | + test("searches by name and description case-insensitively", () => { |
| 32 | + const cwd = makeTempDir("skillet-find-"); |
| 33 | + const homeDir = path.join(cwd, "home"); |
| 34 | + |
| 35 | + createSkill(cwd, ".codex/skills", "alpha", "Alpha deployment workflows"); |
| 36 | + createSkill(cwd, ".codex/skills", "gamma", "Utilities for alpha teams"); |
| 37 | + createSkill(cwd, ".codex/skills", "beta", "Database migrations"); |
| 38 | + |
| 39 | + const lines: string[] = []; |
| 40 | + const exitCode = runFindCommand(["ALPHA"], { |
| 41 | + cwd, |
| 42 | + homeDir, |
| 43 | + stdout: (line) => lines.push(line), |
| 44 | + stderr: () => undefined, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(exitCode).toBe(0); |
| 48 | + expect(lines[0]).toContain("alpha"); |
| 49 | + expect(lines.join("\n")).toContain("gamma"); |
| 50 | + expect(lines.join("\n")).not.toContain("beta"); |
| 51 | + }); |
| 52 | + |
| 53 | + test("lists all discovered skills when query is omitted", () => { |
| 54 | + const cwd = makeTempDir("skillet-find-"); |
| 55 | + const homeDir = path.join(cwd, "home"); |
| 56 | + |
| 57 | + createSkill(cwd, ".codex/skills", "zeta", "Zeta skill"); |
| 58 | + createSkill(cwd, ".codex/skills", "alpha", "Alpha skill"); |
| 59 | + |
| 60 | + const lines: string[] = []; |
| 61 | + const exitCode = runFindCommand([], { |
| 62 | + cwd, |
| 63 | + homeDir, |
| 64 | + stdout: (line) => lines.push(line), |
| 65 | + stderr: () => undefined, |
| 66 | + }); |
| 67 | + |
| 68 | + expect(exitCode).toBe(0); |
| 69 | + expect(lines[0]).toContain("alpha"); |
| 70 | + expect(lines[1]).toContain("zeta"); |
| 71 | + }); |
| 72 | + |
| 73 | + test("returns zero with clear message when no results match", () => { |
| 74 | + const cwd = makeTempDir("skillet-find-"); |
| 75 | + const homeDir = path.join(cwd, "home"); |
| 76 | + createSkill(cwd, ".codex/skills", "alpha", "Alpha skill"); |
| 77 | + |
| 78 | + const lines: string[] = []; |
| 79 | + const exitCode = runFindCommand(["nomatch"], { |
| 80 | + cwd, |
| 81 | + homeDir, |
| 82 | + stdout: (line) => lines.push(line), |
| 83 | + stderr: () => undefined, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(exitCode).toBe(0); |
| 87 | + expect(lines[0]).toBe('No skills found for "nomatch".'); |
| 88 | + }); |
| 89 | + |
| 90 | + test("surfaces invalid skill warnings in verbose mode", () => { |
| 91 | + const cwd = makeTempDir("skillet-find-"); |
| 92 | + const homeDir = path.join(cwd, "home"); |
| 93 | + |
| 94 | + createSkill(cwd, ".codex/skills", "alpha", "Alpha skill"); |
| 95 | + const invalidDir = path.join(cwd, ".codex/skills", "broken"); |
| 96 | + fs.mkdirSync(invalidDir, { recursive: true }); |
| 97 | + fs.writeFileSync(path.join(invalidDir, "SKILL.md"), "not valid"); |
| 98 | + |
| 99 | + const stderr: string[] = []; |
| 100 | + runFindCommand([], { |
| 101 | + cwd, |
| 102 | + homeDir, |
| 103 | + verbose: true, |
| 104 | + stdout: () => undefined, |
| 105 | + stderr: (line) => stderr.push(line), |
| 106 | + }); |
| 107 | + |
| 108 | + expect(stderr.join("\n")).toContain("Warning:"); |
| 109 | + expect(stderr.join("\n")).toContain("broken/SKILL.md"); |
| 110 | + }); |
| 111 | +}); |
0 commit comments