refactor: dedupe plugin test harnesses

This commit is contained in:
Peter Steinberger
2026-04-08 14:18:51 +01:00
parent 3dd19a1705
commit e98dc17866
6 changed files with 113 additions and 134 deletions

View File

@@ -0,0 +1,42 @@
import { vi } from "vitest";
import { resetTaskFlowRegistryForTests } from "../../tasks/task-flow-registry.js";
import {
resetTaskRegistryDeliveryRuntimeForTests,
resetTaskRegistryForTests,
setTaskRegistryDeliveryRuntimeForTests,
} from "../../tasks/task-registry.js";
const runtimeTaskMocks = vi.hoisted(() => ({
sendMessageMock: vi.fn(),
cancelSessionMock: vi.fn(),
killSubagentRunAdminMock: vi.fn(),
}));
vi.mock("../../acp/control-plane/manager.js", () => ({
getAcpSessionManager: () => ({
cancelSession: runtimeTaskMocks.cancelSessionMock,
}),
}));
vi.mock("../../agents/subagent-control.js", () => ({
killSubagentRunAdmin: (params: unknown) => runtimeTaskMocks.killSubagentRunAdminMock(params),
}));
export function getRuntimeTaskMocks() {
return runtimeTaskMocks;
}
export function installRuntimeTaskDeliveryMock(): void {
setTaskRegistryDeliveryRuntimeForTests({
sendMessage: runtimeTaskMocks.sendMessageMock,
});
}
export function resetRuntimeTaskTestState(
taskRegistryOptions?: Parameters<typeof resetTaskRegistryForTests>[0],
): void {
resetTaskRegistryDeliveryRuntimeForTests();
resetTaskRegistryForTests(taskRegistryOptions);
resetTaskFlowRegistryForTests({ persist: false });
vi.clearAllMocks();
}

View File

@@ -1,46 +1,19 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { getTaskFlowById, resetTaskFlowRegistryForTests } from "../../tasks/task-flow-registry.js";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { getTaskFlowById } from "../../tasks/task-flow-registry.js";
import { getTaskById } from "../../tasks/task-registry.js";
import {
getTaskById,
resetTaskRegistryDeliveryRuntimeForTests,
resetTaskRegistryForTests,
setTaskRegistryDeliveryRuntimeForTests,
} from "../../tasks/task-registry.js";
installRuntimeTaskDeliveryMock,
resetRuntimeTaskTestState,
} from "./runtime-task-test-harness.js";
import { createRuntimeTaskFlow } from "./runtime-taskflow.js";
const hoisted = vi.hoisted(() => {
const sendMessageMock = vi.fn();
const cancelSessionMock = vi.fn();
const killSubagentRunAdminMock = vi.fn();
return {
sendMessageMock,
cancelSessionMock,
killSubagentRunAdminMock,
};
});
vi.mock("../../acp/control-plane/manager.js", () => ({
getAcpSessionManager: () => ({
cancelSession: hoisted.cancelSessionMock,
}),
}));
vi.mock("../../agents/subagent-control.js", () => ({
killSubagentRunAdmin: (params: unknown) => hoisted.killSubagentRunAdminMock(params),
}));
afterEach(() => {
resetTaskRegistryDeliveryRuntimeForTests();
resetTaskRegistryForTests({ persist: false });
resetTaskFlowRegistryForTests({ persist: false });
vi.clearAllMocks();
resetRuntimeTaskTestState({ persist: false });
});
describe("runtime TaskFlow", () => {
beforeEach(() => {
setTaskRegistryDeliveryRuntimeForTests({
sendMessage: hoisted.sendMessageMock,
});
installRuntimeTaskDeliveryMock();
});
it("binds managed TaskFlow operations to a session key", () => {

View File

@@ -1,46 +1,21 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { resetTaskFlowRegistryForTests } from "../../tasks/task-flow-registry.js";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
resetTaskRegistryDeliveryRuntimeForTests,
resetTaskRegistryForTests,
setTaskRegistryDeliveryRuntimeForTests,
} from "../../tasks/task-registry.js";
getRuntimeTaskMocks,
installRuntimeTaskDeliveryMock,
resetRuntimeTaskTestState,
} from "./runtime-task-test-harness.js";
import { createRuntimeTaskFlow } from "./runtime-taskflow.js";
import { createRuntimeTaskFlows, createRuntimeTaskRuns } from "./runtime-tasks.js";
const hoisted = vi.hoisted(() => {
const sendMessageMock = vi.fn();
const cancelSessionMock = vi.fn();
const killSubagentRunAdminMock = vi.fn();
return {
sendMessageMock,
cancelSessionMock,
killSubagentRunAdminMock,
};
});
vi.mock("../../acp/control-plane/manager.js", () => ({
getAcpSessionManager: () => ({
cancelSession: hoisted.cancelSessionMock,
}),
}));
vi.mock("../../agents/subagent-control.js", () => ({
killSubagentRunAdmin: (params: unknown) => hoisted.killSubagentRunAdminMock(params),
}));
const runtimeTaskMocks = getRuntimeTaskMocks();
afterEach(() => {
resetTaskRegistryDeliveryRuntimeForTests();
resetTaskRegistryForTests();
resetTaskFlowRegistryForTests({ persist: false });
vi.clearAllMocks();
resetRuntimeTaskTestState();
});
describe("runtime tasks", () => {
beforeEach(() => {
setTaskRegistryDeliveryRuntimeForTests({
sendMessage: hoisted.sendMessageMock,
});
installRuntimeTaskDeliveryMock();
});
it("exposes canonical task and TaskFlow DTOs without leaking raw registry fields", () => {
@@ -185,7 +160,7 @@ describe("runtime tasks", () => {
cfg: {} as never,
});
expect(hoisted.cancelSessionMock).toHaveBeenCalledWith({
expect(runtimeTaskMocks.cancelSessionMock).toHaveBeenCalledWith({
cfg: {},
sessionKey: "agent:main:subagent:child",
reason: "task-cancel",
@@ -232,7 +207,7 @@ describe("runtime tasks", () => {
cfg: {} as never,
});
expect(hoisted.cancelSessionMock).not.toHaveBeenCalled();
expect(runtimeTaskMocks.cancelSessionMock).not.toHaveBeenCalled();
expect(result).toEqual({
found: false,
cancelled: false,