mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-20 21:23:23 +00:00
test: speed up effective tools inventory test
This commit is contained in:
@@ -1,61 +1,132 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { createOpenClawCodingTools } from "./pi-tools.js";
|
||||
import type { AnyAgentTool } from "./tools/common.js";
|
||||
|
||||
function mockTool(params: {
|
||||
name: string;
|
||||
label: string;
|
||||
description: string;
|
||||
displaySummary?: string;
|
||||
}): AnyAgentTool {
|
||||
return {
|
||||
...params,
|
||||
parameters: { type: "object", properties: {} },
|
||||
execute: async () => ({ text: params.description }),
|
||||
} as unknown as AnyAgentTool;
|
||||
}
|
||||
|
||||
const effectiveInventoryState = vi.hoisted(() => ({
|
||||
tools: [
|
||||
mockTool({ name: "exec", label: "Exec", description: "Run shell commands" }),
|
||||
mockTool({ name: "docs_lookup", label: "Docs Lookup", description: "Search docs" }),
|
||||
] as AnyAgentTool[],
|
||||
pluginMeta: {} as Record<string, { pluginId: string } | undefined>,
|
||||
channelMeta: {} as Record<string, { channelId: string } | undefined>,
|
||||
effectivePolicy: {} as { profile?: string; providerProfile?: string },
|
||||
resolvedModelCompat: undefined as Record<string, unknown> | undefined,
|
||||
createToolsMock: vi.fn<typeof createOpenClawCodingTools>(
|
||||
(_options) =>
|
||||
[
|
||||
mockTool({ name: "exec", label: "Exec", description: "Run shell commands" }),
|
||||
mockTool({ name: "docs_lookup", label: "Docs Lookup", description: "Search docs" }),
|
||||
] as AnyAgentTool[],
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("./agent-scope.js", async () => {
|
||||
const actual = await vi.importActual<typeof import("./agent-scope.js")>("./agent-scope.js");
|
||||
return {
|
||||
...actual,
|
||||
resolveSessionAgentId: () => "main",
|
||||
resolveAgentWorkspaceDir: () => "/tmp/workspace-main",
|
||||
resolveAgentDir: () => "/tmp/agents/main/agent",
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("./pi-tools.js", () => ({
|
||||
createOpenClawCodingTools: (options?: Parameters<typeof createOpenClawCodingTools>[0]) =>
|
||||
effectiveInventoryState.createToolsMock(options),
|
||||
}));
|
||||
|
||||
vi.mock("./pi-embedded-runner/model.js", () => ({
|
||||
resolveModel: vi.fn(() => ({
|
||||
model: effectiveInventoryState.resolvedModelCompat
|
||||
? { compat: effectiveInventoryState.resolvedModelCompat }
|
||||
: undefined,
|
||||
authStorage: {} as never,
|
||||
modelRegistry: {} as never,
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("../plugins/tools.js", () => ({
|
||||
getPluginToolMeta: (tool: { name: string }) => effectiveInventoryState.pluginMeta[tool.name],
|
||||
}));
|
||||
|
||||
vi.mock("./channel-tools.js", () => ({
|
||||
getChannelAgentToolMeta: (tool: { name: string }) =>
|
||||
effectiveInventoryState.channelMeta[tool.name],
|
||||
}));
|
||||
|
||||
vi.mock("./pi-tools.policy.js", () => ({
|
||||
resolveEffectiveToolPolicy: () => effectiveInventoryState.effectivePolicy,
|
||||
}));
|
||||
|
||||
let resolveEffectiveToolInventory: typeof import("./tools-effective-inventory.js").resolveEffectiveToolInventory;
|
||||
|
||||
async function loadHarness(options?: {
|
||||
tools?: Array<{ name: string; label?: string; description?: string; displaySummary?: string }>;
|
||||
createToolsMock?: ReturnType<typeof vi.fn>;
|
||||
tools?: AnyAgentTool[];
|
||||
createToolsMock?: typeof effectiveInventoryState.createToolsMock;
|
||||
pluginMeta?: Record<string, { pluginId: string } | undefined>;
|
||||
channelMeta?: Record<string, { channelId: string } | undefined>;
|
||||
effectivePolicy?: { profile?: string; providerProfile?: string };
|
||||
resolvedModelCompat?: Record<string, unknown>;
|
||||
}) {
|
||||
vi.resetModules();
|
||||
vi.doMock("./agent-scope.js", async () => {
|
||||
const actual = await vi.importActual<typeof import("./agent-scope.js")>("./agent-scope.js");
|
||||
return {
|
||||
...actual,
|
||||
resolveSessionAgentId: () => "main",
|
||||
resolveAgentWorkspaceDir: () => "/tmp/workspace-main",
|
||||
resolveAgentDir: () => "/tmp/agents/main/agent",
|
||||
};
|
||||
});
|
||||
const createToolsMock =
|
||||
effectiveInventoryState.tools = options?.tools ?? [
|
||||
mockTool({ name: "exec", label: "Exec", description: "Run shell commands" }),
|
||||
mockTool({ name: "docs_lookup", label: "Docs Lookup", description: "Search docs" }),
|
||||
];
|
||||
effectiveInventoryState.pluginMeta = options?.pluginMeta ?? {};
|
||||
effectiveInventoryState.channelMeta = options?.channelMeta ?? {};
|
||||
effectiveInventoryState.effectivePolicy = options?.effectivePolicy ?? {};
|
||||
effectiveInventoryState.resolvedModelCompat = options?.resolvedModelCompat;
|
||||
effectiveInventoryState.createToolsMock =
|
||||
options?.createToolsMock ??
|
||||
vi.fn(
|
||||
() =>
|
||||
options?.tools ?? [
|
||||
{ name: "exec", label: "Exec", description: "Run shell commands" },
|
||||
{ name: "docs_lookup", label: "Docs Lookup", description: "Search docs" },
|
||||
],
|
||||
);
|
||||
vi.doMock("./pi-tools.js", () => ({
|
||||
createOpenClawCodingTools: createToolsMock,
|
||||
}));
|
||||
vi.doMock("./pi-embedded-runner/model.js", () => ({
|
||||
resolveModel: vi.fn(() => ({
|
||||
model: options?.resolvedModelCompat ? { compat: options.resolvedModelCompat } : undefined,
|
||||
authStorage: {} as never,
|
||||
modelRegistry: {} as never,
|
||||
})),
|
||||
}));
|
||||
vi.doMock("../plugins/tools.js", () => ({
|
||||
getPluginToolMeta: (tool: { name: string }) => options?.pluginMeta?.[tool.name],
|
||||
}));
|
||||
vi.doMock("./channel-tools.js", () => ({
|
||||
getChannelAgentToolMeta: (tool: { name: string }) => options?.channelMeta?.[tool.name],
|
||||
}));
|
||||
vi.doMock("./pi-tools.policy.js", () => ({
|
||||
resolveEffectiveToolPolicy: () => options?.effectivePolicy ?? {},
|
||||
}));
|
||||
return await import("./tools-effective-inventory.js");
|
||||
vi.fn<typeof createOpenClawCodingTools>((_options) => effectiveInventoryState.tools);
|
||||
return {
|
||||
resolveEffectiveToolInventory,
|
||||
createToolsMock: effectiveInventoryState.createToolsMock,
|
||||
};
|
||||
}
|
||||
|
||||
describe("resolveEffectiveToolInventory", () => {
|
||||
beforeAll(async () => {
|
||||
({ resolveEffectiveToolInventory } = await import("./tools-effective-inventory.js"));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
effectiveInventoryState.tools = [
|
||||
mockTool({ name: "exec", label: "Exec", description: "Run shell commands" }),
|
||||
mockTool({ name: "docs_lookup", label: "Docs Lookup", description: "Search docs" }),
|
||||
];
|
||||
effectiveInventoryState.pluginMeta = {};
|
||||
effectiveInventoryState.channelMeta = {};
|
||||
effectiveInventoryState.effectivePolicy = {};
|
||||
effectiveInventoryState.resolvedModelCompat = undefined;
|
||||
effectiveInventoryState.createToolsMock = vi.fn<typeof createOpenClawCodingTools>(
|
||||
(_options) => effectiveInventoryState.tools,
|
||||
);
|
||||
});
|
||||
|
||||
it("groups core, plugin, and channel tools from the effective runtime set", async () => {
|
||||
const { resolveEffectiveToolInventory } = await loadHarness({
|
||||
tools: [
|
||||
{ name: "exec", label: "Exec", description: "Run shell commands" },
|
||||
{ name: "docs_lookup", label: "Docs Lookup", description: "Search docs" },
|
||||
{ name: "message_actions", label: "Message Actions", description: "Act on messages" },
|
||||
mockTool({ name: "exec", label: "Exec", description: "Run shell commands" }),
|
||||
mockTool({ name: "docs_lookup", label: "Docs Lookup", description: "Search docs" }),
|
||||
mockTool({
|
||||
name: "message_actions",
|
||||
label: "Message Actions",
|
||||
description: "Act on messages",
|
||||
}),
|
||||
],
|
||||
pluginMeta: { docs_lookup: { pluginId: "docs" } },
|
||||
channelMeta: { message_actions: { channelId: "telegram" } },
|
||||
@@ -118,8 +189,8 @@ describe("resolveEffectiveToolInventory", () => {
|
||||
it("disambiguates duplicate labels with source ids", async () => {
|
||||
const { resolveEffectiveToolInventory } = await loadHarness({
|
||||
tools: [
|
||||
{ name: "docs_lookup", label: "Lookup", description: "Search docs" },
|
||||
{ name: "jira_lookup", label: "Lookup", description: "Search Jira" },
|
||||
mockTool({ name: "docs_lookup", label: "Lookup", description: "Search docs" }),
|
||||
mockTool({ name: "jira_lookup", label: "Lookup", description: "Search Jira" }),
|
||||
],
|
||||
pluginMeta: {
|
||||
docs_lookup: { pluginId: "docs" },
|
||||
@@ -136,12 +207,12 @@ describe("resolveEffectiveToolInventory", () => {
|
||||
it("prefers displaySummary over raw description", async () => {
|
||||
const { resolveEffectiveToolInventory } = await loadHarness({
|
||||
tools: [
|
||||
{
|
||||
mockTool({
|
||||
name: "cron",
|
||||
label: "Cron",
|
||||
displaySummary: "Schedule and manage cron jobs.",
|
||||
description: "Long raw description\n\nACTIONS:\n- status",
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
@@ -159,12 +230,12 @@ describe("resolveEffectiveToolInventory", () => {
|
||||
it("falls back to a sanitized summary for multi-line raw descriptions", async () => {
|
||||
const { resolveEffectiveToolInventory } = await loadHarness({
|
||||
tools: [
|
||||
{
|
||||
mockTool({
|
||||
name: "cron",
|
||||
label: "Cron",
|
||||
description:
|
||||
'Manage Gateway cron jobs (status/list/add/update/remove/run/runs) and send wake events. Use this for reminders, "check back later" requests, delayed follow-ups, and recurring tasks. Do not emulate scheduling with exec sleep or process polling.\n\nACTIONS:\n- status: Check cron scheduler status\nJOB SCHEMA:\n{ ... }',
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
@@ -182,7 +253,7 @@ describe("resolveEffectiveToolInventory", () => {
|
||||
|
||||
it("includes the resolved tool profile", async () => {
|
||||
const { resolveEffectiveToolInventory } = await loadHarness({
|
||||
tools: [{ name: "exec", label: "Exec", description: "Run shell commands" }],
|
||||
tools: [mockTool({ name: "exec", label: "Exec", description: "Run shell commands" })],
|
||||
effectivePolicy: { profile: "minimal", providerProfile: "coding" },
|
||||
});
|
||||
|
||||
@@ -192,8 +263,8 @@ describe("resolveEffectiveToolInventory", () => {
|
||||
});
|
||||
|
||||
it("passes resolved model compat into effective tool creation", async () => {
|
||||
const createToolsMock = vi.fn(() => [
|
||||
{ name: "exec", label: "Exec", description: "Run shell commands" },
|
||||
const createToolsMock = vi.fn<typeof createOpenClawCodingTools>(() => [
|
||||
mockTool({ name: "exec", label: "Exec", description: "Run shell commands" }),
|
||||
]);
|
||||
const { resolveEffectiveToolInventory } = await loadHarness({
|
||||
createToolsMock,
|
||||
|
||||
Reference in New Issue
Block a user