| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- import { describe, expect } from "bun:test"
- import { Effect, Layer } from "effect"
- import { Skill } from "../../src/skill"
- import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
- import { provideInstance, provideTmpdirInstance, tmpdir } from "../fixture/fixture"
- import { testEffect } from "../lib/effect"
- import path from "path"
- import fs from "fs/promises"
- const node = CrossSpawnSpawner.defaultLayer
- const it = testEffect(Layer.mergeAll(Skill.defaultLayer, node))
- async function createGlobalSkill(homeDir: string) {
- const skillDir = path.join(homeDir, ".claude", "skills", "global-test-skill")
- await fs.mkdir(skillDir, { recursive: true })
- await Bun.write(
- path.join(skillDir, "SKILL.md"),
- `---
- name: global-test-skill
- description: A global skill from ~/.claude/skills for testing.
- ---
- # Global Test Skill
- This skill is loaded from the global home directory.
- `,
- )
- }
- const withHome = <A, E, R>(home: string, self: Effect.Effect<A, E, R>) =>
- Effect.acquireUseRelease(
- Effect.sync(() => {
- const prev = process.env.KILO_TEST_HOME
- process.env.KILO_TEST_HOME = home
- return prev
- }),
- () => self,
- (prev) =>
- Effect.sync(() => {
- process.env.KILO_TEST_HOME = prev
- }),
- )
- const discovered = <T extends { location: string }>(list: readonly T[]) =>
- list.filter((s) => s.location !== Skill.BUILTIN_LOCATION) // kilocode_change
- describe("skill", () => {
- // kilocode_change start
- it.live("discovers skills from .kilo/skill/ directory", () =>
- // kilocode_change end
- provideTmpdirInstance(
- (dir) =>
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Bun.write(
- path.join(dir, ".kilo", "skill", "test-skill", "SKILL.md"),
- `---
- name: test-skill
- description: A test skill for verification.
- ---
- # Test Skill
- Instructions here.
- `,
- ),
- )
- const skill = yield* Skill.Service
- const list = discovered(yield* skill.all()) // kilocode_change
- expect(list.length).toBe(1)
- const item = list.find((x) => x.name === "test-skill")
- expect(item).toBeDefined()
- expect(item!.description).toBe("A test skill for verification.")
- expect(item!.location).toContain(path.join("skill", "test-skill", "SKILL.md"))
- }),
- { git: true },
- ),
- )
- it.live("returns skill directories from Skill.dirs", () =>
- provideTmpdirInstance(
- (dir) =>
- withHome(
- dir,
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Bun.write(
- path.join(dir, ".kilo", "skill", "dir-skill", "SKILL.md"), // kilocode_change: .kilo is primary
- `---
- name: dir-skill
- description: Skill for dirs test.
- ---
- # Dir Skill
- `,
- ),
- )
- const skill = yield* Skill.Service
- const dirs = yield* skill.dirs()
- expect(dirs).toContain(path.join(dir, ".kilo", "skill", "dir-skill")) // kilocode_change: .kilo is primary
- expect(dirs.length).toBe(1)
- }),
- ),
- { git: true },
- ),
- )
- // kilocode_change start
- it.live("discovers multiple skills from .kilo/skill/ directory", () =>
- // kilocode_change end
- provideTmpdirInstance(
- (dir) =>
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Promise.all([
- Bun.write(
- path.join(dir, ".kilo", "skill", "skill-one", "SKILL.md"),
- `---
- name: skill-one
- description: First test skill.
- ---
- # Skill One
- `,
- ),
- Bun.write(
- path.join(dir, ".kilo", "skill", "skill-two", "SKILL.md"),
- `---
- name: skill-two
- description: Second test skill.
- ---
- # Skill Two
- `,
- ),
- ]),
- )
- const skill = yield* Skill.Service
- const list = discovered(yield* skill.all()) // kilocode_change
- expect(list.length).toBe(2)
- expect(list.find((x) => x.name === "skill-one")).toBeDefined()
- expect(list.find((x) => x.name === "skill-two")).toBeDefined()
- }),
- { git: true },
- ),
- )
- it.live("skips skills with missing frontmatter", () =>
- provideTmpdirInstance(
- (dir) =>
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Bun.write(
- path.join(dir, ".kilo", "skill", "no-frontmatter", "SKILL.md"), // kilocode_change: .kilo is primary
- `# No Frontmatter
- Just some content without YAML frontmatter.
- `,
- ),
- )
- const skill = yield* Skill.Service
- expect(discovered(yield* skill.all())).toEqual([]) // kilocode_change
- }),
- { git: true },
- ),
- )
- it.live("discovers skills from .claude/skills/ directory", () =>
- provideTmpdirInstance(
- (dir) =>
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Bun.write(
- path.join(dir, ".claude", "skills", "claude-skill", "SKILL.md"),
- `---
- name: claude-skill
- description: A skill in the .claude/skills directory.
- ---
- # Claude Skill
- `,
- ),
- )
- const skill = yield* Skill.Service
- const list = discovered(yield* skill.all()) // kilocode_change
- expect(list.length).toBe(1)
- const item = list.find((x) => x.name === "claude-skill")
- expect(item).toBeDefined()
- expect(item!.location).toContain(path.join(".claude", "skills", "claude-skill", "SKILL.md"))
- }),
- { git: true },
- ),
- )
- it.live("discovers global skills from ~/.claude/skills/ directory", () =>
- Effect.gen(function* () {
- const tmp = yield* Effect.acquireRelease(
- Effect.promise(() => tmpdir({ git: true })),
- (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
- )
- yield* withHome(
- tmp.path,
- Effect.gen(function* () {
- yield* Effect.promise(() => createGlobalSkill(tmp.path))
- yield* Effect.gen(function* () {
- const skill = yield* Skill.Service
- const list = discovered(yield* skill.all()) // kilocode_change
- expect(list.length).toBe(1)
- expect(list[0].name).toBe("global-test-skill")
- expect(list[0].description).toBe("A global skill from ~/.claude/skills for testing.")
- expect(list[0].location).toContain(path.join(".claude", "skills", "global-test-skill", "SKILL.md"))
- }).pipe(provideInstance(tmp.path))
- }),
- )
- }),
- )
- it.live("returns empty array when no skills exist", () =>
- provideTmpdirInstance(
- () =>
- Effect.gen(function* () {
- const skill = yield* Skill.Service
- expect(discovered(yield* skill.all())).toEqual([]) // kilocode_change
- }),
- { git: true },
- ),
- )
- it.live("discovers skills from .agents/skills/ directory", () =>
- provideTmpdirInstance(
- (dir) =>
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Bun.write(
- path.join(dir, ".agents", "skills", "agent-skill", "SKILL.md"),
- `---
- name: agent-skill
- description: A skill in the .agents/skills directory.
- ---
- # Agent Skill
- `,
- ),
- )
- const skill = yield* Skill.Service
- const list = discovered(yield* skill.all()) // kilocode_change
- expect(list.length).toBe(1)
- const item = list.find((x) => x.name === "agent-skill")
- expect(item).toBeDefined()
- expect(item!.location).toContain(path.join(".agents", "skills", "agent-skill", "SKILL.md"))
- }),
- { git: true },
- ),
- )
- it.live("discovers global skills from ~/.agents/skills/ directory", () =>
- Effect.gen(function* () {
- const tmp = yield* Effect.acquireRelease(
- Effect.promise(() => tmpdir({ git: true })),
- (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
- )
- yield* withHome(
- tmp.path,
- Effect.gen(function* () {
- const skillDir = path.join(tmp.path, ".agents", "skills", "global-agent-skill")
- yield* Effect.promise(() => fs.mkdir(skillDir, { recursive: true }))
- yield* Effect.promise(() =>
- Bun.write(
- path.join(skillDir, "SKILL.md"),
- `---
- name: global-agent-skill
- description: A global skill from ~/.agents/skills for testing.
- ---
- # Global Agent Skill
- This skill is loaded from the global home directory.
- `,
- ),
- )
- yield* Effect.gen(function* () {
- const skill = yield* Skill.Service
- const list = discovered(yield* skill.all()) // kilocode_change
- expect(list.length).toBe(1)
- expect(list[0].name).toBe("global-agent-skill")
- expect(list[0].description).toBe("A global skill from ~/.agents/skills for testing.")
- expect(list[0].location).toContain(path.join(".agents", "skills", "global-agent-skill", "SKILL.md"))
- }).pipe(provideInstance(tmp.path))
- }),
- )
- }),
- )
- it.live("discovers skills from both .claude/skills/ and .agents/skills/", () =>
- provideTmpdirInstance(
- (dir) =>
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Promise.all([
- Bun.write(
- path.join(dir, ".claude", "skills", "claude-skill", "SKILL.md"),
- `---
- name: claude-skill
- description: A skill in the .claude/skills directory.
- ---
- # Claude Skill
- `,
- ),
- Bun.write(
- path.join(dir, ".agents", "skills", "agent-skill", "SKILL.md"),
- `---
- name: agent-skill
- description: A skill in the .agents/skills directory.
- ---
- # Agent Skill
- `,
- ),
- ]),
- )
- const skill = yield* Skill.Service
- const list = discovered(yield* skill.all()) // kilocode_change
- expect(list.length).toBe(2)
- expect(list.find((x) => x.name === "claude-skill")).toBeDefined()
- expect(list.find((x) => x.name === "agent-skill")).toBeDefined()
- }),
- { git: true },
- ),
- )
- it.live("properly resolves directories that skills live in", () =>
- provideTmpdirInstance(
- (dir) =>
- Effect.gen(function* () {
- yield* Effect.promise(() =>
- Promise.all([
- Bun.write(
- path.join(dir, ".claude", "skills", "claude-skill", "SKILL.md"),
- `---
- name: claude-skill
- description: A skill in the .claude/skills directory.
- ---
- # Claude Skill
- `,
- ),
- Bun.write(
- path.join(dir, ".agents", "skills", "agent-skill", "SKILL.md"),
- `---
- name: agent-skill
- description: A skill in the .agents/skills directory.
- ---
- # Agent Skill
- `,
- ),
- Bun.write(
- path.join(dir, ".opencode", "skill", "agent-skill", "SKILL.md"),
- `---
- name: opencode-skill
- description: A skill in the .opencode/skill directory.
- ---
- # OpenCode Skill
- `,
- ),
- Bun.write(
- path.join(dir, ".opencode", "skills", "agent-skill", "SKILL.md"),
- `---
- name: opencode-skill
- description: A skill in the .opencode/skills directory.
- ---
- # OpenCode Skill
- `,
- ),
- ]),
- )
- const skill = yield* Skill.Service
- expect((yield* skill.dirs()).length).toBe(4)
- }),
- { git: true },
- ),
- )
- })
|