-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.mjs
More file actions
696 lines (569 loc) · 26.5 KB
/
Copy pathtest-api.mjs
File metadata and controls
696 lines (569 loc) · 26.5 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/**
* Logical + functional API test suite
* Tests real flows, data integrity, validation, and cross-entity relationships.
* Usage: node test-api.mjs
*/
const BASE = 'http://localhost:3000';
const PROJECT_ID = '20f28ffb-407c-4126-8967-1b218cd73108';
const P = `/api/v1/projects/${PROJECT_ID}`;
// ─── runner ──────────────────────────────────────────────────────────────────
let passed = 0, failed = 0;
const failures = [];
let currentSection = '';
function section(name) {
currentSection = name;
console.log(`\n── ${name} ${'─'.repeat(Math.max(0, 52 - name.length))}`);
}
async function test(name, fn) {
try {
await fn();
console.log(` ✓ ${name}`);
passed++;
} catch (err) {
console.log(` ✗ ${name}`);
console.log(` ${err.message}`);
failed++;
failures.push({ section: currentSection, name, error: err.message });
}
}
function assert(cond, msg) { if (!cond) throw new Error(msg); }
function assertEq(a, b, label) { assert(a === b, `${label}: expected ${JSON.stringify(b)}, got ${JSON.stringify(a)}`); }
async function req(method, path, body) {
const opts = { method, headers: { 'Content-Type': 'application/json' } };
if (body !== undefined) opts.body = JSON.stringify(body);
const res = await fetch(`${BASE}${path}`, opts);
const json = await res.json().catch(() => ({}));
return { status: res.status, ...json };
}
// ─── 1. auth context ─────────────────────────────────────────────────────────
section('Auth Context');
let ME;
await test('/me returns valid user context', async () => {
const r = await req('GET', '/api/v1/me');
assertEq(r.error, null, 'error');
assert(r.data?.userId, 'missing userId');
assert(r.data?.tenantId, 'missing tenantId');
assert(r.data?.userEmail, 'missing userEmail');
ME = r.data;
});
await test('user belongs to expected tenant', async () => {
assert(ME, 'ME not loaded');
assertEq(ME.tenantId, 'c8596765-a759-4872-ac54-80ba3a3fa20c', 'tenantId mismatch');
});
// ─── 2. projects ─────────────────────────────────────────────────────────────
section('Projects');
await test('GET /projects returns array with at least the test project', async () => {
const r = await req('GET', '/api/v1/projects');
assert(Array.isArray(r.data), 'expected array');
assert(r.data.some(p => p.id === PROJECT_ID), 'test project not in list');
});
// ─── 3. plan sets lifecycle ───────────────────────────────────────────────────
section('Plan Sets — Lifecycle');
let planSetId;
await test('GET returns empty or existing list', async () => {
const r = await req('GET', `${P}/plan-sets`);
assert(Array.isArray(r.data), 'expected array');
assertEq(r.error, null, 'error');
});
await test('POST rejects missing file', async () => {
const r = await req('POST', `${P}/plan-sets`, {});
// Should fail — no multipart data
assert(r.status >= 400, `expected 4xx, got ${r.status}`);
});
await test('POST rejects non-PDF file', async () => {
const form = new FormData();
form.append('file', new Blob(['not a pdf'], { type: 'text/plain' }), 'doc.txt');
const res = await fetch(`${BASE}${P}/plan-sets`, { method: 'POST', body: form });
assertEq(res.status, 400, 'status');
});
await test('POST creates plan set from PDF', async () => {
const minPdf = '%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 612 792]>>endobj\nxref\n0 4\n0000000000 65535 f\n0000000009 00000 n\n0000000058 00000 n\n0000000115 00000 n\ntrailer<</Size 4/Root 1 0 R>>\nstartxref\n190\n%%EOF';
const form = new FormData();
form.append('file', new Blob([minPdf], { type: 'application/pdf' }), 'floorplan.pdf');
form.append('name', 'Test Plan Set');
const res = await fetch(`${BASE}${P}/plan-sets`, { method: 'POST', body: form });
const r = await res.json();
assertEq(res.status, 200, 'status');
assert(r.data?.planSetId, 'missing planSetId');
assertEq(r.data?.status, 'processing', 'expected processing status');
planSetId = r.data.planSetId;
});
await test('plan set appears in list after creation', async () => {
const r = await req('GET', `${P}/plan-sets`);
assert(r.data.some(ps => ps.id === planSetId), 'plan set not found in list');
});
await test('PATCH renames plan set', async () => {
assert(planSetId, 'skipped');
const r = await req('PATCH', `${P}/plan-sets`, { planSetId, name: 'Renamed Plan Set' });
assertEq(r.data?.name, 'Renamed Plan Set', 'name');
assertEq(r.error, null, 'error');
});
await test('PATCH rejects rename with empty name', async () => {
assert(planSetId, 'skipped');
const r = await req('PATCH', `${P}/plan-sets`, { planSetId, name: '' });
assert(r.status >= 400, `expected 4xx, got ${r.status}`);
});
await test('PATCH rejects missing planSetId', async () => {
const r = await req('PATCH', `${P}/plan-sets`, { name: 'No ID' });
assert(r.status >= 400, `expected 4xx, got ${r.status}`);
});
// ─── 4. sheets ───────────────────────────────────────────────────────────────
section('Sheets');
await test('GET /sheets returns array', async () => {
const r = await req('GET', `${P}/sheets`);
assert(Array.isArray(r.data), 'expected array');
assertEq(r.error, null, 'error');
});
await test('GET /sheets?showArchived=true includes archived', async () => {
const r = await req('GET', `${P}/sheets?showArchived=true`);
assert(Array.isArray(r.data), 'expected array');
});
await test('GET /sheets default excludes archived', async () => {
const withArchived = await req('GET', `${P}/sheets?showArchived=true`);
const withoutArchived = await req('GET', `${P}/sheets`);
// Non-archived count should be <= total
assert(withoutArchived.data.length <= withArchived.data.length, 'archived filter broken');
});
// ─── 5. tasks — full lifecycle ────────────────────────────────────────────────
section('Tasks — Full Lifecycle');
let taskId, commentId, replyId;
await test('GET /tasks returns array', async () => {
const r = await req('GET', `${P}/tasks`);
assert(Array.isArray(r.data), 'expected array');
});
await test('POST rejects missing title', async () => {
const r = await req('POST', `${P}/tasks`, { status: 'open' });
assert(r.status >= 400, `expected 4xx, got ${r.status}`);
});
await test('POST creates task with all fields', async () => {
const r = await req('POST', `${P}/tasks`, {
title: 'Fix north wall crack',
description: 'Hairline fracture at grid B-4',
priority: 'high',
status: 'open',
due_date: '2026-06-01',
checklist: [
{ text: 'Inspect crack', completed: false, order_index: 0 },
{ text: 'Apply filler', completed: false, order_index: 1 },
],
});
assert(r.data?.id, `expected id: ${JSON.stringify(r).slice(0,200)}`);
assertEq(r.data.title, 'Fix north wall crack', 'title');
assertEq(r.data.priority, 'high', 'priority');
assertEq(r.data.status, 'open', 'status');
assert(Array.isArray(r.data.checklist), 'checklist should be array');
assertEq(r.data.checklist.length, 2, 'checklist length');
taskId = r.data.id;
});
await test('new task appears in GET /tasks list', async () => {
assert(taskId, 'skipped');
const r = await req('GET', `${P}/tasks`);
assert(r.data.some(t => t.id === taskId), 'task not in list');
});
await test('GET /tasks filters by status=open', async () => {
const r = await req('GET', `${P}/tasks?status=open`);
assert(r.data.every(t => t.status === 'open'), 'non-open task in filtered result');
});
await test('GET /tasks filters by priority=high', async () => {
const r = await req('GET', `${P}/tasks?priority=high`);
assert(r.data.every(t => t.priority === 'high'), 'wrong priority in filtered result');
});
await test('GET /tasks search finds by title', async () => {
const r = await req('GET', `${P}/tasks?search=north+wall`);
assert(r.data.some(t => t.id === taskId), 'task not found by search');
});
await test('PATCH updates task title and status', async () => {
assert(taskId, 'skipped');
const r = await req('PATCH', `${P}/tasks`, {
id: taskId,
title: 'Fix north wall crack (updated)',
status: 'closed',
});
assertEq(r.data?.title, 'Fix north wall crack (updated)', 'title');
assertEq(r.data?.status, 'closed', 'status');
});
await test('PATCH rejects missing id', async () => {
const r = await req('PATCH', `${P}/tasks`, { title: 'No ID' });
assertEq(r.status, 400, 'status');
});
await test('PATCH update is persisted — re-fetch confirms', async () => {
assert(taskId, 'skipped');
const r = await req('GET', `${P}/tasks`);
const task = r.data.find(t => t.id === taskId);
assert(task, 'task not found');
assertEq(task.status, 'closed', 'persisted status');
});
// comments
await test('GET comments returns empty array initially', async () => {
assert(taskId, 'skipped');
const r = await req('GET', `${P}/tasks/${taskId}/comments`);
assert(Array.isArray(r.data), 'expected array');
});
await test('POST comment rejects empty body', async () => {
assert(taskId, 'skipped');
const r = await req('POST', `${P}/tasks/${taskId}/comments`, { body: '' });
assertEq(r.status, 400, 'status');
});
await test('POST creates comment', async () => {
assert(taskId, 'skipped');
const r = await req('POST', `${P}/tasks/${taskId}/comments`, {
body: 'Crack confirmed — needs epoxy injection',
});
assert(r.data?.id, `expected id: ${JSON.stringify(r).slice(0,200)}`);
assertEq(r.data.body, 'Crack confirmed — needs epoxy injection', 'body');
commentId = r.data.id;
});
await test('POST creates reply to comment', async () => {
assert(commentId, 'skipped');
const r = await req('POST', `${P}/tasks/${taskId}/comments`, {
body: 'Agreed, scheduling for next week',
parent_comment_id: commentId,
});
assert(r.data?.id, 'expected id');
replyId = r.data.id;
});
await test('GET comments returns both comment and reply', async () => {
assert(commentId && replyId, 'skipped');
const r = await req('GET', `${P}/tasks/${taskId}/comments`);
const flat = r.data.flatMap(c => [c, ...(c.replies || [])]);
assert(flat.some(c => c.id === commentId), 'top comment missing');
assert(flat.some(c => c.id === replyId), 'reply missing');
});
await test('PATCH edits comment body', async () => {
assert(commentId, 'skipped');
const r = await req('PATCH', `${P}/tasks/${taskId}/comments?commentId=${commentId}`, {
body: 'Crack confirmed — epoxy injection scheduled',
});
assertEq(r.data?.body, 'Crack confirmed — epoxy injection scheduled', 'body');
});
await test('DELETE removes comment', async () => {
assert(replyId, 'skipped');
const r = await req('DELETE', `${P}/tasks/${taskId}/comments?commentId=${replyId}`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
// archive & restore via bulk action
await test('bulk-action archives task', async () => {
assert(taskId, 'skipped');
const r = await req('POST', `${P}/bulk-action`, {
entity_type: 'task',
entity_ids: [taskId],
action: 'archive',
});
// success is an array of IDs
assert(r.data?.success?.length >= 1, `expected success array: ${JSON.stringify(r)}`);
});
await test('archived task excluded from default GET', async () => {
assert(taskId, 'skipped');
const r = await req('GET', `${P}/tasks`);
assert(!r.data.some(t => t.id === taskId), 'archived task still visible');
});
await test('archived task visible with include_archived=true', async () => {
assert(taskId, 'skipped');
const r = await req('GET', `${P}/tasks?include_archived=true`);
assert(r.data.some(t => t.id === taskId), 'archived task not found with flag');
});
await test('bulk-action restores task', async () => {
assert(taskId, 'skipped');
const r = await req('POST', `${P}/bulk-action`, {
entity_type: 'task',
entity_ids: [taskId],
action: 'restore',
});
assert(r.data?.success?.length >= 1, `expected success array: ${JSON.stringify(r)}`);
});
await test('restored task visible in default GET', async () => {
assert(taskId, 'skipped');
const r = await req('GET', `${P}/tasks`);
assert(r.data.some(t => t.id === taskId), 'restored task not visible');
});
await test('DELETE permanently removes task', async () => {
assert(taskId, 'skipped');
const r = await req('DELETE', `${P}/tasks?id=${taskId}&mode=permanent`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
await test('permanently deleted task gone even with include_archived', async () => {
assert(taskId, 'skipped');
const r = await req('GET', `${P}/tasks?include_archived=true`);
assert(!r.data.some(t => t.id === taskId), 'permanently deleted task still present');
});
// ─── 6. bulk-action validation ────────────────────────────────────────────────
section('Bulk Actions — Validation');
await test('rejects unknown entity_type', async () => {
const r = await req('POST', `${P}/bulk-action`, {
entity_type: 'invoice',
entity_ids: ['00000000-0000-0000-0000-000000000001'],
action: 'archive',
});
assertEq(r.status, 400, 'status');
});
await test('rejects unknown action', async () => {
const r = await req('POST', `${P}/bulk-action`, {
entity_type: 'task',
entity_ids: ['00000000-0000-0000-0000-000000000001'],
action: 'nuke',
});
assertEq(r.status, 400, 'status');
});
await test('rejects empty entity_ids', async () => {
const r = await req('POST', `${P}/bulk-action`, {
entity_type: 'task',
entity_ids: [],
action: 'archive',
});
assertEq(r.status, 400, 'status');
});
// ─── 7. templates lifecycle ───────────────────────────────────────────────────
section('Templates (Stamp) — Lifecycle');
let templateId;
await test('GET /templates returns array', async () => {
const r = await req('GET', `${P}/templates`);
assert(Array.isArray(r.data), 'expected array');
});
await test('POST rejects missing stamp_code', async () => {
const r = await req('POST', `${P}/templates`, { display_name: 'No Code' });
assertEq(r.status, 400, 'status');
});
await test('POST rejects stamp_code longer than 2 chars', async () => {
const r = await req('POST', `${P}/templates`, { stamp_code: 'XYZ', display_name: 'Too Long' });
assertEq(r.status, 400, 'status');
});
await test('POST creates template', async () => {
const r = await req('POST', `${P}/templates`, {
stamp_code: 'QA',
display_name: 'QA Review',
color: '#FF6B00',
});
assert(r.data?.id, `expected id: ${JSON.stringify(r).slice(0,200)}`);
assertEq(r.data.stamp_code, 'QA', 'stamp_code');
assertEq(r.data.display_name, 'QA Review', 'display_name');
templateId = r.data.id;
});
await test('new template appears in GET list', async () => {
assert(templateId, 'skipped');
const r = await req('GET', `${P}/templates`);
assert(r.data.some(t => t.id === templateId), 'template not in list');
});
await test('DELETE removes template', async () => {
assert(templateId, 'skipped');
const r = await req('DELETE', `${P}/templates/${templateId}`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
await test('deleted template absent from GET list', async () => {
assert(templateId, 'skipped');
const r = await req('GET', `${P}/templates`);
assert(!r.data.some(t => t.id === templateId), 'deleted template still in list');
});
// ─── 8. saved views lifecycle ─────────────────────────────────────────────────
section('Saved Views — Lifecycle');
let viewId;
await test('GET /saved-views returns array', async () => {
const r = await req('GET', `${P}/saved-views`);
assert(Array.isArray(r.data), 'expected array');
});
await test('POST rejects missing name', async () => {
const r = await req('POST', `${P}/saved-views`, { filters: { status: 'open' } });
assertEq(r.status, 400, 'status');
});
await test('POST rejects missing filters', async () => {
const r = await req('POST', `${P}/saved-views`, { name: 'My View' });
assertEq(r.status, 400, 'status');
});
await test('POST creates saved view', async () => {
const r = await req('POST', `${P}/saved-views`, {
name: 'Open High-Priority Tasks',
filters: { status: 'open', priority: 'high' },
});
assert(r.data?.id, `expected id: ${JSON.stringify(r).slice(0,200)}`);
assertEq(r.data.name, 'Open High-Priority Tasks', 'name');
assert(r.data.filters?.status === 'open', 'filters.status');
viewId = r.data.id;
});
await test('PATCH renames saved view and updates filters', async () => {
assert(viewId, 'skipped');
const r = await req('PATCH', `${P}/saved-views`, {
id: viewId,
name: 'Open High-Priority Tasks (Revised)',
filters: { status: 'open', priority: 'high', sort_by: 'due_date' },
});
assertEq(r.data?.name, 'Open High-Priority Tasks (Revised)', 'name');
assert(r.data?.filters?.sort_by === 'due_date', 'filters.sort_by');
});
await test('DELETE removes saved view', async () => {
assert(viewId, 'skipped');
const r = await req('DELETE', `${P}/saved-views?id=${viewId}`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
// ─── 9. linked objects lifecycle ──────────────────────────────────────────────
section('Linked Objects — Lifecycle');
let linkId, linkedTaskId;
await test('setup: create a task to link against', async () => {
const r = await req('POST', `${P}/tasks`, { title: 'Linked Object Test Task' });
assert(r.data?.id, 'expected id');
linkedTaskId = r.data.id;
});
await test('GET rejects without source or linked params', async () => {
const r = await req('GET', `${P}/linked-objects`);
assertEq(r.status, 400, 'status');
});
await test('POST rejects missing required fields', async () => {
const r = await req('POST', `${P}/linked-objects`, {
source_type: 'task',
source_id: linkedTaskId,
// missing linked_type and linked_id
});
assertEq(r.status, 400, 'status');
});
await test('POST creates a linked object', async () => {
assert(linkedTaskId, 'skipped');
const r = await req('POST', `${P}/linked-objects`, {
source_type: 'task',
source_id: linkedTaskId,
linked_type: 'rfi',
linked_id: 'RFI-0042',
linked_title: 'RFI for north wall repair method',
linked_url: 'https://example.com/rfi/42',
});
assert(r.data?.id, `expected id: ${JSON.stringify(r).slice(0,200)}`);
assertEq(r.data.linked_type, 'rfi', 'linked_type');
assertEq(r.data.linked_id, 'RFI-0042', 'linked_id');
linkId = r.data.id;
});
await test('GET finds link by source', async () => {
assert(linkId && linkedTaskId, 'skipped');
const r = await req('GET', `${P}/linked-objects?source_type=task&source_id=${linkedTaskId}`);
assert(Array.isArray(r.data), 'expected array');
assert(r.data.some(l => l.id === linkId), 'link not found by source');
});
await test('GET finds link by linked entity (reverse)', async () => {
assert(linkId, 'skipped');
const r = await req('GET', `${P}/linked-objects?linked_type=rfi&linked_id=RFI-0042`);
assert(r.data.some(l => l.id === linkId), 'link not found by reverse lookup');
});
await test('PATCH updates linked object title', async () => {
assert(linkId, 'skipped');
const r = await req('PATCH', `${P}/linked-objects/${linkId}`, {
linked_title: 'RFI-0042 (Revised)',
});
assertEq(r.data?.linked_title, 'RFI-0042 (Revised)', 'linked_title');
});
await test('DELETE removes linked object', async () => {
assert(linkId, 'skipped');
const r = await req('DELETE', `${P}/linked-objects/${linkId}`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
await test('link absent after deletion', async () => {
assert(linkId && linkedTaskId, 'skipped');
const r = await req('GET', `${P}/linked-objects?source_type=task&source_id=${linkedTaskId}`);
assert(!r.data.some(l => l.id === linkId), 'deleted link still present');
});
await test('cleanup: delete linked-object test task', async () => {
assert(linkedTaskId, 'skipped');
const r = await req('DELETE', `${P}/tasks?id=${linkedTaskId}&mode=permanent`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
// ─── 10. members ──────────────────────────────────────────────────────────────
section('Members');
await test('GET /members returns array with at least one member', async () => {
const r = await req('GET', `${P}/members`);
assert(Array.isArray(r.data), 'expected array');
assert(r.data.length >= 1, 'expected at least current user as member');
});
await test('each member has required fields', async () => {
const r = await req('GET', `${P}/members`);
for (const m of r.data) {
assert(m.user_id, `member missing user_id: ${JSON.stringify(m)}`);
assert(m.role_template_id, `member missing role_template_id: ${JSON.stringify(m)}`);
}
});
await test('POST rejects missing required fields', async () => {
const r = await req('POST', `${P}/members`, { user_name: 'Missing ID and Role' });
assertEq(r.status, 400, 'status');
});
// ─── 11. roles ────────────────────────────────────────────────────────────────
section('Roles');
let roleId;
await test('GET /roles returns array with permissions', async () => {
const r = await req('GET', '/api/v1/roles');
assert(Array.isArray(r.data), 'expected array');
assert(r.data.length >= 1, 'expected at least one role');
const firstRole = r.data[0];
assert(firstRole.id, 'missing id');
assert(firstRole.permissions, 'missing permissions');
roleId = firstRole.id;
});
// ─── 12. reports ──────────────────────────────────────────────────────────────
section('Reports');
await test('GET /reports returns array', async () => {
const r = await req('GET', `${P}/reports`);
assert(Array.isArray(r.data), 'expected array');
assertEq(r.error, null, 'error');
});
await test('GET /reports?range=30 filters correctly', async () => {
const r = await req('GET', `${P}/reports?range=30`);
assert(Array.isArray(r.data), 'expected array');
});
await test('GET /scheduled-reports returns array', async () => {
const r = await req('GET', `${P}/scheduled-reports`);
assert(Array.isArray(r.data), 'expected array');
assertEq(r.error, null, 'error');
});
// ─── 13. archive endpoint ─────────────────────────────────────────────────────
section('Archive Endpoint');
await test('GET /archive returns object with annotations and tasks', async () => {
const r = await req('GET', `${P}/archive`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
assert(r.data !== null, 'data should not be null');
});
await test('GET /archive?type=tasks returns tasks only', async () => {
const r = await req('GET', `${P}/archive?type=tasks`);
assertEq(r.error, null, 'error');
assert('tasks' in (r.data || {}), 'missing tasks key');
});
await test('GET /archive?type=annotations returns annotations only', async () => {
const r = await req('GET', `${P}/archive?type=annotations`);
assertEq(r.error, null, 'error');
assert('annotations' in (r.data || {}), 'missing annotations key');
});
// ─── 14. activity ─────────────────────────────────────────────────────────────
section('Activity Log');
await test('GET /activity returns entries array and total', async () => {
const r = await req('GET', `${P}/activity`);
assertEq(r.error, null, 'error');
assert(Array.isArray(r.data?.entries), 'expected data.entries array');
assert(typeof r.data?.total === 'number', 'expected data.total number');
});
await test('GET /activity?limit=5 respects limit', async () => {
const r = await req('GET', `${P}/activity?limit=5`);
assert(r.data?.entries.length <= 5, `expected max 5, got ${r.data?.entries.length}`);
});
// ─── 15. misc endpoints ───────────────────────────────────────────────────────
section('Misc Endpoints');
await test('GET /trades returns array', async () => {
const r = await req('GET', '/api/v1/trades');
assert(Array.isArray(r.data), 'expected array');
});
await test('GET /projects/:id/audit-log returns data', async () => {
const r = await req('GET', `${P}/audit-log`);
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
// ─── 16. plan set cleanup ─────────────────────────────────────────────────────
section('Cleanup');
await test('DELETE plan set created during tests', async () => {
if (!planSetId) { console.log(' - skipped (no planSetId)'); return; }
const r = await req('DELETE', `${P}/plan-sets`, { planSetId });
assertEq(r.error, null, `error: ${JSON.stringify(r)}`);
});
await test('deleted plan set absent from list', async () => {
if (!planSetId) return;
const r = await req('GET', `${P}/plan-sets`);
assert(!r.data.some(ps => ps.id === planSetId), 'deleted plan set still present');
});
// ─── summary ──────────────────────────────────────────────────────────────────
console.log('\n' + '─'.repeat(56));
console.log(` ${passed} passed | ${failed} failed | ${passed + failed} total`);
if (failures.length) {
console.log('\nFailed:');
failures.forEach(f => console.log(` [${f.section}] ${f.name}\n → ${f.error}`));
}
console.log('');
process.exit(failed > 0 ? 1 : 0);