Skip to content

Commit dc9e67d

Browse files
committed
refactor: share tools catalog test helpers
1 parent b2a1c5c commit dc9e67d

1 file changed

Lines changed: 43 additions & 47 deletions

File tree

‎src/gateway/server-methods/tools-catalog.test.ts‎

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import {
66
} from "../../plugins/tools.js";
77
import { toolsCatalogHandlers } from "./tools-catalog.js";
88

9-
vi.mock("../../config/config.js", () => ({
10-
getRuntimeConfig: vi.fn(() => ({})),
11-
}));
12-
139
vi.mock("../../agents/agent-scope.js", () => ({
1410
listAgentIds: vi.fn(() => ["main"]),
1511
resolveDefaultAgentId: vi.fn(() => "main"),
1612
resolveAgentWorkspaceDir: vi.fn(() => "/tmp/workspace-main"),
1713
resolveAgentDir: vi.fn(() => "/tmp/agents/main/agent"),
1814
}));
1915

16+
vi.mock("../../config/config.js", () => ({
17+
getRuntimeConfig: vi.fn(() => ({})),
18+
}));
19+
2020
const pluginToolMetaState = new Map<string, { pluginId: string; optional: boolean }>();
2121

2222
vi.mock("../../plugins/tools.js", () => ({
@@ -36,6 +36,27 @@ vi.mock("../../plugins/tools.js", () => ({
3636
}));
3737

3838
type RespondCall = [boolean, unknown?, { code: number; message: string }?];
39+
type CatalogTool = {
40+
id: string;
41+
source: "core" | "plugin";
42+
label?: string;
43+
description?: string;
44+
pluginId?: string;
45+
optional?: boolean;
46+
risk?: unknown;
47+
tags?: unknown;
48+
defaultProfiles?: unknown[];
49+
};
50+
type CatalogGroup = {
51+
id?: string;
52+
source: "core" | "plugin";
53+
pluginId?: string;
54+
tools: CatalogTool[];
55+
};
56+
type CatalogPayload = {
57+
agentId?: string;
58+
groups: CatalogGroup[];
59+
};
3960

4061
function createInvokeParams(params: Record<string, unknown>) {
4162
const respond = vi.fn();
@@ -69,6 +90,19 @@ function respondCall(respond: ReturnType<typeof vi.fn>): RespondCall {
6990
return call;
7091
}
7192

93+
function expectInvalidRequest(respond: ReturnType<typeof vi.fn>, message: string) {
94+
const call = respondCall(respond);
95+
expect(call[0]).toBe(false);
96+
expect(call[2]?.code).toBe(ErrorCodes.INVALID_REQUEST);
97+
expect(call[2]?.message).toContain(message);
98+
}
99+
100+
function expectCatalogPayload(respond: ReturnType<typeof vi.fn>): CatalogPayload {
101+
const call = respondCall(respond);
102+
expect(call[0]).toBe(true);
103+
return call[1] as CatalogPayload;
104+
}
105+
72106
describe("tools.catalog handler", () => {
73107
beforeEach(() => {
74108
pluginToolMetaState.clear();
@@ -79,34 +113,19 @@ describe("tools.catalog handler", () => {
79113
it("rejects invalid params", async () => {
80114
const { respond, invoke } = createInvokeParams({ extra: true });
81115
await invoke();
82-
const call = respondCall(respond);
83-
expect(call[0]).toBe(false);
84-
expect(call[2]?.code).toBe(ErrorCodes.INVALID_REQUEST);
85-
expect(call[2]?.message).toContain("invalid tools.catalog params");
116+
expectInvalidRequest(respond, "invalid tools.catalog params");
86117
});
87118

88119
it("rejects unknown agent ids", async () => {
89120
const { respond, invoke } = createInvokeParams({ agentId: "unknown-agent" });
90121
await invoke();
91-
const call = respondCall(respond);
92-
expect(call[0]).toBe(false);
93-
expect(call[2]?.code).toBe(ErrorCodes.INVALID_REQUEST);
94-
expect(call[2]?.message).toContain("unknown agent id");
122+
expectInvalidRequest(respond, "unknown agent id");
95123
});
96124

97125
it("returns core groups including tts and excludes plugins when includePlugins=false", async () => {
98126
const { respond, invoke } = createInvokeParams({ includePlugins: false });
99127
await invoke();
100-
const call = respondCall(respond);
101-
expect(call[0]).toBe(true);
102-
const payload = call[1] as {
103-
agentId: string;
104-
groups: Array<{
105-
id: string;
106-
source: "core" | "plugin";
107-
tools: Array<{ id: string; source: "core" | "plugin" }>;
108-
}>;
109-
};
128+
const payload = expectCatalogPayload(respond);
110129
expect(payload.agentId).toBe("main");
111130
const groups = payload.groups ?? [];
112131
expect(groups.some((group) => group.source === "plugin")).toBe(false);
@@ -117,20 +136,7 @@ describe("tools.catalog handler", () => {
117136
it("includes plugin groups with plugin metadata", async () => {
118137
const { respond, invoke } = createInvokeParams({});
119138
await invoke();
120-
const call = respondCall(respond);
121-
expect(call[0]).toBe(true);
122-
const payload = call[1] as {
123-
groups: Array<{
124-
source: "core" | "plugin";
125-
pluginId?: string;
126-
tools: Array<{
127-
id: string;
128-
source: "core" | "plugin";
129-
pluginId?: string;
130-
optional?: boolean;
131-
}>;
132-
}>;
133-
};
139+
const payload = expectCatalogPayload(respond);
134140
const pluginGroups = payload.groups.filter((group) => group.source === "plugin");
135141
expect(pluginGroups.length).toBeGreaterThan(0);
136142
const voiceCall = pluginGroups
@@ -152,17 +158,7 @@ describe("tools.catalog handler", () => {
152158
it("summarizes plugin tool descriptions the same way as the effective inventory", async () => {
153159
const { respond, invoke } = createInvokeParams({});
154160
await invoke();
155-
const call = respondCall(respond);
156-
expect(call[0]).toBe(true);
157-
const payload = call[1] as {
158-
groups: Array<{
159-
source: "core" | "plugin";
160-
tools: Array<{
161-
id: string;
162-
description: string;
163-
}>;
164-
}>;
165-
};
161+
const payload = expectCatalogPayload(respond);
166162
const matrixRoom = payload.groups
167163
.filter((group) => group.source === "plugin")
168164
.flatMap((group) => group.tools)

0 commit comments

Comments
 (0)