test(skill-tool): isolate skill discovery spies from other suites

This commit is contained in:
Sisyphus
2026-04-18 14:57:30 +09:00
parent 10b1905f60
commit 0e1a946c1d
+23 -18
View File
@@ -1,7 +1,11 @@
/// <reference types="bun-types" />
import { afterEach, describe, expect, it, mock } from "bun:test"
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test"
import type { ToolContext } from "@opencode-ai/plugin/tool"
import type { LoadedSkill } from "../../features/opencode-skill-loader/types"
import * as skillContent from "../../features/opencode-skill-loader/skill-content"
import * as commandDiscovery from "../slashcommand/command-discovery"
import { createSkillTool } from "./tools"
function createMockSkill(name: string): LoadedSkill {
return {
@@ -24,26 +28,27 @@ const loadedSkill = createMockSkill("lazy-skill")
const discoverCommandsSync = mock(() => [])
const getAllSkills = mock(async () => [loadedSkill])
const clearSkillCache = mock(() => {})
const mockContext: ToolContext = {
sessionID: "test-session",
messageID: "msg-1",
agent: "test-agent",
directory: "/test",
worktree: "/test",
abort: new AbortController().signal,
metadata: () => {},
ask: async () => {},
}
const skillContentModuleFactory = () => ({
clearSkillCache,
getAllSkills,
extractSkillTemplate: () => loadedSkill.definition.template ?? "",
injectGitMasterConfig: (body: string) => body,
beforeEach(() => {
mock.restore()
spyOn(commandDiscovery, "discoverCommandsSync").mockImplementation(discoverCommandsSync)
spyOn(skillContent, "getAllSkills").mockImplementation(getAllSkills)
spyOn(skillContent, "clearSkillCache").mockImplementation(clearSkillCache)
})
const commandDiscoveryModuleFactory = () => ({
discoverCommandsSync,
})
mock.module("../../features/opencode-skill-loader/skill-content", skillContentModuleFactory)
mock.module("../../features/opencode-skill-loader/skill-content.ts", skillContentModuleFactory)
mock.module("../slashcommand/command-discovery", commandDiscoveryModuleFactory)
mock.module("../slashcommand/command-discovery.ts", commandDiscoveryModuleFactory)
const { createSkillTool } = await import("./tools")
afterEach(async () => {
await flushMicrotasks()
mock.restore()
})
describe("createSkillTool", () => {
@@ -73,7 +78,7 @@ describe("createSkillTool", () => {
// then
expect(getAllSkills.mock.calls.length).toBe(baselineGetAllSkillsCalls)
await skillTool.execute({ name: "lazy-skill" })
await skillTool.execute({ name: "lazy-skill" }, mockContext)
expect(getAllSkills.mock.calls.length).toBe(baselineGetAllSkillsCalls + 1)
})
@@ -86,7 +91,7 @@ describe("createSkillTool", () => {
const skillTool = createSkillTool({})
void skillTool.description
await flushMicrotasks()
await skillTool.execute({ name: "lazy-skill" })
await skillTool.execute({ name: "lazy-skill" }, mockContext)
// then
expect(clearSkillCache.mock.calls.length).toBe(baselineClearSkillCacheCalls)