opencode-swarm-plugin 0.42.8 → 0.43.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/.hive/issues.jsonl +14 -0
- package/.turbo/turbo-build.log +2 -2
- package/CHANGELOG.md +140 -0
- package/README.md +296 -6
- package/bin/swarm.test.ts +615 -0
- package/bin/swarm.ts +434 -0
- package/dist/dashboard.d.ts +83 -0
- package/dist/dashboard.d.ts.map +1 -0
- package/dist/error-enrichment.d.ts +49 -0
- package/dist/error-enrichment.d.ts.map +1 -0
- package/dist/export-tools.d.ts +76 -0
- package/dist/export-tools.d.ts.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/observability-tools.d.ts +2 -2
- package/dist/query-tools.d.ts +59 -0
- package/dist/query-tools.d.ts.map +1 -0
- package/dist/replay-tools.d.ts +28 -0
- package/dist/replay-tools.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/dashboard.test.ts +611 -0
- package/src/dashboard.ts +462 -0
- package/src/error-enrichment.test.ts +403 -0
- package/src/error-enrichment.ts +219 -0
- package/src/export-tools.test.ts +476 -0
- package/src/export-tools.ts +257 -0
- package/src/query-tools.test.ts +636 -0
- package/src/query-tools.ts +324 -0
- package/src/replay-tools.test.ts +496 -0
- package/src/replay-tools.ts +240 -0
|
@@ -0,0 +1,636 @@
|
|
|
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
|
+
});
|