-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcortex_mcp.test.js
More file actions
177 lines (167 loc) · 5.69 KB
/
Copy pathcortex_mcp.test.js
File metadata and controls
177 lines (167 loc) · 5.69 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
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import { processSession } from "../src/cortex_hook.js";
import { handle } from "../src/cortex_mcp.js";
// Default is now ledger-only; these cases exercise the legacy FILE store (the
// FORGE_LEDGER_ONLY=0 escape hatch). Pin it here so they test that path directly.
process.env.FORGE_LEDGER_ONLY = "0";
test("handle: initialize advertises the forge-cortex server", async () => {
const r = await handle({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {},
});
assert.equal(r.result.serverInfo.name, "forge-cortex");
});
test("handle: tools/list exposes the cortex + preflight tools", async () => {
const r = await handle({ jsonrpc: "2.0", id: 2, method: "tools/list" });
const names = r.result.tools.map((t) => t.name);
for (const t of [
"cortex_lessons",
"cortex_status",
"preflight_check",
"route_task",
"scope_files",
"forge_cost",
"forge_dash_data",
"forge_dash_summary",
"forge_brain",
"forge_ledger_query",
"forge_diagnose",
"forge_doctor",
"forge_remember",
"forge_ledger_ratify",
"forge_ledger_retract",
"rank_code",
"collide_check",
]) {
assert.ok(names.includes(t), `exposes ${t}`);
}
});
test("rank_code without an atlas answers built:false over stdio, never a throw", () => {
const root = mkdtempSync(join(tmpdir(), "forge-mcp-rank-"));
const requests = [
JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: {} }),
JSON.stringify({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: { name: "rank_code", arguments: {} },
}),
].join("\n");
const r = spawnSync("node", [SERVER], {
input: `${requests}\n`,
encoding: "utf8",
env: { ...process.env, FORGE_ROOT: root },
timeout: 10000,
});
const responses = r.stdout
.trim()
.split("\n")
.map((l) => JSON.parse(l));
const call = responses.find((x) => x.id === 2);
assert.deepEqual(JSON.parse(call.result.content[0].text), { built: false });
});
test("handle: notifications get no response; unknown methods error", async () => {
assert.equal(await handle({ method: "notifications/initialized" }), null);
assert.equal((await handle({ id: 9, method: "bogus" })).error.code, -32601);
});
const SERVER = fileURLToPath(new URL("../src/cortex_mcp.js", import.meta.url));
test("live server over stdio returns learned lessons for a repo", () => {
const root = mkdtempSync(join(tmpdir(), "forge-mcp-"));
const session = () => [
{ type: "bash", command: "npm test", exitCode: 1 },
{ type: "edit", file: "src/tax.ts" },
{ type: "edit", file: "src/tax.ts" },
{ type: "edit", file: "src/tax.ts" },
{ type: "bash", command: "npm test", exitCode: 0 },
];
processSession(root, session(), 1);
processSession(root, session(), 2); // → active lesson
const requests = [
JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: {} }),
JSON.stringify({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: { name: "cortex_lessons", arguments: { files: ["src/tax.ts"] } },
}),
].join("\n");
const r = spawnSync("node", [SERVER], {
input: `${requests}\n`,
encoding: "utf8",
env: { ...process.env, FORGE_ROOT: root },
timeout: 10000,
});
const responses = r.stdout
.trim()
.split("\n")
.map((l) => JSON.parse(l));
const call = responses.find((x) => x.id === 2);
assert.match(call.result.content[0].text, /Lessons for the files in play/);
assert.match(call.result.content[0].text, /tax\.ts/);
});
test("forge_remember writes a fact to .forge/brain/ via stdio", () => {
const root = mkdtempSync(join(tmpdir(), "forge-mcp-rem-"));
mkdirSync(join(root, ".forge", "brain"), { recursive: true });
const requests = [
JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: {} }),
JSON.stringify({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: "forge_remember",
arguments: { name: "test-fact", body: "testing MCP write" },
},
}),
].join("\n");
const r = spawnSync("node", [SERVER], {
input: `${requests}\n`,
encoding: "utf8",
env: { ...process.env, FORGE_ROOT: root },
timeout: 10000,
});
const responses = r.stdout
.trim()
.split("\n")
.map((l) => JSON.parse(l));
const call = responses.find((x) => x.id === 2);
assert.match(call.result.content[0].text, /Remembered/);
const written = readFileSync(join(root, ".forge", "brain", "facts", "test-fact.md"), "utf8");
assert.match(written, /testing MCP write/);
});
test("forge_ledger_retract returns error for missing claim via stdio", () => {
const root = mkdtempSync(join(tmpdir(), "forge-mcp-ret-"));
mkdirSync(join(root, ".forge", "ledger"), { recursive: true });
const requests = [
JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: {} }),
JSON.stringify({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: "forge_ledger_retract",
arguments: { id: "nonexistent", reason: "test" },
},
}),
].join("\n");
const r = spawnSync("node", [SERVER], {
input: `${requests}\n`,
encoding: "utf8",
env: { ...process.env, FORGE_ROOT: root },
timeout: 10000,
});
const responses = r.stdout
.trim()
.split("\n")
.map((l) => JSON.parse(l));
const call = responses.find((x) => x.id === 2);
assert.match(call.result.content[0].text, /No claim matching/);
});