|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; |
| 5 | +import * as Path from "effect/Path"; |
| 6 | + |
| 7 | +import { discoverOpenCodeAcpSkills } from "./opencodeAcp.ts"; |
| 8 | + |
| 9 | +const makeFixture = Effect.gen(function* () { |
| 10 | + const fileSystem = yield* FileSystem.FileSystem; |
| 11 | + const path = yield* Path.Path; |
| 12 | + const root = yield* fileSystem.realPath( |
| 13 | + yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-opencode-acp-" }), |
| 14 | + ); |
| 15 | + const write = (relative: string, contents: string) => |
| 16 | + fileSystem |
| 17 | + .makeDirectory(path.dirname(path.join(root, relative)), { recursive: true }) |
| 18 | + .pipe(Effect.andThen(fileSystem.writeFileString(path.join(root, relative), contents))); |
| 19 | + const skill = (description: string) => `---\ndescription: ${description}\n---\n\nBody.\n`; |
| 20 | + const environment = { |
| 21 | + HOME: path.join(root, "home"), |
| 22 | + USERPROFILE: path.join(root, "home"), |
| 23 | + XDG_CONFIG_HOME: path.join(root, "xdg"), |
| 24 | + OPENCODE_CONFIG_DIR: path.join(root, "custom-config"), |
| 25 | + }; |
| 26 | + // Ancestors of the temp directory and the host's system config directory |
| 27 | + // are real OpenCode roots too, so only this fixture's skills are compared. |
| 28 | + const discover = (cwd: string, overrides: NodeJS.ProcessEnv = {}) => |
| 29 | + discoverOpenCodeAcpSkills(path.join(root, cwd), { ...environment, ...overrides }).pipe( |
| 30 | + Effect.map((skills) => skills.filter((skill) => skill.path.startsWith(root))), |
| 31 | + ); |
| 32 | + const descriptions = (cwd: string, overrides: NodeJS.ProcessEnv = {}) => |
| 33 | + discover(cwd, overrides).pipe( |
| 34 | + Effect.map((skills) => |
| 35 | + Object.fromEntries(skills.map((skill) => [skill.name, skill.description ?? null])), |
| 36 | + ), |
| 37 | + ); |
| 38 | + return { root, write, skill, discover, descriptions }; |
| 39 | +}); |
| 40 | + |
| 41 | +it.layer(NodeServices.layer)("discoverOpenCodeAcpSkills", (it) => { |
| 42 | + it.effect("resolves skill roots and duplicates the way OpenCode 2.1 does", () => |
| 43 | + Effect.gen(function* () { |
| 44 | + const { write, skill, descriptions } = yield* makeFixture; |
| 45 | + // Every .claude root loads before any .agents root, config directories |
| 46 | + // override both, and nearer project directories override ancestors. |
| 47 | + yield* write("home/.claude/skills/shared/SKILL.md", skill("global claude")); |
| 48 | + yield* write("home/.agents/skills/shared/SKILL.md", skill("global agents")); |
| 49 | + yield* write("project/.claude/skills/compat/SKILL.md", skill("project claude")); |
| 50 | + yield* write("home/.agents/skills/compat/SKILL.md", skill("global agents")); |
| 51 | + yield* write("project/.claude/skills/claude-only/SKILL.md", skill("project claude")); |
| 52 | + yield* write("custom-config/skills/shared/SKILL.md", skill("custom config")); |
| 53 | + yield* write( |
| 54 | + "xdg/opencode/skills/xdg-only/SKILL.md", |
| 55 | + skill("replaced by OPENCODE_CONFIG_DIR"), |
| 56 | + ); |
| 57 | + yield* write("project/.opencode/skill/nested/SKILL.md", skill("singular")); |
| 58 | + yield* write("project/.opencode/skills/nested/SKILL.md", skill("plural")); |
| 59 | + yield* write("project/app/.opencode/skills/nested/SKILL.md", skill("nearest")); |
| 60 | + yield* write("project/.opencode/skills/flat.md", skill("flat file")); |
| 61 | + yield* write("project/.opencode/skills/flat/SKILL.md", skill("skill directory")); |
| 62 | + yield* write("project/.agents/skills/venv/hidden/SKILL.md", skill("ignored")); |
| 63 | + yield* write("project/.agents/skills/__pycache__/hidden/SKILL.md", skill("ignored")); |
| 64 | + yield* write("project/.agents/skills/plain/SKILL.md", skill("plain")); |
| 65 | + |
| 66 | + expect(yield* descriptions("project/app")).toEqual({ |
| 67 | + "claude-only": "project claude", |
| 68 | + compat: "global agents", |
| 69 | + flat: "skill directory", |
| 70 | + nested: "nearest", |
| 71 | + plain: "plain", |
| 72 | + shared: "custom config", |
| 73 | + }); |
| 74 | + expect( |
| 75 | + yield* descriptions("project/app", { |
| 76 | + OPENCODE_DISABLE_PROJECT_CONFIG: "1", |
| 77 | + OPENCODE_DISABLE_CLAUDE_CODE_SKILLS: "true", |
| 78 | + }), |
| 79 | + ).toEqual({ compat: "global agents", shared: "custom config" }); |
| 80 | + }).pipe(Effect.scoped), |
| 81 | + ); |
| 82 | + |
| 83 | + it.effect("skips skills OpenCode rejects and bounds frontmatter parsing", () => |
| 84 | + Effect.gen(function* () { |
| 85 | + const { write, discover } = yield* makeFixture; |
| 86 | + const skills = "project/.agents/skills"; |
| 87 | + yield* write(`${skills}/no-frontmatter/SKILL.md`, "Just instructions.\n"); |
| 88 | + yield* write( |
| 89 | + `${skills}/colon/SKILL.md`, |
| 90 | + "---\nname: Colon Helper\ndescription: Use when: you need colons\n---\n", |
| 91 | + ); |
| 92 | + yield* write(`${skills}/malformed/SKILL.md`, "---\ndescription: [unclosed\n---\n"); |
| 93 | + yield* write(`${skills}/numeric-name/SKILL.md`, "---\nname: 3\n---\n"); |
| 94 | + yield* write(`${skills}/numeric-description/SKILL.md`, "---\ndescription: 3\n---\n"); |
| 95 | + yield* write(`${skills}/unterminated/SKILL.md`, "---\ndescription: never closed\n"); |
| 96 | + // `yaml` parses a 200KB mapping in seconds, so it is listed unparsed. |
| 97 | + const keys = Array.from({ length: 20_000 }, (_, index) => `key${index}: value`).join("\n"); |
| 98 | + yield* write(`${skills}/huge/SKILL.md`, `---\ndescription: huge\n${keys}\n---\n`); |
| 99 | + |
| 100 | + const discovered = yield* discover("project"); |
| 101 | + expect( |
| 102 | + discovered.map(({ name, displayName, description }) => ({ |
| 103 | + name, |
| 104 | + displayName, |
| 105 | + description, |
| 106 | + })), |
| 107 | + ).toEqual([ |
| 108 | + { name: "colon", displayName: "Colon Helper", description: "Use when: you need colons" }, |
| 109 | + { name: "huge", displayName: undefined, description: undefined }, |
| 110 | + { name: "no-frontmatter", displayName: undefined, description: undefined }, |
| 111 | + ]); |
| 112 | + }).pipe(Effect.scoped), |
| 113 | + ); |
| 114 | +}); |
0 commit comments