opencode-swarm-plugin 0.43.0 → 0.44.1

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.
Files changed (208) hide show
  1. package/bin/cass.characterization.test.ts +422 -0
  2. package/bin/swarm.serve.test.ts +6 -4
  3. package/bin/swarm.test.ts +68 -0
  4. package/bin/swarm.ts +81 -8
  5. package/dist/compaction-prompt-scoring.js +139 -0
  6. package/dist/contributor-tools.d.ts +42 -0
  7. package/dist/contributor-tools.d.ts.map +1 -0
  8. package/dist/eval-capture.js +12811 -0
  9. package/dist/hive.d.ts.map +1 -1
  10. package/dist/index.d.ts +12 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +7728 -62590
  13. package/dist/plugin.js +23833 -78695
  14. package/dist/sessions/agent-discovery.d.ts +59 -0
  15. package/dist/sessions/agent-discovery.d.ts.map +1 -0
  16. package/dist/sessions/index.d.ts +10 -0
  17. package/dist/sessions/index.d.ts.map +1 -0
  18. package/dist/swarm-orchestrate.d.ts.map +1 -1
  19. package/dist/swarm-prompts.d.ts.map +1 -1
  20. package/dist/swarm-review.d.ts.map +1 -1
  21. package/package.json +17 -5
  22. package/.changeset/swarm-insights-data-layer.md +0 -63
  23. package/.hive/analysis/eval-failure-analysis-2025-12-25.md +0 -331
  24. package/.hive/analysis/session-data-quality-audit.md +0 -320
  25. package/.hive/eval-results.json +0 -483
  26. package/.hive/issues.jsonl +0 -138
  27. package/.hive/memories.jsonl +0 -729
  28. package/.opencode/eval-history.jsonl +0 -327
  29. package/.turbo/turbo-build.log +0 -9
  30. package/CHANGELOG.md +0 -2255
  31. package/SCORER-ANALYSIS.md +0 -598
  32. package/docs/analysis/subagent-coordination-patterns.md +0 -902
  33. package/docs/analysis-socratic-planner-pattern.md +0 -504
  34. package/docs/planning/ADR-001-monorepo-structure.md +0 -171
  35. package/docs/planning/ADR-002-package-extraction.md +0 -393
  36. package/docs/planning/ADR-003-performance-improvements.md +0 -451
  37. package/docs/planning/ADR-004-message-queue-features.md +0 -187
  38. package/docs/planning/ADR-005-devtools-observability.md +0 -202
  39. package/docs/planning/ADR-007-swarm-enhancements-worktree-review.md +0 -168
  40. package/docs/planning/ADR-008-worker-handoff-protocol.md +0 -293
  41. package/docs/planning/ADR-009-oh-my-opencode-patterns.md +0 -353
  42. package/docs/planning/ROADMAP.md +0 -368
  43. package/docs/semantic-memory-cli-syntax.md +0 -123
  44. package/docs/swarm-mail-architecture.md +0 -1147
  45. package/docs/testing/context-recovery-test.md +0 -470
  46. package/evals/ARCHITECTURE.md +0 -1189
  47. package/evals/README.md +0 -768
  48. package/evals/compaction-prompt.eval.ts +0 -149
  49. package/evals/compaction-resumption.eval.ts +0 -289
  50. package/evals/coordinator-behavior.eval.ts +0 -307
  51. package/evals/coordinator-session.eval.ts +0 -154
  52. package/evals/evalite.config.ts.bak +0 -15
  53. package/evals/example.eval.ts +0 -31
  54. package/evals/fixtures/compaction-cases.ts +0 -350
  55. package/evals/fixtures/compaction-prompt-cases.ts +0 -311
  56. package/evals/fixtures/coordinator-sessions.ts +0 -328
  57. package/evals/fixtures/decomposition-cases.ts +0 -105
  58. package/evals/lib/compaction-loader.test.ts +0 -248
  59. package/evals/lib/compaction-loader.ts +0 -320
  60. package/evals/lib/data-loader.evalite-test.ts +0 -289
  61. package/evals/lib/data-loader.test.ts +0 -345
  62. package/evals/lib/data-loader.ts +0 -281
  63. package/evals/lib/llm.ts +0 -115
  64. package/evals/scorers/compaction-prompt-scorers.ts +0 -145
  65. package/evals/scorers/compaction-scorers.ts +0 -305
  66. package/evals/scorers/coordinator-discipline.evalite-test.ts +0 -539
  67. package/evals/scorers/coordinator-discipline.ts +0 -325
  68. package/evals/scorers/index.test.ts +0 -146
  69. package/evals/scorers/index.ts +0 -328
  70. package/evals/scorers/outcome-scorers.evalite-test.ts +0 -27
  71. package/evals/scorers/outcome-scorers.ts +0 -349
  72. package/evals/swarm-decomposition.eval.ts +0 -121
  73. package/examples/commands/swarm.md +0 -745
  74. package/examples/plugin-wrapper-template.ts +0 -2426
  75. package/examples/skills/hive-workflow/SKILL.md +0 -212
  76. package/examples/skills/skill-creator/SKILL.md +0 -223
  77. package/examples/skills/swarm-coordination/SKILL.md +0 -292
  78. package/global-skills/cli-builder/SKILL.md +0 -344
  79. package/global-skills/cli-builder/references/advanced-patterns.md +0 -244
  80. package/global-skills/learning-systems/SKILL.md +0 -644
  81. package/global-skills/skill-creator/LICENSE.txt +0 -202
  82. package/global-skills/skill-creator/SKILL.md +0 -352
  83. package/global-skills/skill-creator/references/output-patterns.md +0 -82
  84. package/global-skills/skill-creator/references/workflows.md +0 -28
  85. package/global-skills/swarm-coordination/SKILL.md +0 -995
  86. package/global-skills/swarm-coordination/references/coordinator-patterns.md +0 -235
  87. package/global-skills/swarm-coordination/references/strategies.md +0 -138
  88. package/global-skills/system-design/SKILL.md +0 -213
  89. package/global-skills/testing-patterns/SKILL.md +0 -430
  90. package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +0 -586
  91. package/opencode-swarm-plugin-0.30.7.tgz +0 -0
  92. package/opencode-swarm-plugin-0.31.0.tgz +0 -0
  93. package/scripts/cleanup-test-memories.ts +0 -346
  94. package/scripts/init-skill.ts +0 -222
  95. package/scripts/migrate-unknown-sessions.ts +0 -349
  96. package/scripts/validate-skill.ts +0 -204
  97. package/src/agent-mail.ts +0 -1724
  98. package/src/anti-patterns.test.ts +0 -1167
  99. package/src/anti-patterns.ts +0 -448
  100. package/src/compaction-capture.integration.test.ts +0 -257
  101. package/src/compaction-hook.test.ts +0 -838
  102. package/src/compaction-hook.ts +0 -1204
  103. package/src/compaction-observability.integration.test.ts +0 -139
  104. package/src/compaction-observability.test.ts +0 -187
  105. package/src/compaction-observability.ts +0 -324
  106. package/src/compaction-prompt-scorers.test.ts +0 -475
  107. package/src/compaction-prompt-scoring.ts +0 -300
  108. package/src/dashboard.test.ts +0 -611
  109. package/src/dashboard.ts +0 -462
  110. package/src/error-enrichment.test.ts +0 -403
  111. package/src/error-enrichment.ts +0 -219
  112. package/src/eval-capture.test.ts +0 -1015
  113. package/src/eval-capture.ts +0 -929
  114. package/src/eval-gates.test.ts +0 -306
  115. package/src/eval-gates.ts +0 -218
  116. package/src/eval-history.test.ts +0 -508
  117. package/src/eval-history.ts +0 -214
  118. package/src/eval-learning.test.ts +0 -378
  119. package/src/eval-learning.ts +0 -360
  120. package/src/eval-runner.test.ts +0 -223
  121. package/src/eval-runner.ts +0 -402
  122. package/src/export-tools.test.ts +0 -476
  123. package/src/export-tools.ts +0 -257
  124. package/src/hive.integration.test.ts +0 -2241
  125. package/src/hive.ts +0 -1628
  126. package/src/index.ts +0 -935
  127. package/src/learning.integration.test.ts +0 -1815
  128. package/src/learning.ts +0 -1079
  129. package/src/logger.test.ts +0 -189
  130. package/src/logger.ts +0 -135
  131. package/src/mandate-promotion.test.ts +0 -473
  132. package/src/mandate-promotion.ts +0 -239
  133. package/src/mandate-storage.integration.test.ts +0 -601
  134. package/src/mandate-storage.test.ts +0 -578
  135. package/src/mandate-storage.ts +0 -794
  136. package/src/mandates.ts +0 -540
  137. package/src/memory-tools.test.ts +0 -195
  138. package/src/memory-tools.ts +0 -344
  139. package/src/memory.integration.test.ts +0 -334
  140. package/src/memory.test.ts +0 -158
  141. package/src/memory.ts +0 -527
  142. package/src/model-selection.test.ts +0 -188
  143. package/src/model-selection.ts +0 -68
  144. package/src/observability-tools.test.ts +0 -359
  145. package/src/observability-tools.ts +0 -871
  146. package/src/output-guardrails.test.ts +0 -438
  147. package/src/output-guardrails.ts +0 -381
  148. package/src/pattern-maturity.test.ts +0 -1160
  149. package/src/pattern-maturity.ts +0 -525
  150. package/src/planning-guardrails.test.ts +0 -491
  151. package/src/planning-guardrails.ts +0 -438
  152. package/src/plugin.ts +0 -23
  153. package/src/post-compaction-tracker.test.ts +0 -251
  154. package/src/post-compaction-tracker.ts +0 -237
  155. package/src/query-tools.test.ts +0 -636
  156. package/src/query-tools.ts +0 -324
  157. package/src/rate-limiter.integration.test.ts +0 -466
  158. package/src/rate-limiter.ts +0 -774
  159. package/src/replay-tools.test.ts +0 -496
  160. package/src/replay-tools.ts +0 -240
  161. package/src/repo-crawl.integration.test.ts +0 -441
  162. package/src/repo-crawl.ts +0 -610
  163. package/src/schemas/cell-events.test.ts +0 -347
  164. package/src/schemas/cell-events.ts +0 -807
  165. package/src/schemas/cell.ts +0 -257
  166. package/src/schemas/evaluation.ts +0 -166
  167. package/src/schemas/index.test.ts +0 -199
  168. package/src/schemas/index.ts +0 -286
  169. package/src/schemas/mandate.ts +0 -232
  170. package/src/schemas/swarm-context.ts +0 -115
  171. package/src/schemas/task.ts +0 -161
  172. package/src/schemas/worker-handoff.test.ts +0 -302
  173. package/src/schemas/worker-handoff.ts +0 -131
  174. package/src/skills.integration.test.ts +0 -1192
  175. package/src/skills.test.ts +0 -643
  176. package/src/skills.ts +0 -1549
  177. package/src/storage.integration.test.ts +0 -341
  178. package/src/storage.ts +0 -884
  179. package/src/structured.integration.test.ts +0 -817
  180. package/src/structured.test.ts +0 -1046
  181. package/src/structured.ts +0 -762
  182. package/src/swarm-decompose.test.ts +0 -188
  183. package/src/swarm-decompose.ts +0 -1302
  184. package/src/swarm-deferred.integration.test.ts +0 -157
  185. package/src/swarm-deferred.test.ts +0 -38
  186. package/src/swarm-insights.test.ts +0 -214
  187. package/src/swarm-insights.ts +0 -459
  188. package/src/swarm-mail.integration.test.ts +0 -970
  189. package/src/swarm-mail.ts +0 -739
  190. package/src/swarm-orchestrate.integration.test.ts +0 -282
  191. package/src/swarm-orchestrate.test.ts +0 -548
  192. package/src/swarm-orchestrate.ts +0 -3084
  193. package/src/swarm-prompts.test.ts +0 -1270
  194. package/src/swarm-prompts.ts +0 -2077
  195. package/src/swarm-research.integration.test.ts +0 -701
  196. package/src/swarm-research.test.ts +0 -698
  197. package/src/swarm-research.ts +0 -472
  198. package/src/swarm-review.integration.test.ts +0 -285
  199. package/src/swarm-review.test.ts +0 -879
  200. package/src/swarm-review.ts +0 -709
  201. package/src/swarm-strategies.ts +0 -407
  202. package/src/swarm-worktree.test.ts +0 -501
  203. package/src/swarm-worktree.ts +0 -575
  204. package/src/swarm.integration.test.ts +0 -2377
  205. package/src/swarm.ts +0 -38
  206. package/src/tool-adapter.integration.test.ts +0 -1221
  207. package/src/tool-availability.ts +0 -461
  208. package/tsconfig.json +0 -28
