From d240fb8735d7b35c92338564eefbc39b498f5f38 Mon Sep 17 00:00:00 2001 From: gsxdsm Date: Tue, 9 Jun 2026 11:10:40 -0700 Subject: [PATCH] FN-6108: mock gh auth in github-tracking engine tests Stabilize engine github-tracking tests by fully mocking gh auth-dependent modules. - hoist github-tracking module paths and replace maybeCreateTrackingIssue with shared vi mocks - mock @fusion/core gh availability/auth helpers so dashboard tracking hooks do not touch real gh-cli state - switch per-test cleanup from restoreAllMocks to clearAllMocks and import mocked hook helpers directly Files changed: ...gent-task-creation-github-tracking-flag.test.ts | 44 ++++++++++++++-------- .../agent-tools-github-tracking-end-to-end.test.ts | 43 ++++++++++++++------- 2 files changed, 58 insertions(+), 29 deletions(-) Fusion-Task-Id: FN-6108 Fusion-Task-Lineage: c7e9aac2-9687-45d5-9b96-233421aa19eb --- ...task-creation-github-tracking-flag.test.ts | 44 ++++++++++++------- ...t-tools-github-tracking-end-to-end.test.ts | 43 ++++++++++++------ 2 files changed, 58 insertions(+), 29 deletions(-) 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;