qualia-framework 2.6.0 → 3.1.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 (328) hide show
  1. package/CLAUDE.md +63 -0
  2. package/README.md +108 -30
  3. package/agents/builder.md +110 -0
  4. package/agents/planner.md +186 -0
  5. package/agents/qa-browser.md +186 -0
  6. package/agents/verifier.md +369 -0
  7. package/bin/cli.js +691 -492
  8. package/bin/install.js +622 -0
  9. package/bin/qualia-ui.js +284 -0
  10. package/bin/state.js +824 -0
  11. package/bin/statusline.js +252 -0
  12. package/docs/erp-contract.md +161 -0
  13. package/guide.md +63 -0
  14. package/hooks/auto-update.js +117 -0
  15. package/hooks/block-env-edit.js +52 -0
  16. package/hooks/branch-guard.js +68 -0
  17. package/hooks/migration-guard.js +83 -0
  18. package/hooks/pre-compact.js +52 -0
  19. package/hooks/pre-deploy-gate.js +149 -0
  20. package/hooks/pre-push.js +53 -0
  21. package/hooks/session-start.js +126 -0
  22. package/package.json +30 -20
  23. package/rules/design-reference.md +179 -0
  24. package/rules/frontend.md +126 -0
  25. package/rules/infrastructure.md +87 -0
  26. package/skills/qualia/SKILL.md +88 -0
  27. package/skills/qualia-build/SKILL.md +115 -0
  28. package/skills/qualia-debug/SKILL.md +87 -0
  29. package/skills/qualia-design/SKILL.md +99 -0
  30. package/skills/qualia-handoff/SKILL.md +66 -0
  31. package/skills/qualia-help/SKILL.md +60 -0
  32. package/skills/qualia-idk/SKILL.md +8 -0
  33. package/skills/qualia-learn/SKILL.md +111 -0
  34. package/skills/qualia-new/SKILL.md +323 -0
  35. package/skills/qualia-pause/SKILL.md +63 -0
  36. package/skills/qualia-plan/SKILL.md +101 -0
  37. package/skills/qualia-polish/SKILL.md +207 -0
  38. package/skills/qualia-quick/SKILL.md +37 -0
  39. package/skills/qualia-report/SKILL.md +114 -0
  40. package/skills/qualia-resume/SKILL.md +49 -0
  41. package/skills/qualia-review/SKILL.md +161 -0
  42. package/skills/qualia-ship/SKILL.md +90 -0
  43. package/skills/qualia-skill-new/SKILL.md +167 -0
  44. package/skills/qualia-task/SKILL.md +91 -0
  45. package/skills/qualia-test/SKILL.md +134 -0
  46. package/skills/qualia-verify/SKILL.md +113 -0
  47. package/templates/DESIGN.md +475 -0
  48. package/templates/help.html +476 -0
  49. package/templates/plan.md +42 -0
  50. package/templates/project.md +22 -0
  51. package/templates/state.md +27 -0
  52. package/templates/tracking.json +20 -0
  53. package/tests/bin.test.sh +687 -0
  54. package/tests/hooks.test.sh +384 -0
  55. package/tests/runner.js +1956 -0
  56. package/tests/state.test.sh +713 -0
  57. package/tests/statusline.test.sh +243 -0
  58. package/bin/collect-metrics.sh +0 -62
  59. package/framework/.claudeignore +0 -51
  60. package/framework/CLAUDE.md +0 -51
  61. package/framework/MCP_SETUP.md +0 -229
  62. package/framework/agents/architecture-strategist.md +0 -53
  63. package/framework/agents/backend-agent.md +0 -150
  64. package/framework/agents/code-simplicity-reviewer.md +0 -86
  65. package/framework/agents/frontend-agent.md +0 -111
  66. package/framework/agents/kieran-typescript-reviewer.md +0 -96
  67. package/framework/agents/performance-oracle.md +0 -111
  68. package/framework/agents/qualia-codebase-mapper.md +0 -761
  69. package/framework/agents/qualia-debugger.md +0 -1204
  70. package/framework/agents/qualia-executor.md +0 -882
  71. package/framework/agents/qualia-integration-checker.md +0 -424
  72. package/framework/agents/qualia-phase-researcher.md +0 -457
  73. package/framework/agents/qualia-plan-checker.md +0 -700
  74. package/framework/agents/qualia-planner.md +0 -1245
  75. package/framework/agents/qualia-project-researcher.md +0 -603
  76. package/framework/agents/qualia-research-synthesizer.md +0 -200
  77. package/framework/agents/qualia-roadmapper.md +0 -606
  78. package/framework/agents/qualia-verifier.md +0 -686
  79. package/framework/agents/red-team-qa.md +0 -130
  80. package/framework/agents/security-auditor.md +0 -72
  81. package/framework/agents/team-orchestrator.md +0 -229
  82. package/framework/agents/teams/framework-audit-team.md +0 -66
  83. package/framework/agents/teams/full-stack-team.md +0 -48
  84. package/framework/agents/teams/optimize-team.md +0 -53
  85. package/framework/agents/teams/review-team.md +0 -70
  86. package/framework/agents/teams/ship-team.md +0 -86
  87. package/framework/agents/test-agent.md +0 -182
  88. package/framework/hooks/auto-format.sh +0 -54
  89. package/framework/hooks/block-env-edit.sh +0 -42
  90. package/framework/hooks/branch-guard.sh +0 -43
  91. package/framework/hooks/confirm-delete.sh +0 -59
  92. package/framework/hooks/migration-validate.sh +0 -77
  93. package/framework/hooks/notification-speak.sh +0 -16
  94. package/framework/hooks/pre-commit.sh +0 -100
  95. package/framework/hooks/pre-compact.sh +0 -56
  96. package/framework/hooks/pre-deploy-gate.sh +0 -160
  97. package/framework/hooks/qualia-colors.sh +0 -32
  98. package/framework/hooks/retention-cleanup.sh +0 -62
  99. package/framework/hooks/save-session-state.sh +0 -185
  100. package/framework/hooks/session-context-loader.sh +0 -96
  101. package/framework/hooks/session-learn.sh +0 -32
  102. package/framework/hooks/skill-announce.sh +0 -123
  103. package/framework/hooks/tool-error-announce.sh +0 -27
  104. package/framework/install.ps1 +0 -323
  105. package/framework/install.sh +0 -313
  106. package/framework/qualia-framework/VERSION +0 -1
  107. package/framework/qualia-framework/assets/qualia-logo.png +0 -0
  108. package/framework/qualia-framework/bin/collect-metrics.sh +0 -67
  109. package/framework/qualia-framework/bin/generate-report-docx.py +0 -429
  110. package/framework/qualia-framework/bin/qualia-tools.js +0 -2201
  111. package/framework/qualia-framework/bin/qualia-tools.test.js +0 -1054
  112. package/framework/qualia-framework/references/checkpoints.md +0 -775
  113. package/framework/qualia-framework/references/completion-checklists.md +0 -359
  114. package/framework/qualia-framework/references/continuation-format.md +0 -249
  115. package/framework/qualia-framework/references/continuation-prompt.md +0 -97
  116. package/framework/qualia-framework/references/decimal-phase-calculation.md +0 -65
  117. package/framework/qualia-framework/references/design-quality.md +0 -56
  118. package/framework/qualia-framework/references/employee-guide.md +0 -167
  119. package/framework/qualia-framework/references/git-integration.md +0 -254
  120. package/framework/qualia-framework/references/git-planning-commit.md +0 -50
  121. package/framework/qualia-framework/references/model-profile-resolution.md +0 -32
  122. package/framework/qualia-framework/references/model-profiles.md +0 -73
  123. package/framework/qualia-framework/references/phase-argument-parsing.md +0 -61
  124. package/framework/qualia-framework/references/planning-config.md +0 -195
  125. package/framework/qualia-framework/references/questioning.md +0 -141
  126. package/framework/qualia-framework/references/tdd.md +0 -263
  127. package/framework/qualia-framework/references/ui-brand.md +0 -160
  128. package/framework/qualia-framework/references/verification-patterns.md +0 -612
  129. package/framework/qualia-framework/templates/DEBUG.md +0 -159
  130. package/framework/qualia-framework/templates/DESIGN.md +0 -81
  131. package/framework/qualia-framework/templates/UAT.md +0 -247
  132. package/framework/qualia-framework/templates/codebase/architecture.md +0 -255
  133. package/framework/qualia-framework/templates/codebase/concerns.md +0 -310
  134. package/framework/qualia-framework/templates/codebase/conventions.md +0 -307
  135. package/framework/qualia-framework/templates/codebase/integrations.md +0 -280
  136. package/framework/qualia-framework/templates/codebase/stack.md +0 -186
  137. package/framework/qualia-framework/templates/codebase/structure.md +0 -285
  138. package/framework/qualia-framework/templates/codebase/testing.md +0 -480
  139. package/framework/qualia-framework/templates/config.json +0 -35
  140. package/framework/qualia-framework/templates/context.md +0 -283
  141. package/framework/qualia-framework/templates/continue-here.md +0 -78
  142. package/framework/qualia-framework/templates/debug-subagent-prompt.md +0 -91
  143. package/framework/qualia-framework/templates/discovery.md +0 -146
  144. package/framework/qualia-framework/templates/lab-notes.md +0 -16
  145. package/framework/qualia-framework/templates/milestone-archive.md +0 -123
  146. package/framework/qualia-framework/templates/milestone.md +0 -115
  147. package/framework/qualia-framework/templates/phase-prompt.md +0 -567
  148. package/framework/qualia-framework/templates/planner-subagent-prompt.md +0 -117
  149. package/framework/qualia-framework/templates/project.md +0 -184
  150. package/framework/qualia-framework/templates/projects/ai-agent.md +0 -156
  151. package/framework/qualia-framework/templates/projects/mobile-app.md +0 -181
  152. package/framework/qualia-framework/templates/projects/voice-agent.md +0 -134
  153. package/framework/qualia-framework/templates/projects/website.md +0 -137
  154. package/framework/qualia-framework/templates/requirements.md +0 -231
  155. package/framework/qualia-framework/templates/research-project/ARCHITECTURE.md +0 -204
  156. package/framework/qualia-framework/templates/research-project/FEATURES.md +0 -147
  157. package/framework/qualia-framework/templates/research-project/PITFALLS.md +0 -200
  158. package/framework/qualia-framework/templates/research-project/STACK.md +0 -120
  159. package/framework/qualia-framework/templates/research-project/SUMMARY.md +0 -170
  160. package/framework/qualia-framework/templates/research.md +0 -552
  161. package/framework/qualia-framework/templates/roadmap.md +0 -206
  162. package/framework/qualia-framework/templates/state.md +0 -179
  163. package/framework/qualia-framework/templates/summary-complex.md +0 -59
  164. package/framework/qualia-framework/templates/summary-minimal.md +0 -41
  165. package/framework/qualia-framework/templates/summary-standard.md +0 -48
  166. package/framework/qualia-framework/templates/summary.md +0 -246
  167. package/framework/qualia-framework/templates/user-setup.md +0 -311
  168. package/framework/qualia-framework/templates/verification-report.md +0 -322
  169. package/framework/qualia-framework/workflows/add-phase.md +0 -179
  170. package/framework/qualia-framework/workflows/add-todo.md +0 -157
  171. package/framework/qualia-framework/workflows/audit-milestone.md +0 -241
  172. package/framework/qualia-framework/workflows/check-todos.md +0 -176
  173. package/framework/qualia-framework/workflows/complete-milestone.md +0 -858
  174. package/framework/qualia-framework/workflows/diagnose-issues.md +0 -219
  175. package/framework/qualia-framework/workflows/discovery-phase.md +0 -289
  176. package/framework/qualia-framework/workflows/discuss-phase.md +0 -534
  177. package/framework/qualia-framework/workflows/execute-phase.md +0 -559
  178. package/framework/qualia-framework/workflows/execute-plan.md +0 -438
  179. package/framework/qualia-framework/workflows/help.md +0 -470
  180. package/framework/qualia-framework/workflows/insert-phase.md +0 -220
  181. package/framework/qualia-framework/workflows/list-phase-assumptions.md +0 -178
  182. package/framework/qualia-framework/workflows/map-codebase.md +0 -327
  183. package/framework/qualia-framework/workflows/new-milestone.md +0 -363
  184. package/framework/qualia-framework/workflows/new-project.md +0 -982
  185. package/framework/qualia-framework/workflows/pause-work.md +0 -122
  186. package/framework/qualia-framework/workflows/plan-milestone-gaps.md +0 -256
  187. package/framework/qualia-framework/workflows/plan-phase.md +0 -422
  188. package/framework/qualia-framework/workflows/progress.md +0 -389
  189. package/framework/qualia-framework/workflows/quick.md +0 -252
  190. package/framework/qualia-framework/workflows/remove-phase.md +0 -326
  191. package/framework/qualia-framework/workflows/research-phase.md +0 -74
  192. package/framework/qualia-framework/workflows/resume-project.md +0 -306
  193. package/framework/qualia-framework/workflows/set-profile.md +0 -80
  194. package/framework/qualia-framework/workflows/settings.md +0 -145
  195. package/framework/qualia-framework/workflows/transition.md +0 -556
  196. package/framework/qualia-framework/workflows/update.md +0 -197
  197. package/framework/qualia-framework/workflows/verify-phase.md +0 -195
  198. package/framework/qualia-framework/workflows/verify-work.md +0 -625
  199. package/framework/rules/context7.md +0 -14
  200. package/framework/rules/frontend.md +0 -33
  201. package/framework/rules/speed.md +0 -23
  202. package/framework/scripts/__pycache__/say.cpython-314.pyc +0 -0
  203. package/framework/scripts/apply-retention.sh +0 -120
  204. package/framework/scripts/bootstrap-pop-os.sh +0 -354
  205. package/framework/scripts/claude-voice +0 -13
  206. package/framework/scripts/cleanup.sh +0 -131
  207. package/framework/scripts/cowork-mode.sh +0 -141
  208. package/framework/scripts/generate-project-claude-md.sh +0 -153
  209. package/framework/scripts/load-test-webhook.js +0 -172
  210. package/framework/scripts/say.py +0 -236
  211. package/framework/scripts/showcase-video-recorder/ffmpeg-builder.js +0 -167
  212. package/framework/scripts/showcase-video-recorder/playwright-helpers.js +0 -216
  213. package/framework/scripts/speak.py +0 -55
  214. package/framework/scripts/speak.sh +0 -18
  215. package/framework/scripts/status.sh +0 -138
  216. package/framework/scripts/sync-to-framework.sh +0 -65
  217. package/framework/scripts/voice-hotkey.py +0 -227
  218. package/framework/scripts/voice-input.sh +0 -51
  219. package/framework/skills/animate/SKILL.md +0 -202
  220. package/framework/skills/bolder/SKILL.md +0 -144
  221. package/framework/skills/browser-qa/SKILL.md +0 -536
  222. package/framework/skills/clarify/SKILL.md +0 -179
  223. package/framework/skills/client-handoff/SKILL.md +0 -135
  224. package/framework/skills/collab-onboard/SKILL.md +0 -111
  225. package/framework/skills/colorize/SKILL.md +0 -170
  226. package/framework/skills/critique/SKILL.md +0 -126
  227. package/framework/skills/deep-research/SKILL.md +0 -240
  228. package/framework/skills/delight/SKILL.md +0 -329
  229. package/framework/skills/deploy/SKILL.md +0 -261
  230. package/framework/skills/deploy-verify/SKILL.md +0 -377
  231. package/framework/skills/deploy-verify/scripts/canary-check.sh +0 -206
  232. package/framework/skills/deploy-verify/scripts/check-console-errors.js +0 -147
  233. package/framework/skills/deploy-verify/scripts/check-cwv.js +0 -139
  234. package/framework/skills/deploy-verify/scripts/project-detect.sh +0 -84
  235. package/framework/skills/deploy-verify/scripts/verify.sh +0 -548
  236. package/framework/skills/design-quieter/SKILL.md +0 -130
  237. package/framework/skills/distill/SKILL.md +0 -149
  238. package/framework/skills/docs-lookup/SKILL.md +0 -79
  239. package/framework/skills/fcm-notifications/SKILL.md +0 -125
  240. package/framework/skills/financial-ledger/SKILL.md +0 -1039
  241. package/framework/skills/frontend-master/NOTICE.md +0 -4
  242. package/framework/skills/frontend-master/SKILL.md +0 -127
  243. package/framework/skills/frontend-master/reference/color-and-contrast.md +0 -132
  244. package/framework/skills/frontend-master/reference/interaction-design.md +0 -123
  245. package/framework/skills/frontend-master/reference/motion-design.md +0 -99
  246. package/framework/skills/frontend-master/reference/responsive-design.md +0 -114
  247. package/framework/skills/frontend-master/reference/spatial-design.md +0 -100
  248. package/framework/skills/frontend-master/reference/typography.md +0 -131
  249. package/framework/skills/frontend-master/reference/ux-writing.md +0 -107
  250. package/framework/skills/harden/SKILL.md +0 -357
  251. package/framework/skills/i18n-rtl/SKILL.md +0 -752
  252. package/framework/skills/learn/SKILL.md +0 -95
  253. package/framework/skills/memory/SKILL.md +0 -50
  254. package/framework/skills/mobile-expo/SKILL.md +0 -977
  255. package/framework/skills/mobile-expo/references/store-checklist.md +0 -550
  256. package/framework/skills/nestjs-backend/README.md +0 -73
  257. package/framework/skills/nestjs-backend/SKILL.md +0 -446
  258. package/framework/skills/nestjs-backend/references/templates.md +0 -1173
  259. package/framework/skills/normalize/SKILL.md +0 -79
  260. package/framework/skills/onboard/SKILL.md +0 -242
  261. package/framework/skills/openrouter-agent/SKILL.md +0 -922
  262. package/framework/skills/polish/SKILL.md +0 -209
  263. package/framework/skills/pr/SKILL.md +0 -66
  264. package/framework/skills/qualia/SKILL.md +0 -199
  265. package/framework/skills/qualia-add-todo/SKILL.md +0 -68
  266. package/framework/skills/qualia-audit-milestone/SKILL.md +0 -95
  267. package/framework/skills/qualia-check-todos/SKILL.md +0 -55
  268. package/framework/skills/qualia-complete-milestone/SKILL.md +0 -134
  269. package/framework/skills/qualia-debug/SKILL.md +0 -149
  270. package/framework/skills/qualia-design/SKILL.md +0 -203
  271. package/framework/skills/qualia-discuss-phase/SKILL.md +0 -72
  272. package/framework/skills/qualia-evolve/SKILL.md +0 -200
  273. package/framework/skills/qualia-execute-phase/SKILL.md +0 -89
  274. package/framework/skills/qualia-framework-audit/SKILL.md +0 -604
  275. package/framework/skills/qualia-guide/SKILL.md +0 -32
  276. package/framework/skills/qualia-help/SKILL.md +0 -114
  277. package/framework/skills/qualia-idk/SKILL.md +0 -352
  278. package/framework/skills/qualia-list-phase-assumptions/SKILL.md +0 -67
  279. package/framework/skills/qualia-new-milestone/SKILL.md +0 -72
  280. package/framework/skills/qualia-new-project/SKILL.md +0 -232
  281. package/framework/skills/qualia-optimize/SKILL.md +0 -417
  282. package/framework/skills/qualia-pause-work/SKILL.md +0 -96
  283. package/framework/skills/qualia-plan-milestone-gaps/SKILL.md +0 -57
  284. package/framework/skills/qualia-plan-phase/SKILL.md +0 -104
  285. package/framework/skills/qualia-production-check/SKILL.md +0 -0
  286. package/framework/skills/qualia-progress/SKILL.md +0 -53
  287. package/framework/skills/qualia-quick/SKILL.md +0 -89
  288. package/framework/skills/qualia-report/SKILL.md +0 -166
  289. package/framework/skills/qualia-research-phase/SKILL.md +0 -88
  290. package/framework/skills/qualia-resume-work/SKILL.md +0 -62
  291. package/framework/skills/qualia-review/SKILL.md +0 -263
  292. package/framework/skills/qualia-start/SKILL.md +0 -161
  293. package/framework/skills/qualia-verify-work/SKILL.md +0 -132
  294. package/framework/skills/rag/SKILL.md +0 -750
  295. package/framework/skills/responsive/SKILL.md +0 -231
  296. package/framework/skills/retro/SKILL.md +0 -284
  297. package/framework/skills/sakani-conventions/SKILL.md +0 -136
  298. package/framework/skills/sakani-conventions/evals/evals.json +0 -23
  299. package/framework/skills/sakani-conventions/references/entities.md +0 -365
  300. package/framework/skills/sakani-conventions/references/error-codes.md +0 -95
  301. package/framework/skills/seo-master/SKILL.md +0 -490
  302. package/framework/skills/seo-master/references/checklist.md +0 -199
  303. package/framework/skills/seo-master/references/structured-data.md +0 -609
  304. package/framework/skills/ship/SKILL.md +0 -239
  305. package/framework/skills/stack-researcher/SKILL.md +0 -215
  306. package/framework/skills/status/SKILL.md +0 -154
  307. package/framework/skills/status/scripts/health-check.sh +0 -562
  308. package/framework/skills/subscription-payments/SKILL.md +0 -250
  309. package/framework/skills/supabase/SKILL.md +0 -973
  310. package/framework/skills/supabase/references/templates.md +0 -159
  311. package/framework/skills/team/SKILL.md +0 -67
  312. package/framework/skills/test-runner/SKILL.md +0 -202
  313. package/framework/skills/voice-agent/SKILL.md +0 -1312
  314. package/framework/skills/zoho-workflow/SKILL.md +0 -51
  315. package/framework/statusline-command.sh +0 -117
  316. package/framework/teams/default/inboxes/plan-04.json +0 -9
  317. package/framework/teams/review-team.md +0 -75
  318. package/framework/teams/ship-team.md +0 -86
  319. package/profiles/fawzi.json +0 -16
  320. package/profiles/hasan.json +0 -16
  321. package/profiles/moayad.json +0 -16
  322. package/templates/CLAUDE-owner.md +0 -52
  323. package/templates/CLAUDE.md.hbs +0 -58
  324. package/templates/env.claude.template +0 -12
  325. package/templates/settings.json +0 -172
  326. package/uninstall.sh +0 -90
  327. /package/{framework/rules → rules}/deployment.md +0 -0
  328. /package/{framework/rules → rules}/security.md +0 -0
