test: speed up auto-reply registry tests

This commit is contained in:
Peter Steinberger
2026-04-07 11:35:14 +01:00
parent be3b7cf875
commit ce18c3e9e7
2 changed files with 28 additions and 47 deletions

View File

@@ -1,43 +1,26 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js";
import {
buildCommandText,
buildCommandTextFromArgs,
findCommandByNativeName,
getCommandDetection,
listChatCommands,
listChatCommandsForConfig,
listNativeCommandSpecs,
listNativeCommandSpecsForConfig,
normalizeCommandBody,
parseCommandArgs,
resolveCommandArgChoices,
resolveCommandArgMenu,
serializeCommandArgs,
shouldHandleTextCommands,
} from "./commands-registry.js";
import type { ChatCommandDefinition } from "./commands-registry.types.js";
let setActivePluginRegistry: typeof import("../plugins/runtime.js").setActivePluginRegistry;
let buildCommandText: typeof import("./commands-registry.js").buildCommandText;
let buildCommandTextFromArgs: typeof import("./commands-registry.js").buildCommandTextFromArgs;
let findCommandByNativeName: typeof import("./commands-registry.js").findCommandByNativeName;
let getCommandDetection: typeof import("./commands-registry.js").getCommandDetection;
let listChatCommands: typeof import("./commands-registry.js").listChatCommands;
let listChatCommandsForConfig: typeof import("./commands-registry.js").listChatCommandsForConfig;
let listNativeCommandSpecs: typeof import("./commands-registry.js").listNativeCommandSpecs;
let listNativeCommandSpecsForConfig: typeof import("./commands-registry.js").listNativeCommandSpecsForConfig;
let normalizeCommandBody: typeof import("./commands-registry.js").normalizeCommandBody;
let parseCommandArgs: typeof import("./commands-registry.js").parseCommandArgs;
let resolveCommandArgChoices: typeof import("./commands-registry.js").resolveCommandArgChoices;
let resolveCommandArgMenu: typeof import("./commands-registry.js").resolveCommandArgMenu;
let serializeCommandArgs: typeof import("./commands-registry.js").serializeCommandArgs;
let shouldHandleTextCommands: typeof import("./commands-registry.js").shouldHandleTextCommands;
beforeEach(async () => {
vi.resetModules();
beforeEach(() => {
vi.doUnmock("../channels/plugins/index.js");
({ setActivePluginRegistry } = await import("../plugins/runtime.js"));
({
buildCommandText,
buildCommandTextFromArgs,
findCommandByNativeName,
getCommandDetection,
listChatCommands,
listChatCommandsForConfig,
listNativeCommandSpecs,
listNativeCommandSpecsForConfig,
normalizeCommandBody,
parseCommandArgs,
resolveCommandArgChoices,
resolveCommandArgMenu,
serializeCommandArgs,
shouldHandleTextCommands,
} = await import("./commands-registry.js"));
setActivePluginRegistry(createTestRegistry([]));
});

View File

@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import type { SessionEntry } from "../../config/sessions.js";
import type { HookRunner } from "../../plugins/hooks.js";
import { initSessionState } from "./session.js";
const hookRunnerMocks = vi.hoisted(() => ({
hasHooks: vi.fn<HookRunner["hasHooks"]>(),
@@ -12,7 +13,14 @@ const hookRunnerMocks = vi.hoisted(() => ({
runSessionEnd: vi.fn<HookRunner["runSessionEnd"]>(),
}));
let initSessionState: typeof import("./session.js").initSessionState;
vi.mock("../../plugins/hook-runner-global.js", () => ({
getGlobalHookRunner: () =>
({
hasHooks: hookRunnerMocks.hasHooks,
runSessionStart: hookRunnerMocks.runSessionStart,
runSessionEnd: hookRunnerMocks.runSessionEnd,
}) as unknown as HookRunner,
}));
async function createStorePath(prefix: string): Promise<string> {
const root = await fs.mkdtemp(path.join(os.tmpdir(), `${prefix}-`));
@@ -46,16 +54,7 @@ async function writeTranscript(
}
describe("session hook context wiring", () => {
beforeEach(async () => {
vi.resetModules();
vi.doMock("../../plugins/hook-runner-global.js", () => ({
getGlobalHookRunner: () =>
({
hasHooks: hookRunnerMocks.hasHooks,
runSessionStart: hookRunnerMocks.runSessionStart,
runSessionEnd: hookRunnerMocks.runSessionEnd,
}) as unknown as HookRunner,
}));
beforeEach(() => {
hookRunnerMocks.hasHooks.mockReset();
hookRunnerMocks.runSessionStart.mockReset();
hookRunnerMocks.runSessionEnd.mockReset();
@@ -64,7 +63,6 @@ describe("session hook context wiring", () => {
hookRunnerMocks.hasHooks.mockImplementation(
(hookName) => hookName === "session_start" || hookName === "session_end",
);
({ initSessionState } = await import("./session.js"));
});
afterEach(() => {