forked from mksglu/context-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-plugin.test.ts
More file actions
315 lines (261 loc) · 12.2 KB
/
opencode-plugin.test.ts
File metadata and controls
315 lines (261 loc) · 12.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import "./setup-home";
/**
* Tests for the OpenCode TypeScript plugin entry point.
*
* Tests the ContextModePlugin factory and its three hooks:
* - tool.execute.before (routing enforcement)
* - tool.execute.after (session event capture)
* - experimental.session.compacting (snapshot generation)
*/
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { mkdtempSync, rmSync, existsSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
// ── Test helpers ──────────────────────────────────────────
/**
* Create a plugin instance with DB in a temp directory.
* Uses dynamic import to resolve routing module from project root.
*/
async function createTestPlugin(tempDir: string) {
// Import the plugin module
const { ContextModePlugin } = await import("../src/opencode-plugin.js");
// Monkey-patch the session dir to use temp directory
// The plugin uses homedir() internally, but we can control the DB path
// by creating the plugin with a unique directory that produces a unique hash
return ContextModePlugin({ directory: tempDir });
}
// ── Tests ─────────────────────────────────────────────────
describe("ContextModePlugin", () => {
let tempDir: string;
beforeAll(() => {
tempDir = mkdtempSync(join(tmpdir(), "opencode-plugin-test-"));
});
afterAll(() => {
try {
rmSync(tempDir, { recursive: true, force: true });
} catch { /* cleanup best effort */ }
});
// ── Factory ───────────────────────────────────────────
describe("factory", () => {
it("returns object with 3 hook handlers", async () => {
const plugin = await createTestPlugin(join(tempDir, "factory-test"));
expect(plugin).toHaveProperty("tool.execute.before");
expect(plugin).toHaveProperty("tool.execute.after");
expect(plugin).toHaveProperty("experimental.session.compacting");
expect(typeof plugin["tool.execute.before"]).toBe("function");
expect(typeof plugin["tool.execute.after"]).toBe("function");
expect(typeof plugin["experimental.session.compacting"]).toBe("function");
});
it("does not write AGENTS.md routing instructions on startup", async () => {
const projectDir = join(tempDir, "factory-startup-routing");
mkdirSync(projectDir, { recursive: true });
await createTestPlugin(projectDir);
const agentsPath = join(projectDir, "AGENTS.md");
expect(existsSync(agentsPath)).toBe(false);
});
});
// ── tool.execute.before ───────────────────────────────
describe("tool.execute.before", () => {
it("modifies curl commands to block them", async () => {
const plugin = await createTestPlugin(join(tempDir, "before-curl"));
const input = { tool: "Bash", sessionID: "test-session", callID: "call-1" };
const output = { args: { command: "curl https://example.com/data" } };
// Routing should throw for blocked commands (deny action)
// or modify the args to replace the command
try {
await plugin["tool.execute.before"](input, output);
// If it didn't throw, the command was modified in output.args
expect(output.args.command).toMatch(/^echo /);
expect(output.args.command).toContain("context-mode");
} catch (e: any) {
// deny/ask action throws — still correct behavior
expect(e.message).toContain("context-mode");
}
});
it("modifies wget commands to block them", async () => {
const plugin = await createTestPlugin(join(tempDir, "before-wget"));
const input = { tool: "Bash", sessionID: "test-session", callID: "call-2" };
const output = { args: { command: "wget https://example.com/file" } };
try {
await plugin["tool.execute.before"](input, output);
expect(output.args.command).toMatch(/^echo /);
expect(output.args.command).toContain("context-mode");
} catch (e: any) {
expect(e.message).toContain("context-mode");
}
});
it("passes through normal tool calls", async () => {
const plugin = await createTestPlugin(join(tempDir, "before-pass"));
// TaskCreate is not routed — should passthrough
const result = await plugin["tool.execute.before"](
{ tool: "TaskCreate", sessionID: "test-session", callID: "call-3" },
{ args: { subject: "test task" } },
);
expect(result).toBeUndefined();
});
it("handles empty input gracefully", async () => {
const plugin = await createTestPlugin(join(tempDir, "before-empty"));
const result = await plugin["tool.execute.before"](
{} as any,
{ args: {} } as any,
);
expect(result).toBeUndefined();
});
});
// ── tool.execute.after ────────────────────────────────
describe("tool.execute.after", () => {
it("captures file read events without throwing", async () => {
const plugin = await createTestPlugin(join(tempDir, "after-read"));
// Should not throw
await expect(
plugin["tool.execute.after"](
{ tool: "Read", sessionID: "test-session", callID: "call-1", args: { file_path: "/test/file.ts" } },
{ title: "Read", output: "file contents here", metadata: {} },
),
).resolves.toBeUndefined();
});
it("captures file write events", async () => {
const plugin = await createTestPlugin(join(tempDir, "after-write"));
await expect(
plugin["tool.execute.after"](
{ tool: "Write", sessionID: "test-session", callID: "call-2", args: { file_path: "/test/new-file.ts", content: "code" } },
{ title: "Write", output: "", metadata: {} },
),
).resolves.toBeUndefined();
});
it("captures git events from Bash", async () => {
const plugin = await createTestPlugin(join(tempDir, "after-git"));
await expect(
plugin["tool.execute.after"](
{ tool: "Bash", sessionID: "test-session", callID: "call-3", args: { command: "git commit -m 'test'" } },
{ title: "Bash", output: "[main abc1234] test", metadata: {} },
),
).resolves.toBeUndefined();
});
it("handles empty input gracefully", async () => {
const plugin = await createTestPlugin(join(tempDir, "after-empty"));
await expect(
plugin["tool.execute.after"](
{} as any,
{ title: "", output: "", metadata: {} } as any,
),
).resolves.toBeUndefined();
});
});
// ── experimental.session.compacting ───────────────────
describe("experimental.session.compacting", () => {
it("returns empty string when no events captured", async () => {
const plugin = await createTestPlugin(join(tempDir, "compact-empty"));
const output = { context: [] as string[], prompt: undefined };
const snapshot = await plugin["experimental.session.compacting"](
{ sessionID: "test-session" },
output,
);
expect(snapshot).toBe("");
});
it("returns snapshot XML after events are captured", async () => {
const plugin = await createTestPlugin(join(tempDir, "compact-snap"));
// Capture several events first
await plugin["tool.execute.after"](
{ tool: "Read", sessionID: "test-session", callID: "call-1", args: { file_path: "/src/index.ts" } },
{ title: "Read", output: "export default {}", metadata: {} },
);
await plugin["tool.execute.after"](
{ tool: "Edit", sessionID: "test-session", callID: "call-2", args: { file_path: "/src/index.ts", old_string: "{}", new_string: "{ foo: 1 }" } },
{ title: "Edit", output: "", metadata: {} },
);
await plugin["tool.execute.after"](
{ tool: "Bash", sessionID: "test-session", callID: "call-3", args: { command: "git status" } },
{ title: "Bash", output: "On branch main", metadata: {} },
);
const output = { context: [] as string[], prompt: undefined };
const snapshot = await plugin["experimental.session.compacting"](
{ sessionID: "test-session" },
output,
);
expect(snapshot.length).toBeGreaterThan(0);
expect(snapshot).toContain("session_resume");
expect(snapshot).toContain("<files");
expect(snapshot).toContain("index.ts");
});
it("can be called multiple times (increments compact count)", async () => {
const plugin = await createTestPlugin(join(tempDir, "compact-multi"));
await plugin["tool.execute.after"](
{ tool: "Read", sessionID: "test-session", callID: "call-1", args: { file_path: "/test/a.ts" } },
{ title: "Read", output: "code", metadata: {} },
);
const output1 = { context: [] as string[], prompt: undefined };
const snap1 = await plugin["experimental.session.compacting"](
{ sessionID: "test-session" },
output1,
);
expect(snap1.length).toBeGreaterThan(0);
// Capture more events
await plugin["tool.execute.after"](
{ tool: "Write", sessionID: "test-session", callID: "call-2", args: { file_path: "/test/b.ts", content: "new file" } },
{ title: "Write", output: "", metadata: {} },
);
const output2 = { context: [] as string[], prompt: undefined };
const snap2 = await plugin["experimental.session.compacting"](
{ sessionID: "test-session" },
output2,
);
expect(snap2.length).toBeGreaterThan(0);
});
});
// ── Integration: before + after + compact ─────────────
describe("end-to-end flow", () => {
it("captures events from allowed tools and generates snapshot", async () => {
const plugin = await createTestPlugin(join(tempDir, "e2e-flow"));
// Normal tool call passes through before hook
await plugin["tool.execute.before"](
{ tool: "Read", sessionID: "test-session", callID: "call-1" },
{ args: { file_path: "/app/main.ts" } },
);
// After hook captures the event
await plugin["tool.execute.after"](
{ tool: "Read", sessionID: "test-session", callID: "call-1", args: { file_path: "/app/main.ts" } },
{ title: "Read", output: "console.log('hello')", metadata: {} },
);
// Compacting generates snapshot
const output = { context: [] as string[], prompt: undefined };
const snapshot = await plugin["experimental.session.compacting"](
{ sessionID: "test-session" },
output,
);
expect(snapshot).toContain("session_resume");
expect(snapshot).toContain("<files");
expect(snapshot).toContain("main.ts");
});
it("blocked tool command is replaced before execution", async () => {
const plugin = await createTestPlugin(join(tempDir, "e2e-block"));
const beforeInput = { tool: "Bash", sessionID: "test-session", callID: "call-1" };
const beforeOutput = { args: { command: "curl https://evil.com" } };
// Before hook blocks/modifies the command
let blocked = false;
try {
await plugin["tool.execute.before"](beforeInput, beforeOutput);
// If modified (not thrown), the command was replaced
expect(beforeOutput.args.command).toContain("context-mode");
} catch (e: any) {
// deny action throws
blocked = true;
expect(e.message).toContain("context-mode");
}
if (!blocked) {
// After hook still runs (with the replaced command)
await plugin["tool.execute.after"](
{ tool: "Bash", sessionID: "test-session", callID: "call-1", args: beforeOutput.args },
{ title: "Bash", output: beforeOutput.args.command, metadata: {} },
);
}
// Snapshot should be empty (echo/blocked commands don't generate events)
const compactOutput = { context: [] as string[], prompt: undefined };
const snapshot = await plugin["experimental.session.compacting"](
{ sessionID: "test-session" },
compactOutput,
);
expect(snapshot).toBe("");
});
});
});