test(skill-tool): isolate skill discovery spies from other suites
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
/// <reference types="bun-types" />
|
/// <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 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 {
|
function createMockSkill(name: string): LoadedSkill {
|
||||||
return {
|
return {
|
||||||
@@ -24,26 +28,27 @@ const loadedSkill = createMockSkill("lazy-skill")
|
|||||||
const discoverCommandsSync = mock(() => [])
|
const discoverCommandsSync = mock(() => [])
|
||||||
const getAllSkills = mock(async () => [loadedSkill])
|
const getAllSkills = mock(async () => [loadedSkill])
|
||||||
const clearSkillCache = mock(() => {})
|
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 = () => ({
|
beforeEach(() => {
|
||||||
clearSkillCache,
|
mock.restore()
|
||||||
getAllSkills,
|
spyOn(commandDiscovery, "discoverCommandsSync").mockImplementation(discoverCommandsSync)
|
||||||
extractSkillTemplate: () => loadedSkill.definition.template ?? "",
|
spyOn(skillContent, "getAllSkills").mockImplementation(getAllSkills)
|
||||||
injectGitMasterConfig: (body: string) => body,
|
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 () => {
|
afterEach(async () => {
|
||||||
await flushMicrotasks()
|
await flushMicrotasks()
|
||||||
|
mock.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("createSkillTool", () => {
|
describe("createSkillTool", () => {
|
||||||
@@ -73,7 +78,7 @@ describe("createSkillTool", () => {
|
|||||||
// then
|
// then
|
||||||
expect(getAllSkills.mock.calls.length).toBe(baselineGetAllSkillsCalls)
|
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)
|
expect(getAllSkills.mock.calls.length).toBe(baselineGetAllSkillsCalls + 1)
|
||||||
})
|
})
|
||||||
@@ -86,7 +91,7 @@ describe("createSkillTool", () => {
|
|||||||
const skillTool = createSkillTool({})
|
const skillTool = createSkillTool({})
|
||||||
void skillTool.description
|
void skillTool.description
|
||||||
await flushMicrotasks()
|
await flushMicrotasks()
|
||||||
await skillTool.execute({ name: "lazy-skill" })
|
await skillTool.execute({ name: "lazy-skill" }, mockContext)
|
||||||
|
|
||||||
// then
|
// then
|
||||||
expect(clearSkillCache.mock.calls.length).toBe(baselineClearSkillCacheCalls)
|
expect(clearSkillCache.mock.calls.length).toBe(baselineClearSkillCacheCalls)
|
||||||
|
|||||||
Reference in New Issue
Block a user