refactor(auto-reply): share agent-runner test harness mocks

This commit is contained in:
Peter Steinberger
2026-02-15 14:24:06 +00:00
parent af34c8fafe
commit 33a3a56ee1
3 changed files with 53 additions and 46 deletions

View File

@@ -4,6 +4,11 @@ import type { TypingMode } from "../../config/types.js";
import type { TemplateContext } from "../templating.js";
import type { GetReplyOptions } from "../types.js";
import type { FollowupRun, QueueSettings } from "./queue.js";
import {
embeddedPiMockFactory,
modelFallbackMockFactory,
queueMockFactory,
} from "./agent-runner.test-harness.mocks.js";
import { createMockTypingController } from "./test-helpers.js";
// Avoid exporting vitest mock types (TS2742 under pnpm + d.ts emit).
@@ -40,34 +45,14 @@ export function installRunReplyAgentTypingHeartbeatTestHooks() {
}
vi.mock("../../agents/model-fallback.js", () => ({
runWithModelFallback: async ({
provider,
model,
run,
}: {
provider: string;
model: string;
run: (provider: string, model: string) => Promise<unknown>;
}) => ({
result: await run(provider, model),
provider,
model,
}),
...modelFallbackMockFactory(),
}));
vi.mock("../../agents/pi-embedded.js", () => ({
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
runEmbeddedPiAgent: (params: unknown) => state.runEmbeddedPiAgentMock(params),
...embeddedPiMockFactory(state),
}));
vi.mock("./queue.js", async () => {
const actual = await vi.importActual<typeof import("./queue.js")>("./queue.js");
return {
...actual,
enqueueFollowupRun: vi.fn(),
scheduleFollowupDrain: vi.fn(),
};
});
vi.mock("./queue.js", queueMockFactory);
export function createMinimalRun(params?: {
opts?: GetReplyOptions;

View File

@@ -3,6 +3,11 @@ import path from "node:path";
import { vi } from "vitest";
import type { TemplateContext } from "../templating.js";
import type { FollowupRun, QueueSettings } from "./queue.js";
import {
embeddedPiMockFactory,
modelFallbackMockFactory,
queueMockFactory,
} from "./agent-runner.test-harness.mocks.js";
import { createMockTypingController } from "./test-helpers.js";
// Avoid exporting vitest mock types (TS2742 under pnpm + d.ts emit).
@@ -31,19 +36,7 @@ export function getRunCliAgentMock(): AnyMock {
export type { EmbeddedRunParams };
vi.mock("../../agents/model-fallback.js", () => ({
runWithModelFallback: async ({
provider,
model,
run,
}: {
provider: string;
model: string;
run: (provider: string, model: string) => Promise<unknown>;
}) => ({
result: await run(provider, model),
provider,
model,
}),
...modelFallbackMockFactory(),
}));
vi.mock("../../agents/cli-runner.js", () => ({
@@ -51,18 +44,10 @@ vi.mock("../../agents/cli-runner.js", () => ({
}));
vi.mock("../../agents/pi-embedded.js", () => ({
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
runEmbeddedPiAgent: (params: unknown) => state.runEmbeddedPiAgentMock(params),
...embeddedPiMockFactory(state),
}));
vi.mock("./queue.js", async () => {
const actual = await vi.importActual<typeof import("./queue.js")>("./queue.js");
return {
...actual,
enqueueFollowupRun: vi.fn(),
scheduleFollowupDrain: vi.fn(),
};
});
vi.mock("./queue.js", queueMockFactory);
export async function seedSessionStore(params: {
storePath: string;

View File

@@ -0,0 +1,37 @@
import { vi } from "vitest";
export function modelFallbackMockFactory(): Record<string, unknown> {
return {
runWithModelFallback: async ({
provider,
model,
run,
}: {
provider: string;
model: string;
run: (provider: string, model: string) => Promise<unknown>;
}) => ({
result: await run(provider, model),
provider,
model,
}),
};
}
export function embeddedPiMockFactory(state: {
runEmbeddedPiAgentMock: (params: unknown) => unknown;
}): Record<string, unknown> {
return {
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
runEmbeddedPiAgent: (params: unknown) => state.runEmbeddedPiAgentMock(params),
};
}
export async function queueMockFactory(): Promise<Record<string, unknown>> {
const actual = await vi.importActual<typeof import("./queue.js")>("./queue.js");
return {
...actual,
enqueueFollowupRun: vi.fn(),
scheduleFollowupDrain: vi.fn(),
};
}