qualia-framework 3.2.0 → 3.3.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.
Files changed (58) hide show
  1. package/CLAUDE.md +3 -4
  2. package/README.md +59 -23
  3. package/agents/plan-checker.md +158 -0
  4. package/agents/planner.md +52 -0
  5. package/agents/research-synthesizer.md +86 -0
  6. package/agents/researcher.md +119 -0
  7. package/agents/roadmapper.md +157 -0
  8. package/agents/verifier.md +180 -32
  9. package/bin/cli.js +403 -9
  10. package/bin/install.js +219 -70
  11. package/bin/qualia-ui.js +11 -11
  12. package/bin/state.js +200 -6
  13. package/bin/statusline.js +4 -4
  14. package/docs/erp-contract.md +161 -0
  15. package/hooks/branch-guard.js +23 -2
  16. package/hooks/migration-guard.js +23 -0
  17. package/hooks/pre-compact.js +20 -0
  18. package/hooks/pre-deploy-gate.js +39 -0
  19. package/hooks/pre-push.js +20 -0
  20. package/hooks/session-start.js +16 -43
  21. package/package.json +6 -4
  22. package/references/questioning.md +123 -0
  23. package/rules/infrastructure.md +87 -0
  24. package/skills/qualia/SKILL.md +1 -0
  25. package/skills/qualia-build/SKILL.md +18 -0
  26. package/skills/qualia-design/SKILL.md +14 -8
  27. package/skills/qualia-discuss/SKILL.md +115 -0
  28. package/skills/qualia-help/SKILL.md +60 -0
  29. package/skills/qualia-learn/SKILL.md +27 -4
  30. package/skills/qualia-map/SKILL.md +145 -0
  31. package/skills/qualia-milestone/SKILL.md +148 -0
  32. package/skills/qualia-new/SKILL.md +374 -229
  33. package/skills/qualia-plan/SKILL.md +135 -30
  34. package/skills/qualia-polish/SKILL.md +167 -117
  35. package/skills/qualia-report/SKILL.md +17 -8
  36. package/skills/qualia-research/SKILL.md +124 -0
  37. package/skills/qualia-review/SKILL.md +126 -41
  38. package/skills/qualia-test/SKILL.md +134 -0
  39. package/skills/qualia-verify/SKILL.md +1 -1
  40. package/templates/DESIGN.md +440 -102
  41. package/templates/help.html +476 -0
  42. package/templates/phase-context.md +48 -0
  43. package/templates/plan.md +14 -0
  44. package/templates/projects/ai-agent.md +55 -0
  45. package/templates/projects/mobile-app.md +56 -0
  46. package/templates/projects/voice-agent.md +55 -0
  47. package/templates/projects/website.md +58 -0
  48. package/templates/requirements.md +69 -0
  49. package/templates/research-project/ARCHITECTURE.md +70 -0
  50. package/templates/research-project/FEATURES.md +60 -0
  51. package/templates/research-project/PITFALLS.md +73 -0
  52. package/templates/research-project/STACK.md +51 -0
  53. package/templates/research-project/SUMMARY.md +86 -0
  54. package/templates/roadmap.md +71 -0
  55. package/tests/bin.test.sh +20 -6
  56. package/tests/hooks.test.sh +76 -7
  57. package/tests/runner.js +1915 -0
  58. package/tests/state.test.sh +189 -11
