opencode-swarm-plugin 0.31.7 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +4 -4
- package/.turbo/turbo-test.log +324 -316
- package/CHANGELOG.md +394 -0
- package/README.md +129 -181
- package/bin/swarm.test.ts +31 -0
- package/bin/swarm.ts +635 -140
- package/dist/compaction-hook.d.ts +1 -1
- package/dist/compaction-hook.d.ts.map +1 -1
- package/dist/hive.d.ts.map +1 -1
- package/dist/index.d.ts +17 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +653 -139
- package/dist/memory-tools.d.ts.map +1 -1
- package/dist/memory.d.ts +5 -4
- package/dist/memory.d.ts.map +1 -1
- package/dist/observability-tools.d.ts +116 -0
- package/dist/observability-tools.d.ts.map +1 -0
- package/dist/plugin.js +648 -136
- package/dist/skills.d.ts.map +1 -1
- package/dist/swarm-orchestrate.d.ts +29 -5
- package/dist/swarm-orchestrate.d.ts.map +1 -1
- package/dist/swarm-prompts.d.ts +66 -0
- package/dist/swarm-prompts.d.ts.map +1 -1
- package/dist/swarm.d.ts +17 -2
- package/dist/swarm.d.ts.map +1 -1
- package/evals/lib/{data-loader.test.ts → data-loader.evalite-test.ts} +7 -6
- package/evals/lib/data-loader.ts +1 -1
- package/evals/scorers/{outcome-scorers.test.ts → outcome-scorers.evalite-test.ts} +1 -1
- package/examples/plugin-wrapper-template.ts +316 -12
- package/global-skills/swarm-coordination/SKILL.md +118 -8
- package/package.json +3 -2
- package/src/compaction-hook.ts +5 -3
- package/src/hive.integration.test.ts +83 -1
- package/src/hive.ts +37 -12
- package/src/index.ts +25 -1
- package/src/mandate-storage.integration.test.ts +601 -0
- package/src/memory-tools.ts +6 -4
- package/src/memory.integration.test.ts +117 -49
- package/src/memory.test.ts +41 -217
- package/src/memory.ts +12 -8
- package/src/observability-tools.test.ts +346 -0
- package/src/observability-tools.ts +594 -0
- package/src/repo-crawl.integration.test.ts +441 -0
- package/src/skills.integration.test.ts +1192 -0
- package/src/skills.test.ts +42 -1
- package/src/skills.ts +8 -4
- package/src/structured.integration.test.ts +817 -0
- package/src/swarm-deferred.integration.test.ts +157 -0
- package/src/swarm-deferred.test.ts +38 -0
- package/src/swarm-mail.integration.test.ts +15 -19
- package/src/swarm-orchestrate.integration.test.ts +282 -0
- package/src/swarm-orchestrate.test.ts +123 -0
- package/src/swarm-orchestrate.ts +279 -201
- package/src/swarm-prompts.test.ts +481 -0
- package/src/swarm-prompts.ts +297 -0
- package/src/swarm-research.integration.test.ts +544 -0
- package/src/swarm-research.test.ts +698 -0
- package/src/swarm-research.ts +472 -0
- package/src/swarm-review.integration.test.ts +290 -0
- package/src/swarm.integration.test.ts +23 -20
- package/src/swarm.ts +6 -3
- package/src/tool-adapter.integration.test.ts +1221 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for mandate_* tools
|
|
3
|
+
*
|
|
4
|
+
* Tests the complete tool execution flow through tool.execute():
|
|
5
|
+
* - mandate_file: Submit mandates
|
|
6
|
+
* - mandate_vote: Cast votes with promotion logic
|
|
7
|
+
* - mandate_query: Semantic search
|
|
8
|
+
* - mandate_list: List with filters
|
|
9
|
+
* - mandate_stats: Statistics calculation
|
|
10
|
+
*
|
|
11
|
+
* ## Test Pattern
|
|
12
|
+
* - Call tool.execute() directly (simulates plugin invocation)
|
|
13
|
+
* - Use InMemoryMandateStorage for isolation
|
|
14
|
+
* - Test happy paths and real-world workflows
|
|
15
|
+
* - Unit tests cover storage implementation details
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ToolContext } from "@opencode-ai/plugin";
|
|
19
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
20
|
+
import {
|
|
21
|
+
mandate_file,
|
|
22
|
+
mandate_list,
|
|
23
|
+
mandate_query,
|
|
24
|
+
mandate_stats,
|
|
25
|
+
mandate_vote,
|
|
26
|
+
} from "./mandates";
|
|
27
|
+
import {
|
|
28
|
+
type MandateStorage,
|
|
29
|
+
createMandateStorage,
|
|
30
|
+
resetMandateStorage,
|
|
31
|
+
setMandateStorage,
|
|
32
|
+
} from "./mandate-storage";
|
|
33
|
+
|
|
34
|
+
// Mock ToolContext for tool execution
|
|
35
|
+
const mockCtx = {} as ToolContext;
|
|
36
|
+
|
|
37
|
+
// Test storage instance
|
|
38
|
+
let storage: MandateStorage;
|
|
39
|
+
|
|
40
|
+
describe("Mandate Tools Integration", () => {
|
|
41
|
+
beforeEach(async () => {
|
|
42
|
+
// Create isolated in-memory storage for each test
|
|
43
|
+
storage = createMandateStorage({ backend: "memory" });
|
|
44
|
+
setMandateStorage(storage);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(async () => {
|
|
48
|
+
// Clean up
|
|
49
|
+
await resetMandateStorage();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// ============================================================================
|
|
53
|
+
// mandate_file - Create and Store Mandate
|
|
54
|
+
// ============================================================================
|
|
55
|
+
|
|
56
|
+
describe("mandate_file integration", () => {
|
|
57
|
+
it("creates mandate entry successfully", async () => {
|
|
58
|
+
const result = await mandate_file.execute(
|
|
59
|
+
{
|
|
60
|
+
content: "Always use Effect for async operations",
|
|
61
|
+
content_type: "tip",
|
|
62
|
+
tags: ["async", "effect", "best-practices"],
|
|
63
|
+
},
|
|
64
|
+
mockCtx,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const parsed = JSON.parse(result);
|
|
68
|
+
expect(parsed.success).toBe(true);
|
|
69
|
+
expect(parsed.mandate).toBeDefined();
|
|
70
|
+
expect(parsed.mandate.id).toContain("mandate-");
|
|
71
|
+
expect(parsed.mandate.content).toBe("Always use Effect for async operations");
|
|
72
|
+
expect(parsed.mandate.status).toBe("candidate");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("supports all content types", async () => {
|
|
76
|
+
const types = ["idea", "tip", "lore", "snippet", "feature_request"] as const;
|
|
77
|
+
|
|
78
|
+
for (const type of types) {
|
|
79
|
+
const result = await mandate_file.execute(
|
|
80
|
+
{
|
|
81
|
+
content: `Test ${type}`,
|
|
82
|
+
content_type: type,
|
|
83
|
+
tags: [],
|
|
84
|
+
},
|
|
85
|
+
mockCtx,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const parsed = JSON.parse(result);
|
|
89
|
+
expect(parsed.success).toBe(true);
|
|
90
|
+
expect(parsed.mandate.content_type).toBe(type);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("supports metadata for snippets", async () => {
|
|
95
|
+
const result = await mandate_file.execute(
|
|
96
|
+
{
|
|
97
|
+
content: 'const retry = <T>(fn: () => Promise<T>, n: number) => fn().catch(e => n > 0 ? retry(fn, n - 1) : Promise.reject(e));',
|
|
98
|
+
content_type: "snippet",
|
|
99
|
+
tags: ["typescript"],
|
|
100
|
+
metadata: { language: "typescript", category: "retry" },
|
|
101
|
+
},
|
|
102
|
+
mockCtx,
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const parsed = JSON.parse(result);
|
|
106
|
+
expect(parsed.success).toBe(true);
|
|
107
|
+
expect(parsed.mandate.metadata).toEqual({
|
|
108
|
+
language: "typescript",
|
|
109
|
+
category: "retry",
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("generates unique IDs", async () => {
|
|
114
|
+
const results = await Promise.all([
|
|
115
|
+
mandate_file.execute(
|
|
116
|
+
{ content: "M1", content_type: "tip", tags: [] },
|
|
117
|
+
mockCtx,
|
|
118
|
+
),
|
|
119
|
+
mandate_file.execute(
|
|
120
|
+
{ content: "M2", content_type: "tip", tags: [] },
|
|
121
|
+
mockCtx,
|
|
122
|
+
),
|
|
123
|
+
mandate_file.execute(
|
|
124
|
+
{ content: "M3", content_type: "tip", tags: [] },
|
|
125
|
+
mockCtx,
|
|
126
|
+
),
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
const ids = results.map((r) => JSON.parse(r).mandate.id);
|
|
130
|
+
const uniqueIds = new Set(ids);
|
|
131
|
+
expect(uniqueIds.size).toBe(3);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// ============================================================================
|
|
136
|
+
// mandate_vote - Cast Votes with Promotion
|
|
137
|
+
// ============================================================================
|
|
138
|
+
|
|
139
|
+
describe("mandate_vote integration", () => {
|
|
140
|
+
let mandateId: string;
|
|
141
|
+
|
|
142
|
+
beforeEach(async () => {
|
|
143
|
+
// Create a mandate to vote on
|
|
144
|
+
const createResult = await mandate_file.execute(
|
|
145
|
+
{
|
|
146
|
+
content: "Use TDD for all new features",
|
|
147
|
+
content_type: "tip",
|
|
148
|
+
tags: ["testing"],
|
|
149
|
+
},
|
|
150
|
+
mockCtx,
|
|
151
|
+
);
|
|
152
|
+
mandateId = JSON.parse(createResult).mandate.id;
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("casts upvote successfully with promotion data", async () => {
|
|
156
|
+
const result = await mandate_vote.execute(
|
|
157
|
+
{
|
|
158
|
+
mandate_id: mandateId,
|
|
159
|
+
vote_type: "upvote",
|
|
160
|
+
agent_name: "TestAgent",
|
|
161
|
+
},
|
|
162
|
+
mockCtx,
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const parsed = JSON.parse(result);
|
|
166
|
+
expect(parsed.success).toBe(true);
|
|
167
|
+
expect(parsed.vote).toBeDefined();
|
|
168
|
+
expect(parsed.vote.vote_type).toBe("upvote");
|
|
169
|
+
expect(parsed.promotion).toBeDefined();
|
|
170
|
+
expect(parsed.promotion.score).toBeDefined();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("casts downvote successfully", async () => {
|
|
174
|
+
const result = await mandate_vote.execute(
|
|
175
|
+
{
|
|
176
|
+
mandate_id: mandateId,
|
|
177
|
+
vote_type: "downvote",
|
|
178
|
+
agent_name: "TestAgent",
|
|
179
|
+
},
|
|
180
|
+
mockCtx,
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const parsed = JSON.parse(result);
|
|
184
|
+
expect(parsed.success).toBe(true);
|
|
185
|
+
expect(parsed.vote.vote_type).toBe("downvote");
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("defaults weight to 1.0", async () => {
|
|
189
|
+
const result = await mandate_vote.execute(
|
|
190
|
+
{
|
|
191
|
+
mandate_id: mandateId,
|
|
192
|
+
vote_type: "upvote",
|
|
193
|
+
agent_name: "TestAgent",
|
|
194
|
+
},
|
|
195
|
+
mockCtx,
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const parsed = JSON.parse(result);
|
|
199
|
+
expect(parsed.success).toBe(true);
|
|
200
|
+
// Tool hardcodes weight to 1.0 (see mandates.ts line 170)
|
|
201
|
+
expect(parsed.vote.weight).toBe(1.0);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("prevents duplicate votes from same agent", async () => {
|
|
205
|
+
// First vote succeeds
|
|
206
|
+
const result1 = await mandate_vote.execute(
|
|
207
|
+
{
|
|
208
|
+
mandate_id: mandateId,
|
|
209
|
+
vote_type: "upvote",
|
|
210
|
+
agent_name: "Agent1",
|
|
211
|
+
},
|
|
212
|
+
mockCtx,
|
|
213
|
+
);
|
|
214
|
+
expect(JSON.parse(result1).success).toBe(true);
|
|
215
|
+
|
|
216
|
+
// Second vote from same agent fails
|
|
217
|
+
await expect(
|
|
218
|
+
mandate_vote.execute(
|
|
219
|
+
{
|
|
220
|
+
mandate_id: mandateId,
|
|
221
|
+
vote_type: "downvote",
|
|
222
|
+
agent_name: "Agent1",
|
|
223
|
+
},
|
|
224
|
+
mockCtx,
|
|
225
|
+
),
|
|
226
|
+
).rejects.toThrow("already voted");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("allows different agents to vote", async () => {
|
|
230
|
+
const result1 = await mandate_vote.execute(
|
|
231
|
+
{
|
|
232
|
+
mandate_id: mandateId,
|
|
233
|
+
vote_type: "upvote",
|
|
234
|
+
agent_name: "Agent1",
|
|
235
|
+
},
|
|
236
|
+
mockCtx,
|
|
237
|
+
);
|
|
238
|
+
expect(JSON.parse(result1).success).toBe(true);
|
|
239
|
+
|
|
240
|
+
const result2 = await mandate_vote.execute(
|
|
241
|
+
{
|
|
242
|
+
mandate_id: mandateId,
|
|
243
|
+
vote_type: "upvote",
|
|
244
|
+
agent_name: "Agent2",
|
|
245
|
+
},
|
|
246
|
+
mockCtx,
|
|
247
|
+
);
|
|
248
|
+
expect(JSON.parse(result2).success).toBe(true);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// ============================================================================
|
|
253
|
+
// mandate_query - Semantic Search
|
|
254
|
+
// ============================================================================
|
|
255
|
+
|
|
256
|
+
describe("mandate_query integration", () => {
|
|
257
|
+
beforeEach(async () => {
|
|
258
|
+
// Create searchable mandates
|
|
259
|
+
await mandate_file.execute(
|
|
260
|
+
{
|
|
261
|
+
content: "Always use Effect for async operations",
|
|
262
|
+
content_type: "tip",
|
|
263
|
+
tags: ["async", "effect"],
|
|
264
|
+
},
|
|
265
|
+
mockCtx,
|
|
266
|
+
);
|
|
267
|
+
await mandate_file.execute(
|
|
268
|
+
{
|
|
269
|
+
content: "Prefer semantic memory for persistence",
|
|
270
|
+
content_type: "tip",
|
|
271
|
+
tags: ["storage"],
|
|
272
|
+
},
|
|
273
|
+
mockCtx,
|
|
274
|
+
);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("searches by content text", async () => {
|
|
278
|
+
const result = await mandate_query.execute(
|
|
279
|
+
{ query: "Effect" },
|
|
280
|
+
mockCtx,
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
const parsed = JSON.parse(result);
|
|
284
|
+
expect(parsed.count).toBeGreaterThanOrEqual(1);
|
|
285
|
+
expect(parsed.results.length).toBeGreaterThanOrEqual(1);
|
|
286
|
+
expect(parsed.results[0]).toHaveProperty("content");
|
|
287
|
+
expect(parsed.results[0]).toHaveProperty("score");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("limits results", async () => {
|
|
291
|
+
const result = await mandate_query.execute(
|
|
292
|
+
{ query: "tip", limit: 1 },
|
|
293
|
+
mockCtx,
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
const parsed = JSON.parse(result);
|
|
297
|
+
expect(parsed.results.length).toBeLessThanOrEqual(1);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("returns empty for non-matching query", async () => {
|
|
301
|
+
const result = await mandate_query.execute(
|
|
302
|
+
{ query: "nonexistent123xyz" },
|
|
303
|
+
mockCtx,
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
const parsed = JSON.parse(result);
|
|
307
|
+
expect(parsed.count).toBe(0);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
// ============================================================================
|
|
312
|
+
// mandate_list - List with Filters
|
|
313
|
+
// ============================================================================
|
|
314
|
+
|
|
315
|
+
describe("mandate_list integration", () => {
|
|
316
|
+
beforeEach(async () => {
|
|
317
|
+
await mandate_file.execute(
|
|
318
|
+
{ content: "Tip 1", content_type: "tip", tags: [] },
|
|
319
|
+
mockCtx,
|
|
320
|
+
);
|
|
321
|
+
await mandate_file.execute(
|
|
322
|
+
{ content: "Idea 1", content_type: "idea", tags: [] },
|
|
323
|
+
mockCtx,
|
|
324
|
+
);
|
|
325
|
+
await mandate_file.execute(
|
|
326
|
+
{ content: "Lore 1", content_type: "lore", tags: [] },
|
|
327
|
+
mockCtx,
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("lists all mandates when no filter", async () => {
|
|
332
|
+
const result = await mandate_list.execute({}, mockCtx);
|
|
333
|
+
|
|
334
|
+
const parsed = JSON.parse(result);
|
|
335
|
+
expect(parsed.count).toBe(3);
|
|
336
|
+
expect(parsed.results.length).toBe(3);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("filters by content_type", async () => {
|
|
340
|
+
const result = await mandate_list.execute(
|
|
341
|
+
{ content_type: "tip" },
|
|
342
|
+
mockCtx,
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
const parsed = JSON.parse(result);
|
|
346
|
+
expect(parsed.count).toBe(1);
|
|
347
|
+
expect(parsed.results[0].content_type).toBe("tip");
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("filters by status", async () => {
|
|
351
|
+
const result = await mandate_list.execute(
|
|
352
|
+
{ status: "candidate" },
|
|
353
|
+
mockCtx,
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
const parsed = JSON.parse(result);
|
|
357
|
+
// All new mandates start as candidate
|
|
358
|
+
expect(parsed.count).toBe(3);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("returns empty list when no matches", async () => {
|
|
362
|
+
const result = await mandate_list.execute(
|
|
363
|
+
{ status: "mandate" }, // No mandates promoted yet
|
|
364
|
+
mockCtx,
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const parsed = JSON.parse(result);
|
|
368
|
+
expect(parsed.count).toBe(0);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
// ============================================================================
|
|
373
|
+
// mandate_stats - Statistics
|
|
374
|
+
// ============================================================================
|
|
375
|
+
|
|
376
|
+
describe("mandate_stats integration", () => {
|
|
377
|
+
it("returns zero stats for empty storage", async () => {
|
|
378
|
+
const result = await mandate_stats.execute({}, mockCtx);
|
|
379
|
+
|
|
380
|
+
const parsed = JSON.parse(result);
|
|
381
|
+
expect(parsed.total_mandates).toBe(0);
|
|
382
|
+
expect(parsed.by_status).toBeDefined();
|
|
383
|
+
expect(parsed.by_content_type).toBeDefined();
|
|
384
|
+
expect(parsed.total_votes).toBe(0);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("counts mandates by status", async () => {
|
|
388
|
+
await mandate_file.execute(
|
|
389
|
+
{ content: "Tip 1", content_type: "tip", tags: [] },
|
|
390
|
+
mockCtx,
|
|
391
|
+
);
|
|
392
|
+
await mandate_file.execute(
|
|
393
|
+
{ content: "Tip 2", content_type: "tip", tags: [] },
|
|
394
|
+
mockCtx,
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
const result = await mandate_stats.execute({}, mockCtx);
|
|
398
|
+
|
|
399
|
+
const parsed = JSON.parse(result);
|
|
400
|
+
expect(parsed.total_mandates).toBe(2);
|
|
401
|
+
expect(parsed.by_status.candidate).toBe(2);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it("counts mandates by content type", async () => {
|
|
405
|
+
await mandate_file.execute(
|
|
406
|
+
{ content: "Tip 1", content_type: "tip", tags: [] },
|
|
407
|
+
mockCtx,
|
|
408
|
+
);
|
|
409
|
+
await mandate_file.execute(
|
|
410
|
+
{ content: "Idea 1", content_type: "idea", tags: [] },
|
|
411
|
+
mockCtx,
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
const result = await mandate_stats.execute({}, mockCtx);
|
|
415
|
+
|
|
416
|
+
const parsed = JSON.parse(result);
|
|
417
|
+
expect(parsed.by_content_type.tip).toBe(1);
|
|
418
|
+
expect(parsed.by_content_type.idea).toBe(1);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("counts total votes after voting", async () => {
|
|
422
|
+
// Create mandate
|
|
423
|
+
const createResult = await mandate_file.execute(
|
|
424
|
+
{ content: "Test", content_type: "tip", tags: [] },
|
|
425
|
+
mockCtx,
|
|
426
|
+
);
|
|
427
|
+
const mandateId = JSON.parse(createResult).mandate.id;
|
|
428
|
+
|
|
429
|
+
// Cast votes
|
|
430
|
+
await mandate_vote.execute(
|
|
431
|
+
{
|
|
432
|
+
mandate_id: mandateId,
|
|
433
|
+
vote_type: "upvote",
|
|
434
|
+
agent_name: "Agent1",
|
|
435
|
+
},
|
|
436
|
+
mockCtx,
|
|
437
|
+
);
|
|
438
|
+
await mandate_vote.execute(
|
|
439
|
+
{
|
|
440
|
+
mandate_id: mandateId,
|
|
441
|
+
vote_type: "upvote",
|
|
442
|
+
agent_name: "Agent2",
|
|
443
|
+
},
|
|
444
|
+
mockCtx,
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
const result = await mandate_stats.execute({}, mockCtx);
|
|
448
|
+
|
|
449
|
+
const parsed = JSON.parse(result);
|
|
450
|
+
expect(parsed.total_votes).toBe(2);
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
// ============================================================================
|
|
455
|
+
// Integration: Complete Workflows
|
|
456
|
+
// ============================================================================
|
|
457
|
+
|
|
458
|
+
describe("Complete voting workflow", () => {
|
|
459
|
+
it("file → vote → query → stats workflow", async () => {
|
|
460
|
+
// 1. File a mandate
|
|
461
|
+
const createResult = await mandate_file.execute(
|
|
462
|
+
{
|
|
463
|
+
content: "Always write integration tests",
|
|
464
|
+
content_type: "tip",
|
|
465
|
+
tags: ["testing"],
|
|
466
|
+
},
|
|
467
|
+
mockCtx,
|
|
468
|
+
);
|
|
469
|
+
const mandateId = JSON.parse(createResult).mandate.id;
|
|
470
|
+
|
|
471
|
+
// 2. Cast votes
|
|
472
|
+
await mandate_vote.execute(
|
|
473
|
+
{
|
|
474
|
+
mandate_id: mandateId,
|
|
475
|
+
vote_type: "upvote",
|
|
476
|
+
agent_name: "Agent1",
|
|
477
|
+
},
|
|
478
|
+
mockCtx,
|
|
479
|
+
);
|
|
480
|
+
await mandate_vote.execute(
|
|
481
|
+
{
|
|
482
|
+
mandate_id: mandateId,
|
|
483
|
+
vote_type: "upvote",
|
|
484
|
+
agent_name: "Agent2",
|
|
485
|
+
},
|
|
486
|
+
mockCtx,
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
// 3. Query finds it
|
|
490
|
+
const queryResult = await mandate_query.execute(
|
|
491
|
+
{ query: "integration tests" },
|
|
492
|
+
mockCtx,
|
|
493
|
+
);
|
|
494
|
+
const query = JSON.parse(queryResult);
|
|
495
|
+
expect(query.count).toBeGreaterThanOrEqual(1);
|
|
496
|
+
|
|
497
|
+
// 4. Stats reflect votes
|
|
498
|
+
const statsResult = await mandate_stats.execute({}, mockCtx);
|
|
499
|
+
const stats = JSON.parse(statsResult);
|
|
500
|
+
expect(stats.total_votes).toBe(2);
|
|
501
|
+
expect(stats.total_mandates).toBe(1);
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it("supports multiple mandates with independent voting", async () => {
|
|
505
|
+
// Create multiple mandates
|
|
506
|
+
const create1 = await mandate_file.execute(
|
|
507
|
+
{ content: "Mandate 1", content_type: "tip", tags: [] },
|
|
508
|
+
mockCtx,
|
|
509
|
+
);
|
|
510
|
+
const create2 = await mandate_file.execute(
|
|
511
|
+
{ content: "Mandate 2", content_type: "idea", tags: [] },
|
|
512
|
+
mockCtx,
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
const mandate1Id = JSON.parse(create1).mandate.id;
|
|
516
|
+
const mandate2Id = JSON.parse(create2).mandate.id;
|
|
517
|
+
|
|
518
|
+
// Vote on both (different agents)
|
|
519
|
+
await mandate_vote.execute(
|
|
520
|
+
{
|
|
521
|
+
mandate_id: mandate1Id,
|
|
522
|
+
vote_type: "upvote",
|
|
523
|
+
agent_name: "Agent1",
|
|
524
|
+
},
|
|
525
|
+
mockCtx,
|
|
526
|
+
);
|
|
527
|
+
await mandate_vote.execute(
|
|
528
|
+
{
|
|
529
|
+
mandate_id: mandate2Id,
|
|
530
|
+
vote_type: "downvote",
|
|
531
|
+
agent_name: "Agent1",
|
|
532
|
+
},
|
|
533
|
+
mockCtx,
|
|
534
|
+
);
|
|
535
|
+
|
|
536
|
+
// Stats show correct counts
|
|
537
|
+
const statsResult = await mandate_stats.execute({}, mockCtx);
|
|
538
|
+
const stats = JSON.parse(statsResult);
|
|
539
|
+
expect(stats.total_votes).toBe(2);
|
|
540
|
+
expect(stats.total_mandates).toBe(2);
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
// ============================================================================
|
|
545
|
+
// Edge Cases
|
|
546
|
+
// ============================================================================
|
|
547
|
+
|
|
548
|
+
describe("Edge cases and error handling", () => {
|
|
549
|
+
it("handles unicode and special characters", async () => {
|
|
550
|
+
const result = await mandate_file.execute(
|
|
551
|
+
{
|
|
552
|
+
content: "Use emoji for clarity 🎉 和 你好",
|
|
553
|
+
content_type: "tip",
|
|
554
|
+
tags: ["unicode"],
|
|
555
|
+
},
|
|
556
|
+
mockCtx,
|
|
557
|
+
);
|
|
558
|
+
|
|
559
|
+
const parsed = JSON.parse(result);
|
|
560
|
+
expect(parsed.success).toBe(true);
|
|
561
|
+
expect(parsed.mandate.content).toContain("🎉");
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
it("handles empty tags array", async () => {
|
|
565
|
+
const result = await mandate_file.execute(
|
|
566
|
+
{
|
|
567
|
+
content: "No tags mandate",
|
|
568
|
+
content_type: "tip",
|
|
569
|
+
tags: [],
|
|
570
|
+
},
|
|
571
|
+
mockCtx,
|
|
572
|
+
);
|
|
573
|
+
|
|
574
|
+
const parsed = JSON.parse(result);
|
|
575
|
+
expect(parsed.success).toBe(true);
|
|
576
|
+
expect(parsed.mandate.tags).toEqual([]);
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
it("handles large metadata objects", async () => {
|
|
580
|
+
const largeMetadata = {
|
|
581
|
+
key1: "value1",
|
|
582
|
+
nested: { deep: { object: "value" } },
|
|
583
|
+
array: [1, 2, 3, 4, 5],
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
const result = await mandate_file.execute(
|
|
587
|
+
{
|
|
588
|
+
content: "Mandate with metadata",
|
|
589
|
+
content_type: "snippet",
|
|
590
|
+
tags: [],
|
|
591
|
+
metadata: largeMetadata,
|
|
592
|
+
},
|
|
593
|
+
mockCtx,
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
const parsed = JSON.parse(result);
|
|
597
|
+
expect(parsed.success).toBe(true);
|
|
598
|
+
expect(parsed.mandate.metadata).toEqual(largeMetadata);
|
|
599
|
+
});
|
|
600
|
+
});
|
|
601
|
+
});
|
package/src/memory-tools.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { tool } from "@opencode-ai/plugin";
|
|
17
|
-
import {
|
|
17
|
+
import { getSwarmMailLibSQL } from "swarm-mail";
|
|
18
18
|
import {
|
|
19
19
|
createMemoryAdapter,
|
|
20
20
|
type MemoryAdapter,
|
|
@@ -76,9 +76,11 @@ async function getMemoryAdapter(
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
// Create new adapter
|
|
79
|
-
const swarmMail = await
|
|
80
|
-
const
|
|
81
|
-
|
|
79
|
+
const swarmMail = await getSwarmMailLibSQL(path);
|
|
80
|
+
const dbAdapter = await swarmMail.getDatabase();
|
|
81
|
+
|
|
82
|
+
// createMemoryAdapter now accepts DatabaseAdapter directly and converts internally
|
|
83
|
+
cachedAdapter = await createMemoryAdapter(dbAdapter);
|
|
82
84
|
cachedProjectPath = path;
|
|
83
85
|
|
|
84
86
|
return cachedAdapter;
|