mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-28 05:16:23 -06:00
test: avoid repeated module reloads in unit tests
This commit is contained in:
@@ -12,7 +12,6 @@ type FormatChannelPrimerLine = typeof import("../channels/registry.js").formatCh
|
||||
type FormatChannelSelectionLine =
|
||||
typeof import("../channels/registry.js").formatChannelSelectionLine;
|
||||
type IsChannelConfigured = typeof import("../config/channel-configured.js").isChannelConfigured;
|
||||
type ChannelSetupStatusModule = typeof import("./channel-setup.status.js");
|
||||
type NoteChannelPrimerChannels = Parameters<
|
||||
typeof import("./channel-setup.status.js").noteChannelPrimer
|
||||
>[1];
|
||||
@@ -74,11 +73,13 @@ vi.mock("../plugins/bundled-sources.js", () => ({
|
||||
findBundledPluginSourceInMap: () => undefined,
|
||||
}));
|
||||
|
||||
let collectChannelStatus: ChannelSetupStatusModule["collectChannelStatus"];
|
||||
let noteChannelStatus: ChannelSetupStatusModule["noteChannelStatus"];
|
||||
let noteChannelPrimer: ChannelSetupStatusModule["noteChannelPrimer"];
|
||||
let resolveChannelSelectionNoteLines: ChannelSetupStatusModule["resolveChannelSelectionNoteLines"];
|
||||
let resolveChannelSetupSelectionContributions: ChannelSetupStatusModule["resolveChannelSetupSelectionContributions"];
|
||||
import {
|
||||
collectChannelStatus,
|
||||
noteChannelPrimer,
|
||||
noteChannelStatus,
|
||||
resolveChannelSelectionNoteLines,
|
||||
resolveChannelSetupSelectionContributions,
|
||||
} from "./channel-setup.status.js";
|
||||
|
||||
function requireFirstMockCall<const Calls extends readonly unknown[][]>(
|
||||
calls: Calls,
|
||||
@@ -92,8 +93,7 @@ function requireFirstMockCall<const Calls extends readonly unknown[][]>(
|
||||
}
|
||||
|
||||
describe("resolveChannelSetupSelectionContributions", () => {
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
listChatChannels.mockReturnValue([
|
||||
makeMeta("discord", "Discord"),
|
||||
@@ -105,13 +105,6 @@ describe("resolveChannelSetupSelectionContributions", () => {
|
||||
);
|
||||
formatChannelSelectionLine.mockImplementation((meta) => `${meta.label} — ${meta.blurb}`);
|
||||
isChannelConfigured.mockReturnValue(false);
|
||||
({
|
||||
collectChannelStatus,
|
||||
noteChannelStatus,
|
||||
noteChannelPrimer,
|
||||
resolveChannelSelectionNoteLines,
|
||||
resolveChannelSetupSelectionContributions,
|
||||
} = await import("./channel-setup.status.js"));
|
||||
});
|
||||
|
||||
it("sorts channels alphabetically by picker label", () => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/types.js";
|
||||
import type { ImageGenerationProviderPlugin } from "../plugins/types.js";
|
||||
import { getImageGenerationProvider, listImageGenerationProviders } from "./provider-registry.js";
|
||||
|
||||
const resolvePluginCapabilityProvidersMock = vi.hoisted(() =>
|
||||
vi.fn<() => ImageGenerationProviderPlugin[]>(() => []),
|
||||
@@ -25,33 +26,21 @@ function createProvider(
|
||||
};
|
||||
}
|
||||
|
||||
type ImageProviderRegistry = typeof import("./provider-registry.js");
|
||||
|
||||
function requireImageProvider(
|
||||
registry: ImageProviderRegistry,
|
||||
id: string,
|
||||
): ImageGenerationProviderPlugin {
|
||||
const provider = registry.getImageGenerationProvider(id);
|
||||
function requireImageProvider(id: string): ImageGenerationProviderPlugin {
|
||||
const provider = getImageGenerationProvider(id);
|
||||
if (!provider) {
|
||||
throw new Error(`expected image generation provider ${id}`);
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
async function loadProviderRegistry(): Promise<ImageProviderRegistry> {
|
||||
vi.resetModules();
|
||||
return await import("./provider-registry.js");
|
||||
}
|
||||
|
||||
describe("image-generation provider registry", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
resolvePluginCapabilityProvidersMock.mockReset();
|
||||
resolvePluginCapabilityProvidersMock.mockReturnValue([]);
|
||||
});
|
||||
|
||||
it("delegates provider resolution to the capability provider boundary", async () => {
|
||||
const { listImageGenerationProviders } = await loadProviderRegistry();
|
||||
it("delegates provider resolution to the capability provider boundary", () => {
|
||||
const cfg = {} as OpenClawConfig;
|
||||
|
||||
expect(listImageGenerationProviders(cfg)).toStrictEqual([]);
|
||||
@@ -61,9 +50,8 @@ describe("image-generation provider registry", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("uses active plugin providers without loading from disk", async () => {
|
||||
it("uses active plugin providers without loading from disk", () => {
|
||||
resolvePluginCapabilityProvidersMock.mockReturnValue([createProvider({ id: "custom-image" })]);
|
||||
const { getImageGenerationProvider } = await loadProviderRegistry();
|
||||
|
||||
const provider = getImageGenerationProvider("custom-image");
|
||||
|
||||
@@ -74,17 +62,15 @@ describe("image-generation provider registry", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("ignores prototype-like provider ids and aliases", async () => {
|
||||
it("ignores prototype-like provider ids and aliases", () => {
|
||||
resolvePluginCapabilityProvidersMock.mockReturnValue([
|
||||
createProvider({ id: "__proto__", aliases: ["constructor", "prototype"] }),
|
||||
createProvider({ id: "safe-image", aliases: ["safe-alias", "constructor"] }),
|
||||
]);
|
||||
const registry = await loadProviderRegistry();
|
||||
const { getImageGenerationProvider, listImageGenerationProviders } = registry;
|
||||
|
||||
expect(listImageGenerationProviders().map((provider) => provider.id)).toEqual(["safe-image"]);
|
||||
expect(getImageGenerationProvider("__proto__")).toBeUndefined();
|
||||
expect(getImageGenerationProvider("constructor")).toBeUndefined();
|
||||
expect(requireImageProvider(registry, "safe-alias").id).toBe("safe-image");
|
||||
expect(requireImageProvider("safe-alias").id).toBe("safe-image");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { VideoGenerationProviderPlugin } from "../plugins/types.js";
|
||||
import { getVideoGenerationProvider, listVideoGenerationProviders } from "./provider-registry.js";
|
||||
|
||||
const resolvePluginCapabilityProvidersMock = vi.hoisted(() =>
|
||||
vi.fn<() => VideoGenerationProviderPlugin[]>(() => []),
|
||||
@@ -21,34 +22,21 @@ function createProvider(
|
||||
};
|
||||
}
|
||||
|
||||
type VideoProviderRegistry = typeof import("./provider-registry.js");
|
||||
|
||||
function requireVideoProvider(
|
||||
registry: VideoProviderRegistry,
|
||||
id: string,
|
||||
): VideoGenerationProviderPlugin {
|
||||
const provider = registry.getVideoGenerationProvider(id);
|
||||
function requireVideoProvider(id: string): VideoGenerationProviderPlugin {
|
||||
const provider = getVideoGenerationProvider(id);
|
||||
if (!provider) {
|
||||
throw new Error(`expected video generation provider ${id}`);
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
async function loadProviderRegistry(): Promise<VideoProviderRegistry> {
|
||||
vi.resetModules();
|
||||
return await import("./provider-registry.js");
|
||||
}
|
||||
|
||||
describe("video-generation provider registry", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
resolvePluginCapabilityProvidersMock.mockReset();
|
||||
resolvePluginCapabilityProvidersMock.mockReturnValue([]);
|
||||
});
|
||||
|
||||
it("delegates provider resolution to the capability provider boundary", async () => {
|
||||
const { listVideoGenerationProviders } = await loadProviderRegistry();
|
||||
|
||||
it("delegates provider resolution to the capability provider boundary", () => {
|
||||
expect(listVideoGenerationProviders()).toStrictEqual([]);
|
||||
expect(resolvePluginCapabilityProvidersMock).toHaveBeenCalledWith({
|
||||
key: "videoGenerationProviders",
|
||||
@@ -56,9 +44,8 @@ describe("video-generation provider registry", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("uses active plugin providers without loading from disk", async () => {
|
||||
it("uses active plugin providers without loading from disk", () => {
|
||||
resolvePluginCapabilityProvidersMock.mockReturnValue([createProvider({ id: "custom-video" })]);
|
||||
const { getVideoGenerationProvider } = await loadProviderRegistry();
|
||||
|
||||
const provider = getVideoGenerationProvider("custom-video");
|
||||
|
||||
@@ -69,17 +56,15 @@ describe("video-generation provider registry", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("ignores prototype-like provider ids and aliases", async () => {
|
||||
it("ignores prototype-like provider ids and aliases", () => {
|
||||
resolvePluginCapabilityProvidersMock.mockReturnValue([
|
||||
createProvider({ id: "__proto__", aliases: ["constructor", "prototype"] }),
|
||||
createProvider({ id: "safe-video", aliases: ["safe-alias", "constructor"] }),
|
||||
]);
|
||||
const registry = await loadProviderRegistry();
|
||||
const { getVideoGenerationProvider, listVideoGenerationProviders } = registry;
|
||||
|
||||
expect(listVideoGenerationProviders().map((provider) => provider.id)).toEqual(["safe-video"]);
|
||||
expect(getVideoGenerationProvider("__proto__")).toBeUndefined();
|
||||
expect(getVideoGenerationProvider("constructor")).toBeUndefined();
|
||||
expect(requireVideoProvider(registry, "safe-alias").id).toBe("safe-video");
|
||||
expect(requireVideoProvider("safe-alias").id).toBe("safe-video");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user