test: speed up cli and command suites

This commit is contained in:
Peter Steinberger
2026-03-24 22:15:40 +00:00
parent 3dc139b0c0
commit d282667321
3 changed files with 17 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
const readFileSyncMock = vi.hoisted(() => vi.fn());
@@ -19,13 +19,16 @@ vi.mock("../channels/registry.js", () => ({
CHAT_CHANNEL_ORDER: ["telegram", "discord"],
}));
async function loadModule() {
return await import("./channel-options.js");
}
let resolveCliChannelOptions: typeof import("./channel-options.js").resolveCliChannelOptions;
let __testing: typeof import("./channel-options.js").__testing;
beforeAll(async () => {
({ resolveCliChannelOptions, __testing } = await import("./channel-options.js"));
});
describe("resolveCliChannelOptions", () => {
afterEach(() => {
vi.resetModules();
__testing.resetPrecomputedChannelOptionsForTests();
vi.clearAllMocks();
});
@@ -34,8 +37,7 @@ describe("resolveCliChannelOptions", () => {
JSON.stringify({ channelOptions: ["cached", "telegram", "cached"] }),
);
const mod = await loadModule();
expect(mod.resolveCliChannelOptions()).toEqual(["cached", "telegram"]);
expect(resolveCliChannelOptions()).toEqual(["cached", "telegram"]);
});
it("falls back to core channel order when metadata is missing", async () => {
@@ -43,16 +45,14 @@ describe("resolveCliChannelOptions", () => {
throw new Error("ENOENT");
});
const mod = await loadModule();
expect(mod.resolveCliChannelOptions()).toEqual(["telegram", "discord"]);
expect(resolveCliChannelOptions()).toEqual(["telegram", "discord"]);
});
it("ignores external catalog env during CLI bootstrap", async () => {
process.env.OPENCLAW_PLUGIN_CATALOG_PATHS = "/tmp/plugins-catalog.json";
readFileSyncMock.mockReturnValue(JSON.stringify({ channelOptions: ["cached", "telegram"] }));
const mod = await loadModule();
expect(mod.resolveCliChannelOptions()).toEqual(["cached", "telegram"]);
expect(resolveCliChannelOptions()).toEqual(["cached", "telegram"]);
delete process.env.OPENCLAW_PLUGIN_CATALOG_PATHS;
});
});

View File

@@ -51,3 +51,9 @@ export function resolveCliChannelOptions(): string[] {
export function formatCliChannelOptions(extra: string[] = []): string {
return [...extra, ...resolveCliChannelOptions()].join("|");
}
export const __testing = {
resetPrecomputedChannelOptionsForTests(): void {
precomputedChannelOptions = undefined;
},
};

View File

@@ -64,7 +64,6 @@ function mockSnapshot(token: unknown = "abc") {
describe("dashboardCommand", () => {
beforeAll(async () => {
vi.resetModules();
({ dashboardCommand } = await import("./dashboard.js"));
});