@@ -0,0 +1,1956 @@
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 >= 8, `Expected 8+ 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 ---
884
+
885
+ it("block-env-edit blocks .env files", () => {
886
+ const r = runHook("block-env-edit.js", {
887
+ tool_input: { file_path: "/project/.env" },
888
+ });
889
+ assert.equal(r.status, 2);
890
+ });
891
+
892
+ it("block-env-edit blocks .env.local files", () => {
893
+ const r = runHook("block-env-edit.js", {
894
+ tool_input: { file_path: "/project/.env.local" },
895
+ });
896
+ assert.equal(r.status, 2);
897
+ });
898
+
899
+ it("block-env-edit blocks .env.production files", () => {
900
+ const r = runHook("block-env-edit.js", {
901
+ tool_input: { file_path: ".env.production" },
902
+ });
903
+ assert.equal(r.status, 2);
904
+ });
905
+
906
+ it("block-env-edit blocks Windows-style .env paths", () => {
907
+ const r = runHook("block-env-edit.js", {
908
+ tool_input: { file_path: "C:\\project\\.env.local" },
909
+ });
910
+ assert.equal(r.status, 2);
911
+ });
912
+
913
+ it("block-env-edit allows non-env files", () => {
914
+ const r = runHook("block-env-edit.js", {
915
+ tool_input: { file_path: "src/app.tsx" },
916
+ });
917
+ assert.equal(r.status, 0);
918
+ });
919
+
920
+ it("block-env-edit allows component files", () => {
921
+ const r = runHook("block-env-edit.js", {
922
+ tool_input: { file_path: "components/Footer.tsx" },
923
+ });
924
+ assert.equal(r.status, 0);
925
+ });
926
+
927
+ // --- pre-push.js ---
928
+
929
+ it("pre-push.js references tracking.json", () => {
930
+ const content = fs.readFileSync(path.join(HOOKS, "pre-push.js"), "utf8");
931
+ assert.match(content, /tracking\.json/);
932
+ });
933
+
934
+ it("pre-push.js stamps last_commit", () => {
935
+ const content = fs.readFileSync(path.join(HOOKS, "pre-push.js"), "utf8");
936
+ assert.match(content, /last_commit/);
937
+ });
938
+
939
+ it("pre-push.js exits 0 with no tracking.json", () => {
940
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-push-"));
941
+ try {
942
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-push.js")], {
943
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
944
+ });
945
+ assert.equal(r.status, 0);
946
+ } finally {
947
+ fs.rmSync(tmpDir, { recursive: true, force: true });
948
+ }
949
+ });
950
+
951
+ // --- session-start.js ---
952
+
953
+ it("session-start.js exits 0 with no project", () => {
954
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ss-"));
955
+ try {
956
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "session-start.js")], {
957
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
958
+ });
959
+ assert.equal(r.status, 0);
960
+ } finally {
961
+ fs.rmSync(tmpDir, { recursive: true, force: true });
962
+ }
963
+ });
964
+
965
+ it("session-start.js exits 0 with STATE.md", () => {
966
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ss-"));
967
+ try {
968
+ const planningDir = path.join(tmpDir, ".planning");
969
+ fs.mkdirSync(planningDir, { recursive: true });
970
+ fs.writeFileSync(path.join(planningDir, "STATE.md"), "# Project State\nPhase: 1 of 3 — Foundation\nStatus: setup\n");
971
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "session-start.js")], {
972
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
973
+ });
974
+ assert.equal(r.status, 0);
975
+ } finally {
976
+ fs.rmSync(tmpDir, { recursive: true, force: true });
977
+ }
978
+ });
979
+
980
+ // --- pre-compact.js ---
981
+
982
+ it("pre-compact.js exits 0 with no STATE.md", () => {
983
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pc-"));
984
+ try {
985
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-compact.js")], {
986
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
987
+ });
988
+ assert.equal(r.status, 0);
989
+ } finally {
990
+ fs.rmSync(tmpDir, { recursive: true, force: true });
991
+ }
992
+ });
993
+
994
+ // --- auto-update.js ---
995
+
996
+ it("auto-update.js exits 0 and writes cache timestamp", () => {
997
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-au-"));
998
+ try {
999
+ fs.mkdirSync(path.join(tmpHome, ".claude"), { recursive: true });
1000
+ fs.writeFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), JSON.stringify({
1001
+ code: "QS-FAWZI-01", version: "99.99.99",
1002
+ }));
1003
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "auto-update.js")], {
1004
+ encoding: "utf8", timeout: 5000,
1005
+ env: { ...process.env, HOME: tmpHome, USERPROFILE: tmpHome },
1006
+ stdio: ["pipe", "pipe", "pipe"],
1007
+ });
1008
+ assert.equal(r.status, 0);
1009
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", ".qualia-last-update-check")));
1010
+ } finally {
1011
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1012
+ }
1013
+ });
1014
+
1015
+ // --- pre-deploy-gate.js ---
1016
+
1017
+ it("pre-deploy-gate: empty project exits 0", () => {
1018
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1019
+ try {
1020
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1021
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
1022
+ });
1023
+ assert.equal(r.status, 0);
1024
+ } finally {
1025
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1026
+ }
1027
+ });
1028
+
1029
+ it("pre-deploy-gate: no tsconfig -> TS gate skipped", () => {
1030
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1031
+ try {
1032
+ fs.mkdirSync(path.join(tmpDir, "src"), { recursive: true });
1033
+ fs.writeFileSync(path.join(tmpDir, "src", "app.ts"), "export const x = 1;");
1034
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1035
+ encoding: "utf8", cwd: tmpDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
1036
+ });
1037
+ assert.equal(r.status, 0);
1038
+ } finally {
1039
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1040
+ }
1041
+ });
1042
+
1043
+ it("pre-deploy-gate: service_role in app/ -> blocked", () => {
1044
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1045
+ try {
1046
+ fs.mkdirSync(path.join(tmpDir, "app"), { recursive: true });
1047
+ fs.writeFileSync(path.join(tmpDir, "app", "page.tsx"), 'const key = "service_role_literal_leak";\nexport default function P(){return null}');
1048
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1049
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1050
+ });
1051
+ assert.equal(r.status, 1);
1052
+ const combined = r.stdout + r.stderr;
1053
+ assert.match(combined, /BLOCKED/);
1054
+ assert.match(combined, /service_role/);
1055
+ } finally {
1056
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1057
+ }
1058
+ });
1059
+
1060
+ it("pre-deploy-gate: service_role in components/ -> blocked", () => {
1061
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1062
+ try {
1063
+ fs.mkdirSync(path.join(tmpDir, "components"), { recursive: true });
1064
+ fs.writeFileSync(path.join(tmpDir, "components", "Widget.tsx"), 'const key = "service_role_literal_leak";');
1065
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1066
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1067
+ });
1068
+ assert.equal(r.status, 1);
1069
+ } finally {
1070
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1071
+ }
1072
+ });
1073
+
1074
+ it("pre-deploy-gate: .server.ts is exempt", () => {
1075
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1076
+ try {
1077
+ fs.mkdirSync(path.join(tmpDir, "app", "api"), { recursive: true });
1078
+ fs.writeFileSync(path.join(tmpDir, "app", "api", "route.server.ts"), 'const key = "service_role_legit_server_key";');
1079
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1080
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1081
+ });
1082
+ assert.equal(r.status, 0);
1083
+ } finally {
1084
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1085
+ }
1086
+ });
1087
+
1088
+ it("pre-deploy-gate: files under server/ are exempt", () => {
1089
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1090
+ try {
1091
+ fs.mkdirSync(path.join(tmpDir, "app", "server"), { recursive: true });
1092
+ fs.writeFileSync(path.join(tmpDir, "app", "server", "admin.ts"), 'const key = "service_role_legit_server_dir";');
1093
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1094
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1095
+ });
1096
+ assert.equal(r.status, 0);
1097
+ } finally {
1098
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1099
+ }
1100
+ });
1101
+
1102
+ it("pre-deploy-gate: node_modules not walked", () => {
1103
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1104
+ try {
1105
+ fs.mkdirSync(path.join(tmpDir, "app", "node_modules", "evil"), { recursive: true });
1106
+ fs.writeFileSync(path.join(tmpDir, "app", "node_modules", "evil", "index.ts"), 'const key = "service_role_in_node_modules";');
1107
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1108
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1109
+ });
1110
+ assert.equal(r.status, 0);
1111
+ } finally {
1112
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1113
+ }
1114
+ });
1115
+
1116
+ it("pre-deploy-gate: clean project -> all gates pass", () => {
1117
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1118
+ try {
1119
+ fs.mkdirSync(path.join(tmpDir, "app"), { recursive: true });
1120
+ fs.mkdirSync(path.join(tmpDir, "components"), { recursive: true });
1121
+ fs.mkdirSync(path.join(tmpDir, "lib"), { recursive: true });
1122
+ fs.writeFileSync(path.join(tmpDir, "app", "page.tsx"), "export const a = 1;");
1123
+ fs.writeFileSync(path.join(tmpDir, "components", "Widget.tsx"), "export const b = 2;");
1124
+ fs.writeFileSync(path.join(tmpDir, "lib", "util.ts"), "export const c = 3;");
1125
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1126
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1127
+ });
1128
+ assert.equal(r.status, 0);
1129
+ assert.match(r.stdout + r.stderr, /All gates passed/);
1130
+ } finally {
1131
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1132
+ }
1133
+ });
1134
+
1135
+ it("pre-deploy-gate: route.ts with service_role -> exempt", () => {
1136
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1137
+ try {
1138
+ fs.mkdirSync(path.join(tmpDir, "app", "api", "auth"), { recursive: true });
1139
+ fs.writeFileSync(path.join(tmpDir, "app", "api", "auth", "route.ts"),
1140
+ 'const key = process.env.SUPABASE_SERVICE_ROLE_KEY; export async function POST() {}');
1141
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1142
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1143
+ });
1144
+ assert.equal(r.status, 0);
1145
+ } finally {
1146
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1147
+ }
1148
+ });
1149
+
1150
+ it("pre-deploy-gate: middleware.ts with service_role -> exempt", () => {
1151
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1152
+ try {
1153
+ fs.writeFileSync(path.join(tmpDir, "middleware.ts"),
1154
+ 'import { service_role } from "./config"; export function middleware() {}');
1155
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1156
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1157
+ });
1158
+ assert.equal(r.status, 0);
1159
+ } finally {
1160
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1161
+ }
1162
+ });
1163
+
1164
+ it("pre-deploy-gate: app/api/ file with service_role -> exempt", () => {
1165
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1166
+ try {
1167
+ fs.mkdirSync(path.join(tmpDir, "app", "api", "webhook"), { recursive: true });
1168
+ fs.writeFileSync(path.join(tmpDir, "app", "api", "webhook", "route.js"),
1169
+ 'const sr = "service_role"; export async function GET() { return new Response(sr); }');
1170
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1171
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1172
+ });
1173
+ assert.equal(r.status, 0);
1174
+ } finally {
1175
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1176
+ }
1177
+ });
1178
+
1179
+ it("pre-deploy-gate: 'use server' file with service_role -> exempt", () => {
1180
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1181
+ try {
1182
+ fs.mkdirSync(path.join(tmpDir, "app", "admin"), { recursive: true });
1183
+ fs.writeFileSync(path.join(tmpDir, "app", "admin", "actions.ts"),
1184
+ '"use server"\nconst key = process.env.SUPABASE_SERVICE_ROLE_KEY;\nexport async function deleteUser() {}\n');
1185
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1186
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1187
+ });
1188
+ assert.equal(r.status, 0);
1189
+ } finally {
1190
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1191
+ }
1192
+ });
1193
+
1194
+ it("pre-deploy-gate: regular page.tsx with service_role -> blocked", () => {
1195
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-pdg-"));
1196
+ try {
1197
+ fs.mkdirSync(path.join(tmpDir, "app", "admin"), { recursive: true });
1198
+ fs.writeFileSync(path.join(tmpDir, "app", "admin", "page.tsx"),
1199
+ 'const key = "service_role"; export default function Page() { return <div>{key}</div>; }');
1200
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "pre-deploy-gate.js")], {
1201
+ encoding: "utf8", cwd: tmpDir, timeout: 10000, stdio: ["pipe", "pipe", "pipe"],
1202
+ });
1203
+ assert.equal(r.status, 1);
1204
+ } finally {
1205
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1206
+ }
1207
+ });
1208
+
1209
+ // --- branch-guard.js ---
1210
+
1211
+ it("branch-guard: OWNER on main -> 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", "main", "-q"], { cwd: projDir, stdio: "pipe" });
1219
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "OWNER" }));
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: EMPLOYEE on main -> blocked", () => {
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
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1237
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1238
+ spawnSync("git", ["checkout", "-b", "main", "-q"], { cwd: projDir, stdio: "pipe" });
1239
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "EMPLOYEE" }));
1240
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1241
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1242
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1243
+ stdio: ["pipe", "pipe", "pipe"],
1244
+ });
1245
+ assert.equal(r.status, 2);
1246
+ assert.match(r.stdout, /BLOCKED/);
1247
+ } finally {
1248
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1249
+ }
1250
+ });
1251
+
1252
+ it("branch-guard: EMPLOYEE on feature branch -> allowed", () => {
1253
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1254
+ try {
1255
+ const projDir = path.join(tmpDir, "proj");
1256
+ fs.mkdirSync(projDir, { recursive: true });
1257
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1258
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1259
+ spawnSync("git", ["checkout", "-b", "feature/xyz", "-q"], { cwd: projDir, stdio: "pipe" });
1260
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "EMPLOYEE" }));
1261
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1262
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1263
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1264
+ stdio: ["pipe", "pipe", "pipe"],
1265
+ });
1266
+ assert.equal(r.status, 0);
1267
+ } finally {
1268
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1269
+ }
1270
+ });
1271
+
1272
+ it("branch-guard: missing config -> blocked (fails closed)", () => {
1273
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1274
+ try {
1275
+ const projDir = path.join(tmpDir, "proj");
1276
+ fs.mkdirSync(projDir, { recursive: true });
1277
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1278
+ spawnSync("git", ["checkout", "-b", "feature/x", "-q"], { cwd: projDir, stdio: "pipe" });
1279
+ // No .claude/.qualia-config.json
1280
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1281
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1282
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1283
+ stdio: ["pipe", "pipe", "pipe"],
1284
+ });
1285
+ assert.equal(r.status, 2);
1286
+ } finally {
1287
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1288
+ }
1289
+ });
1290
+
1291
+ it("branch-guard: malformed config JSON -> blocked", () => {
1292
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1293
+ try {
1294
+ const projDir = path.join(tmpDir, "proj");
1295
+ fs.mkdirSync(projDir, { recursive: true });
1296
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1297
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1298
+ spawnSync("git", ["checkout", "-b", "feature/x", "-q"], { cwd: projDir, stdio: "pipe" });
1299
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), "not json{");
1300
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1301
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1302
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1303
+ stdio: ["pipe", "pipe", "pipe"],
1304
+ });
1305
+ assert.equal(r.status, 2);
1306
+ } finally {
1307
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1308
+ }
1309
+ });
1310
+
1311
+ it("branch-guard: empty role field -> blocked", () => {
1312
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-bg-"));
1313
+ try {
1314
+ const projDir = path.join(tmpDir, "proj");
1315
+ fs.mkdirSync(projDir, { recursive: true });
1316
+ fs.mkdirSync(path.join(tmpDir, ".claude"), { recursive: true });
1317
+ spawnSync("git", ["init", "-q"], { cwd: projDir });
1318
+ spawnSync("git", ["checkout", "-b", "feature/x", "-q"], { cwd: projDir, stdio: "pipe" });
1319
+ fs.writeFileSync(path.join(tmpDir, ".claude", ".qualia-config.json"), JSON.stringify({ role: "" }));
1320
+ const r = spawnSync(process.execPath, [path.join(HOOKS, "branch-guard.js")], {
1321
+ encoding: "utf8", cwd: projDir, timeout: 5000,
1322
+ env: { ...process.env, HOME: tmpDir, USERPROFILE: tmpDir },
1323
+ stdio: ["pipe", "pipe", "pipe"],
1324
+ });
1325
+ assert.equal(r.status, 2);
1326
+ } finally {
1327
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1328
+ }
1329
+ });
1330
+ });
1331
+
1332
+ // ═══════════════════════════════════════════════════════════
1333
+ // Statusline Tests
1334
+ // ═══════════════════════════════════════════════════════════
1335
+
1336
+ describe("Statusline", () => {
1337
+ it("statusline.js passes syntax check", () => {
1338
+ const r = spawnSync(process.execPath, ["--check", path.join(BIN, "statusline.js")], {
1339
+ encoding: "utf8", timeout: 5000,
1340
+ });
1341
+ assert.equal(r.status, 0);
1342
+ });
1343
+
1344
+ it("statusline.js runs without crashing", () => {
1345
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1346
+ encoding: "utf8", timeout: 5000,
1347
+ env: { ...process.env, HOME: os.tmpdir(), USERPROFILE: os.tmpdir() },
1348
+ stdio: ["pipe", "pipe", "pipe"],
1349
+ });
1350
+ assert.equal(r.status, 0);
1351
+ });
1352
+
1353
+ it("qualia-ui.js passes syntax check", () => {
1354
+ const r = spawnSync(process.execPath, ["--check", path.join(BIN, "qualia-ui.js")], {
1355
+ encoding: "utf8", timeout: 5000,
1356
+ });
1357
+ assert.equal(r.status, 0);
1358
+ });
1359
+
1360
+ it("statusline renders 2 lines with minimal input", () => {
1361
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-nonexist-${process.pid}`);
1362
+ const json = JSON.stringify({
1363
+ model: { display_name: "Claude Opus 4.6" },
1364
+ workspace: { current_dir: nonexist },
1365
+ context_window: { used_percentage: 0 },
1366
+ cost: { total_cost_usd: 0 },
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
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1377
+ assert.equal(lines.length, 2);
1378
+ assert.match(r.stdout, /qualia-sl-nonexist/);
1379
+ assert.match(r.stdout, /Claude Opus 4\.6/);
1380
+ });
1381
+
1382
+ it("statusline shows cost formatting", () => {
1383
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-cost-${process.pid}`);
1384
+ const json = JSON.stringify({
1385
+ model: { display_name: "M" },
1386
+ workspace: { current_dir: nonexist },
1387
+ context_window: { used_percentage: 10 },
1388
+ cost: { total_cost_usd: 2.47, total_duration_ms: 0 },
1389
+ agent: {},
1390
+ worktree: {},
1391
+ });
1392
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1393
+ encoding: "utf8", timeout: 5000,
1394
+ input: json,
1395
+ stdio: ["pipe", "pipe", "pipe"],
1396
+ });
1397
+ assert.equal(r.status, 0);
1398
+ assert.match(r.stdout, /\$2\.47/);
1399
+ });
1400
+
1401
+ it("statusline shows duration in seconds under 60s", () => {
1402
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-dur-${process.pid}`);
1403
+ const json = JSON.stringify({
1404
+ model: { display_name: "M" },
1405
+ workspace: { current_dir: nonexist },
1406
+ context_window: { used_percentage: 10 },
1407
+ cost: { total_cost_usd: 0, total_duration_ms: 45000 },
1408
+ agent: {},
1409
+ worktree: {},
1410
+ });
1411
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1412
+ encoding: "utf8", timeout: 5000,
1413
+ input: json,
1414
+ stdio: ["pipe", "pipe", "pipe"],
1415
+ });
1416
+ assert.equal(r.status, 0);
1417
+ assert.match(r.stdout, /45s/);
1418
+ });
1419
+
1420
+ it("statusline shows duration in minutes over 60s", () => {
1421
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-durm-${process.pid}`);
1422
+ const json = JSON.stringify({
1423
+ model: { display_name: "M" },
1424
+ workspace: { current_dir: nonexist },
1425
+ context_window: { used_percentage: 10 },
1426
+ cost: { total_cost_usd: 0, total_duration_ms: 125000 },
1427
+ agent: {},
1428
+ worktree: {},
1429
+ });
1430
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1431
+ encoding: "utf8", timeout: 5000,
1432
+ input: json,
1433
+ stdio: ["pipe", "pipe", "pipe"],
1434
+ });
1435
+ assert.equal(r.status, 0);
1436
+ assert.match(r.stdout, /2m/);
1437
+ });
1438
+
1439
+ it("statusline renders agent name", () => {
1440
+ const nonexist = path.join(os.tmpdir(), `qualia-sl-agent-${process.pid}`);
1441
+ const json = JSON.stringify({
1442
+ model: { display_name: "M" },
1443
+ workspace: { current_dir: nonexist },
1444
+ context_window: { used_percentage: 10 },
1445
+ cost: { total_cost_usd: 0 },
1446
+ agent: { name: "qualia-planner" },
1447
+ worktree: {},
1448
+ });
1449
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1450
+ encoding: "utf8", timeout: 5000,
1451
+ input: json,
1452
+ stdio: ["pipe", "pipe", "pipe"],
1453
+ });
1454
+ assert.equal(r.status, 0);
1455
+ assert.match(r.stdout, /qualia-planner/);
1456
+ });
1457
+
1458
+ it("statusline handles empty stdin gracefully", () => {
1459
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1460
+ encoding: "utf8", timeout: 5000,
1461
+ input: "",
1462
+ stdio: ["pipe", "pipe", "pipe"],
1463
+ });
1464
+ assert.equal(r.status, 0);
1465
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1466
+ assert.equal(lines.length, 2);
1467
+ });
1468
+
1469
+ it("statusline handles invalid JSON gracefully", () => {
1470
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1471
+ encoding: "utf8", timeout: 5000,
1472
+ input: "not json{",
1473
+ stdio: ["pipe", "pipe", "pipe"],
1474
+ });
1475
+ assert.equal(r.status, 0);
1476
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1477
+ assert.equal(lines.length, 2);
1478
+ });
1479
+
1480
+ it("statusline shows phase info from tracking.json", () => {
1481
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-sl-phase-"));
1482
+ try {
1483
+ fs.mkdirSync(path.join(tmpDir, ".planning"), { recursive: true });
1484
+ fs.writeFileSync(path.join(tmpDir, ".planning", "tracking.json"),
1485
+ JSON.stringify({ phase: 2, total_phases: 4, status: "built" }));
1486
+ const json = JSON.stringify({
1487
+ model: { display_name: "M" },
1488
+ workspace: { current_dir: tmpDir },
1489
+ context_window: { used_percentage: 10 },
1490
+ cost: { total_cost_usd: 0 },
1491
+ agent: {},
1492
+ worktree: {},
1493
+ });
1494
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1495
+ encoding: "utf8", timeout: 5000,
1496
+ input: json,
1497
+ stdio: ["pipe", "pipe", "pipe"],
1498
+ });
1499
+ assert.equal(r.status, 0);
1500
+ assert.match(r.stdout, /P2\/4/);
1501
+ assert.match(r.stdout, /built/);
1502
+ } finally {
1503
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1504
+ }
1505
+ });
1506
+
1507
+ it("statusline handles malformed tracking.json", () => {
1508
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-sl-bad-"));
1509
+ try {
1510
+ fs.mkdirSync(path.join(tmpDir, ".planning"), { recursive: true });
1511
+ fs.writeFileSync(path.join(tmpDir, ".planning", "tracking.json"), "not json");
1512
+ const json = JSON.stringify({
1513
+ model: { display_name: "M" },
1514
+ workspace: { current_dir: tmpDir },
1515
+ context_window: { used_percentage: 10 },
1516
+ cost: { total_cost_usd: 0 },
1517
+ agent: {},
1518
+ worktree: {},
1519
+ });
1520
+ const r = spawnSync(process.execPath, [path.join(BIN, "statusline.js")], {
1521
+ encoding: "utf8", timeout: 5000,
1522
+ input: json,
1523
+ stdio: ["pipe", "pipe", "pipe"],
1524
+ });
1525
+ assert.equal(r.status, 0);
1526
+ const lines = r.stdout.split("\n").filter(l => l.length > 0);
1527
+ assert.equal(lines.length, 2);
1528
+ } finally {
1529
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1530
+ }
1531
+ });
1532
+ });
1533
+
1534
+ // ═══════════════════════════════════════════════════════════
1535
+ // qualia-ui.js Tests
1536
+ // ═══════════════════════════════════════════════════════════
1537
+
1538
+ describe("qualia-ui.js", () => {
1539
+ const UI = path.join(BIN, "qualia-ui.js");
1540
+
1541
+ function runUI(args, opts = {}) {
1542
+ const tmpHome = opts.home || os.tmpdir();
1543
+ const r = spawnSync(process.execPath, [UI, ...args], {
1544
+ encoding: "utf8", timeout: 5000,
1545
+ cwd: opts.cwd || os.tmpdir(),
1546
+ env: { ...process.env, HOME: tmpHome, USERPROFILE: tmpHome },
1547
+ stdio: ["pipe", "pipe", "pipe"],
1548
+ });
1549
+ return { stdout: r.stdout || "", stderr: r.stderr || "", status: r.status };
1550
+ }
1551
+
1552
+ it("banner router renders QUALIA + SMART ROUTER", () => {
1553
+ const r = runUI(["banner", "router"]);
1554
+ assert.equal(r.status, 0);
1555
+ const clean = stripAnsi(r.stdout);
1556
+ assert.match(clean, /QUALIA/);
1557
+ assert.match(clean, /SMART ROUTER/);
1558
+ });
1559
+
1560
+ it("banner plan 1 foundation renders PLANNING + Phase 1", () => {
1561
+ const r = runUI(["banner", "plan", "1", "foundation"]);
1562
+ assert.equal(r.status, 0);
1563
+ const clean = stripAnsi(r.stdout);
1564
+ assert.match(clean, /PLANNING/);
1565
+ assert.match(clean, /Phase 1/);
1566
+ });
1567
+
1568
+ it("banner unknown action falls back to uppercased label", () => {
1569
+ const r = runUI(["banner", "frobnicate"]);
1570
+ assert.equal(r.status, 0);
1571
+ const clean = stripAnsi(r.stdout);
1572
+ assert.match(clean, /QUALIA/);
1573
+ assert.match(clean, /FROBNICATE/);
1574
+ });
1575
+
1576
+ it("context without project shows No project detected", () => {
1577
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ui-"));
1578
+ try {
1579
+ const r = runUI(["context"], { cwd: tmpDir, home: tmpDir });
1580
+ assert.equal(r.status, 0);
1581
+ const clean = stripAnsi(r.stdout);
1582
+ assert.match(clean, /Project/);
1583
+ assert.match(clean, /No project detected/);
1584
+ } finally {
1585
+ fs.rmSync(tmpDir, { recursive: true, force: true });
1586
+ }
1587
+ });
1588
+
1589
+ it("ok renders checkmark + message", () => {
1590
+ const r = runUI(["ok", "hello world"]);
1591
+ assert.equal(r.status, 0);
1592
+ const clean = stripAnsi(r.stdout);
1593
+ assert.match(clean, /hello world/);
1594
+ assert.match(r.stdout, /\u2713/); // checkmark
1595
+ });
1596
+
1597
+ it("fail renders cross + message", () => {
1598
+ const r = runUI(["fail", "nope nope"]);
1599
+ assert.equal(r.status, 0);
1600
+ const clean = stripAnsi(r.stdout);
1601
+ assert.match(clean, /nope nope/);
1602
+ assert.match(r.stdout, /\u2717/); // cross
1603
+ });
1604
+
1605
+ it("warn renders message", () => {
1606
+ const r = runUI(["warn", "careful"]);
1607
+ assert.equal(r.status, 0);
1608
+ assert.match(stripAnsi(r.stdout), /careful/);
1609
+ });
1610
+
1611
+ it("info renders message", () => {
1612
+ const r = runUI(["info", "just fyi"]);
1613
+ assert.equal(r.status, 0);
1614
+ assert.match(stripAnsi(r.stdout), /just fyi/);
1615
+ });
1616
+
1617
+ it("divider renders horizontal rule", () => {
1618
+ const r = runUI(["divider"]);
1619
+ assert.equal(r.status, 0);
1620
+ assert.match(r.stdout, /\u2501/); // ━ character
1621
+ });
1622
+
1623
+ it("spawn renders agent + description", () => {
1624
+ const r = runUI(["spawn", "builder", "task 3"]);
1625
+ assert.equal(r.status, 0);
1626
+ const clean = stripAnsi(r.stdout);
1627
+ assert.match(clean, /Spawning/);
1628
+ assert.match(clean, /builder/);
1629
+ assert.match(clean, /task 3/);
1630
+ });
1631
+
1632
+ it("wave renders wave header with task count", () => {
1633
+ const r = runUI(["wave", "1", "3", "5"]);
1634
+ assert.equal(r.status, 0);
1635
+ const clean = stripAnsi(r.stdout);
1636
+ assert.match(clean, /Wave 1\/3/);
1637
+ assert.match(clean, /5 tasks/);
1638
+ });
1639
+
1640
+ it("task renders number + title", () => {
1641
+ const r = runUI(["task", "2", "Build login form"]);
1642
+ assert.equal(r.status, 0);
1643
+ const clean = stripAnsi(r.stdout);
1644
+ assert.match(clean, /Build login form/);
1645
+ assert.match(clean, /2\./);
1646
+ });
1647
+
1648
+ it("done renders checkmark + title + commit", () => {
1649
+ const r = runUI(["done", "3", "TaskDone", "abc1234"]);
1650
+ assert.equal(r.status, 0);
1651
+ const clean = stripAnsi(r.stdout);
1652
+ assert.match(clean, /TaskDone/);
1653
+ assert.match(clean, /abc1234/);
1654
+ assert.match(r.stdout, /\u2713/);
1655
+ });
1656
+
1657
+ it("next renders next command", () => {
1658
+ const r = runUI(["next", "/qualia-build"]);
1659
+ assert.equal(r.status, 0);
1660
+ const clean = stripAnsi(r.stdout);
1661
+ assert.match(clean, /Next:/);
1662
+ assert.match(clean, /\/qualia-build/);
1663
+ });
1664
+
1665
+ it("end renders final status + next command", () => {
1666
+ const r = runUI(["end", "SHIPPED", "/qualia-handoff"]);
1667
+ assert.equal(r.status, 0);
1668
+ const clean = stripAnsi(r.stdout);
1669
+ assert.match(clean, /SHIPPED/);
1670
+ assert.match(clean, /\/qualia-handoff/);
1671
+ });
1672
+
1673
+ it("unknown command exits 1 with Usage on stderr", () => {
1674
+ const r = runUI(["frobnicate"]);
1675
+ assert.equal(r.status, 1);
1676
+ assert.match(r.stderr, /Usage:/);
1677
+ });
1678
+
1679
+ it("banner router with config shows OWNER + name", () => {
1680
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-ui-cfg-"));
1681
+ try {
1682
+ fs.mkdirSync(path.join(tmpHome, ".claude"), { recursive: true });
1683
+ fs.writeFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), JSON.stringify({
1684
+ code: "QS-FAWZI-01",
1685
+ installed_by: "Fawzi Goussous",
1686
+ role: "OWNER",
1687
+ version: "2.8.1",
1688
+ installed_at: "2026-04-10",
1689
+ }));
1690
+ const r = runUI(["banner", "router"], { home: tmpHome, cwd: tmpHome });
1691
+ assert.equal(r.status, 0);
1692
+ const clean = stripAnsi(r.stdout);
1693
+ assert.match(clean, /OWNER/);
1694
+ assert.match(clean, /Fawzi Goussous/);
1695
+ } finally {
1696
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1697
+ }
1698
+ });
1699
+ });
1700
+
1701
+ // ═══════════════════════════════════════════════════════════
1702
+ // Install Tests
1703
+ // ═══════════════════════════════════════════════════════════
1704
+
1705
+ describe("install.js", () => {
1706
+ const INSTALL = path.join(BIN, "install.js");
1707
+
1708
+ function runInstall(code, home) {
1709
+ const r = spawnSync(process.execPath, [INSTALL], {
1710
+ encoding: "utf8", timeout: 15000,
1711
+ input: code + "\n",
1712
+ env: { ...process.env, HOME: home, USERPROFILE: home },
1713
+ stdio: ["pipe", "pipe", "pipe"],
1714
+ });
1715
+ return { stdout: r.stdout || "", stderr: r.stderr || "", status: r.status };
1716
+ }
1717
+
1718
+ it("valid code installs everything", () => {
1719
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1720
+ try {
1721
+ const r = runInstall("QS-FAWZI-01", tmpHome);
1722
+ assert.equal(r.status, 0);
1723
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "skills", "qualia", "SKILL.md")));
1724
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "hooks", "session-start.js")));
1725
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "bin", "state.js")));
1726
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "bin", "qualia-ui.js")));
1727
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "bin", "statusline.js")));
1728
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", ".qualia-config.json")));
1729
+ } finally {
1730
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1731
+ }
1732
+ });
1733
+
1734
+ it("config JSON has correct fields", () => {
1735
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1736
+ try {
1737
+ runInstall("QS-FAWZI-01", tmpHome);
1738
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1739
+ assert.equal(config.code, "QS-FAWZI-01");
1740
+ assert.equal(config.installed_by, "Fawzi Goussous");
1741
+ assert.equal(config.role, "OWNER");
1742
+ } finally {
1743
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1744
+ }
1745
+ });
1746
+
1747
+ it("CLAUDE.md role placeholder replaced", () => {
1748
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1749
+ try {
1750
+ runInstall("QS-FAWZI-01", tmpHome);
1751
+ const claude = fs.readFileSync(path.join(tmpHome, ".claude", "CLAUDE.md"), "utf8");
1752
+ assert.match(claude, /Role: OWNER/);
1753
+ assert.doesNotMatch(claude, /\{\{ROLE\}\}/);
1754
+ } finally {
1755
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1756
+ }
1757
+ });
1758
+
1759
+ it("8 hooks installed", () => {
1760
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1761
+ try {
1762
+ runInstall("QS-FAWZI-01", tmpHome);
1763
+ const hooks = fs.readdirSync(path.join(tmpHome, ".claude", "hooks")).filter(f => f.endsWith(".js"));
1764
+ assert.equal(hooks.length, 8);
1765
+ } finally {
1766
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1767
+ }
1768
+ });
1769
+
1770
+ it("settings.json has hooks and statusLine", () => {
1771
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1772
+ try {
1773
+ runInstall("QS-FAWZI-01", tmpHome);
1774
+ const settings = fs.readFileSync(path.join(tmpHome, ".claude", "settings.json"), "utf8");
1775
+ assert.match(settings, /SessionStart/);
1776
+ assert.match(settings, /PreToolUse/);
1777
+ assert.match(settings, /statusLine/);
1778
+ } finally {
1779
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1780
+ }
1781
+ });
1782
+
1783
+ it("lowercase code is normalized", () => {
1784
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1785
+ try {
1786
+ const r = runInstall("qs-fawzi-01", tmpHome);
1787
+ assert.equal(r.status, 0);
1788
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1789
+ assert.equal(config.code, "QS-FAWZI-01");
1790
+ } finally {
1791
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1792
+ }
1793
+ });
1794
+
1795
+ it("O/0 typo tolerance in code suffix", () => {
1796
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1797
+ try {
1798
+ const r = runInstall("QS-FAWZI-O1", tmpHome);
1799
+ assert.equal(r.status, 0);
1800
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1801
+ assert.equal(config.code, "QS-FAWZI-01");
1802
+ } finally {
1803
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1804
+ }
1805
+ });
1806
+
1807
+ it("EMPLOYEE role set correctly for MOAYAD", () => {
1808
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1809
+ try {
1810
+ const r = runInstall("QS-MOAYAD-03", tmpHome);
1811
+ assert.equal(r.status, 0);
1812
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1813
+ assert.equal(config.code, "QS-MOAYAD-03");
1814
+ assert.equal(config.installed_by, "Moayad");
1815
+ assert.equal(config.role, "EMPLOYEE");
1816
+ } finally {
1817
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1818
+ }
1819
+ });
1820
+
1821
+ it("invalid code exits 1", () => {
1822
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1823
+ try {
1824
+ const r = runInstall("QS-BOGUS-99", tmpHome);
1825
+ assert.equal(r.status, 1);
1826
+ assert.match(stripAnsi(r.stdout), /Invalid code/);
1827
+ assert.ok(!fs.existsSync(path.join(tmpHome, ".claude", ".qualia-config.json")));
1828
+ } finally {
1829
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1830
+ }
1831
+ });
1832
+
1833
+ it("empty code exits 1", () => {
1834
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1835
+ try {
1836
+ const r = spawnSync(process.execPath, [INSTALL], {
1837
+ encoding: "utf8", timeout: 15000,
1838
+ input: "\n",
1839
+ env: { ...process.env, HOME: tmpHome, USERPROFILE: tmpHome },
1840
+ stdio: ["pipe", "pipe", "pipe"],
1841
+ });
1842
+ assert.equal(r.status, 1);
1843
+ assert.match(stripAnsi(r.stdout || ""), /Invalid code/);
1844
+ } finally {
1845
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1846
+ }
1847
+ });
1848
+
1849
+ it("whitespace-padded code is accepted", () => {
1850
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1851
+ try {
1852
+ const r = runInstall(" QS-FAWZI-01 ", tmpHome);
1853
+ assert.equal(r.status, 0);
1854
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1855
+ assert.equal(config.code, "QS-FAWZI-01");
1856
+ } finally {
1857
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1858
+ }
1859
+ });
1860
+
1861
+ it("settings.json merge preserves custom keys", () => {
1862
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1863
+ try {
1864
+ fs.mkdirSync(path.join(tmpHome, ".claude"), { recursive: true });
1865
+ fs.writeFileSync(path.join(tmpHome, ".claude", "settings.json"), JSON.stringify({
1866
+ customKey: "preserved",
1867
+ env: { MY_CUSTOM_VAR: "hello" },
1868
+ }));
1869
+ const r = runInstall("QS-FAWZI-01", tmpHome);
1870
+ assert.equal(r.status, 0);
1871
+ const settings = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", "settings.json"), "utf8"));
1872
+ assert.equal(settings.customKey, "preserved");
1873
+ assert.equal(settings.env.MY_CUSTOM_VAR, "hello");
1874
+ assert.ok(settings.hooks);
1875
+ assert.ok(settings.statusLine);
1876
+ } finally {
1877
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1878
+ }
1879
+ });
1880
+
1881
+ it("knowledge files created on first install", () => {
1882
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1883
+ try {
1884
+ runInstall("QS-FAWZI-01", tmpHome);
1885
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "knowledge", "learned-patterns.md")));
1886
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "knowledge", "common-fixes.md")));
1887
+ assert.ok(fs.existsSync(path.join(tmpHome, ".claude", "knowledge", "client-prefs.md")));
1888
+ } finally {
1889
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1890
+ }
1891
+ });
1892
+
1893
+ it("re-install preserves user edits in knowledge files", () => {
1894
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1895
+ try {
1896
+ runInstall("QS-FAWZI-01", tmpHome);
1897
+ fs.appendFileSync(path.join(tmpHome, ".claude", "knowledge", "learned-patterns.md"),
1898
+ "\n## CUSTOM LEARNING — DO NOT OVERWRITE\n");
1899
+ runInstall("QS-FAWZI-01", tmpHome);
1900
+ const content = fs.readFileSync(path.join(tmpHome, ".claude", "knowledge", "learned-patterns.md"), "utf8");
1901
+ assert.match(content, /CUSTOM LEARNING/);
1902
+ } finally {
1903
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1904
+ }
1905
+ });
1906
+
1907
+ it("templates copied to qualia-templates/", () => {
1908
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1909
+ try {
1910
+ runInstall("QS-FAWZI-01", tmpHome);
1911
+ const tmplDir = path.join(tmpHome, ".claude", "qualia-templates");
1912
+ assert.ok(fs.existsSync(tmplDir));
1913
+ const files = fs.readdirSync(tmplDir);
1914
+ assert.ok(files.length > 0, `Expected templates, found ${files.length}`);
1915
+ } finally {
1916
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1917
+ }
1918
+ });
1919
+
1920
+ it("agents copied", () => {
1921
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1922
+ try {
1923
+ runInstall("QS-FAWZI-01", tmpHome);
1924
+ const agentDir = path.join(tmpHome, ".claude", "agents");
1925
+ assert.ok(fs.existsSync(agentDir));
1926
+ const files = fs.readdirSync(agentDir);
1927
+ assert.ok(files.length > 0);
1928
+ } finally {
1929
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1930
+ }
1931
+ });
1932
+
1933
+ it("rules copied", () => {
1934
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1935
+ try {
1936
+ runInstall("QS-FAWZI-01", tmpHome);
1937
+ const rulesDir = path.join(tmpHome, ".claude", "rules");
1938
+ assert.ok(fs.existsSync(rulesDir));
1939
+ const files = fs.readdirSync(rulesDir);
1940
+ assert.ok(files.length > 0);
1941
+ } finally {
1942
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1943
+ }
1944
+ });
1945
+
1946
+ it("config version matches package.json", () => {
1947
+ const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "qualia-install-"));
1948
+ try {
1949
+ runInstall("QS-FAWZI-01", tmpHome);
1950
+ const config = JSON.parse(fs.readFileSync(path.join(tmpHome, ".claude", ".qualia-config.json"), "utf8"));
1951
+ assert.equal(config.version, PKG_VERSION);
1952
+ } finally {
1953
+ fs.rmSync(tmpHome, { recursive: true, force: true });
1954
+ }
1955
+ });
1956
+ });