@@ -0,0 +1,1915 @@
1
+ #!/usr/bin/env node
2
+ // Cross-platform test runner — works on Fedora, EndeavourOS, macOS, and Windows
3
+ // Uses node:test (built-in, no dependencies)
4
+
5
+ const { describe, it } = require("node:test");
6
+ const assert = require("node:assert/strict");
7
+ const { spawnSync } = require("child_process");
8
+ const path = require("path");
9
+ const fs = require("fs");
10
+ const os = require("os");
11
+
12
+ const ROOT = path.resolve(__dirname, "..");
13
+ const BIN = path.join(ROOT, "bin");
14
+ const HOOKS = path.join(ROOT, "hooks");
15
+
16
+ // Helper: run a bin/ script and return {stdout, stderr, status}
17
+ function run(script, args = [], opts = {}) {
18
+ const result = spawnSync(process.execPath, [path.join(BIN, script), ...args], {
19
+ encoding: "utf8",
20
+ timeout: 10000,
21
+ cwd: opts.cwd || ROOT,
22
+ env: { ...process.env, ...opts.env },
23
+ input: opts.input || undefined,
24
+ stdio: ["pipe", "pipe", "pipe"],
25
+ });
26
+ return { stdout: result.stdout || "", stderr: result.stderr || "", status: result.status };
27
+ }
28
+
29
+ // Helper: run a hook with JSON input on stdin
30
+ function runHook(hookFile, jsonInput) {
31
+ const hookPath = path.join(HOOKS, hookFile);
32
+ const result = spawnSync(process.execPath, [hookPath], {
33
+ encoding: "utf8",
34
+ timeout: 5000,
35
+ input: JSON.stringify(jsonInput),
36
+ env: { ...process.env, HOME: os.tmpdir(), USERPROFILE: os.tmpdir() },
37
+ stdio: ["pipe", "pipe", "pipe"],
38
+ });
39
+ return { stdout: result.stdout || "", stderr: result.stderr || "", status: result.status };
40
+ }
41
+
42
+ // Helper: run state.js with args in a given cwd
43
+ function runState(args, cwd) {
44
+ const result = spawnSync(process.execPath, [path.join(BIN, "state.js"), ...args], {
45
+ encoding: "utf8",
46
+ timeout: 5000,
47
+ cwd,
48
+ stdio: ["pipe", "pipe", "pipe"],
49
+ });
50
+ return { stdout: result.stdout || "", stderr: result.stderr || "", status: result.status };
51
+ }
52
+
53
+ // Helper: create temp directory with .planning
54
+ function withTempPlanning(fn) {
55
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-test-"));
56
+ const planningDir = path.join(tmpDir, ".planning");
57
+ fs.mkdirSync(planningDir, { recursive: true });
58
+ try {
59
+ fn(tmpDir, planningDir);
60
+ } finally {
61
+ fs.rmSync(tmpDir, { recursive: true, force: true });
62
+ }
63
+ }
64
+
65
+ // Helper: create a full temp project (init with 2 phases)
66
+ function makeProject() {
67
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-proj-"));
68
+ const r = spawnSync(process.execPath, [
69
+ path.join(BIN, "state.js"), "init",
70
+ "--project", "TestProject",
71
+ "--phases", '[{"name":"Foundation","goal":"Auth"},{"name":"Core","goal":"Features"}]',
72
+ ], {
73
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
74
+ });
75
+ if (r.status !== 0) {
76
+ throw new Error(`makeProject init failed: ${r.stdout} ${r.stderr}`);
77
+ }
78
+ return tmpDir;
79
+ }
80
+
81
+ // Helper: write a valid plan file
82
+ function makeValidPlan(dir, phase) {
83
+ phase = phase || 1;
84
+ const plan = `---
85
+ phase: ${phase}
86
+ goal: "Test goal"
87
+ tasks: 1
88
+ waves: 1
89
+ ---
90
+
91
+ # Phase ${phase}: Test
92
+
93
+ Goal: Test goal
94
+
95
+ ## Task 1 — Test task
96
+ **Wave:** 1
97
+ **Files:** src/test.ts
98
+ **Action:** Create test file
99
+ **Done when:** File exists
100
+
101
+ ## Success Criteria
102
+ - [ ] Test passes
103
+ `;
104
+ fs.writeFileSync(path.join(dir, ".planning", `phase-${phase}-plan.md`), plan);
105
+ }
106
+
107
+ // Helper: strip ANSI escape codes
108
+ function stripAnsi(str) {
109
+ return str.replace(/\x1b\[[0-9;]*m/g, "");
110
+ }
111
+
112
+ // Helper: get package version
113
+ const PKG_VERSION = JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf8")).version;
114
+
115
+ // ═══════════════════════════════════════════════════════════
116
+ // CLI Tests
117
+ // ═══════════════════════════════════════════════════════════
118
+
119
+ describe("CLI", () => {
120
+ it("no args shows help banner", () => {
121
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
122
+ try {
123
+ const r = run("cli.js", [], { env: { HOME: tmpHome, USERPROFILE: tmpHome } });
124
+ assert.equal(r.status, 0);
125
+ const clean = stripAnsi(r.stdout);
126
+ assert.match(clean, /Qualia Framework/);
127
+ assert.match(clean, /Commands:/);
128
+ } finally {
129
+ fs.rmSync(tmpHome, { recursive: true, force: true });
130
+ }
131
+ });
132
+
133
+ it("help mentions all commands", () => {
134
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
135
+ try {
136
+ const r = run("cli.js", ["help"], { env: { HOME: tmpHome, USERPROFILE: tmpHome } });
137
+ assert.equal(r.status, 0);
138
+ const clean = stripAnsi(r.stdout);
139
+ assert.match(clean, /install/);
140
+ assert.match(clean, /update/);
141
+ assert.match(clean, /version/);
142
+ assert.match(clean, /uninstall/);
143
+ assert.match(clean, /migrate/);
144
+ assert.match(clean, /team/);
145
+ assert.match(clean, /traces/);
146
+ assert.match(clean, /analytics/);
147
+ } finally {
148
+ fs.rmSync(tmpHome, { recursive: true, force: true });
149
+ }
150
+ });
151
+
152
+ it("shows version", () => {
153
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
154
+ try {
155
+ const r = run("cli.js", ["version"], {
156
+ env: { HOME: tmpHome, USERPROFILE: tmpHome, npm_config_registry: "http://127.0.0.1:1/" },
157
+ });
158
+ assert.equal(r.status, 0);
159
+ const clean = stripAnsi(r.stdout);
160
+ assert.match(clean, /Installed:/);
161
+ assert.match(clean, new RegExp(PKG_VERSION.replace(/\./g, "\\.")));
162
+ } finally {
163
+ fs.rmSync(tmpHome, { recursive: true, force: true });
164
+ }
165
+ });
166
+
167
+ it("-v is alias for version", () => {
168
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
169
+ try {
170
+ const r = run("cli.js", ["-v"], {
171
+ env: { HOME: tmpHome, USERPROFILE: tmpHome, npm_config_registry: "http://127.0.0.1:1/" },
172
+ });
173
+ assert.equal(r.status, 0);
174
+ const clean = stripAnsi(r.stdout);
175
+ assert.match(clean, /Installed:/);
176
+ } finally {
177
+ fs.rmSync(tmpHome, { recursive: true, force: true });
178
+ }
179
+ });
180
+
181
+ it("--version is alias for version", () => {
182
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
183
+ try {
184
+ const r = run("cli.js", ["--version"], {
185
+ env: { HOME: tmpHome, USERPROFILE: tmpHome, npm_config_registry: "http://127.0.0.1:1/" },
186
+ });
187
+ assert.equal(r.status, 0);
188
+ const clean = stripAnsi(r.stdout);
189
+ assert.match(clean, /Installed:/);
190
+ } finally {
191
+ fs.rmSync(tmpHome, { recursive: true, force: true });
192
+ }
193
+ });
194
+
195
+ it("unknown command falls through to help", () => {
196
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
197
+ try {
198
+ const r = run("cli.js", ["frobnicate"], { env: { HOME: tmpHome, USERPROFILE: tmpHome } });
199
+ assert.equal(r.status, 0);
200
+ assert.match(stripAnsi(r.stdout), /Qualia Framework/);
201
+ } finally {
202
+ fs.rmSync(tmpHome, { recursive: true, force: true });
203
+ }
204
+ });
205
+
206
+ it("team list works", () => {
207
+ const r = run("cli.js", ["team", "list"]);
208
+ assert.equal(r.status, 0);
209
+ assert.match(stripAnsi(r.stdout), /QS-FAWZI-01/);
210
+ });
211
+
212
+ it("traces handles missing traces dir", () => {
213
+ const r = run("cli.js", ["traces"]);
214
+ assert.equal(r.status, 0);
215
+ });
216
+
217
+ it("analytics handles missing traces dir", () => {
218
+ const r = run("cli.js", ["analytics"]);
219
+ assert.equal(r.status, 0);
220
+ });
221
+
222
+ it("version with config shows User line", () => {
223
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
224
+ try {
225
+ fs.mkdirSync(path.join(tmpHome, ".claude"), { recursive: true });
226
+ fs.writeFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), JSON.stringify({
227
+ code: "QS-FAWZI-01",
228
+ installed_by: "Fawzi Goussous",
229
+ role: "OWNER",
230
+ version: "2.8.1",
231
+ installed_at: "2026-04-10",
232
+ }));
233
+ const r = run("cli.js", ["version"], {
234
+ env: { HOME: tmpHome, USERPROFILE: tmpHome, npm_config_registry: "http://127.0.0.1:1/" },
235
+ });
236
+ assert.equal(r.status, 0);
237
+ const clean = stripAnsi(r.stdout);
238
+ assert.match(clean, /User:/);
239
+ assert.match(clean, /Fawzi Goussous/);
240
+ assert.match(clean, /OWNER/);
241
+ } finally {
242
+ fs.rmSync(tmpHome, { recursive: true, force: true });
243
+ }
244
+ });
245
+
246
+ it("update without config exits 1", () => {
247
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
248
+ try {
249
+ const r = run("cli.js", ["update"], { env: { HOME: tmpHome, USERPROFILE: tmpHome } });
250
+ assert.equal(r.status, 1);
251
+ assert.match(stripAnsi(r.stdout), /No install code saved/);
252
+ } finally {
253
+ fs.rmSync(tmpHome, { recursive: true, force: true });
254
+ }
255
+ });
256
+
257
+ it("upgrade alias behaves same as update", () => {
258
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-cli-"));
259
+ try {
260
+ const r = run("cli.js", ["upgrade"], { env: { HOME: tmpHome, USERPROFILE: tmpHome } });
261
+ assert.equal(r.status, 1);
262
+ assert.match(stripAnsi(r.stdout), /No install code saved/);
263
+ } finally {
264
+ fs.rmSync(tmpHome, { recursive: true, force: true });
265
+ }
266
+ });
267
+ });
268
+
269
+ // ═══════════════════════════════════════════════════════════
270
+ // State Machine Tests
271
+ // ═══════════════════════════════════════════════════════════
272
+
273
+ describe("State Machine", () => {
274
+ it("check fails without .planning directory", () => {
275
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-state-"));
276
+ try {
277
+ const r = runState(["check"], tmpDir);
278
+ assert.equal(r.status, 1);
279
+ const out = JSON.parse(r.stdout);
280
+ assert.equal(out.ok, false);
281
+ assert.equal(out.error, "NO_PROJECT");
282
+ } finally {
283
+ fs.rmSync(tmpDir, { recursive: true, force: true });
284
+ }
285
+ });
286
+
287
+ it("init creates state and tracking files", () => {
288
+ withTempPlanning((tmpDir) => {
289
+ const r = spawnSync(process.execPath, [
290
+ path.join(BIN, "state.js"), "init",
291
+ "--project", "test-proj",
292
+ "--phases", '[{"name":"Foundation","goal":"Auth"}]',
293
+ ], { encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"] });
294
+ assert.equal(r.status, 0);
295
+ const out = JSON.parse(r.stdout);
296
+ assert.equal(out.ok, true);
297
+ assert.equal(out.project, "test-proj");
298
+ assert.ok(fs.existsSync(path.join(tmpDir, ".planning", "STATE.md")));
299
+ assert.ok(fs.existsSync(path.join(tmpDir, ".planning", "tracking.json")));
300
+ });
301
+ });
302
+
303
+ it("init tracking.json has correct fields", () => {
304
+ const tmpDir = makeProject();
305
+ try {
306
+ const tracking = JSON.parse(fs.readFileSync(path.join(tmpDir, ".planning", "tracking.json"), "utf8"));
307
+ assert.equal(tracking.project, "TestProject");
308
+ assert.equal(tracking.total_phases, 2);
309
+ assert.equal(tracking.phase, 1);
310
+ assert.equal(tracking.status, "setup");
311
+ } finally {
312
+ fs.rmSync(tmpDir, { recursive: true, force: true });
313
+ }
314
+ });
315
+
316
+ it("init STATE.md has correct header", () => {
317
+ const tmpDir = makeProject();
318
+ try {
319
+ const state = fs.readFileSync(path.join(tmpDir, ".planning", "STATE.md"), "utf8");
320
+ assert.match(state, /Phase: 1 of 2 — Foundation/);
321
+ assert.match(state, /Status: setup/);
322
+ } finally {
323
+ fs.rmSync(tmpDir, { recursive: true, force: true });
324
+ }
325
+ });
326
+
327
+ it("check reads back init state", () => {
328
+ const tmpDir = makeProject();
329
+ try {
330
+ const r = runState(["check"], tmpDir);
331
+ assert.equal(r.status, 0);
332
+ const out = JSON.parse(r.stdout);
333
+ assert.equal(out.ok, true);
334
+ assert.equal(out.phase, 1);
335
+ assert.equal(out.status, "setup");
336
+ assert.equal(out.total_phases, 2);
337
+ } finally {
338
+ fs.rmSync(tmpDir, { recursive: true, force: true });
339
+ }
340
+ });
341
+
342
+ it("transition requires --to", () => {
343
+ const tmpDir = makeProject();
344
+ try {
345
+ const r = runState(["transition"], tmpDir);
346
+ assert.equal(r.status, 1);
347
+ const out = JSON.parse(r.stdout);
348
+ assert.equal(out.error, "MISSING_ARG");
349
+ } finally {
350
+ fs.rmSync(tmpDir, { recursive: true, force: true });
351
+ }
352
+ });
353
+
354
+ it("transition rejects invalid status jumps (setup -> built)", () => {
355
+ const tmpDir = makeProject();
356
+ try {
357
+ const r = runState(["transition", "--to", "built"], tmpDir);
358
+ assert.equal(r.status, 1);
359
+ const out = JSON.parse(r.stdout);
360
+ assert.equal(out.error, "PRECONDITION_FAILED");
361
+ assert.match(out.message, /Cannot go from 'setup' to 'built'/);
362
+ } finally {
363
+ fs.rmSync(tmpDir, { recursive: true, force: true });
364
+ }
365
+ });
366
+
367
+ it("setup -> planned succeeds with plan file", () => {
368
+ const tmpDir = makeProject();
369
+ try {
370
+ makeValidPlan(tmpDir, 1);
371
+ const r = runState(["transition", "--to", "planned"], tmpDir);
372
+ assert.equal(r.status, 0);
373
+ const out = JSON.parse(r.stdout);
374
+ assert.equal(out.ok, true);
375
+ assert.equal(out.status, "planned");
376
+ assert.equal(out.previous_status, "setup");
377
+ } finally {
378
+ fs.rmSync(tmpDir, { recursive: true, force: true });
379
+ }
380
+ });
381
+
382
+ it("planned -> built records tasks_done/tasks_total", () => {
383
+ const tmpDir = makeProject();
384
+ try {
385
+ makeValidPlan(tmpDir, 1);
386
+ runState(["transition", "--to", "planned"], tmpDir);
387
+ const r = runState(["transition", "--to", "built", "--tasks-done", "5", "--tasks-total", "5"], tmpDir);
388
+ assert.equal(r.status, 0);
389
+ const out = JSON.parse(r.stdout);
390
+ assert.equal(out.ok, true);
391
+ assert.equal(out.status, "built");
392
+ const tracking = JSON.parse(fs.readFileSync(path.join(tmpDir, ".planning", "tracking.json"), "utf8"));
393
+ assert.equal(tracking.tasks_done, 5);
394
+ assert.equal(tracking.tasks_total, 5);
395
+ } finally {
396
+ fs.rmSync(tmpDir, { recursive: true, force: true });
397
+ }
398
+ });
399
+
400
+ it("built -> verified(pass) auto-advances to phase 2", () => {
401
+ const tmpDir = makeProject();
402
+ try {
403
+ makeValidPlan(tmpDir, 1);
404
+ runState(["transition", "--to", "planned"], tmpDir);
405
+ runState(["transition", "--to", "built", "--tasks-done", "5", "--tasks-total", "5"], tmpDir);
406
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-verification.md"), "pass");
407
+ const r = runState(["transition", "--to", "verified", "--verification", "pass"], tmpDir);
408
+ assert.equal(r.status, 0);
409
+ const out = JSON.parse(r.stdout);
410
+ assert.equal(out.ok, true);
411
+ assert.equal(out.phase, 2);
412
+ assert.equal(out.status, "setup");
413
+ } finally {
414
+ fs.rmSync(tmpDir, { recursive: true, force: true });
415
+ }
416
+ });
417
+
418
+ it("built -> verified(fail) stays on same phase", () => {
419
+ const tmpDir = makeProject();
420
+ try {
421
+ makeValidPlan(tmpDir, 1);
422
+ runState(["transition", "--to", "planned"], tmpDir);
423
+ runState(["transition", "--to", "built", "--tasks-done", "3", "--tasks-total", "5"], tmpDir);
424
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-verification.md"), "fail");
425
+ const r = runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
426
+ assert.equal(r.status, 0);
427
+ const out = JSON.parse(r.stdout);
428
+ assert.equal(out.ok, true);
429
+ assert.equal(out.phase, 1);
430
+ assert.equal(out.status, "verified");
431
+ assert.equal(out.verification, "fail");
432
+ } finally {
433
+ fs.rmSync(tmpDir, { recursive: true, force: true });
434
+ }
435
+ });
436
+
437
+ it("planned -> verified fails (requires built)", () => {
438
+ const tmpDir = makeProject();
439
+ try {
440
+ makeValidPlan(tmpDir, 1);
441
+ runState(["transition", "--to", "planned"], tmpDir);
442
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-verification.md"), "");
443
+ const r = runState(["transition", "--to", "verified", "--verification", "pass"], tmpDir);
444
+ assert.equal(r.status, 1);
445
+ const out = JSON.parse(r.stdout);
446
+ assert.equal(out.error, "PRECONDITION_FAILED");
447
+ } finally {
448
+ fs.rmSync(tmpDir, { recursive: true, force: true });
449
+ }
450
+ });
451
+
452
+ it("setup -> planned fails without plan file (MISSING_FILE)", () => {
453
+ const tmpDir = makeProject();
454
+ try {
455
+ const r = runState(["transition", "--to", "planned"], tmpDir);
456
+ assert.equal(r.status, 1);
457
+ const out = JSON.parse(r.stdout);
458
+ assert.equal(out.error, "MISSING_FILE");
459
+ assert.match(out.message, /phase-1-plan\.md/);
460
+ } finally {
461
+ fs.rmSync(tmpDir, { recursive: true, force: true });
462
+ }
463
+ });
464
+
465
+ it("built -> verified fails without verification file", () => {
466
+ const tmpDir = makeProject();
467
+ try {
468
+ makeValidPlan(tmpDir, 1);
469
+ runState(["transition", "--to", "planned"], tmpDir);
470
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
471
+ const r = runState(["transition", "--to", "verified", "--verification", "pass"], tmpDir);
472
+ assert.equal(r.status, 1);
473
+ const out = JSON.parse(r.stdout);
474
+ assert.equal(out.error, "MISSING_FILE");
475
+ assert.match(out.message, /phase-1-verification\.md/);
476
+ } finally {
477
+ fs.rmSync(tmpDir, { recursive: true, force: true });
478
+ }
479
+ });
480
+
481
+ it("built -> verified without --verification -> MISSING_ARG", () => {
482
+ const tmpDir = makeProject();
483
+ try {
484
+ makeValidPlan(tmpDir, 1);
485
+ runState(["transition", "--to", "planned"], tmpDir);
486
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
487
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-verification.md"), "");
488
+ const r = runState(["transition", "--to", "verified"], tmpDir);
489
+ assert.equal(r.status, 1);
490
+ const out = JSON.parse(r.stdout);
491
+ assert.equal(out.error, "MISSING_ARG");
492
+ assert.match(out.message, /verification/);
493
+ } finally {
494
+ fs.rmSync(tmpDir, { recursive: true, force: true });
495
+ }
496
+ });
497
+
498
+ it("unknown target -> INVALID_STATUS", () => {
499
+ const tmpDir = makeProject();
500
+ try {
501
+ const r = runState(["transition", "--to", "frobnicate"], tmpDir);
502
+ assert.equal(r.status, 1);
503
+ const out = JSON.parse(r.stdout);
504
+ assert.equal(out.error, "INVALID_STATUS");
505
+ } finally {
506
+ fs.rmSync(tmpDir, { recursive: true, force: true });
507
+ }
508
+ });
509
+
510
+ it("unknown command shows usage", () => {
511
+ const r = runState(["bogus"], ROOT);
512
+ assert.equal(r.status, 1);
513
+ const out = JSON.parse(r.stdout);
514
+ assert.equal(out.error, "UNKNOWN_COMMAND");
515
+ });
516
+
517
+ it("validate-plan accepts well-formed plan", () => {
518
+ const tmpDir = makeProject();
519
+ try {
520
+ makeValidPlan(tmpDir, 1);
521
+ const r = runState(["validate-plan", "--phase", "1"], tmpDir);
522
+ assert.equal(r.status, 0);
523
+ const out = JSON.parse(r.stdout);
524
+ assert.equal(out.ok, true);
525
+ assert.equal(out.task_count, 1);
526
+ } finally {
527
+ fs.rmSync(tmpDir, { recursive: true, force: true });
528
+ }
529
+ });
530
+
531
+ it("validate-plan rejects non-existent plan", () => {
532
+ withTempPlanning((tmpDir) => {
533
+ const r = runState(["validate-plan", "--phase", "1"], tmpDir);
534
+ assert.equal(r.status, 1);
535
+ const out = JSON.parse(r.stdout);
536
+ assert.equal(out.error, "MISSING_FILE");
537
+ });
538
+ });
539
+
540
+ it("validate-plan rejects plan without tasks", () => {
541
+ const tmpDir = makeProject();
542
+ try {
543
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-plan.md"), "---\ngoal: test\n---\n\nNo tasks here.\n");
544
+ const r = runState(["validate-plan", "--phase", "1"], tmpDir);
545
+ assert.equal(r.status, 1);
546
+ const out = JSON.parse(r.stdout);
547
+ assert.equal(out.error, "PLAN_VALIDATION_FAILED");
548
+ } finally {
549
+ fs.rmSync(tmpDir, { recursive: true, force: true });
550
+ }
551
+ });
552
+
553
+ it("validate-plan rejects plan missing Done when", () => {
554
+ const tmpDir = makeProject();
555
+ try {
556
+ const plan = `---
557
+ phase: 1
558
+ goal: "Test"
559
+ tasks: 1
560
+ waves: 1
561
+ ---
562
+ ## Task 1 — Incomplete
563
+ **Wave:** 1
564
+ **Files:** test.ts
565
+ **Action:** Do something
566
+
567
+ ## Success Criteria
568
+ - [ ] Works
569
+ `;
570
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-plan.md"), plan);
571
+ const r = runState(["validate-plan", "--phase", "1"], tmpDir);
572
+ assert.equal(r.status, 1);
573
+ const out = JSON.parse(r.stdout);
574
+ assert.equal(out.error, "PLAN_VALIDATION_FAILED");
575
+ // The error detail is in the errors array, mentioning "Done when"
576
+ const errStr = JSON.stringify(out.errors || []);
577
+ assert.match(errStr, /Done when/);
578
+ } finally {
579
+ fs.rmSync(tmpDir, { recursive: true, force: true });
580
+ }
581
+ });
582
+
583
+ it("transition to planned with invalid plan -> INVALID_PLAN", () => {
584
+ const tmpDir = makeProject();
585
+ try {
586
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-plan.md"), "# Empty plan with no tasks");
587
+ const r = runState(["transition", "--to", "planned"], tmpDir);
588
+ assert.equal(r.status, 1);
589
+ const out = JSON.parse(r.stdout);
590
+ assert.equal(out.error, "INVALID_PLAN");
591
+ } finally {
592
+ fs.rmSync(tmpDir, { recursive: true, force: true });
593
+ }
594
+ });
595
+
596
+ it("fix repairs malformed STATE.md", () => {
597
+ const tmpDir = makeProject();
598
+ try {
599
+ fs.writeFileSync(path.join(tmpDir, ".planning", "STATE.md"), "corrupted content");
600
+ const r = runState(["fix"], tmpDir);
601
+ assert.equal(r.status, 0);
602
+ const out = JSON.parse(r.stdout);
603
+ assert.equal(out.ok, true);
604
+ assert.equal(out.fixed, true);
605
+ } finally {
606
+ fs.rmSync(tmpDir, { recursive: true, force: true });
607
+ }
608
+ });
609
+
610
+ it("fix on well-formed STATE.md is idempotent", () => {
611
+ const tmpDir = makeProject();
612
+ try {
613
+ const r = runState(["fix"], tmpDir);
614
+ assert.equal(r.status, 0);
615
+ const out = JSON.parse(r.stdout);
616
+ assert.equal(out.previous_errors, 0);
617
+ // Check output is still valid
618
+ const r2 = runState(["check"], tmpDir);
619
+ const out2 = JSON.parse(r2.stdout);
620
+ assert.equal(out2.ok, true);
621
+ assert.equal(out2.phase, 1);
622
+ assert.equal(out2.total_phases, 2);
623
+ } finally {
624
+ fs.rmSync(tmpDir, { recursive: true, force: true });
625
+ }
626
+ });
627
+
628
+ it("gap cycle circuit breaker blocks after limit", () => {
629
+ const tmpDir = makeProject();
630
+ try {
631
+ makeValidPlan(tmpDir, 1);
632
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-verification.md"), "");
633
+
634
+ // Cycle 1: planned -> built -> verified(fail) -> planned
635
+ runState(["transition", "--to", "planned"], tmpDir);
636
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
637
+ runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
638
+ let r = runState(["transition", "--to", "planned"], tmpDir);
639
+ assert.equal(r.status, 0);
640
+ let out = JSON.parse(r.stdout);
641
+ assert.equal(out.gap_cycles, 1);
642
+
643
+ // Cycle 2
644
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
645
+ runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
646
+ r = runState(["transition", "--to", "planned"], tmpDir);
647
+ assert.equal(r.status, 0);
648
+ out = JSON.parse(r.stdout);
649
+ assert.equal(out.gap_cycles, 2);
650
+
651
+ // Cycle 3: should be blocked
652
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
653
+ runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
654
+ r = runState(["transition", "--to", "planned"], tmpDir);
655
+ assert.equal(r.status, 1);
656
+ out = JSON.parse(r.stdout);
657
+ assert.equal(out.error, "GAP_CYCLE_LIMIT");
658
+ } finally {
659
+ fs.rmSync(tmpDir, { recursive: true, force: true });
660
+ }
661
+ });
662
+
663
+ it("verified(pass) resets gap_cycles to 0", () => {
664
+ const tmpDir = makeProject();
665
+ try {
666
+ makeValidPlan(tmpDir, 1);
667
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-verification.md"), "");
668
+
669
+ // One fail cycle
670
+ runState(["transition", "--to", "planned"], tmpDir);
671
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
672
+ runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
673
+ runState(["transition", "--to", "planned"], tmpDir);
674
+
675
+ // Now pass
676
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
677
+ runState(["transition", "--to", "verified", "--verification", "pass"], tmpDir);
678
+
679
+ const tracking = JSON.parse(fs.readFileSync(path.join(tmpDir, ".planning", "tracking.json"), "utf8"));
680
+ // gap_cycles for phase 1 should be reset to 0
681
+ assert.ok(tracking.gap_cycles);
682
+ assert.equal(tracking.gap_cycles["1"], 0);
683
+ } finally {
684
+ fs.rmSync(tmpDir, { recursive: true, force: true });
685
+ }
686
+ });
687
+
688
+ it("configurable gap_cycle_limit allows more cycles", () => {
689
+ const tmpDir = makeProject();
690
+ try {
691
+ makeValidPlan(tmpDir, 1);
692
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-verification.md"), "");
693
+
694
+ // Set custom limit
695
+ const trackingPath = path.join(tmpDir, ".planning", "tracking.json");
696
+ const tracking = JSON.parse(fs.readFileSync(trackingPath, "utf8"));
697
+ tracking.gap_cycle_limit = 5;
698
+ fs.writeFileSync(trackingPath, JSON.stringify(tracking, null, 2));
699
+
700
+ // 3 gap closure cycles (default limit is 2, but we set 5)
701
+ runState(["transition", "--to", "planned"], tmpDir);
702
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
703
+ runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
704
+ runState(["transition", "--to", "planned"], tmpDir);
705
+
706
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
707
+ runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
708
+ runState(["transition", "--to", "planned"], tmpDir);
709
+
710
+ runState(["transition", "--to", "built", "--tasks-done", "1", "--tasks-total", "1"], tmpDir);
711
+ runState(["transition", "--to", "verified", "--verification", "fail"], tmpDir);
712
+ // 3rd closure should succeed (limit is 5)
713
+ const r = runState(["transition", "--to", "planned"], tmpDir);
714
+ assert.equal(r.status, 0);
715
+ const out = JSON.parse(r.stdout);
716
+ assert.equal(out.ok, true);
717
+ } finally {
718
+ fs.rmSync(tmpDir, { recursive: true, force: true });
719
+ }
720
+ });
721
+
722
+ it("--to note records notes without status change", () => {
723
+ const tmpDir = makeProject();
724
+ try {
725
+ const r = runState(["transition", "--to", "note", "--notes", "hello world"], tmpDir);
726
+ assert.equal(r.status, 0);
727
+ const out = JSON.parse(r.stdout);
728
+ assert.equal(out.ok, true);
729
+ assert.equal(out.action, "note");
730
+ assert.equal(out.status, "setup");
731
+ const tracking = JSON.parse(fs.readFileSync(path.join(tmpDir, ".planning", "tracking.json"), "utf8"));
732
+ assert.equal(tracking.notes, "hello world");
733
+ } finally {
734
+ fs.rmSync(tmpDir, { recursive: true, force: true });
735
+ }
736
+ });
737
+
738
+ it("--to activity succeeds without status change", () => {
739
+ const tmpDir = makeProject();
740
+ try {
741
+ const r = runState(["transition", "--to", "activity"], tmpDir);
742
+ assert.equal(r.status, 0);
743
+ const out = JSON.parse(r.stdout);
744
+ assert.equal(out.ok, true);
745
+ assert.equal(out.action, "activity");
746
+ assert.equal(out.status, "setup");
747
+ } finally {
748
+ fs.rmSync(tmpDir, { recursive: true, force: true });
749
+ }
750
+ });
751
+
752
+ it("--force bypasses precondition (setup -> built)", () => {
753
+ const tmpDir = makeProject();
754
+ try {
755
+ const r = runState(["transition", "--to", "built", "--force"], tmpDir);
756
+ assert.equal(r.status, 0);
757
+ const out = JSON.parse(r.stdout);
758
+ assert.equal(out.ok, true);
759
+ assert.equal(out.status, "built");
760
+ } finally {
761
+ fs.rmSync(tmpDir, { recursive: true, force: true });
762
+ }
763
+ });
764
+
765
+ it("--force does NOT bypass MISSING_FILE", () => {
766
+ const tmpDir = makeProject();
767
+ try {
768
+ const r = runState(["transition", "--to", "planned", "--force"], tmpDir);
769
+ assert.equal(r.status, 1);
770
+ const out = JSON.parse(r.stdout);
771
+ assert.equal(out.error, "MISSING_FILE");
772
+ } finally {
773
+ fs.rmSync(tmpDir, { recursive: true, force: true });
774
+ }
775
+ });
776
+
777
+ it("--force does NOT bypass INVALID_PLAN", () => {
778
+ const tmpDir = makeProject();
779
+ try {
780
+ fs.writeFileSync(path.join(tmpDir, ".planning", "phase-1-plan.md"), "# No tasks here");
781
+ const r = runState(["transition", "--to", "planned", "--force"], tmpDir);
782
+ assert.equal(r.status, 1);
783
+ const out = JSON.parse(r.stdout);
784
+ assert.equal(out.error, "INVALID_PLAN");
785
+ } finally {
786
+ fs.rmSync(tmpDir, { recursive: true, force: true });
787
+ }
788
+ });
789
+
790
+ it("check includes gap_cycle_limit in output", () => {
791
+ const tmpDir = makeProject();
792
+ try {
793
+ const r = runState(["check"], tmpDir);
794
+ const out = JSON.parse(r.stdout);
795
+ assert.ok("gap_cycle_limit" in out);
796
+ } finally {
797
+ fs.rmSync(tmpDir, { recursive: true, force: true });
798
+ }
799
+ });
800
+ });
801
+
802
+ // ═══════════════════════════════════════════════════════════
803
+ // Hook Tests
804
+ // ═══════════════════════════════════════════════════════════
805
+
806
+ describe("Hooks", () => {
807
+ it("all hooks pass syntax check", () => {
808
+ const hooks = fs.readdirSync(HOOKS).filter(f => f.endsWith(".js"));
809
+ assert.ok(hooks.length >= 7, `Expected 7+ hooks, found ${hooks.length}`);
810
+ for (const hook of hooks) {
811
+ const r = spawnSync(process.execPath, ["--check", path.join(HOOKS, hook)], {
812
+ encoding: "utf8", timeout: 5000,
813
+ });
814
+ assert.equal(r.status, 0, `Syntax error in ${hook}: ${r.stderr}`);
815
+ }
816
+ });
817
+
818
+ // --- migration-guard.js ---
819
+
820
+ it("migration-guard blocks DROP without IF EXISTS", () => {
821
+ const r = runHook("migration-guard.js", {
822
+ tool_input: { file_path: "migrations/001.sql", content: "DROP TABLE users;" },
823
+ });
824
+ assert.equal(r.status, 2, "Should block (exit 2)");
825
+ });
826
+
827
+ it("migration-guard allows DROP TABLE IF EXISTS", () => {
828
+ const r = runHook("migration-guard.js", {
829
+ tool_input: { file_path: "migrations/001.sql", content: "DROP TABLE IF EXISTS users;" },
830
+ });
831
+ assert.equal(r.status, 0);
832
+ });
833
+
834
+ it("migration-guard blocks DELETE without WHERE", () => {
835
+ const r = runHook("migration-guard.js", {
836
+ tool_input: { file_path: "migrations/002.sql", content: "DELETE FROM users;" },
837
+ });
838
+ assert.equal(r.status, 2);
839
+ });
840
+
841
+ it("migration-guard allows DELETE with WHERE", () => {
842
+ const r = runHook("migration-guard.js", {
843
+ tool_input: { file_path: "migrations/002.sql", content: "DELETE FROM users WHERE id = 1;" },
844
+ });
845
+ assert.equal(r.status, 0);
846
+ });
847
+
848
+ it("migration-guard blocks TRUNCATE", () => {
849
+ const r = runHook("migration-guard.js", {
850
+ tool_input: { file_path: "migrations/003.sql", content: "TRUNCATE TABLE sessions;" },
851
+ });
852
+ assert.equal(r.status, 2);
853
+ });
854
+
855
+ it("migration-guard blocks CREATE TABLE without RLS", () => {
856
+ const r = runHook("migration-guard.js", {
857
+ tool_input: { file_path: "migrations/003.sql", content: "CREATE TABLE users (id uuid primary key);" },
858
+ });
859
+ assert.equal(r.status, 2);
860
+ });
861
+
862
+ it("migration-guard allows CREATE TABLE with RLS", () => {
863
+ const r = runHook("migration-guard.js", {
864
+ tool_input: { file_path: "migrations/003.sql", content: "CREATE TABLE users (id uuid primary key);\nALTER TABLE users ENABLE ROW LEVEL SECURITY;" },
865
+ });
866
+ assert.equal(r.status, 0);
867
+ });
868
+
869
+ it("migration-guard allows safe ALTER TABLE", () => {
870
+ const r = runHook("migration-guard.js", {
871
+ tool_input: { file_path: "migrations/005.sql", content: "ALTER TABLE users ADD COLUMN email text;" },
872
+ });
873
+ assert.equal(r.status, 0);
874
+ });
875
+
876
+ it("migration-guard ignores non-migration files", () => {
877
+ const r = runHook("migration-guard.js", {
878
+ tool_input: { file_path: "src/app.tsx", content: "DROP TABLE users;" },
879
+ });
880
+ assert.equal(r.status, 0);
881
+ });
882
+
883
+ // block-env-edit.js was retired in v3.2.0 — team now has full read/write on
884
+ // .env* files. See CHANGELOG v3.2.0 and bin/install.js DEPRECATED_HOOKS.
885
+
886
+ // --- pre-push.js ---
887
+
888
+ it("pre-push.js references tracking.json", () => {
889
+ const content = fs.readFileSync(path.join(HOOKS, "pre-push.js"), "utf8");
890
+ assert.match(content, /tracking\.json/);
891
+ });
892
+
893
+ it("pre-push.js stamps last_commit", () => {
894
+ const content = fs.readFileSync(path.join(HOOKS, "pre-push.js"), "utf8");
895
+ assert.match(content, /last_commit/);
896
+ });
897
+
898
+ it("pre-push.js exits 0 with no tracking.json", () => {
899
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-push-"));
900
+ try {
901
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-push.js")], {
902
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
903
+ });
904
+ assert.equal(r.status, 0);
905
+ } finally {
906
+ fs.rmSync(tmpDir, { recursive: true, force: true });
907
+ }
908
+ });
909
+
910
+ // --- session-start.js ---
911
+
912
+ it("session-start.js exits 0 with no project", () => {
913
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ss-"));
914
+ try {
915
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "session-start.js")], {
916
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
917
+ });
918
+ assert.equal(r.status, 0);
919
+ } finally {
920
+ fs.rmSync(tmpDir, { recursive: true, force: true });
921
+ }
922
+ });
923
+
924
+ it("session-start.js exits 0 with STATE.md", () => {
925
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ss-"));
926
+ try {
927
+ const planningDir = path.join(tmpDir, ".planning");
928
+ fs.mkdirSync(planningDir, { recursive: true });
929
+ fs.writeFileSync(path.join(planningDir, "STATE.md"), "# Project State\nPhase: 1 of 3 — Foundation\nStatus: setup\n");
930
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "session-start.js")], {
931
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
932
+ });
933
+ assert.equal(r.status, 0);
934
+ } finally {
935
+ fs.rmSync(tmpDir, { recursive: true, force: true });
936
+ }
937
+ });
938
+
939
+ // --- pre-compact.js ---
940
+
941
+ it("pre-compact.js exits 0 with no STATE.md", () => {
942
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pc-"));
943
+ try {
944
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-compact.js")], {
945
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
946
+ });
947
+ assert.equal(r.status, 0);
948
+ } finally {
949
+ fs.rmSync(tmpDir, { recursive: true, force: true });
950
+ }
951
+ });
952
+
953
+ // --- auto-update.js ---
954
+
955
+ it("auto-update.js exits 0 and writes cache timestamp", () => {
956
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-au-"));
957
+ try {
958
+ fs.mkdirSync(path.join(tmpHome, ".claude"), { recursive: true });
959
+ fs.writeFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), JSON.stringify({
960
+ code: "QS-FAWZI-01", version: "99.99.99",
961
+ }));
962
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "auto-update.js")], {
963
+ encoding: "utf8", timeout: 5000,
964
+ env: { ...process.env, HOME: tmpHome, USERPROFILE: tmpHome },
965
+ stdio: ["pipe", "pipe", "pipe"],
966
+ });
967
+ assert.equal(r.status, 0);
968
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", ".qualia-last-update-check")));
969
+ } finally {
970
+ fs.rmSync(tmpHome, { recursive: true, force: true });
971
+ }
972
+ });
973
+
974
+ // --- pre-deploy-gate.js ---
975
+
976
+ it("pre-deploy-gate: empty project exits 0", () => {
977
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
978
+ try {
979
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
980
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
981
+ });
982
+ assert.equal(r.status, 0);
983
+ } finally {
984
+ fs.rmSync(tmpDir, { recursive: true, force: true });
985
+ }
986
+ });
987
+
988
+ it("pre-deploy-gate: no tsconfig -> TS gate skipped", () => {
989
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
990
+ try {
991
+ fs.mkdirSync(path.join(tmpDir, "src"), { recursive: true });
992
+ fs.writeFileSync(path.join(tmpDir, "src", "app.ts"), "export const x = 1;");
993
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
994
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
995
+ });
996
+ assert.equal(r.status, 0);
997
+ } finally {
998
+ fs.rmSync(tmpDir, { recursive: true, force: true });
999
+ }
1000
+ });
1001
+
1002
+ it("pre-deploy-gate: service_role in app/ -> blocked", () => {
1003
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1004
+ try {
1005
+ fs.mkdirSync(path.join(tmpDir, "app"), { recursive: true });
1006
+ fs.writeFileSync(path.join(tmpDir, "app", "page.tsx"), 'const key = "service_role_literal_leak";\nexport default function P(){return null}');
1007
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1008
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1009
+ });
1010
+ assert.equal(r.status, 1);
1011
+ const combined = r.stdout + r.stderr;
1012
+ assert.match(combined, /BLOCKED/);
1013
+ assert.match(combined, /service_role/);
1014
+ } finally {
1015
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1016
+ }
1017
+ });
1018
+
1019
+ it("pre-deploy-gate: service_role in components/ -> blocked", () => {
1020
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1021
+ try {
1022
+ fs.mkdirSync(path.join(tmpDir, "components"), { recursive: true });
1023
+ fs.writeFileSync(path.join(tmpDir, "components", "Widget.tsx"), 'const key = "service_role_literal_leak";');
1024
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1025
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1026
+ });
1027
+ assert.equal(r.status, 1);
1028
+ } finally {
1029
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1030
+ }
1031
+ });
1032
+
1033
+ it("pre-deploy-gate: .server.ts is exempt", () => {
1034
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1035
+ try {
1036
+ fs.mkdirSync(path.join(tmpDir, "app", "api"), { recursive: true });
1037
+ fs.writeFileSync(path.join(tmpDir, "app", "api", "route.server.ts"), 'const key = "service_role_legit_server_key";');
1038
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1039
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1040
+ });
1041
+ assert.equal(r.status, 0);
1042
+ } finally {
1043
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1044
+ }
1045
+ });
1046
+
1047
+ it("pre-deploy-gate: files under server/ are exempt", () => {
1048
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1049
+ try {
1050
+ fs.mkdirSync(path.join(tmpDir, "app", "server"), { recursive: true });
1051
+ fs.writeFileSync(path.join(tmpDir, "app", "server", "admin.ts"), 'const key = "service_role_legit_server_dir";');
1052
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1053
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1054
+ });
1055
+ assert.equal(r.status, 0);
1056
+ } finally {
1057
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1058
+ }
1059
+ });
1060
+
1061
+ it("pre-deploy-gate: node_modules not walked", () => {
1062
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1063
+ try {
1064
+ fs.mkdirSync(path.join(tmpDir, "app", "node_modules", "evil"), { recursive: true });
1065
+ fs.writeFileSync(path.join(tmpDir, "app", "node_modules", "evil", "index.ts"), 'const key = "service_role_in_node_modules";');
1066
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1067
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1068
+ });
1069
+ assert.equal(r.status, 0);
1070
+ } finally {
1071
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1072
+ }
1073
+ });
1074
+
1075
+ it("pre-deploy-gate: clean project -> all gates pass", () => {
1076
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1077
+ try {
1078
+ fs.mkdirSync(path.join(tmpDir, "app"), { recursive: true });
1079
+ fs.mkdirSync(path.join(tmpDir, "components"), { recursive: true });
1080
+ fs.mkdirSync(path.join(tmpDir, "lib"), { recursive: true });
1081
+ fs.writeFileSync(path.join(tmpDir, "app", "page.tsx"), "export const a = 1;");
1082
+ fs.writeFileSync(path.join(tmpDir, "components", "Widget.tsx"), "export const b = 2;");
1083
+ fs.writeFileSync(path.join(tmpDir, "lib", "util.ts"), "export const c = 3;");
1084
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1085
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1086
+ });
1087
+ assert.equal(r.status, 0);
1088
+ assert.match(r.stdout + r.stderr, /All gates passed/);
1089
+ } finally {
1090
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1091
+ }
1092
+ });
1093
+
1094
+ it("pre-deploy-gate: route.ts with service_role -> exempt", () => {
1095
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1096
+ try {
1097
+ fs.mkdirSync(path.join(tmpDir, "app", "api", "auth"), { recursive: true });
1098
+ fs.writeFileSync(path.join(tmpDir, "app", "api", "auth", "route.ts"),
1099
+ 'const key = process.env.SUPABASE_SERVICE_ROLE_KEY; export async function POST() {}');
1100
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1101
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1102
+ });
1103
+ assert.equal(r.status, 0);
1104
+ } finally {
1105
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1106
+ }
1107
+ });
1108
+
1109
+ it("pre-deploy-gate: middleware.ts with service_role -> exempt", () => {
1110
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1111
+ try {
1112
+ fs.writeFileSync(path.join(tmpDir, "middleware.ts"),
1113
+ 'import { service_role } from "./config"; export function middleware() {}');
1114
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1115
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1116
+ });
1117
+ assert.equal(r.status, 0);
1118
+ } finally {
1119
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1120
+ }
1121
+ });
1122
+
1123
+ it("pre-deploy-gate: app/api/ file with service_role -> exempt", () => {
1124
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1125
+ try {
1126
+ fs.mkdirSync(path.join(tmpDir, "app", "api", "webhook"), { recursive: true });
1127
+ fs.writeFileSync(path.join(tmpDir, "app", "api", "webhook", "route.js"),
1128
+ 'const sr = "service_role"; export async function GET() { return new Response(sr); }');
1129
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1130
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1131
+ });
1132
+ assert.equal(r.status, 0);
1133
+ } finally {
1134
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1135
+ }
1136
+ });
1137
+
1138
+ it("pre-deploy-gate: 'use server' file with service_role -> exempt", () => {
1139
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1140
+ try {
1141
+ fs.mkdirSync(path.join(tmpDir, "app", "admin"), { recursive: true });
1142
+ fs.writeFileSync(path.join(tmpDir, "app", "admin", "actions.ts"),
1143
+ '"use server"\nconst key = process.env.SUPABASE_SERVICE_ROLE_KEY;\nexport async function deleteUser() {}\n');
1144
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1145
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1146
+ });
1147
+ assert.equal(r.status, 0);
1148
+ } finally {
1149
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1150
+ }
1151
+ });
1152
+
1153
+ it("pre-deploy-gate: regular page.tsx with service_role -> blocked", () => {
1154
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1155
+ try {
1156
+ fs.mkdirSync(path.join(tmpDir, "app", "admin"), { recursive: true });
1157
+ fs.writeFileSync(path.join(tmpDir, "app", "admin", "page.tsx"),
1158
+ 'const key = "service_role"; export default function Page() { return <div>{key}</div>; }');
1159
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1160
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1161
+ });
1162
+ assert.equal(r.status, 1);
1163
+ } finally {
1164
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1165
+ }
1166
+ });
1167
+
1168
+ // --- branch-guard.js ---
1169
+
1170
+ it("branch-guard: OWNER on main -> allowed", () => {
1171
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1172
+ try {
1173
+ const projDir = path.join(tmpDir, "proj");
1174
+ fs.mkdirSync(projDir, { recursive: true });
1175
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1176
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1177
+ spawnSync("git", ["checkout", "-b", "main", "-q"], { cwd: projDir, stdio: "pipe" });
1178
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "OWNER" }));
1179
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1180
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1181
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1182
+ stdio: ["pipe", "pipe", "pipe"],
1183
+ });
1184
+ assert.equal(r.status, 0);
1185
+ } finally {
1186
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1187
+ }
1188
+ });
1189
+
1190
+ it("branch-guard: EMPLOYEE on main -> blocked", () => {
1191
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1192
+ try {
1193
+ const projDir = path.join(tmpDir, "proj");
1194
+ fs.mkdirSync(projDir, { recursive: true });
1195
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1196
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1197
+ spawnSync("git", ["checkout", "-b", "main", "-q"], { cwd: projDir, stdio: "pipe" });
1198
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "EMPLOYEE" }));
1199
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1200
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1201
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1202
+ stdio: ["pipe", "pipe", "pipe"],
1203
+ });
1204
+ assert.equal(r.status, 2);
1205
+ assert.match(r.stdout, /BLOCKED/);
1206
+ } finally {
1207
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1208
+ }
1209
+ });
1210
+
1211
+ it("branch-guard: EMPLOYEE on feature branch -> allowed", () => {
1212
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1213
+ try {
1214
+ const projDir = path.join(tmpDir, "proj");
1215
+ fs.mkdirSync(projDir, { recursive: true });
1216
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1217
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1218
+ spawnSync("git", ["checkout", "-b", "feature/xyz", "-q"], { cwd: projDir, stdio: "pipe" });
1219
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "EMPLOYEE" }));
1220
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1221
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1222
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1223
+ stdio: ["pipe", "pipe", "pipe"],
1224
+ });
1225
+ assert.equal(r.status, 0);
1226
+ } finally {
1227
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1228
+ }
1229
+ });
1230
+
1231
+ it("branch-guard: missing config -> blocked (fails closed)", () => {
1232
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1233
+ try {
1234
+ const projDir = path.join(tmpDir, "proj");
1235
+ fs.mkdirSync(projDir, { recursive: true });
1236
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1237
+ spawnSync("git", ["checkout", "-b", "feature/x", "-q"], { cwd: projDir, stdio: "pipe" });
1238
+ // No .claude/.qualia-config.json
1239
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1240
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1241
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1242
+ stdio: ["pipe", "pipe", "pipe"],
1243
+ });
1244
+ assert.equal(r.status, 2);
1245
+ } finally {
1246
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1247
+ }
1248
+ });
1249
+
1250
+ it("branch-guard: malformed config JSON -> blocked", () => {
1251
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1252
+ try {
1253
+ const projDir = path.join(tmpDir, "proj");
1254
+ fs.mkdirSync(projDir, { recursive: true });
1255
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1256
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1257
+ spawnSync("git", ["checkout", "-b", "feature/x", "-q"], { cwd: projDir, stdio: "pipe" });
1258
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), "not json{");
1259
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1260
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1261
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1262
+ stdio: ["pipe", "pipe", "pipe"],
1263
+ });
1264
+ assert.equal(r.status, 2);
1265
+ } finally {
1266
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1267
+ }
1268
+ });
1269
+
1270
+ it("branch-guard: empty role field -> blocked", () => {
1271
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1272
+ try {
1273
+ const projDir = path.join(tmpDir, "proj");
1274
+ fs.mkdirSync(projDir, { recursive: true });
1275
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1276
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1277
+ spawnSync("git", ["checkout", "-b", "feature/x", "-q"], { cwd: projDir, stdio: "pipe" });
1278
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "" }));
1279
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1280
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1281
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1282
+ stdio: ["pipe", "pipe", "pipe"],
1283
+ });
1284
+ assert.equal(r.status, 2);
1285
+ } finally {
1286
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1287
+ }
1288
+ });
1289
+ });
1290
+
1291
+ // ═══════════════════════════════════════════════════════════
1292
+ // Statusline Tests
1293
+ // ═══════════════════════════════════════════════════════════
1294
+
1295
+ describe("Statusline", () => {
1296
+ it("statusline.js passes syntax check", () => {
1297
+ const r = spawnSync(process.execPath, ["--check", path.join(BIN, "statusline.js")], {
1298
+ encoding: "utf8", timeout: 5000,
1299
+ });
1300
+ assert.equal(r.status, 0);
1301
+ });
1302
+
1303
+ it("statusline.js runs without crashing", () => {
1304
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1305
+ encoding: "utf8", timeout: 5000,
1306
+ env: { ...process.env, HOME: os.tmpdir(), USERPROFILE: os.tmpdir() },
1307
+ stdio: ["pipe", "pipe", "pipe"],
1308
+ });
1309
+ assert.equal(r.status, 0);
1310
+ });
1311
+
1312
+ it("qualia-ui.js passes syntax check", () => {
1313
+ const r = spawnSync(process.execPath, ["--check", path.join(BIN, "qualia-ui.js")], {
1314
+ encoding: "utf8", timeout: 5000,
1315
+ });
1316
+ assert.equal(r.status, 0);
1317
+ });
1318
+
1319
+ it("statusline renders 2 lines with minimal input", () => {
1320
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-nonexist-${process.pid}`);
1321
+ const json = JSON.stringify({
1322
+ model: { display_name: "Claude Opus 4.6" },
1323
+ workspace: { current_dir: nonexist },
1324
+ context_window: { used_percentage: 0 },
1325
+ cost: { total_cost_usd: 0 },
1326
+ agent: {},
1327
+ worktree: {},
1328
+ });
1329
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1330
+ encoding: "utf8", timeout: 5000,
1331
+ input: json,
1332
+ stdio: ["pipe", "pipe", "pipe"],
1333
+ });
1334
+ assert.equal(r.status, 0);
1335
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1336
+ assert.equal(lines.length, 2);
1337
+ assert.match(r.stdout, /qualia-sl-nonexist/);
1338
+ assert.match(r.stdout, /Claude Opus 4\.6/);
1339
+ });
1340
+
1341
+ it("statusline shows cost formatting", () => {
1342
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-cost-${process.pid}`);
1343
+ const json = JSON.stringify({
1344
+ model: { display_name: "M" },
1345
+ workspace: { current_dir: nonexist },
1346
+ context_window: { used_percentage: 10 },
1347
+ cost: { total_cost_usd: 2.47, total_duration_ms: 0 },
1348
+ agent: {},
1349
+ worktree: {},
1350
+ });
1351
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1352
+ encoding: "utf8", timeout: 5000,
1353
+ input: json,
1354
+ stdio: ["pipe", "pipe", "pipe"],
1355
+ });
1356
+ assert.equal(r.status, 0);
1357
+ assert.match(r.stdout, /\$2\.47/);
1358
+ });
1359
+
1360
+ it("statusline shows duration in seconds under 60s", () => {
1361
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-dur-${process.pid}`);
1362
+ const json = JSON.stringify({
1363
+ model: { display_name: "M" },
1364
+ workspace: { current_dir: nonexist },
1365
+ context_window: { used_percentage: 10 },
1366
+ cost: { total_cost_usd: 0, total_duration_ms: 45000 },
1367
+ agent: {},
1368
+ worktree: {},
1369
+ });
1370
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1371
+ encoding: "utf8", timeout: 5000,
1372
+ input: json,
1373
+ stdio: ["pipe", "pipe", "pipe"],
1374
+ });
1375
+ assert.equal(r.status, 0);
1376
+ assert.match(r.stdout, /45s/);
1377
+ });
1378
+
1379
+ it("statusline shows duration in minutes over 60s", () => {
1380
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-durm-${process.pid}`);
1381
+ const json = JSON.stringify({
1382
+ model: { display_name: "M" },
1383
+ workspace: { current_dir: nonexist },
1384
+ context_window: { used_percentage: 10 },
1385
+ cost: { total_cost_usd: 0, total_duration_ms: 125000 },
1386
+ agent: {},
1387
+ worktree: {},
1388
+ });
1389
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1390
+ encoding: "utf8", timeout: 5000,
1391
+ input: json,
1392
+ stdio: ["pipe", "pipe", "pipe"],
1393
+ });
1394
+ assert.equal(r.status, 0);
1395
+ assert.match(r.stdout, /2m/);
1396
+ });
1397
+
1398
+ it("statusline renders agent name", () => {
1399
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-agent-${process.pid}`);
1400
+ const json = JSON.stringify({
1401
+ model: { display_name: "M" },
1402
+ workspace: { current_dir: nonexist },
1403
+ context_window: { used_percentage: 10 },
1404
+ cost: { total_cost_usd: 0 },
1405
+ agent: { name: "qualia-planner" },
1406
+ worktree: {},
1407
+ });
1408
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1409
+ encoding: "utf8", timeout: 5000,
1410
+ input: json,
1411
+ stdio: ["pipe", "pipe", "pipe"],
1412
+ });
1413
+ assert.equal(r.status, 0);
1414
+ assert.match(r.stdout, /qualia-planner/);
1415
+ });
1416
+
1417
+ it("statusline handles empty stdin gracefully", () => {
1418
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1419
+ encoding: "utf8", timeout: 5000,
1420
+ input: "",
1421
+ stdio: ["pipe", "pipe", "pipe"],
1422
+ });
1423
+ assert.equal(r.status, 0);
1424
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1425
+ assert.equal(lines.length, 2);
1426
+ });
1427
+
1428
+ it("statusline handles invalid JSON gracefully", () => {
1429
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1430
+ encoding: "utf8", timeout: 5000,
1431
+ input: "not json{",
1432
+ stdio: ["pipe", "pipe", "pipe"],
1433
+ });
1434
+ assert.equal(r.status, 0);
1435
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1436
+ assert.equal(lines.length, 2);
1437
+ });
1438
+
1439
+ it("statusline shows phase info from tracking.json", () => {
1440
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-sl-phase-"));
1441
+ try {
1442
+ fs.mkdirSync(path.join(tmpDir, ".planning"), { recursive: true });
1443
+ fs.writeFileSync(path.join(tmpDir, ".planning", "tracking.json"),
1444
+ JSON.stringify({ phase: 2, total_phases: 4, status: "built" }));
1445
+ const json = JSON.stringify({
1446
+ model: { display_name: "M" },
1447
+ workspace: { current_dir: tmpDir },
1448
+ context_window: { used_percentage: 10 },
1449
+ cost: { total_cost_usd: 0 },
1450
+ agent: {},
1451
+ worktree: {},
1452
+ });
1453
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1454
+ encoding: "utf8", timeout: 5000,
1455
+ input: json,
1456
+ stdio: ["pipe", "pipe", "pipe"],
1457
+ });
1458
+ assert.equal(r.status, 0);
1459
+ assert.match(r.stdout, /P2\/4/);
1460
+ assert.match(r.stdout, /built/);
1461
+ } finally {
1462
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1463
+ }
1464
+ });
1465
+
1466
+ it("statusline handles malformed tracking.json", () => {
1467
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-sl-bad-"));
1468
+ try {
1469
+ fs.mkdirSync(path.join(tmpDir, ".planning"), { recursive: true });
1470
+ fs.writeFileSync(path.join(tmpDir, ".planning", "tracking.json"), "not json");
1471
+ const json = JSON.stringify({
1472
+ model: { display_name: "M" },
1473
+ workspace: { current_dir: tmpDir },
1474
+ context_window: { used_percentage: 10 },
1475
+ cost: { total_cost_usd: 0 },
1476
+ agent: {},
1477
+ worktree: {},
1478
+ });
1479
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1480
+ encoding: "utf8", timeout: 5000,
1481
+ input: json,
1482
+ stdio: ["pipe", "pipe", "pipe"],
1483
+ });
1484
+ assert.equal(r.status, 0);
1485
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1486
+ assert.equal(lines.length, 2);
1487
+ } finally {
1488
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1489
+ }
1490
+ });
1491
+ });
1492
+
1493
+ // ═══════════════════════════════════════════════════════════
1494
+ // qualia-ui.js Tests
1495
+ // ═══════════════════════════════════════════════════════════
1496
+
1497
+ describe("qualia-ui.js", () => {
1498
+ const UI = path.join(BIN, "qualia-ui.js");
1499
+
1500
+ function runUI(args, opts = {}) {
1501
+ const tmpHome = opts.home || os.tmpdir();
1502
+ const r = spawnSync(process.execPath, [UI, ...args], {
1503
+ encoding: "utf8", timeout: 5000,
1504
+ cwd: opts.cwd || os.tmpdir(),
1505
+ env: { ...process.env, HOME: tmpHome, USERPROFILE: tmpHome },
1506
+ stdio: ["pipe", "pipe", "pipe"],
1507
+ });
1508
+ return { stdout: r.stdout || "", stderr: r.stderr || "", status: r.status };
1509
+ }
1510
+
1511
+ it("banner router renders QUALIA + SMART ROUTER", () => {
1512
+ const r = runUI(["banner", "router"]);
1513
+ assert.equal(r.status, 0);
1514
+ const clean = stripAnsi(r.stdout);
1515
+ assert.match(clean, /QUALIA/);
1516
+ assert.match(clean, /SMART ROUTER/);
1517
+ });
1518
+
1519
+ it("banner plan 1 foundation renders PLANNING + Phase 1", () => {
1520
+ const r = runUI(["banner", "plan", "1", "foundation"]);
1521
+ assert.equal(r.status, 0);
1522
+ const clean = stripAnsi(r.stdout);
1523
+ assert.match(clean, /PLANNING/);
1524
+ assert.match(clean, /Phase 1/);
1525
+ });
1526
+
1527
+ it("banner unknown action falls back to uppercased label", () => {
1528
+ const r = runUI(["banner", "frobnicate"]);
1529
+ assert.equal(r.status, 0);
1530
+ const clean = stripAnsi(r.stdout);
1531
+ assert.match(clean, /QUALIA/);
1532
+ assert.match(clean, /FROBNICATE/);
1533
+ });
1534
+
1535
+ it("context without project shows No project detected", () => {
1536
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ui-"));
1537
+ try {
1538
+ const r = runUI(["context"], { cwd: tmpDir, home: tmpDir });
1539
+ assert.equal(r.status, 0);
1540
+ const clean = stripAnsi(r.stdout);
1541
+ assert.match(clean, /Project/);
1542
+ assert.match(clean, /No project detected/);
1543
+ } finally {
1544
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1545
+ }
1546
+ });
1547
+
1548
+ it("ok renders checkmark + message", () => {
1549
+ const r = runUI(["ok", "hello world"]);
1550
+ assert.equal(r.status, 0);
1551
+ const clean = stripAnsi(r.stdout);
1552
+ assert.match(clean, /hello world/);
1553
+ assert.match(r.stdout, /\u2713/); // checkmark
1554
+ });
1555
+
1556
+ it("fail renders cross + message", () => {
1557
+ const r = runUI(["fail", "nope nope"]);
1558
+ assert.equal(r.status, 0);
1559
+ const clean = stripAnsi(r.stdout);
1560
+ assert.match(clean, /nope nope/);
1561
+ assert.match(r.stdout, /\u2717/); // cross
1562
+ });
1563
+
1564
+ it("warn renders message", () => {
1565
+ const r = runUI(["warn", "careful"]);
1566
+ assert.equal(r.status, 0);
1567
+ assert.match(stripAnsi(r.stdout), /careful/);
1568
+ });
1569
+
1570
+ it("info renders message", () => {
1571
+ const r = runUI(["info", "just fyi"]);
1572
+ assert.equal(r.status, 0);
1573
+ assert.match(stripAnsi(r.stdout), /just fyi/);
1574
+ });
1575
+
1576
+ it("divider renders horizontal rule", () => {
1577
+ const r = runUI(["divider"]);
1578
+ assert.equal(r.status, 0);
1579
+ assert.match(r.stdout, /\u2501/); // ━ character
1580
+ });
1581
+
1582
+ it("spawn renders agent + description", () => {
1583
+ const r = runUI(["spawn", "builder", "task 3"]);
1584
+ assert.equal(r.status, 0);
1585
+ const clean = stripAnsi(r.stdout);
1586
+ assert.match(clean, /Spawning/);
1587
+ assert.match(clean, /builder/);
1588
+ assert.match(clean, /task 3/);
1589
+ });
1590
+
1591
+ it("wave renders wave header with task count", () => {
1592
+ const r = runUI(["wave", "1", "3", "5"]);
1593
+ assert.equal(r.status, 0);
1594
+ const clean = stripAnsi(r.stdout);
1595
+ assert.match(clean, /Wave 1\/3/);
1596
+ assert.match(clean, /5 tasks/);
1597
+ });
1598
+
1599
+ it("task renders number + title", () => {
1600
+ const r = runUI(["task", "2", "Build login form"]);
1601
+ assert.equal(r.status, 0);
1602
+ const clean = stripAnsi(r.stdout);
1603
+ assert.match(clean, /Build login form/);
1604
+ assert.match(clean, /2\./);
1605
+ });
1606
+
1607
+ it("done renders checkmark + title + commit", () => {
1608
+ const r = runUI(["done", "3", "TaskDone", "abc1234"]);
1609
+ assert.equal(r.status, 0);
1610
+ const clean = stripAnsi(r.stdout);
1611
+ assert.match(clean, /TaskDone/);
1612
+ assert.match(clean, /abc1234/);
1613
+ assert.match(r.stdout, /\u2713/);
1614
+ });
1615
+
1616
+ it("next renders next command", () => {
1617
+ const r = runUI(["next", "/qualia-build"]);
1618
+ assert.equal(r.status, 0);
1619
+ const clean = stripAnsi(r.stdout);
1620
+ assert.match(clean, /Next:/);
1621
+ assert.match(clean, /\/qualia-build/);
1622
+ });
1623
+
1624
+ it("end renders final status + next command", () => {
1625
+ const r = runUI(["end", "SHIPPED", "/qualia-handoff"]);
1626
+ assert.equal(r.status, 0);
1627
+ const clean = stripAnsi(r.stdout);
1628
+ assert.match(clean, /SHIPPED/);
1629
+ assert.match(clean, /\/qualia-handoff/);
1630
+ });
1631
+
1632
+ it("unknown command exits 1 with Usage on stderr", () => {
1633
+ const r = runUI(["frobnicate"]);
1634
+ assert.equal(r.status, 1);
1635
+ assert.match(r.stderr, /Usage:/);
1636
+ });
1637
+
1638
+ it("banner router with config shows OWNER + name", () => {
1639
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ui-cfg-"));
1640
+ try {
1641
+ fs.mkdirSync(path.join(tmpHome, ".claude"), { recursive: true });
1642
+ fs.writeFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), JSON.stringify({
1643
+ code: "QS-FAWZI-01",
1644
+ installed_by: "Fawzi Goussous",
1645
+ role: "OWNER",
1646
+ version: "2.8.1",
1647
+ installed_at: "2026-04-10",
1648
+ }));
1649
+ const r = runUI(["banner", "router"], { home: tmpHome, cwd: tmpHome });
1650
+ assert.equal(r.status, 0);
1651
+ const clean = stripAnsi(r.stdout);
1652
+ assert.match(clean, /OWNER/);
1653
+ assert.match(clean, /Fawzi Goussous/);
1654
+ } finally {
1655
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1656
+ }
1657
+ });
1658
+ });
1659
+
1660
+ // ═══════════════════════════════════════════════════════════
1661
+ // Install Tests
1662
+ // ═══════════════════════════════════════════════════════════
1663
+
1664
+ describe("install.js", () => {
1665
+ const INSTALL = path.join(BIN, "install.js");
1666
+
1667
+ function runInstall(code, home) {
1668
+ const r = spawnSync(process.execPath, [INSTALL], {
1669
+ encoding: "utf8", timeout: 15000,
1670
+ input: code + "\n",
1671
+ env: { ...process.env, HOME: home, USERPROFILE: home },
1672
+ stdio: ["pipe", "pipe", "pipe"],
1673
+ });
1674
+ return { stdout: r.stdout || "", stderr: r.stderr || "", status: r.status };
1675
+ }
1676
+
1677
+ it("valid code installs everything", () => {
1678
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1679
+ try {
1680
+ const r = runInstall("QS-FAWZI-01", tmpHome);
1681
+ assert.equal(r.status, 0);
1682
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "skills", "qualia", "SKILL.md")));
1683
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "hooks", "session-start.js")));
1684
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "bin", "state.js")));
1685
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "bin", "qualia-ui.js")));
1686
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "bin", "statusline.js")));
1687
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", ".qualia-config.json")));
1688
+ } finally {
1689
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1690
+ }
1691
+ });
1692
+
1693
+ it("config JSON has correct fields", () => {
1694
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1695
+ try {
1696
+ runInstall("QS-FAWZI-01", tmpHome);
1697
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1698
+ assert.equal(config.code, "QS-FAWZI-01");
1699
+ assert.equal(config.installed_by, "Fawzi Goussous");
1700
+ assert.equal(config.role, "OWNER");
1701
+ } finally {
1702
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1703
+ }
1704
+ });
1705
+
1706
+ it("CLAUDE.md role placeholder replaced", () => {
1707
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1708
+ try {
1709
+ runInstall("QS-FAWZI-01", tmpHome);
1710
+ const claude = fs.readFileSync(path.join(tmpHome, ".claude", "CLAUDE.md"), "utf8");
1711
+ assert.match(claude, /Role: OWNER/);
1712
+ assert.doesNotMatch(claude, /\{\{ROLE\}\}/);
1713
+ } finally {
1714
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1715
+ }
1716
+ });
1717
+
1718
+ it("7 hooks installed (block-env-edit removed in v3.2.0)", () => {
1719
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1720
+ try {
1721
+ runInstall("QS-FAWZI-01", tmpHome);
1722
+ const hooks = fs.readdirSync(path.join(tmpHome, ".claude", "hooks")).filter(f => f.endsWith(".js"));
1723
+ assert.equal(hooks.length, 7);
1724
+ } finally {
1725
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1726
+ }
1727
+ });
1728
+
1729
+ it("settings.json has hooks and statusLine", () => {
1730
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1731
+ try {
1732
+ runInstall("QS-FAWZI-01", tmpHome);
1733
+ const settings = fs.readFileSync(path.join(tmpHome, ".claude", "settings.json"), "utf8");
1734
+ assert.match(settings, /SessionStart/);
1735
+ assert.match(settings, /PreToolUse/);
1736
+ assert.match(settings, /statusLine/);
1737
+ } finally {
1738
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1739
+ }
1740
+ });
1741
+
1742
+ it("lowercase code is normalized", () => {
1743
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1744
+ try {
1745
+ const r = runInstall("qs-fawzi-01", tmpHome);
1746
+ assert.equal(r.status, 0);
1747
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1748
+ assert.equal(config.code, "QS-FAWZI-01");
1749
+ } finally {
1750
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1751
+ }
1752
+ });
1753
+
1754
+ it("O/0 typo tolerance in code suffix", () => {
1755
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1756
+ try {
1757
+ const r = runInstall("QS-FAWZI-O1", tmpHome);
1758
+ assert.equal(r.status, 0);
1759
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1760
+ assert.equal(config.code, "QS-FAWZI-01");
1761
+ } finally {
1762
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1763
+ }
1764
+ });
1765
+
1766
+ it("EMPLOYEE role set correctly for MOAYAD", () => {
1767
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1768
+ try {
1769
+ const r = runInstall("QS-MOAYAD-03", tmpHome);
1770
+ assert.equal(r.status, 0);
1771
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1772
+ assert.equal(config.code, "QS-MOAYAD-03");
1773
+ assert.equal(config.installed_by, "Moayad");
1774
+ assert.equal(config.role, "EMPLOYEE");
1775
+ } finally {
1776
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1777
+ }
1778
+ });
1779
+
1780
+ it("invalid code exits 1", () => {
1781
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1782
+ try {
1783
+ const r = runInstall("QS-BOGUS-99", tmpHome);
1784
+ assert.equal(r.status, 1);
1785
+ assert.match(stripAnsi(r.stdout), /Invalid code/);
1786
+ assert.ok(!fs.existsSync(path.join(tmpHome, ".claude", ".qualia-config.json")));
1787
+ } finally {
1788
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1789
+ }
1790
+ });
1791
+
1792
+ it("empty code exits 1", () => {
1793
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1794
+ try {
1795
+ const r = spawnSync(process.execPath, [INSTALL], {
1796
+ encoding: "utf8", timeout: 15000,
1797
+ input: "\n",
1798
+ env: { ...process.env, HOME: tmpHome, USERPROFILE: tmpHome },
1799
+ stdio: ["pipe", "pipe", "pipe"],
1800
+ });
1801
+ assert.equal(r.status, 1);
1802
+ assert.match(stripAnsi(r.stdout || ""), /Invalid code/);
1803
+ } finally {
1804
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1805
+ }
1806
+ });
1807
+
1808
+ it("whitespace-padded code is accepted", () => {
1809
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1810
+ try {
1811
+ const r = runInstall(" QS-FAWZI-01 ", tmpHome);
1812
+ assert.equal(r.status, 0);
1813
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1814
+ assert.equal(config.code, "QS-FAWZI-01");
1815
+ } finally {
1816
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1817
+ }
1818
+ });
1819
+
1820
+ it("settings.json merge preserves custom keys", () => {
1821
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1822
+ try {
1823
+ fs.mkdirSync(path.join(tmpHome, ".claude"), { recursive: true });
1824
+ fs.writeFileSync(path.join(tmpHome, ".claude", "settings.json"), JSON.stringify({
1825
+ customKey: "preserved",
1826
+ env: { MY_CUSTOM_VAR: "hello" },
1827
+ }));
1828
+ const r = runInstall("QS-FAWZI-01", tmpHome);
1829
+ assert.equal(r.status, 0);
1830
+ const settings = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", "settings.json"), "utf8"));
1831
+ assert.equal(settings.customKey, "preserved");
1832
+ assert.equal(settings.env.MY_CUSTOM_VAR, "hello");
1833
+ assert.ok(settings.hooks);
1834
+ assert.ok(settings.statusLine);
1835
+ } finally {
1836
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1837
+ }
1838
+ });
1839
+
1840
+ it("knowledge files created on first install", () => {
1841
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1842
+ try {
1843
+ runInstall("QS-FAWZI-01", tmpHome);
1844
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "knowledge", "learned-patterns.md")));
1845
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "knowledge", "common-fixes.md")));
1846
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "knowledge", "client-prefs.md")));
1847
+ } finally {
1848
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1849
+ }
1850
+ });
1851
+
1852
+ it("re-install preserves user edits in knowledge files", () => {
1853
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1854
+ try {
1855
+ runInstall("QS-FAWZI-01", tmpHome);
1856
+ fs.appendFileSync(path.join(tmpHome, ".claude", "knowledge", "learned-patterns.md"),
1857
+ "\n## CUSTOM LEARNING — DO NOT OVERWRITE\n");
1858
+ runInstall("QS-FAWZI-01", tmpHome);
1859
+ const content = fs.readFileSync(path.join(tmpHome, ".claude", "knowledge", "learned-patterns.md"), "utf8");
1860
+ assert.match(content, /CUSTOM LEARNING/);
1861
+ } finally {
1862
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1863
+ }
1864
+ });
1865
+
1866
+ it("templates copied to qualia-templates/", () => {
1867
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1868
+ try {
1869
+ runInstall("QS-FAWZI-01", tmpHome);
1870
+ const tmplDir = path.join(tmpHome, ".claude", "qualia-templates");
1871
+ assert.ok(fs.existsSync(tmplDir));
1872
+ const files = fs.readdirSync(tmplDir);
1873
+ assert.ok(files.length > 0, `Expected templates, found ${files.length}`);
1874
+ } finally {
1875
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1876
+ }
1877
+ });
1878
+
1879
+ it("agents copied", () => {
1880
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1881
+ try {
1882
+ runInstall("QS-FAWZI-01", tmpHome);
1883
+ const agentDir = path.join(tmpHome, ".claude", "agents");
1884
+ assert.ok(fs.existsSync(agentDir));
1885
+ const files = fs.readdirSync(agentDir);
1886
+ assert.ok(files.length > 0);
1887
+ } finally {
1888
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1889
+ }
1890
+ });
1891
+
1892
+ it("rules copied", () => {
1893
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1894
+ try {
1895
+ runInstall("QS-FAWZI-01", tmpHome);
1896
+ const rulesDir = path.join(tmpHome, ".claude", "rules");
1897
+ assert.ok(fs.existsSync(rulesDir));
1898
+ const files = fs.readdirSync(rulesDir);
1899
+ assert.ok(files.length > 0);
1900
+ } finally {
1901
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1902
+ }
1903
+ });
1904
+
1905
+ it("config version matches package.json", () => {
1906
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1907
+ try {
1908
+ runInstall("QS-FAWZI-01", tmpHome);
1909
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1910
+ assert.equal(config.version, PKG_VERSION);
1911
+ } finally {
1912
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1913
+ }
1914
+ });
1915
+ });