|
| 1 | +import fs from "fs/promises" |
| 2 | +import path from "path" |
| 3 | +import { describe, expect } from "bun:test" |
| 4 | +import { Effect, Layer } from "effect" |
| 5 | +import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder" |
| 6 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 7 | +import { ConfigDocument } from "@opencode-ai/core/config/document" |
| 8 | +import { Global } from "@opencode-ai/core/global" |
| 9 | +import { Location } from "@opencode-ai/core/location" |
| 10 | +import { AbsolutePath } from "@opencode-ai/core/schema" |
| 11 | +import { SkillV2 } from "@opencode-ai/core/skill" |
| 12 | +import { SkillCatalog } from "@opencode-ai/core/skill-catalog" |
| 13 | +import { SkillDiscovery } from "@opencode-ai/core/skill/discovery" |
| 14 | +import { location } from "./fixture/location" |
| 15 | +import { tmpdir } from "./fixture/tmpdir" |
| 16 | +import { testEffect } from "./lib/effect" |
| 17 | + |
| 18 | +const discovery = Layer.succeed(SkillDiscovery.Service, SkillDiscovery.Service.of({ pull: () => Effect.succeed([]) })) |
| 19 | + |
| 20 | +function testLayer(directory: string, globalDirectory = path.join(directory, "global"), projectDirectory = directory) { |
| 21 | + const locationLayer = Layer.succeed( |
| 22 | + Location.Service, |
| 23 | + Location.Service.of( |
| 24 | + location({ directory: AbsolutePath.make(directory) }, { projectDirectory: AbsolutePath.make(projectDirectory) }), |
| 25 | + ), |
| 26 | + ) |
| 27 | + return AppNodeBuilder.build(LayerNode.group([SkillCatalog.node, SkillV2.node, ConfigDocument.node]), [ |
| 28 | + [Location.node, locationLayer], |
| 29 | + [Global.node, Global.layerWith({ config: globalDirectory })], |
| 30 | + [SkillDiscovery.node, discovery], |
| 31 | + ]) |
| 32 | +} |
| 33 | + |
| 34 | +function withTmp<A, E, R>(run: (tmp: { path: string }) => Effect.Effect<A, E, R>) { |
| 35 | + return Effect.acquireRelease( |
| 36 | + Effect.promise(() => tmpdir()), |
| 37 | + (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), |
| 38 | + ).pipe(Effect.flatMap(run)) |
| 39 | +} |
| 40 | + |
| 41 | +function write(directory: string, name: string, description: string) { |
| 42 | + return fs.writeFile( |
| 43 | + path.join(directory, name, "SKILL.md"), |
| 44 | + `---\nname: ${name}\ndescription: ${description}\n---\n# ${name}`, |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +const it = testEffect(Layer.empty) |
| 49 | + |
| 50 | +describe("SkillCatalog", () => { |
| 51 | + it.live("reports both the winner and the shadowed loser, with provenance", () => |
| 52 | + withTmp((tmp) => |
| 53 | + Effect.gen(function* () { |
| 54 | + const first = path.join(tmp.path, "first") |
| 55 | + const second = path.join(tmp.path, "second") |
| 56 | + yield* Effect.promise(async () => { |
| 57 | + await fs.mkdir(path.join(first, "review"), { recursive: true }) |
| 58 | + await fs.mkdir(path.join(second, "review"), { recursive: true }) |
| 59 | + await write(first, "review", "First") |
| 60 | + await write(second, "review", "Second") |
| 61 | + }) |
| 62 | + |
| 63 | + return yield* Effect.gen(function* () { |
| 64 | + const skills = yield* SkillV2.Service |
| 65 | + yield* skills.transform((editor) => { |
| 66 | + editor.source({ type: "directory", path: AbsolutePath.make(first) }) |
| 67 | + editor.source({ type: "directory", path: AbsolutePath.make(second) }) |
| 68 | + }) |
| 69 | + |
| 70 | + const catalog = yield* SkillCatalog.Service |
| 71 | + const entries = yield* catalog.list() |
| 72 | + |
| 73 | + expect(entries).toHaveLength(2) |
| 74 | + const winner = entries.find((entry) => !entry.shadowedBy)! |
| 75 | + const loser = entries.find((entry) => entry.shadowedBy)! |
| 76 | + expect(winner.skill.description).toBe("Second") |
| 77 | + expect(winner.sourceIndex).toBe(1) |
| 78 | + expect(loser.skill.description).toBe("First") |
| 79 | + expect(loser.sourceIndex).toBe(0) |
| 80 | + expect(loser.shadowedBy).toEqual({ source: { type: "directory", path: AbsolutePath.make(second) }, sourceIndex: 1 }) |
| 81 | + |
| 82 | + // Same fact `SkillV2.list()` reports (winners only) -- the catalog must not disagree. |
| 83 | + const list = yield* skills.list() |
| 84 | + expect(list).toHaveLength(1) |
| 85 | + expect(list[0]!.description).toBe("Second") |
| 86 | + }).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie) |
| 87 | + }), |
| 88 | + ), |
| 89 | + ) |
| 90 | + |
| 91 | + // Delete-the-fix, the shape Ethan's ruling specified: perturb which source is registered LAST |
| 92 | + // (closer-wins) and confirm the winner moves on BOTH surfaces together. A catalog computed by |
| 93 | + // its own independent walk could pass the test above by coincidence while still disagreeing |
| 94 | + // with SkillV2.list() under a different ordering -- this is what rules that out. Both calls |
| 95 | + // read `SkillV2.Service.entries()`, so there is no ordering under which they COULD diverge; if |
| 96 | + // a future change made the catalog re-derive its own merge, this is the test that would catch it. |
| 97 | + it.live("perturbing registration order moves the winner on SkillV2.list() and the catalog together", () => |
| 98 | + withTmp((tmp) => |
| 99 | + Effect.gen(function* () { |
| 100 | + const first = path.join(tmp.path, "first") |
| 101 | + const second = path.join(tmp.path, "second") |
| 102 | + yield* Effect.promise(async () => { |
| 103 | + await fs.mkdir(path.join(first, "review"), { recursive: true }) |
| 104 | + await fs.mkdir(path.join(second, "review"), { recursive: true }) |
| 105 | + await write(first, "review", "First") |
| 106 | + await write(second, "review", "Second") |
| 107 | + }) |
| 108 | + |
| 109 | + return yield* Effect.gen(function* () { |
| 110 | + const skills = yield* SkillV2.Service |
| 111 | + // Reversed from the test above: `first` registered LAST, so it should win this time. |
| 112 | + yield* skills.transform((editor) => { |
| 113 | + editor.source({ type: "directory", path: AbsolutePath.make(second) }) |
| 114 | + editor.source({ type: "directory", path: AbsolutePath.make(first) }) |
| 115 | + }) |
| 116 | + |
| 117 | + const catalog = yield* SkillCatalog.Service |
| 118 | + const entries = yield* catalog.list() |
| 119 | + const winner = entries.find((entry) => !entry.shadowedBy)! |
| 120 | + expect(winner.skill.description).toBe("First") |
| 121 | + |
| 122 | + const list = yield* skills.list() |
| 123 | + expect(list[0]!.description).toBe("First") |
| 124 | + }).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie) |
| 125 | + }), |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + it.live("a directory source under the project root is attributed to the project target", () => |
| 130 | + withTmp((tmp) => |
| 131 | + Effect.gen(function* () { |
| 132 | + const skillDir = path.join(tmp.path, "skill") |
| 133 | + yield* Effect.promise(async () => { |
| 134 | + await fs.mkdir(path.join(skillDir, "deploy"), { recursive: true }) |
| 135 | + await write(skillDir, "deploy", "Deploy") |
| 136 | + await fs.writeFile(path.join(tmp.path, "opencode.json"), "{}") |
| 137 | + }) |
| 138 | + |
| 139 | + return yield* Effect.gen(function* () { |
| 140 | + const skills = yield* SkillV2.Service |
| 141 | + yield* skills.transform((editor) => editor.source({ type: "directory", path: AbsolutePath.make(skillDir) })) |
| 142 | + |
| 143 | + const catalog = yield* SkillCatalog.Service |
| 144 | + const entries = yield* catalog.list() |
| 145 | + expect(entries).toHaveLength(1) |
| 146 | + expect(entries[0]!.target).toBeDefined() |
| 147 | + |
| 148 | + const documents = yield* ConfigDocument.Service |
| 149 | + const project = (yield* documents.listTargets()).find((target) => target.kind === "project")! |
| 150 | + expect(entries[0]!.target).toBe(project.id) |
| 151 | + }).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie) |
| 152 | + }), |
| 153 | + ), |
| 154 | + ) |
| 155 | + |
| 156 | + it.live("a bare .opencode-tier directory with no co-located config falls back to the nearest enclosing target", () => |
| 157 | + withTmp((tmp) => |
| 158 | + Effect.gen(function* () { |
| 159 | + // Mirrors config/plugin/skill.ts's own convention: a `.opencode/skill` directory with no |
| 160 | + // opencode.json(c) sitting IN THAT SAME directory. `ConfigDocument.listTargets()` (chunk |
| 161 | + // 1 scope) never lists `.opencode` directories as targets themselves -- see that method's |
| 162 | + // own doc comment -- so this directory source has no CO-LOCATED target to match exactly. |
| 163 | + const dotOpencode = path.join(tmp.path, ".opencode") |
| 164 | + const skillDir = path.join(dotOpencode, "skill") |
| 165 | + yield* Effect.promise(async () => { |
| 166 | + await fs.mkdir(path.join(skillDir, "deploy"), { recursive: true }) |
| 167 | + await write(skillDir, "deploy", "Deploy") |
| 168 | + await fs.writeFile(path.join(tmp.path, "opencode.json"), "{}") |
| 169 | + }) |
| 170 | + |
| 171 | + return yield* Effect.gen(function* () { |
| 172 | + const skills = yield* SkillV2.Service |
| 173 | + yield* skills.transform((editor) => editor.source({ type: "directory", path: AbsolutePath.make(skillDir) })) |
| 174 | + |
| 175 | + const catalog = yield* SkillCatalog.Service |
| 176 | + const entries = yield* catalog.list() |
| 177 | + |
| 178 | + const documents = yield* ConfigDocument.Service |
| 179 | + const project = (yield* documents.listTargets()).find((target) => target.kind === "project")! |
| 180 | + // Not undefined, and not the global fallback either -- the project opencode.json one |
| 181 | + // directory up is the nearest ENCLOSING target, not the only possible one. |
| 182 | + expect(entries[0]!.target).toBe(project.id) |
| 183 | + }).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie) |
| 184 | + }), |
| 185 | + ), |
| 186 | + ) |
| 187 | + |
| 188 | + // Delete-the-fix for the fallback branch specifically: without `?? targets.find(kind === |
| 189 | + // "global")`, `findEnclosingTarget` returns undefined here (confirmed red -- neither the |
| 190 | + // project nor the "global" test fixture directory encloses a sibling tmpdir), silently |
| 191 | + // reporting "no location" for a source that DOES have an enclosing config, just not a nearby |
| 192 | + // one. A directory source registered from outside the project tree entirely (e.g. an absolute |
| 193 | + // path in a document's `skills: []` array, config/plugin/skill.ts) has no project-tier target |
| 194 | + // above it at all -- global is the only target left that structurally encloses it. |
| 195 | + it.live("a directory source outside the project tree entirely falls back to the global target", () => |
| 196 | + withTmp((tmp) => |
| 197 | + Effect.gen(function* () { |
| 198 | + const outside = yield* Effect.promise(() => tmpdir()) |
| 199 | + const skillDir = path.join(outside.path, "skill") |
| 200 | + yield* Effect.promise(async () => { |
| 201 | + await fs.mkdir(path.join(skillDir, "deploy"), { recursive: true }) |
| 202 | + await write(skillDir, "deploy", "Deploy") |
| 203 | + await fs.writeFile(path.join(tmp.path, "opencode.json"), "{}") |
| 204 | + }) |
| 205 | + |
| 206 | + return yield* Effect.gen(function* () { |
| 207 | + const skills = yield* SkillV2.Service |
| 208 | + yield* skills.transform((editor) => editor.source({ type: "directory", path: AbsolutePath.make(skillDir) })) |
| 209 | + |
| 210 | + const catalog = yield* SkillCatalog.Service |
| 211 | + const entries = yield* catalog.list() |
| 212 | + |
| 213 | + const documents = yield* ConfigDocument.Service |
| 214 | + const global = (yield* documents.listTargets()).find((target) => target.kind === "global")! |
| 215 | + expect(entries[0]!.target).toBe(global.id) |
| 216 | + |
| 217 | + yield* Effect.promise(() => outside[Symbol.asyncDispose]()) |
| 218 | + }).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie) |
| 219 | + }), |
| 220 | + ), |
| 221 | + ) |
| 222 | + |
| 223 | + it.live("url and embedded sources carry no target -- there is no location to attribute", () => |
| 224 | + withTmp((tmp) => |
| 225 | + Effect.gen(function* () { |
| 226 | + return yield* Effect.gen(function* () { |
| 227 | + const skills = yield* SkillV2.Service |
| 228 | + yield* skills.transform((editor) => { |
| 229 | + editor.source({ |
| 230 | + type: "embedded", |
| 231 | + skill: { name: "builtin", location: AbsolutePath.make("/builtin/skill"), content: "# builtin" }, |
| 232 | + }) |
| 233 | + }) |
| 234 | + |
| 235 | + const catalog = yield* SkillCatalog.Service |
| 236 | + const entries = yield* catalog.list() |
| 237 | + expect(entries).toHaveLength(1) |
| 238 | + expect(entries[0]!.target).toBeUndefined() |
| 239 | + }).pipe(Effect.provide(testLayer(tmp.path))).pipe(Effect.orDie) |
| 240 | + }), |
| 241 | + ), |
| 242 | + ) |
| 243 | +}) |
0 commit comments