@@ -1,636 +0,0 @@
1
- /**
2
- * RED PHASE: Failing tests for query-tools
3
- *
4
- * These tests define the contract for:
5
- * - 10 preset queries (SQL string generation)
6
- * - executeQuery() (custom SQL execution against libSQL)
7
- * - formatAsTable() (aligned box-drawing output)
8
- * - formatAsCSV() (proper escaping)
9
- * - formatAsJSON() (valid JSON array)
10
- *
11
- * Implementation comes in GREEN phase (query-tools.ts doesn't exist yet).
12
- */
13
-
14
- import { describe, test, expect, beforeAll, afterAll } from "bun:test";
15
- import {
16
- createInMemorySwarmMailLibSQL,
17
- type SwarmMailAdapter,
18
- type DatabaseAdapter,
19
- } from "swarm-mail";
20
-
21
- // Import from non-existent file (will fail until GREEN phase)
22
- import {
23
- presetQueries,
24
- executeQuery,
25
- formatAsTable,
26
- formatAsCSV,
27
- formatAsJSON,
28
- type PresetQueryName,
29
- type QueryResult,
30
- } from "./query-tools";
31
-
32
- describe("Query Tools - RED Phase Tests", () => {
33
- let swarmMail: SwarmMailAdapter;
34
- let db: DatabaseAdapter;
35
-
36
- beforeAll(async () => {
37
- swarmMail = await createInMemorySwarmMailLibSQL("query-tools-test");
38
- db = await swarmMail.getDatabase();
39
-
40
- // Insert test data for queries
41
- await db.query(
42
- `INSERT INTO events (type, project_key, timestamp, data) VALUES (?, ?, ?, ?)`,
43
- [
44
- "subtask_outcome",
45
- "/test/project",
46
- Date.now(),
47
- JSON.stringify({
48
- agent_name: "AgentA",
49
- epic_id: "epic1",
50
- bead_id: "bead1",
51
- success: false,
52
- strategy: "file-based",
53
- durationMs: 5000
54
- }),
55
- ],
56
- );
57
-
58
- await db.query(
59
- `INSERT INTO events (type, project_key, timestamp, data) VALUES (?, ?, ?, ?)`,
60
- [
61
- "subtask_outcome",
62
- "/test/project",
63
- Date.now() + 1000,
64
- JSON.stringify({
65
- agent_name: "AgentB",
66
- epic_id: "epic1",
67
- bead_id: "bead2",
68
- success: true,
69
- strategy: "feature-based",
70
- durationMs: 3000
71
- }),
72
- ],
73
- );
74
-
75
- await db.query(
76
- `INSERT INTO events (type, project_key, timestamp, data) VALUES (?, ?, ?, ?)`,
77
- [
78
- "reservation_created",
79
- "/test/project",
80
- Date.now() + 2000,
81
- JSON.stringify({
82
- agent_name: "AgentA",
83
- epic_id: "epic1",
84
- bead_id: "bead1",
85
- paths: ["src/file.ts"],
86
- exclusive: true
87
- }),
88
- ],
89
- );
90
-
91
- await db.query(
92
- `INSERT INTO events (type, project_key, timestamp, data) VALUES (?, ?, ?, ?)`,
93
- [
94
- "reservation_created",
95
- "/test/project",
96
- Date.now() + 3000,
97
- JSON.stringify({
98
- agent_name: "AgentB",
99
- epic_id: "epic1",
100
- bead_id: "bead2",
101
- paths: ["src/file.ts"],
102
- exclusive: true
103
- }),
104
- ],
105
- );
106
-
107
- await db.query(
108
- `INSERT INTO events (type, project_key, timestamp, data) VALUES (?, ?, ?, ?)`,
109
- [
110
- "review_feedback",
111
- "/test/project",
112
- Date.now() + 4000,
113
- JSON.stringify({
114
- agent_name: "Coordinator",
115
- epic_id: "epic1",
116
- bead_id: "bead1",
117
- status: "needs_changes",
118
- issues: ["Type error"]
119
- }),
120
- ],
121
- );
122
-
123
- await db.query(
124
- `INSERT INTO events (type, project_key, timestamp, data) VALUES (?, ?, ?, ?)`,
125
- [
126
- "decomposition_failed",
127
- "/test/project",
128
- Date.now() + 5000,
129
- JSON.stringify({
130
- agent_name: "Planner",
131
- epic_id: "epic2",
132
- error: "Invalid JSON",
133
- strategy: "risk-based"
134
- }),
135
- ],
136
- );
137
-
138
- await db.query(
139
- `INSERT INTO events (type, project_key, timestamp, data) VALUES (?, ?, ?, ?)`,
140
- [
141
- "compaction",
142
- "/test/project",
143
- Date.now() + 6000,
144
- JSON.stringify({
145
- agent_name: "System",
146
- beforeSize: 100000,
147
- afterSize: 50000,
148
- ratio: 0.5
149
- }),
150
- ],
151
- );
152
- });
153
-
154
- afterAll(async () => {
155
- await swarmMail.close();
156
- });
157
-
158
- describe("Preset Queries - SQL Generation", () => {
159
- test("failed_decompositions query returns valid SQL string", () => {
160
- const sql = presetQueries.failed_decompositions;
161
-
162
- expect(typeof sql).toBe("string");
163
- expect(sql).toContain("SELECT");
164
- expect(sql).toContain("decomposition_failed");
165
- expect(sql.toLowerCase()).toContain("group by");
166
- });
167
-
168
- test("duration_by_strategy query returns valid SQL string", () => {
169
- const sql = presetQueries.duration_by_strategy;
170
-
171
- expect(typeof sql).toBe("string");
172
- expect(sql).toContain("SELECT");
173
- expect(sql).toContain("subtask_outcome");
174
- expect(sql.toLowerCase()).toContain("avg");
175
- expect(sql.toLowerCase()).toContain("group by");
176
- });
177
-
178
- test("file_conflicts query returns valid SQL string", () => {
179
- const sql = presetQueries.file_conflicts;
180
-
181
- expect(typeof sql).toBe("string");
182
- expect(sql).toContain("SELECT");
183
- expect(sql).toContain("reservation_created");
184
- expect(sql.toLowerCase()).toContain("json_each");
185
- });
186
-
187
- test("worker_success_rate query returns valid SQL string", () => {
188
- const sql = presetQueries.worker_success_rate;
189
-
190
- expect(typeof sql).toBe("string");
191
- expect(sql).toContain("SELECT");
192
- expect(sql).toContain("subtask_outcome");
193
- expect(sql.toLowerCase()).toContain("cast");
194
- expect(sql.toLowerCase()).toContain("group by");
195
- });
196
-
197
- test("review_rejections query returns valid SQL string", () => {
198
- const sql = presetQueries.review_rejections;
199
-
200
- expect(typeof sql).toBe("string");
201
- expect(sql).toContain("SELECT");
202
- expect(sql).toContain("review_feedback");
203
- expect(sql).toContain("needs_changes");
204
- });
205
-
206
- test("blocked_tasks query returns valid SQL string", () => {
207
- const sql = presetQueries.blocked_tasks;
208
-
209
- expect(typeof sql).toBe("string");
210
- expect(sql).toContain("SELECT");
211
- expect(sql.toLowerCase()).toContain("where");
212
- expect(sql.toLowerCase()).toContain("status");
213
- });
214
-
215
- test("agent_activity query returns valid SQL string", () => {
216
- const sql = presetQueries.agent_activity;
217
-
218
- expect(typeof sql).toBe("string");
219
- expect(sql).toContain("SELECT");
220
- expect(sql.toLowerCase()).toContain("count");
221
- expect(sql.toLowerCase()).toContain("group by");
222
- expect(sql.toLowerCase()).toContain("agent_name");
223
- });
224
-
225
- test("event_frequency query returns valid SQL string", () => {
226
- const sql = presetQueries.event_frequency;
227
-
228
- expect(typeof sql).toBe("string");
229
- expect(sql).toContain("SELECT");
230
- expect(sql.toLowerCase()).toContain("strftime");
231
- expect(sql.toLowerCase()).toContain("group by");
232
- });
233
-
234
- test("error_patterns query returns valid SQL string", () => {
235
- const sql = presetQueries.error_patterns;
236
-
237
- expect(typeof sql).toBe("string");
238
- expect(sql).toContain("SELECT");
239
- expect(sql.toLowerCase()).toContain("json_extract");
240
- expect(sql.toLowerCase()).toContain("group by");
241
- });
242
-
243
- test("compaction_stats query returns valid SQL string", () => {
244
- const sql = presetQueries.compaction_stats;
245
-
246
- expect(typeof sql).toBe("string");
247
- expect(sql).toContain("SELECT");
248
- expect(sql).toContain("compaction");
249
- expect(sql.toLowerCase()).toContain("avg");
250
- });
251
-
252
- test("all preset query names are valid PresetQueryName types", () => {
253
- const validNames: PresetQueryName[] = [
254
- "failed_decompositions",
255
- "duration_by_strategy",
256
- "file_conflicts",
257
- "worker_success_rate",
258
- "review_rejections",
259
- "blocked_tasks",
260
- "agent_activity",
261
- "event_frequency",
262
- "error_patterns",
263
- "compaction_stats",
264
- ];
265
-
266
- for (const name of validNames) {
267
- expect(presetQueries[name]).toBeDefined();
268
- expect(typeof presetQueries[name]).toBe("string");
269
- }
270
- });
271
- });
272
-
273
- describe("executeQuery - Custom SQL Execution", () => {
274
- test("executes custom SQL and returns QueryResult", async () => {
275
- const sql = "SELECT COUNT(*) as total FROM events";
276
- const result = await executeQuery(db, sql);
277
-
278
- expect(result).toHaveProperty("rows");
279
- expect(result).toHaveProperty("columns");
280
- expect(result).toHaveProperty("rowCount");
281
- expect(result).toHaveProperty("executionTimeMs");
282
- expect(Array.isArray(result.rows)).toBe(true);
283
- expect(result.rows.length).toBeGreaterThan(0);
284
- });
285
-
286
- test("executes parameterized query safely", async () => {
287
- const sql = "SELECT * FROM events WHERE type = ?";
288
- const result = await executeQuery(db, sql, ["subtask_outcome"]);
289
-
290
- expect(result.rows.length).toBe(2);
291
- expect(result.rowCount).toBe(2);
292
- });
293
-
294
- test("returns execution time in milliseconds", async () => {
295
- const sql = "SELECT * FROM events LIMIT 1";
296
- const result = await executeQuery(db, sql);
297
-
298
- expect(typeof result.executionTimeMs).toBe("number");
299
- expect(result.executionTimeMs).toBeGreaterThanOrEqual(0);
300
- });
301
-
302
- test("returns column names from query", async () => {
303
- const sql = "SELECT id, type, timestamp FROM events LIMIT 1";
304
- const result = await executeQuery(db, sql);
305
-
306
- expect(Array.isArray(result.columns)).toBe(true);
307
- expect(result.columns).toContain("id");
308
- expect(result.columns).toContain("type");
309
- expect(result.columns).toContain("timestamp");
310
- });
311
-
312
- test("handles empty result set", async () => {
313
- const sql = "SELECT * FROM events WHERE type = 'nonexistent'";
314
- const result = await executeQuery(db, sql);
315
-
316
- expect(result.rows).toEqual([]);
317
- expect(result.rowCount).toBe(0);
318
- });
319
-
320
- test("prevents SQL injection via parameterization", async () => {
321
- // Malicious input that would drop table if not parameterized
322
- const maliciousInput = "'; DROP TABLE events; --";
323
- const sql = "SELECT * FROM events WHERE type = ?";
324
-
325
- // Should treat as literal string, not execute SQL
326
- const result = await executeQuery(db, sql, [maliciousInput]);
327
-
328
- expect(result.rows).toEqual([]);
329
-
330
- // Verify table still exists by querying it
331
- const tableCheck = await executeQuery(db, "SELECT COUNT(*) as count FROM events");
332
- expect(tableCheck.rows[0].count).toBeGreaterThan(0);
333
- });
334
- });
335
-
336
- describe("formatAsTable - Box-Drawing Alignment", () => {
337
- test("formats result as aligned table with box-drawing characters", () => {
338
- const result: QueryResult = {
339
- columns: ["name", "count", "percent"],
340
- rows: [
341
- { name: "AgentA", count: 5, percent: 50.0 },
342
- { name: "AgentB", count: 3, percent: 30.0 },
343
- { name: "LongAgentName", count: 2, percent: 20.0 },
344
- ],
345
- rowCount: 3,
346
- executionTimeMs: 12.5,
347
- };
348
-
349
- const table = formatAsTable(result);
350
-
351
- expect(table).toContain("┌");
352
- expect(table).toContain("┐");
353
- expect(table).toContain("└");
354
- expect(table).toContain("┘");
355
- expect(table).toContain("│");
356
- expect(table).toContain("─");
357
- expect(table).toContain("name");
358
- expect(table).toContain("count");
359
- expect(table).toContain("percent");
360
- expect(table).toContain("AgentA");
361
- expect(table).toContain("LongAgentName");
362
- });
363
-
364
- test("aligns columns properly with varying widths", () => {
365
- const result: QueryResult = {
366
- columns: ["short", "verylongcolumnname"],
367
- rows: [
368
- { short: "a", verylongcolumnname: "value1" },
369
- { short: "bb", verylongcolumnname: "val" },
370
- ],
371
- rowCount: 2,
372
- executionTimeMs: 5,
373
- };
374
-
375
- const table = formatAsTable(result);
376
- const lines = table.split("\n");
377
-
378
- // All lines should have same width (proper alignment)
379
- const widths = lines.map((line) => line.length);
380
- const uniqueWidths = new Set(widths.filter((w) => w > 0));
381
- expect(uniqueWidths.size).toBe(1); // All non-empty lines same width
382
- });
383
-
384
- test("handles empty result set gracefully", () => {
385
- const result: QueryResult = {
386
- columns: ["name", "count"],
387
- rows: [],
388
- rowCount: 0,
389
- executionTimeMs: 1,
390
- };
391
-
392
- const table = formatAsTable(result);
393
-
394
- expect(table).toContain("name");
395
- expect(table).toContain("count");
396
- expect(table).toContain("0 rows"); // Should indicate no results
397
- });
398
-
399
- test("handles null and undefined values", () => {
400
- const result: QueryResult = {
401
- columns: ["name", "value"],
402
- rows: [
403
- { name: "test", value: null },
404
- { name: "another", value: undefined },
405
- ],
406
- rowCount: 2,
407
- executionTimeMs: 2,
408
- };
409
-
410
- const table = formatAsTable(result);
411
-
412
- expect(table).toContain("NULL"); // null values shown as NULL
413
- });
414
-
415
- test("includes execution time in footer", () => {
416
- const result: QueryResult = {
417
- columns: ["id"],
418
- rows: [{ id: 1 }],
419
- rowCount: 1,
420
- executionTimeMs: 42.123,
421
- };
422
-
423
- const table = formatAsTable(result);
424
-
425
- expect(table).toContain("42.1"); // Should show execution time
426
- expect(table).toContain("ms");
427
- });
428
- });
429
-
430
- describe("formatAsCSV - Proper Escaping", () => {
431
- test("formats result as CSV with headers", () => {
432
- const result: QueryResult = {
433
- columns: ["name", "count"],
434
- rows: [
435
- { name: "AgentA", count: 5 },
436
- { name: "AgentB", count: 3 },
437
- ],
438
- rowCount: 2,
439
- executionTimeMs: 5,
440
- };
441
-
442
- const csv = formatAsCSV(result);
443
-
444
- expect(csv).toContain("name,count");
445
- expect(csv).toContain("AgentA,5");
446
- expect(csv).toContain("AgentB,3");
447
- });
448
-
449
- test("escapes fields containing commas", () => {
450
- const result: QueryResult = {
451
- columns: ["name", "description"],
452
- rows: [{ name: "Test", description: "Has, a comma" }],
453
- rowCount: 1,
454
- executionTimeMs: 1,
455
- };
456
-
457
- const csv = formatAsCSV(result);
458
-
459
- expect(csv).toContain('"Has, a comma"');
460
- });
461
-
462
- test("escapes fields containing quotes", () => {
463
- const result: QueryResult = {
464
- columns: ["name", "message"],
465
- rows: [{ name: "Test", message: 'She said "hello"' }],
466
- rowCount: 1,
467
- executionTimeMs: 1,
468
- };
469
-
470
- const csv = formatAsCSV(result);
471
-
472
- // Quotes should be doubled and field wrapped
473
- expect(csv).toContain('She said ""hello""');
474
- });
475
-
476
- test("escapes fields containing newlines", () => {
477
- const result: QueryResult = {
478
- columns: ["name", "text"],
479
- rows: [{ name: "Test", text: "Line1\nLine2" }],
480
- rowCount: 1,
481
- executionTimeMs: 1,
482
- };
483
-
484
- const csv = formatAsCSV(result);
485
-
486
- expect(csv).toContain('"Line1\nLine2"');
487
- });
488
-
489
- test("handles null and undefined values as empty strings", () => {
490
- const result: QueryResult = {
491
- columns: ["name", "value"],
492
- rows: [
493
- { name: "test1", value: null },
494
- { name: "test2", value: undefined },
495
- ],
496
- rowCount: 2,
497
- executionTimeMs: 1,
498
- };
499
-
500
- const csv = formatAsCSV(result);
501
- const lines = csv.split("\n");
502
-
503
- expect(lines[1]).toContain("test1,");
504
- expect(lines[2]).toContain("test2,");
505
- });
506
-
507
- test("handles empty result set", () => {
508
- const result: QueryResult = {
509
- columns: ["name", "count"],
510
- rows: [],
511
- rowCount: 0,
512
- executionTimeMs: 1,
513
- };
514
-
515
- const csv = formatAsCSV(result);
516
-
517
- expect(csv).toBe("name,count"); // Headers only, no rows
518
- });
519
- });
520
-
521
- describe("formatAsJSON - Valid JSON Array", () => {
522
- test("formats result as valid JSON array", () => {
523
- const result: QueryResult = {
524
- columns: ["name", "count"],
525
- rows: [
526
- { name: "AgentA", count: 5 },
527
- { name: "AgentB", count: 3 },
528
- ],
529
- rowCount: 2,
530
- executionTimeMs: 5,
531
- };
532
-
533
- const json = formatAsJSON(result);
534
-
535
- // Should be parseable
536
- const parsed = JSON.parse(json);
537
- expect(Array.isArray(parsed)).toBe(true);
538
- expect(parsed.length).toBe(2);
539
- expect(parsed[0]).toEqual({ name: "AgentA", count: 5 });
540
- expect(parsed[1]).toEqual({ name: "AgentB", count: 3 });
541
- });
542
-
543
- test("handles empty result set as empty array", () => {
544
- const result: QueryResult = {
545
- columns: ["name"],
546
- rows: [],
547
- rowCount: 0,
548
- executionTimeMs: 1,
549
- };
550
-
551
- const json = formatAsJSON(result);
552
- const parsed = JSON.parse(json);
553
-
554
- expect(parsed).toEqual([]);
555
- });
556
-
557
- test("preserves null values correctly", () => {
558
- const result: QueryResult = {
559
- columns: ["name", "value"],
560
- rows: [{ name: "test", value: null }],
561
- rowCount: 1,
562
- executionTimeMs: 1,
563
- };
564
-
565
- const json = formatAsJSON(result);
566
- const parsed = JSON.parse(json);
567
-
568
- expect(parsed[0].value).toBeNull();
569
- });
570
-
571
- test("handles nested objects in rows", () => {
572
- const result: QueryResult = {
573
- columns: ["id", "data"],
574
- rows: [{ id: 1, data: { nested: "value", count: 42 } }],
575
- rowCount: 1,
576
- executionTimeMs: 1,
577
- };
578
-
579
- const json = formatAsJSON(result);
580
- const parsed = JSON.parse(json);
581
-
582
- expect(parsed[0].data).toEqual({ nested: "value", count: 42 });
583
- });
584
-
585
- test("pretty prints with indentation", () => {
586
- const result: QueryResult = {
587
- columns: ["name"],
588
- rows: [{ name: "test" }],
589
- rowCount: 1,
590
- executionTimeMs: 1,
591
- };
592
-
593
- const json = formatAsJSON(result);
594
-
595
- // Should have newlines and indentation
596
- expect(json).toContain("\n");
597
- expect(json).toContain(" "); // 2-space indent
598
- });
599
- });
600
-
601
- describe("Type Safety", () => {
602
- test("PresetQueryName type includes all 10 queries", () => {
603
- // This is a compile-time check, but we can verify at runtime
604
- const names: PresetQueryName[] = [
605
- "failed_decompositions",
606
- "duration_by_strategy",
607
- "file_conflicts",
608
- "worker_success_rate",
609
- "review_rejections",
610
- "blocked_tasks",
611
- "agent_activity",
612
- "event_frequency",
613
- "error_patterns",
614
- "compaction_stats",
615
- ];
616
-
617
- // TypeScript should accept all these without error
618
- expect(names.length).toBe(10);
619
- });
620
-
621
- test("QueryResult type has required fields", () => {
622
- const result: QueryResult = {
623
- columns: ["test"],
624
- rows: [{ test: "value" }],
625
- rowCount: 1,
626
- executionTimeMs: 5,
627
- };
628
-
629
- // Should compile without type errors
630
- expect(result.columns).toBeDefined();
631
- expect(result.rows).toBeDefined();
632
- expect(result.rowCount).toBeDefined();
633
- expect(result.executionTimeMs).toBeDefined();
634
- });
635
- });
636
- });