-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.test.ts
More file actions
186 lines (166 loc) · 6.73 KB
/
tools.test.ts
File metadata and controls
186 lines (166 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Tests for RETHUNK_GIT_TOOLS tool allowlist filtering in registerRethunkGitTools / selectToolRegistrars.
*/
import { describe, expect, test } from "bun:test";
import type { FastMCP } from "fastmcp";
import { captureToolDefinitions } from "./test-harness.js";
import { registerRethunkGitTools, selectToolRegistrars } from "./tools.js";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** All 23 canonical tool names, in registration order, verified from source. */
const ALL_TOOL_NAMES: string[] = [
// Read-only
"git_status",
"git_inventory",
"git_parity",
"list_presets",
"git_log",
"git_diff_summary",
"git_diff",
"git_show",
"git_worktree_list",
"git_stash_list",
"git_fetch",
"git_blame",
"git_branch_list",
"git_reflog",
// Mutating
"batch_commit",
"git_push",
"git_merge",
"git_cherry_pick",
"git_reset_soft",
"git_tag",
"git_worktree_add",
"git_worktree_remove",
"git_stash_apply",
] as const;
/** Minimal registrar stub used for pure unit tests of selectToolRegistrars. */
const STUB_REGISTRARS = ALL_TOOL_NAMES.map((name) => ({
name,
register: (_server: FastMCP) => undefined,
}));
// ---------------------------------------------------------------------------
// selectToolRegistrars — pure unit tests
// ---------------------------------------------------------------------------
describe("selectToolRegistrars", () => {
test("unset env → all tools returned", () => {
const { selected, unknown } = selectToolRegistrars(undefined, STUB_REGISTRARS);
expect(selected.map((r) => r.name)).toEqual(ALL_TOOL_NAMES);
expect(unknown).toEqual([]);
});
test("empty string env → all tools returned", () => {
const { selected, unknown } = selectToolRegistrars("", STUB_REGISTRARS);
expect(selected.map((r) => r.name)).toEqual(ALL_TOOL_NAMES);
expect(unknown).toEqual([]);
});
test("whitespace-only env → all tools returned", () => {
const { selected, unknown } = selectToolRegistrars(" , , ", STUB_REGISTRARS);
expect(selected.map((r) => r.name)).toEqual(ALL_TOOL_NAMES);
expect(unknown).toEqual([]);
});
test("subset env → exactly those tools in canonical order", () => {
const { selected, unknown } = selectToolRegistrars(
"git_push,git_status,batch_commit",
STUB_REGISTRARS,
);
// canonical order: git_status (0) < batch_commit (14) < git_push (15)
expect(selected.map((r) => r.name)).toEqual(["git_status", "batch_commit", "git_push"]);
expect(unknown).toEqual([]);
});
test("subset env with extra whitespace → trimmed and matched", () => {
const { selected, unknown } = selectToolRegistrars(" git_log , git_diff ", STUB_REGISTRARS);
expect(selected.map((r) => r.name)).toEqual(["git_log", "git_diff"]);
expect(unknown).toEqual([]);
});
test("unknown name → reported in unknown, valid ones still registered", () => {
const { selected, unknown } = selectToolRegistrars(
"git_status,typo_tool,git_log",
STUB_REGISTRARS,
);
expect(selected.map((r) => r.name)).toEqual(["git_status", "git_log"]);
expect(unknown).toEqual(["typo_tool"]);
});
test("all unknown names → empty selected, all reported as unknown", () => {
const { selected, unknown } = selectToolRegistrars("not_a_tool,also_bad", STUB_REGISTRARS);
expect(selected).toEqual([]);
expect(unknown).toEqual(["not_a_tool", "also_bad"]);
});
test("duplicate token → deduplicated; registered exactly once", () => {
const { selected, unknown } = selectToolRegistrars(
"git_status,git_status,git_log",
STUB_REGISTRARS,
);
expect(selected.map((r) => r.name)).toEqual(["git_status", "git_log"]);
expect(unknown).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// registerRethunkGitTools — integration: actual addTool stubs
// ---------------------------------------------------------------------------
describe("registerRethunkGitTools", () => {
test("unset RETHUNK_GIT_TOOLS → all 23 tools registered", () => {
const savedEnv = process.env.RETHUNK_GIT_TOOLS;
delete process.env.RETHUNK_GIT_TOOLS;
try {
const tools = captureToolDefinitions(registerRethunkGitTools);
expect(tools.map((t) => t.name).sort()).toEqual([...ALL_TOOL_NAMES].sort());
} finally {
if (savedEnv !== undefined) process.env.RETHUNK_GIT_TOOLS = savedEnv;
}
});
test("subset RETHUNK_GIT_TOOLS → only listed tools registered", () => {
const savedEnv = process.env.RETHUNK_GIT_TOOLS;
process.env.RETHUNK_GIT_TOOLS = "git_status,git_diff_summary,batch_commit";
try {
const tools = captureToolDefinitions(registerRethunkGitTools);
expect(tools.map((t) => t.name)).toEqual(["git_status", "git_diff_summary", "batch_commit"]);
} finally {
if (savedEnv !== undefined) process.env.RETHUNK_GIT_TOOLS = savedEnv;
else delete process.env.RETHUNK_GIT_TOOLS;
}
});
test("all-unknown RETHUNK_GIT_TOOLS → zero tools registered", () => {
const savedEnv = process.env.RETHUNK_GIT_TOOLS;
process.env.RETHUNK_GIT_TOOLS = "not_a_tool";
try {
const tools = captureToolDefinitions(registerRethunkGitTools);
expect(tools).toEqual([]);
} finally {
if (savedEnv !== undefined) process.env.RETHUNK_GIT_TOOLS = savedEnv;
else delete process.env.RETHUNK_GIT_TOOLS;
}
});
test("empty RETHUNK_GIT_TOOLS → all 23 tools registered", () => {
const savedEnv = process.env.RETHUNK_GIT_TOOLS;
process.env.RETHUNK_GIT_TOOLS = "";
try {
const tools = captureToolDefinitions(registerRethunkGitTools);
expect(tools.map((t) => t.name).sort()).toEqual([...ALL_TOOL_NAMES].sort());
} finally {
if (savedEnv !== undefined) process.env.RETHUNK_GIT_TOOLS = savedEnv;
else delete process.env.RETHUNK_GIT_TOOLS;
}
});
test("declared tool names match actual addTool names — drift guard", () => {
// Register all tools unconditionally, capture real addTool names.
const savedEnv = process.env.RETHUNK_GIT_TOOLS;
delete process.env.RETHUNK_GIT_TOOLS;
try {
const tools = captureToolDefinitions(registerRethunkGitTools);
const captured = new Set(tools.map((t) => t.name));
const declared = new Set(ALL_TOOL_NAMES);
// Every declared name must appear as a real addTool call.
for (const name of declared) {
expect(captured.has(name)).toBe(true);
}
// Every captured name must appear in our declared list.
for (const name of captured) {
expect(declared.has(name)).toBe(true);
}
} finally {
if (savedEnv !== undefined) process.env.RETHUNK_GIT_TOOLS = savedEnv;
}
});
});