diff --git a/packages/engine/src/__tests__/agent-task-creation-github-tracking-flag.test.ts b/packages/engine/src/__tests__/agent-task-creation-github-tracking-flag.test.ts index 4e4188a254..284d86f4fa 100644 --- a/packages/engine/src/__tests__/agent-task-creation-github-tracking-flag.test.ts +++ b/packages/engine/src/__tests__/agent-task-creation-github-tracking-flag.test.ts @@ -7,12 +7,30 @@ import { TaskStore, setTaskCreatedHook } from "@fusion/core"; import { HeartbeatMonitor } from "../agent-heartbeat.js"; import { createDelegateTaskTool, createTaskCreateTool } from "../agent-tools.js"; -const githubTrackingHookModulePromise: Promise = import( - new URL("../../../dashboard/src/github-tracking-hook.js", import.meta.url).href -); -const githubTrackingModulePromise: Promise = import( - new URL("../../../dashboard/src/github-tracking.js", import.meta.url).href -); +const { githubTrackingHookPath, githubTrackingPath, maybeCreateTrackingIssueMock } = vi.hoisted(() => ({ + githubTrackingHookPath: new URL("../../../dashboard/src/github-tracking-hook.js", import.meta.url).href, + githubTrackingPath: new URL("../../../dashboard/src/github-tracking.js", import.meta.url).href, + maybeCreateTrackingIssueMock: vi.fn(async () => ({ + created: false as const, + reason: "no_repo_configured" as const, + })), +})); + +vi.mock("@fusion/core", async (importOriginal) => { + const { createEngineCoreMock } = await import("../test/mockCore.js"); + return createEngineCoreMock(() => importOriginal(), { + isGhAvailable: vi.fn(() => true), + isGhAuthenticated: vi.fn(() => true), + }); +}); + +vi.mock(githubTrackingPath, async (importOriginal) => { + const original = await importOriginal(); + return { + ...original, + maybeCreateTrackingIssue: maybeCreateTrackingIssueMock, + }; +}); function makeTmpDir(prefix: string): string { return mkdtempSync(join(tmpdir(), prefix)); @@ -25,7 +43,7 @@ describe("agent task creation githubTracking.enabled persistence", () => { beforeEach(async () => { setTaskCreatedHook(undefined); - vi.restoreAllMocks(); + vi.clearAllMocks(); rootDir = makeTmpDir("kb-engine-agent-task-create-gh-flag-"); globalDir = makeTmpDir("kb-engine-agent-task-create-gh-flag-global-"); store = new TaskStore(rootDir, globalDir, { inMemoryDb: true }); @@ -79,15 +97,11 @@ describe("agent task creation githubTracking.enabled persistence", () => { }, }, ])("persists githubTracking.enabled and invokes tracking hook for $name", async ({ run }) => { - const githubTrackingHookModule = await githubTrackingHookModulePromise; - const githubTrackingModule = await githubTrackingModulePromise; + const { registerGithubTrackingHook } = await import(githubTrackingHookPath); + const { maybeCreateTrackingIssue } = await import(githubTrackingPath); + const maybeCreateSpy = vi.mocked(maybeCreateTrackingIssue); - const maybeCreateSpy = vi.spyOn(githubTrackingModule, "maybeCreateTrackingIssue").mockImplementation(async () => ({ - created: false, - reason: "no_repo_configured", - })); - - githubTrackingHookModule.registerGithubTrackingHook(); + registerGithubTrackingHook(); const result = await run(); const taskId = (result as { details?: { taskId?: string } }).details?.taskId as string; diff --git a/packages/engine/src/__tests__/agent-tools-github-tracking-end-to-end.test.ts b/packages/engine/src/__tests__/agent-tools-github-tracking-end-to-end.test.ts index 1532474d9b..0a01fbf1b7 100644 --- a/packages/engine/src/__tests__/agent-tools-github-tracking-end-to-end.test.ts +++ b/packages/engine/src/__tests__/agent-tools-github-tracking-end-to-end.test.ts @@ -6,12 +6,30 @@ import { tmpdir } from "node:os"; import { TaskStore, resolveTaskGithubTracking, setTaskCreatedHook } from "@fusion/core"; import { createDelegateTaskTool, createTaskCreateTool } from "../agent-tools.js"; -const githubTrackingHookModulePromise: Promise = import( - new URL("../../../dashboard/src/github-tracking-hook.js", import.meta.url).href -); -const githubTrackingModulePromise: Promise = import( - new URL("../../../dashboard/src/github-tracking.js", import.meta.url).href -); +const { githubTrackingHookPath, githubTrackingPath, maybeCreateTrackingIssueMock } = vi.hoisted(() => ({ + githubTrackingHookPath: new URL("../../../dashboard/src/github-tracking-hook.js", import.meta.url).href, + githubTrackingPath: new URL("../../../dashboard/src/github-tracking.js", import.meta.url).href, + maybeCreateTrackingIssueMock: vi.fn(async () => ({ + created: false as const, + reason: "tracking_disabled" as const, + })), +})); + +vi.mock("@fusion/core", async (importOriginal) => { + const { createEngineCoreMock } = await import("../test/mockCore.js"); + return createEngineCoreMock(() => importOriginal(), { + isGhAvailable: vi.fn(() => true), + isGhAuthenticated: vi.fn(() => true), + }); +}); + +vi.mock(githubTrackingPath, async (importOriginal) => { + const original = await importOriginal(); + return { + ...original, + maybeCreateTrackingIssue: maybeCreateTrackingIssueMock, + }; +}); function makeTmpDir(prefix: string): string { return mkdtempSync(join(tmpdir(), prefix)); @@ -24,7 +42,7 @@ describe("agent tool github tracking end-to-end", () => { beforeEach(async () => { setTaskCreatedHook(undefined); - vi.restoreAllMocks(); + vi.clearAllMocks(); rootDir = makeTmpDir("kb-engine-agent-tools-gh-track-e2e-"); globalDir = makeTmpDir("kb-engine-agent-tools-gh-track-e2e-global-"); store = new TaskStore(rootDir, globalDir, { inMemoryDb: true }); @@ -66,14 +84,11 @@ describe("agent tool github tracking end-to-end", () => { ), }, ])("invokes maybeCreateTrackingIssue for $name", async ({ run }) => { - const githubTrackingModule = await githubTrackingModulePromise; - const githubTrackingHookModule = await githubTrackingHookModulePromise; - const maybeCreateSpy = vi.spyOn(githubTrackingModule, "maybeCreateTrackingIssue").mockResolvedValue({ - created: false, - reason: "tracking_disabled", - }); + const { registerGithubTrackingHook } = await import(githubTrackingHookPath); + const { maybeCreateTrackingIssue } = await import(githubTrackingPath); + const maybeCreateSpy = vi.mocked(maybeCreateTrackingIssue); - githubTrackingHookModule.registerGithubTrackingHook(); + registerGithubTrackingHook(); const result = await run(); const taskId = (result as { details?: { taskId?: string } }).details?.taskId as string;