gsd-pi 2.26.0 → 2.27.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 (171) hide show
  1. package/README.md +43 -6
  2. package/dist/cli.js +4 -2
  3. package/dist/headless.d.ts +3 -0
  4. package/dist/headless.js +136 -8
  5. package/dist/help-text.js +3 -0
  6. package/dist/loader.js +33 -4
  7. package/dist/resources/extensions/bg-shell/index.ts +19 -2
  8. package/dist/resources/extensions/bg-shell/process-manager.ts +45 -0
  9. package/dist/resources/extensions/bg-shell/types.ts +21 -1
  10. package/dist/resources/extensions/gsd/auto/session.ts +224 -0
  11. package/dist/resources/extensions/gsd/auto-budget.ts +32 -0
  12. package/dist/resources/extensions/gsd/auto-dashboard.ts +63 -10
  13. package/dist/resources/extensions/gsd/auto-direct-dispatch.ts +229 -0
  14. package/dist/resources/extensions/gsd/auto-dispatch.ts +23 -10
  15. package/dist/resources/extensions/gsd/auto-model-selection.ts +179 -0
  16. package/dist/resources/extensions/gsd/auto-observability.ts +74 -0
  17. package/dist/resources/extensions/gsd/auto-prompts.ts +0 -1
  18. package/dist/resources/extensions/gsd/auto-timeout-recovery.ts +262 -0
  19. package/dist/resources/extensions/gsd/auto-tool-tracking.ts +54 -0
  20. package/dist/resources/extensions/gsd/auto-unit-closeout.ts +46 -0
  21. package/dist/resources/extensions/gsd/auto-worktree-sync.ts +207 -0
  22. package/dist/resources/extensions/gsd/auto.ts +977 -1551
  23. package/dist/resources/extensions/gsd/commands.ts +3 -3
  24. package/dist/resources/extensions/gsd/dashboard-overlay.ts +47 -72
  25. package/dist/resources/extensions/gsd/doctor-proactive.ts +9 -4
  26. package/dist/resources/extensions/gsd/export-html.ts +1001 -0
  27. package/dist/resources/extensions/gsd/export.ts +49 -1
  28. package/dist/resources/extensions/gsd/git-service.ts +6 -0
  29. package/dist/resources/extensions/gsd/gitignore.ts +4 -1
  30. package/dist/resources/extensions/gsd/guided-flow.ts +24 -5
  31. package/dist/resources/extensions/gsd/index.ts +54 -1
  32. package/dist/resources/extensions/gsd/native-git-bridge.ts +30 -2
  33. package/dist/resources/extensions/gsd/observability-validator.ts +21 -0
  34. package/dist/resources/extensions/gsd/parallel-orchestrator.ts +231 -20
  35. package/dist/resources/extensions/gsd/preferences.ts +62 -1
  36. package/dist/resources/extensions/gsd/prompts/execute-task.md +4 -3
  37. package/dist/resources/extensions/gsd/prompts/system.md +1 -1
  38. package/dist/resources/extensions/gsd/reports.ts +510 -0
  39. package/dist/resources/extensions/gsd/roadmap-slices.ts +1 -1
  40. package/dist/resources/extensions/gsd/skills/gsd-headless/SKILL.md +178 -0
  41. package/dist/resources/extensions/gsd/skills/gsd-headless/references/answer-injection.md +54 -0
  42. package/dist/resources/extensions/gsd/skills/gsd-headless/references/commands.md +59 -0
  43. package/dist/resources/extensions/gsd/skills/gsd-headless/references/multi-session.md +185 -0
  44. package/dist/resources/extensions/gsd/state.ts +30 -0
  45. package/dist/resources/extensions/gsd/templates/task-summary.md +9 -0
  46. package/dist/resources/extensions/gsd/tests/auto-dashboard.test.ts +13 -0
  47. package/dist/resources/extensions/gsd/tests/continue-here.test.ts +81 -0
  48. package/dist/resources/extensions/gsd/tests/derive-state-db.test.ts +5 -0
  49. package/dist/resources/extensions/gsd/tests/derive-state-deps.test.ts +1 -0
  50. package/dist/resources/extensions/gsd/tests/derive-state-draft.test.ts +1 -0
  51. package/dist/resources/extensions/gsd/tests/derive-state.test.ts +10 -1
  52. package/dist/resources/extensions/gsd/tests/dispatch-missing-task-plans.test.ts +132 -0
  53. package/dist/resources/extensions/gsd/tests/doctor-proactive.test.ts +14 -0
  54. package/dist/resources/extensions/gsd/tests/integration-mixed-milestones.test.ts +1 -0
  55. package/dist/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +1 -1
  56. package/dist/resources/extensions/gsd/tests/native-has-changes-cache.test.ts +61 -0
  57. package/dist/resources/extensions/gsd/tests/network-error-fallback.test.ts +51 -1
  58. package/dist/resources/extensions/gsd/tests/parallel-budget-atomicity.test.ts +331 -0
  59. package/dist/resources/extensions/gsd/tests/parallel-crash-recovery.test.ts +298 -0
  60. package/dist/resources/extensions/gsd/tests/parallel-merge.test.ts +465 -0
  61. package/dist/resources/extensions/gsd/tests/parallel-orchestration.test.ts +39 -10
  62. package/dist/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +71 -0
  63. package/dist/resources/extensions/gsd/tests/replan-slice.test.ts +42 -0
  64. package/dist/resources/extensions/gsd/tests/triage-dispatch.test.ts +9 -9
  65. package/dist/resources/extensions/gsd/tests/verification-evidence.test.ts +743 -0
  66. package/dist/resources/extensions/gsd/tests/verification-gate.test.ts +965 -0
  67. package/dist/resources/extensions/gsd/tests/visualizer-data.test.ts +1 -1
  68. package/dist/resources/extensions/gsd/tests/visualizer-overlay.test.ts +44 -10
  69. package/dist/resources/extensions/gsd/tests/worktree.test.ts +3 -1
  70. package/dist/resources/extensions/gsd/types.ts +38 -0
  71. package/dist/resources/extensions/gsd/verification-evidence.ts +183 -0
  72. package/dist/resources/extensions/gsd/verification-gate.ts +567 -0
  73. package/dist/resources/extensions/gsd/visualizer-data.ts +25 -3
  74. package/dist/resources/extensions/gsd/visualizer-overlay.ts +31 -21
  75. package/dist/resources/extensions/gsd/visualizer-views.ts +15 -66
  76. package/dist/resources/extensions/search-the-web/tool-search.ts +26 -0
  77. package/dist/resources/extensions/shared/format-utils.ts +85 -0
  78. package/dist/resources/extensions/shared/tests/format-utils.test.ts +153 -0
  79. package/dist/resources/extensions/subagent/index.ts +46 -1
  80. package/dist/resources/extensions/subagent/isolation.ts +9 -6
  81. package/package.json +1 -1
  82. package/packages/pi-ai/dist/providers/openai-completions.js +7 -4
  83. package/packages/pi-ai/dist/providers/openai-completions.js.map +1 -1
  84. package/packages/pi-ai/src/providers/openai-completions.ts +7 -4
  85. package/packages/pi-coding-agent/dist/core/lsp/client.d.ts.map +1 -1
  86. package/packages/pi-coding-agent/dist/core/lsp/client.js +7 -0
  87. package/packages/pi-coding-agent/dist/core/lsp/client.js.map +1 -1
  88. package/packages/pi-coding-agent/dist/core/lsp/config.d.ts.map +1 -1
  89. package/packages/pi-coding-agent/dist/core/lsp/config.js +9 -2
  90. package/packages/pi-coding-agent/dist/core/lsp/config.js.map +1 -1
  91. package/packages/pi-coding-agent/src/core/lsp/client.ts +8 -0
  92. package/packages/pi-coding-agent/src/core/lsp/config.ts +9 -2
  93. package/packages/pi-tui/dist/components/editor.d.ts.map +1 -1
  94. package/packages/pi-tui/dist/components/editor.js +1 -1
  95. package/packages/pi-tui/dist/components/editor.js.map +1 -1
  96. package/packages/pi-tui/src/components/editor.ts +3 -1
  97. package/scripts/link-workspace-packages.cjs +22 -6
  98. package/src/resources/extensions/bg-shell/index.ts +19 -2
  99. package/src/resources/extensions/bg-shell/process-manager.ts +45 -0
  100. package/src/resources/extensions/bg-shell/types.ts +21 -1
  101. package/src/resources/extensions/gsd/auto/session.ts +224 -0
  102. package/src/resources/extensions/gsd/auto-budget.ts +32 -0
  103. package/src/resources/extensions/gsd/auto-dashboard.ts +63 -10
  104. package/src/resources/extensions/gsd/auto-direct-dispatch.ts +229 -0
  105. package/src/resources/extensions/gsd/auto-dispatch.ts +23 -10
  106. package/src/resources/extensions/gsd/auto-model-selection.ts +179 -0
  107. package/src/resources/extensions/gsd/auto-observability.ts +74 -0
  108. package/src/resources/extensions/gsd/auto-prompts.ts +0 -1
  109. package/src/resources/extensions/gsd/auto-timeout-recovery.ts +262 -0
  110. package/src/resources/extensions/gsd/auto-tool-tracking.ts +54 -0
  111. package/src/resources/extensions/gsd/auto-unit-closeout.ts +46 -0
  112. package/src/resources/extensions/gsd/auto-worktree-sync.ts +207 -0
  113. package/src/resources/extensions/gsd/auto.ts +977 -1551
  114. package/src/resources/extensions/gsd/commands.ts +3 -3
  115. package/src/resources/extensions/gsd/dashboard-overlay.ts +47 -72
  116. package/src/resources/extensions/gsd/doctor-proactive.ts +9 -4
  117. package/src/resources/extensions/gsd/export-html.ts +1001 -0
  118. package/src/resources/extensions/gsd/export.ts +49 -1
  119. package/src/resources/extensions/gsd/git-service.ts +6 -0
  120. package/src/resources/extensions/gsd/gitignore.ts +4 -1
  121. package/src/resources/extensions/gsd/guided-flow.ts +24 -5
  122. package/src/resources/extensions/gsd/index.ts +54 -1
  123. package/src/resources/extensions/gsd/native-git-bridge.ts +30 -2
  124. package/src/resources/extensions/gsd/observability-validator.ts +21 -0
  125. package/src/resources/extensions/gsd/parallel-orchestrator.ts +231 -20
  126. package/src/resources/extensions/gsd/preferences.ts +62 -1
  127. package/src/resources/extensions/gsd/prompts/execute-task.md +4 -3
  128. package/src/resources/extensions/gsd/prompts/system.md +1 -1
  129. package/src/resources/extensions/gsd/reports.ts +510 -0
  130. package/src/resources/extensions/gsd/roadmap-slices.ts +1 -1
  131. package/src/resources/extensions/gsd/skills/gsd-headless/SKILL.md +178 -0
  132. package/src/resources/extensions/gsd/skills/gsd-headless/references/answer-injection.md +54 -0
  133. package/src/resources/extensions/gsd/skills/gsd-headless/references/commands.md +59 -0
  134. package/src/resources/extensions/gsd/skills/gsd-headless/references/multi-session.md +185 -0
  135. package/src/resources/extensions/gsd/state.ts +30 -0
  136. package/src/resources/extensions/gsd/templates/task-summary.md +9 -0
  137. package/src/resources/extensions/gsd/tests/auto-dashboard.test.ts +13 -0
  138. package/src/resources/extensions/gsd/tests/continue-here.test.ts +81 -0
  139. package/src/resources/extensions/gsd/tests/derive-state-db.test.ts +5 -0
  140. package/src/resources/extensions/gsd/tests/derive-state-deps.test.ts +1 -0
  141. package/src/resources/extensions/gsd/tests/derive-state-draft.test.ts +1 -0
  142. package/src/resources/extensions/gsd/tests/derive-state.test.ts +10 -1
  143. package/src/resources/extensions/gsd/tests/dispatch-missing-task-plans.test.ts +132 -0
  144. package/src/resources/extensions/gsd/tests/doctor-proactive.test.ts +14 -0
  145. package/src/resources/extensions/gsd/tests/integration-mixed-milestones.test.ts +1 -0
  146. package/src/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +1 -1
  147. package/src/resources/extensions/gsd/tests/native-has-changes-cache.test.ts +61 -0
  148. package/src/resources/extensions/gsd/tests/network-error-fallback.test.ts +51 -1
  149. package/src/resources/extensions/gsd/tests/parallel-budget-atomicity.test.ts +331 -0
  150. package/src/resources/extensions/gsd/tests/parallel-crash-recovery.test.ts +298 -0
  151. package/src/resources/extensions/gsd/tests/parallel-merge.test.ts +465 -0
  152. package/src/resources/extensions/gsd/tests/parallel-orchestration.test.ts +39 -10
  153. package/src/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +71 -0
  154. package/src/resources/extensions/gsd/tests/replan-slice.test.ts +42 -0
  155. package/src/resources/extensions/gsd/tests/triage-dispatch.test.ts +9 -9
  156. package/src/resources/extensions/gsd/tests/verification-evidence.test.ts +743 -0
  157. package/src/resources/extensions/gsd/tests/verification-gate.test.ts +965 -0
  158. package/src/resources/extensions/gsd/tests/visualizer-data.test.ts +1 -1
  159. package/src/resources/extensions/gsd/tests/visualizer-overlay.test.ts +44 -10
  160. package/src/resources/extensions/gsd/tests/worktree.test.ts +3 -1
  161. package/src/resources/extensions/gsd/types.ts +38 -0
  162. package/src/resources/extensions/gsd/verification-evidence.ts +183 -0
  163. package/src/resources/extensions/gsd/verification-gate.ts +567 -0
  164. package/src/resources/extensions/gsd/visualizer-data.ts +25 -3
  165. package/src/resources/extensions/gsd/visualizer-overlay.ts +31 -21
  166. package/src/resources/extensions/gsd/visualizer-views.ts +15 -66
  167. package/src/resources/extensions/search-the-web/tool-search.ts +26 -0
  168. package/src/resources/extensions/shared/format-utils.ts +85 -0
  169. package/src/resources/extensions/shared/tests/format-utils.test.ts +153 -0
  170. package/src/resources/extensions/subagent/index.ts +46 -1
  171. package/src/resources/extensions/subagent/isolation.ts +9 -6
@@ -0,0 +1,965 @@
1
+ /**
2
+ * Unit tests for the verification gate — command discovery and execution.
3
+ *
4
+ * Tests cover:
5
+ * 1. Discovery from explicit preference commands
6
+ * 2. Discovery from task plan verify field
7
+ * 3. Discovery from package.json typecheck/lint/test scripts
8
+ * 4. First-non-empty-wins precedence
9
+ * 5. All commands pass → gate passes
10
+ * 6. One command fails → gate fails with exit code + stderr
11
+ * 7. Missing package.json → 0 checks → pass
12
+ * 8. Empty scripts → 0 checks → pass
13
+ * 9. Preference validation for verification keys
14
+ * 10. spawnSync error (command not found) → failure with exit code 127
15
+ * 11. Dependency audit — git diff detection, npm audit parsing, graceful failures
16
+ */
17
+
18
+ import test from "node:test";
19
+ import assert from "node:assert/strict";
20
+ import { mkdirSync, writeFileSync, rmSync } from "node:fs";
21
+ import { join } from "node:path";
22
+ import { tmpdir } from "node:os";
23
+ import { discoverCommands, runVerificationGate, formatFailureContext, captureRuntimeErrors, runDependencyAudit } from "../verification-gate.ts";
24
+ import type { CaptureRuntimeErrorsOptions, DependencyAuditOptions } from "../verification-gate.ts";
25
+ import { validatePreferences } from "../preferences.ts";
26
+
27
+ function makeTempDir(prefix: string): string {
28
+ const dir = join(
29
+ tmpdir(),
30
+ `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}`,
31
+ );
32
+ mkdirSync(dir, { recursive: true });
33
+ return dir;
34
+ }
35
+
36
+ // ─── Discovery Tests ─────────────────────────────────────────────────────────
37
+
38
+ test("verification-gate: discoverCommands from preference commands", () => {
39
+ const tmp = makeTempDir("vg-pref");
40
+ try {
41
+ const result = discoverCommands({
42
+ preferenceCommands: ["npm run lint", "npm run test"],
43
+ cwd: tmp,
44
+ });
45
+ assert.deepStrictEqual(result.commands, ["npm run lint", "npm run test"]);
46
+ assert.equal(result.source, "preference");
47
+ } finally {
48
+ rmSync(tmp, { recursive: true, force: true });
49
+ }
50
+ });
51
+
52
+ test("verification-gate: discoverCommands from task plan verify field", () => {
53
+ const tmp = makeTempDir("vg-taskplan");
54
+ try {
55
+ const result = discoverCommands({
56
+ taskPlanVerify: "npm run lint && npm run test",
57
+ cwd: tmp,
58
+ });
59
+ assert.deepStrictEqual(result.commands, ["npm run lint", "npm run test"]);
60
+ assert.equal(result.source, "task-plan");
61
+ } finally {
62
+ rmSync(tmp, { recursive: true, force: true });
63
+ }
64
+ });
65
+
66
+ test("verification-gate: discoverCommands from package.json scripts", () => {
67
+ const tmp = makeTempDir("vg-pkg");
68
+ try {
69
+ writeFileSync(
70
+ join(tmp, "package.json"),
71
+ JSON.stringify({
72
+ scripts: {
73
+ typecheck: "tsc --noEmit",
74
+ lint: "eslint .",
75
+ test: "vitest",
76
+ build: "tsc", // should NOT be included
77
+ },
78
+ }),
79
+ );
80
+ const result = discoverCommands({ cwd: tmp });
81
+ assert.deepStrictEqual(result.commands, [
82
+ "npm run typecheck",
83
+ "npm run lint",
84
+ "npm run test",
85
+ ]);
86
+ assert.equal(result.source, "package-json");
87
+ } finally {
88
+ rmSync(tmp, { recursive: true, force: true });
89
+ }
90
+ });
91
+
92
+ test("verification-gate: first-non-empty-wins — preference beats task plan and package.json", () => {
93
+ const tmp = makeTempDir("vg-precedence");
94
+ try {
95
+ writeFileSync(
96
+ join(tmp, "package.json"),
97
+ JSON.stringify({ scripts: { lint: "eslint ." } }),
98
+ );
99
+ const result = discoverCommands({
100
+ preferenceCommands: ["custom-check"],
101
+ taskPlanVerify: "npm run lint",
102
+ cwd: tmp,
103
+ });
104
+ assert.deepStrictEqual(result.commands, ["custom-check"]);
105
+ assert.equal(result.source, "preference");
106
+ } finally {
107
+ rmSync(tmp, { recursive: true, force: true });
108
+ }
109
+ });
110
+
111
+ test("verification-gate: task plan verify beats package.json", () => {
112
+ const tmp = makeTempDir("vg-tp-beats-pkg");
113
+ try {
114
+ writeFileSync(
115
+ join(tmp, "package.json"),
116
+ JSON.stringify({ scripts: { lint: "eslint ." } }),
117
+ );
118
+ const result = discoverCommands({
119
+ taskPlanVerify: "custom-verify",
120
+ cwd: tmp,
121
+ });
122
+ assert.deepStrictEqual(result.commands, ["custom-verify"]);
123
+ assert.equal(result.source, "task-plan");
124
+ } finally {
125
+ rmSync(tmp, { recursive: true, force: true });
126
+ }
127
+ });
128
+
129
+ test("verification-gate: missing package.json → 0 checks, source none", () => {
130
+ const tmp = makeTempDir("vg-no-pkg");
131
+ try {
132
+ const result = discoverCommands({ cwd: tmp });
133
+ assert.deepStrictEqual(result.commands, []);
134
+ assert.equal(result.source, "none");
135
+ } finally {
136
+ rmSync(tmp, { recursive: true, force: true });
137
+ }
138
+ });
139
+
140
+ test("verification-gate: package.json with no matching scripts → 0 checks", () => {
141
+ const tmp = makeTempDir("vg-no-scripts");
142
+ try {
143
+ writeFileSync(
144
+ join(tmp, "package.json"),
145
+ JSON.stringify({ scripts: { build: "tsc", start: "node index.js" } }),
146
+ );
147
+ const result = discoverCommands({ cwd: tmp });
148
+ assert.deepStrictEqual(result.commands, []);
149
+ assert.equal(result.source, "none");
150
+ } finally {
151
+ rmSync(tmp, { recursive: true, force: true });
152
+ }
153
+ });
154
+
155
+ test("verification-gate: empty preference array falls through to task plan", () => {
156
+ const tmp = makeTempDir("vg-empty-pref");
157
+ try {
158
+ const result = discoverCommands({
159
+ preferenceCommands: [],
160
+ taskPlanVerify: "echo ok",
161
+ cwd: tmp,
162
+ });
163
+ assert.deepStrictEqual(result.commands, ["echo ok"]);
164
+ assert.equal(result.source, "task-plan");
165
+ } finally {
166
+ rmSync(tmp, { recursive: true, force: true });
167
+ }
168
+ });
169
+
170
+ // ─── Execution Tests ─────────────────────────────────────────────────────────
171
+
172
+ test("verification-gate: all commands pass → gate passes", () => {
173
+ const tmp = makeTempDir("vg-pass");
174
+ try {
175
+ const result = runVerificationGate({
176
+ basePath: tmp,
177
+ unitId: "T01",
178
+ cwd: tmp,
179
+ preferenceCommands: ["echo hello", "echo world"],
180
+ });
181
+ assert.equal(result.passed, true);
182
+ assert.equal(result.checks.length, 2);
183
+ assert.equal(result.discoverySource, "preference");
184
+ assert.equal(result.checks[0].exitCode, 0);
185
+ assert.equal(result.checks[1].exitCode, 0);
186
+ assert.ok(result.checks[0].stdout.includes("hello"));
187
+ assert.ok(result.checks[1].stdout.includes("world"));
188
+ assert.equal(typeof result.timestamp, "number");
189
+ } finally {
190
+ rmSync(tmp, { recursive: true, force: true });
191
+ }
192
+ });
193
+
194
+ test("verification-gate: one command fails → gate fails with exit code + stderr", () => {
195
+ const tmp = makeTempDir("vg-fail");
196
+ try {
197
+ const result = runVerificationGate({
198
+ basePath: tmp,
199
+ unitId: "T01",
200
+ cwd: tmp,
201
+ preferenceCommands: ["echo ok", "sh -c 'echo err >&2; exit 1'"],
202
+ });
203
+ assert.equal(result.passed, false);
204
+ assert.equal(result.checks.length, 2);
205
+ assert.equal(result.checks[0].exitCode, 0);
206
+ assert.equal(result.checks[1].exitCode, 1);
207
+ assert.ok(result.checks[1].stderr.includes("err"));
208
+ } finally {
209
+ rmSync(tmp, { recursive: true, force: true });
210
+ }
211
+ });
212
+
213
+ test("verification-gate: no commands discovered → gate passes with 0 checks", () => {
214
+ const tmp = makeTempDir("vg-empty");
215
+ try {
216
+ const result = runVerificationGate({
217
+ basePath: tmp,
218
+ unitId: "T01",
219
+ cwd: tmp,
220
+ });
221
+ assert.equal(result.passed, true);
222
+ assert.equal(result.checks.length, 0);
223
+ assert.equal(result.discoverySource, "none");
224
+ } finally {
225
+ rmSync(tmp, { recursive: true, force: true });
226
+ }
227
+ });
228
+
229
+ test("verification-gate: command not found → exit code 127", () => {
230
+ const tmp = makeTempDir("vg-notfound");
231
+ try {
232
+ const result = runVerificationGate({
233
+ basePath: tmp,
234
+ unitId: "T01",
235
+ cwd: tmp,
236
+ preferenceCommands: ["__nonexistent_command_xyz_42__"],
237
+ });
238
+ assert.equal(result.passed, false);
239
+ assert.equal(result.checks.length, 1);
240
+ assert.ok(result.checks[0].exitCode !== 0, "should have non-zero exit code");
241
+ assert.ok(result.checks[0].durationMs >= 0);
242
+ } finally {
243
+ rmSync(tmp, { recursive: true, force: true });
244
+ }
245
+ });
246
+
247
+ test("verification-gate: each check has durationMs", () => {
248
+ const tmp = makeTempDir("vg-duration");
249
+ try {
250
+ const result = runVerificationGate({
251
+ basePath: tmp,
252
+ unitId: "T01",
253
+ cwd: tmp,
254
+ preferenceCommands: ["echo fast"],
255
+ });
256
+ assert.equal(result.checks.length, 1);
257
+ assert.equal(typeof result.checks[0].durationMs, "number");
258
+ assert.ok(result.checks[0].durationMs >= 0);
259
+ } finally {
260
+ rmSync(tmp, { recursive: true, force: true });
261
+ }
262
+ });
263
+
264
+ // ─── Preference Validation Tests ─────────────────────────────────────────────
265
+
266
+ test("verification-gate: validatePreferences accepts valid verification keys", () => {
267
+ const result = validatePreferences({
268
+ verification_commands: ["npm run lint", "npm run test"],
269
+ verification_auto_fix: true,
270
+ verification_max_retries: 3,
271
+ });
272
+ assert.deepStrictEqual(result.preferences.verification_commands, [
273
+ "npm run lint",
274
+ "npm run test",
275
+ ]);
276
+ assert.equal(result.preferences.verification_auto_fix, true);
277
+ assert.equal(result.preferences.verification_max_retries, 3);
278
+ assert.equal(result.errors.length, 0);
279
+ });
280
+
281
+ test("verification-gate: validatePreferences rejects non-array verification_commands", () => {
282
+ const result = validatePreferences({
283
+ verification_commands: "npm run lint" as unknown as string[],
284
+ });
285
+ assert.ok(result.errors.some((e) => e.includes("verification_commands")));
286
+ assert.equal(result.preferences.verification_commands, undefined);
287
+ });
288
+
289
+ test("verification-gate: validatePreferences rejects non-boolean verification_auto_fix", () => {
290
+ const result = validatePreferences({
291
+ verification_auto_fix: "yes" as unknown as boolean,
292
+ });
293
+ assert.ok(result.errors.some((e) => e.includes("verification_auto_fix")));
294
+ assert.equal(result.preferences.verification_auto_fix, undefined);
295
+ });
296
+
297
+ test("verification-gate: validatePreferences rejects negative verification_max_retries", () => {
298
+ const result = validatePreferences({
299
+ verification_max_retries: -1,
300
+ });
301
+ assert.ok(result.errors.some((e) => e.includes("verification_max_retries")));
302
+ assert.equal(result.preferences.verification_max_retries, undefined);
303
+ });
304
+
305
+ test("verification-gate: validatePreferences rejects non-string items in verification_commands", () => {
306
+ const result = validatePreferences({
307
+ verification_commands: ["npm run lint", 42 as unknown as string],
308
+ });
309
+ assert.ok(result.errors.some((e) => e.includes("verification_commands")));
310
+ assert.equal(result.preferences.verification_commands, undefined);
311
+ });
312
+
313
+ test("verification-gate: validatePreferences floors verification_max_retries", () => {
314
+ const result = validatePreferences({
315
+ verification_max_retries: 2.7,
316
+ });
317
+ assert.equal(result.preferences.verification_max_retries, 2);
318
+ assert.equal(result.errors.length, 0);
319
+ });
320
+
321
+ // ─── Additional Discovery Tests (T02) ───────────────────────────────────────
322
+
323
+ test("verification-gate: package.json with only test script → returns only npm run test", () => {
324
+ const tmp = makeTempDir("vg-only-test");
325
+ try {
326
+ writeFileSync(
327
+ join(tmp, "package.json"),
328
+ JSON.stringify({
329
+ scripts: {
330
+ test: "vitest",
331
+ build: "tsc",
332
+ start: "node index.js",
333
+ },
334
+ }),
335
+ );
336
+ const result = discoverCommands({ cwd: tmp });
337
+ assert.deepStrictEqual(result.commands, ["npm run test"]);
338
+ assert.equal(result.source, "package-json");
339
+ } finally {
340
+ rmSync(tmp, { recursive: true, force: true });
341
+ }
342
+ });
343
+
344
+ test("verification-gate: taskPlanVerify with single command (no &&)", () => {
345
+ const tmp = makeTempDir("vg-tp-single");
346
+ try {
347
+ const result = discoverCommands({
348
+ taskPlanVerify: "npm test",
349
+ cwd: tmp,
350
+ });
351
+ assert.deepStrictEqual(result.commands, ["npm test"]);
352
+ assert.equal(result.source, "task-plan");
353
+ } finally {
354
+ rmSync(tmp, { recursive: true, force: true });
355
+ }
356
+ });
357
+
358
+ test("verification-gate: whitespace-only preference commands fall through", () => {
359
+ const tmp = makeTempDir("vg-ws-pref");
360
+ try {
361
+ writeFileSync(
362
+ join(tmp, "package.json"),
363
+ JSON.stringify({ scripts: { lint: "eslint ." } }),
364
+ );
365
+ const result = discoverCommands({
366
+ preferenceCommands: [" ", ""],
367
+ cwd: tmp,
368
+ });
369
+ // Whitespace-only strings are trimmed to empty and filtered out
370
+ assert.equal(result.source, "package-json");
371
+ assert.deepStrictEqual(result.commands, ["npm run lint"]);
372
+ } finally {
373
+ rmSync(tmp, { recursive: true, force: true });
374
+ }
375
+ });
376
+
377
+ // ─── Additional Execution Tests (T02) ───────────────────────────────────────
378
+
379
+ test("verification-gate: one command fails — remaining commands still run (non-short-circuit)", () => {
380
+ const tmp = makeTempDir("vg-no-short-circuit");
381
+ try {
382
+ // First fails, second and third should still execute
383
+ const result = runVerificationGate({
384
+ basePath: tmp,
385
+ unitId: "T02",
386
+ cwd: tmp,
387
+ preferenceCommands: [
388
+ "sh -c 'exit 1'",
389
+ "echo second",
390
+ "echo third",
391
+ ],
392
+ });
393
+ assert.equal(result.passed, false);
394
+ assert.equal(result.checks.length, 3, "all 3 commands should run");
395
+ assert.equal(result.checks[0].exitCode, 1, "first command fails");
396
+ assert.equal(result.checks[1].exitCode, 0, "second command runs and passes");
397
+ assert.ok(result.checks[1].stdout.includes("second"));
398
+ assert.equal(result.checks[2].exitCode, 0, "third command runs and passes");
399
+ assert.ok(result.checks[2].stdout.includes("third"));
400
+ } finally {
401
+ rmSync(tmp, { recursive: true, force: true });
402
+ }
403
+ });
404
+
405
+ test("verification-gate: gate execution uses cwd for spawnSync", () => {
406
+ const tmp = makeTempDir("vg-cwd");
407
+ try {
408
+ // pwd should report the temp dir
409
+ const result = runVerificationGate({
410
+ basePath: tmp,
411
+ unitId: "T02",
412
+ cwd: tmp,
413
+ preferenceCommands: ["pwd"],
414
+ });
415
+ assert.equal(result.passed, true);
416
+ assert.equal(result.checks.length, 1);
417
+ // The stdout should contain the tmp dir path (resolving symlinks)
418
+ assert.ok(result.checks[0].stdout.trim().length > 0, "pwd should produce output");
419
+ } finally {
420
+ rmSync(tmp, { recursive: true, force: true });
421
+ }
422
+ });
423
+
424
+ // ─── Additional Preference Validation Tests (T02) ──────────────────────────
425
+
426
+ test("verification-gate: verification_commands produces no unknown-key warnings", () => {
427
+ const result = validatePreferences({
428
+ verification_commands: ["npm test"],
429
+ });
430
+ const unknownWarnings = (result.warnings ?? []).filter(w => w.includes("unknown"));
431
+ assert.equal(unknownWarnings.length, 0, "verification_commands is a known key");
432
+ assert.equal(result.errors.length, 0);
433
+ });
434
+
435
+ test("verification-gate: verification_auto_fix produces no unknown-key warnings", () => {
436
+ const result = validatePreferences({
437
+ verification_auto_fix: true,
438
+ });
439
+ const unknownWarnings = (result.warnings ?? []).filter(w => w.includes("unknown"));
440
+ assert.equal(unknownWarnings.length, 0, "verification_auto_fix is a known key");
441
+ assert.equal(result.errors.length, 0);
442
+ });
443
+
444
+ test("verification-gate: verification_max_retries produces no unknown-key warnings", () => {
445
+ const result = validatePreferences({
446
+ verification_max_retries: 2,
447
+ });
448
+ const unknownWarnings = (result.warnings ?? []).filter(w => w.includes("unknown"));
449
+ assert.equal(unknownWarnings.length, 0, "verification_max_retries is a known key");
450
+ assert.equal(result.errors.length, 0);
451
+ });
452
+
453
+ test("verification-gate: verification_max_retries -1 produces a validation error", () => {
454
+ const result = validatePreferences({
455
+ verification_max_retries: -1,
456
+ });
457
+ assert.ok(
458
+ result.errors.some(e => e.includes("verification_max_retries")),
459
+ "negative max_retries should error",
460
+ );
461
+ assert.equal(result.preferences.verification_max_retries, undefined);
462
+ });
463
+
464
+ // ─── formatFailureContext Tests (S03/T01) ─────────────────────────────────────
465
+
466
+ test("formatFailureContext: formats a single failure with command, exit code, stderr", () => {
467
+ const result: import("../types.ts").VerificationResult = {
468
+ passed: false,
469
+ checks: [
470
+ { command: "npm run lint", exitCode: 1, stdout: "", stderr: "error: unused var", durationMs: 500 },
471
+ ],
472
+ discoverySource: "preference",
473
+ timestamp: Date.now(),
474
+ };
475
+ const output = formatFailureContext(result);
476
+ assert.ok(output.startsWith("## Verification Failures"), "should start with header");
477
+ assert.ok(output.includes("`npm run lint`"), "should include command name");
478
+ assert.ok(output.includes("exit code 1"), "should include exit code");
479
+ assert.ok(output.includes("error: unused var"), "should include stderr content");
480
+ assert.ok(output.includes("```stderr"), "should have stderr code block");
481
+ });
482
+
483
+ test("formatFailureContext: formats multiple failures", () => {
484
+ const result: import("../types.ts").VerificationResult = {
485
+ passed: false,
486
+ checks: [
487
+ { command: "npm run lint", exitCode: 1, stdout: "", stderr: "lint error", durationMs: 100 },
488
+ { command: "npm run test", exitCode: 2, stdout: "", stderr: "test failure", durationMs: 200 },
489
+ { command: "npm run typecheck", exitCode: 0, stdout: "ok", stderr: "", durationMs: 50 },
490
+ ],
491
+ discoverySource: "preference",
492
+ timestamp: Date.now(),
493
+ };
494
+ const output = formatFailureContext(result);
495
+ assert.ok(output.includes("`npm run lint`"), "should include first failed command");
496
+ assert.ok(output.includes("exit code 1"), "should include first exit code");
497
+ assert.ok(output.includes("`npm run test`"), "should include second failed command");
498
+ assert.ok(output.includes("exit code 2"), "should include second exit code");
499
+ // Passing check should NOT appear
500
+ assert.ok(!output.includes("npm run typecheck"), "should not include passing command");
501
+ });
502
+
503
+ test("formatFailureContext: truncates stderr longer than 2000 chars", () => {
504
+ const longStderr = "x".repeat(3000);
505
+ const result: import("../types.ts").VerificationResult = {
506
+ passed: false,
507
+ checks: [
508
+ { command: "big-err", exitCode: 1, stdout: "", stderr: longStderr, durationMs: 100 },
509
+ ],
510
+ discoverySource: "preference",
511
+ timestamp: Date.now(),
512
+ };
513
+ const output = formatFailureContext(result);
514
+ // The output should contain 2000 x's followed by truncation marker, not 3000
515
+ assert.ok(!output.includes("x".repeat(2001)), "should not contain more than 2000 chars of stderr");
516
+ assert.ok(output.includes("…[truncated]"), "should include truncation marker");
517
+ });
518
+
519
+ test("formatFailureContext: returns empty string when all checks pass", () => {
520
+ const result: import("../types.ts").VerificationResult = {
521
+ passed: true,
522
+ checks: [
523
+ { command: "npm run lint", exitCode: 0, stdout: "ok", stderr: "", durationMs: 100 },
524
+ { command: "npm run test", exitCode: 0, stdout: "ok", stderr: "", durationMs: 200 },
525
+ ],
526
+ discoverySource: "preference",
527
+ timestamp: Date.now(),
528
+ };
529
+ assert.equal(formatFailureContext(result), "");
530
+ });
531
+
532
+ test("formatFailureContext: returns empty string for empty checks array", () => {
533
+ const result: import("../types.ts").VerificationResult = {
534
+ passed: true,
535
+ checks: [],
536
+ discoverySource: "none",
537
+ timestamp: Date.now(),
538
+ };
539
+ assert.equal(formatFailureContext(result), "");
540
+ });
541
+
542
+ test("formatFailureContext: caps total output at 10,000 chars", () => {
543
+ // Generate many failures to exceed 10,000 chars total
544
+ const checks: import("../types.ts").VerificationCheck[] = [];
545
+ for (let i = 0; i < 20; i++) {
546
+ checks.push({
547
+ command: `failing-command-${i}`,
548
+ exitCode: 1,
549
+ stdout: "",
550
+ stderr: "e".repeat(1000), // 1000 chars each, 20 * ~1050 (with formatting) > 10,000
551
+ durationMs: 100,
552
+ });
553
+ }
554
+ const result: import("../types.ts").VerificationResult = {
555
+ passed: false,
556
+ checks,
557
+ discoverySource: "preference",
558
+ timestamp: Date.now(),
559
+ };
560
+ const output = formatFailureContext(result);
561
+ assert.ok(output.length <= 10_100, `total output should be capped near 10,000 chars, got ${output.length}`);
562
+ assert.ok(output.includes("…[remaining failures truncated]"), "should include total truncation marker");
563
+ });
564
+
565
+ // ─── captureRuntimeErrors Tests (S04/T01) ─────────────────────────────────────
566
+
567
+ function makeProc(overrides: Record<string, unknown>) {
568
+ return {
569
+ id: "p1",
570
+ label: "test-server",
571
+ status: "ready",
572
+ alive: true,
573
+ exitCode: null,
574
+ signal: null,
575
+ recentErrors: [] as string[],
576
+ ...overrides,
577
+ };
578
+ }
579
+
580
+ function makeLogs(entries: Array<{ type: string; text: string }>) {
581
+ return entries.map((e, i) => ({
582
+ type: e.type,
583
+ text: e.text,
584
+ timestamp: Date.now() + i,
585
+ url: "http://localhost:3000",
586
+ }));
587
+ }
588
+
589
+ test("captureRuntimeErrors: crashed bg-shell process → blocking crash error", async () => {
590
+ const processes = new Map<string, unknown>([
591
+ ["p1", makeProc({ status: "crashed", alive: false, exitCode: 1 })],
592
+ ]);
593
+ const result = await captureRuntimeErrors({
594
+ getProcesses: () => processes,
595
+ getConsoleLogs: () => [],
596
+ });
597
+ assert.equal(result.length, 1);
598
+ assert.equal(result[0].source, "bg-shell");
599
+ assert.equal(result[0].severity, "crash");
600
+ assert.equal(result[0].blocking, true);
601
+ assert.ok(result[0].message.includes("test-server"));
602
+ });
603
+
604
+ test("captureRuntimeErrors: bg-shell non-zero exit + not alive → blocking crash error", async () => {
605
+ const processes = new Map<string, unknown>([
606
+ ["p1", makeProc({ status: "exited", alive: false, exitCode: 137 })],
607
+ ]);
608
+ const result = await captureRuntimeErrors({
609
+ getProcesses: () => processes,
610
+ getConsoleLogs: () => [],
611
+ });
612
+ assert.equal(result.length, 1);
613
+ assert.equal(result[0].severity, "crash");
614
+ assert.equal(result[0].blocking, true);
615
+ assert.ok(result[0].message.includes("exitCode=137"));
616
+ });
617
+
618
+ test("captureRuntimeErrors: bg-shell SIGABRT/SIGSEGV/SIGBUS → blocking crash error", async () => {
619
+ for (const sig of ["SIGABRT", "SIGSEGV", "SIGBUS"]) {
620
+ const processes = new Map<string, unknown>([
621
+ ["p1", makeProc({ signal: sig, alive: false, exitCode: null })],
622
+ ]);
623
+ const result = await captureRuntimeErrors({
624
+ getProcesses: () => processes,
625
+ getConsoleLogs: () => [],
626
+ });
627
+ assert.equal(result.length, 1, `${sig} should produce 1 error`);
628
+ assert.equal(result[0].severity, "crash");
629
+ assert.equal(result[0].blocking, true);
630
+ assert.ok(result[0].message.includes(sig), `message should contain ${sig}`);
631
+ }
632
+ });
633
+
634
+ test("captureRuntimeErrors: alive bg-shell process with recentErrors → non-blocking error", async () => {
635
+ const processes = new Map<string, unknown>([
636
+ ["p1", makeProc({ alive: true, recentErrors: ["TypeError: foo", "RangeError: bar"] })],
637
+ ]);
638
+ const result = await captureRuntimeErrors({
639
+ getProcesses: () => processes,
640
+ getConsoleLogs: () => [],
641
+ });
642
+ assert.equal(result.length, 1);
643
+ assert.equal(result[0].source, "bg-shell");
644
+ assert.equal(result[0].severity, "error");
645
+ assert.equal(result[0].blocking, false);
646
+ assert.ok(result[0].message.includes("TypeError: foo"));
647
+ assert.ok(result[0].message.includes("RangeError: bar"));
648
+ });
649
+
650
+ test("captureRuntimeErrors: browser unhandled rejection → blocking crash error", async () => {
651
+ const logs = makeLogs([
652
+ { type: "error", text: "Unhandled promise rejection: some error" },
653
+ ]);
654
+ const result = await captureRuntimeErrors({
655
+ getProcesses: () => new Map(),
656
+ getConsoleLogs: () => logs,
657
+ });
658
+ assert.equal(result.length, 1);
659
+ assert.equal(result[0].source, "browser");
660
+ assert.equal(result[0].severity, "crash");
661
+ assert.equal(result[0].blocking, true);
662
+ assert.ok(result[0].message.includes("Unhandled"));
663
+ });
664
+
665
+ test("captureRuntimeErrors: browser UnhandledRejection (case variation) → blocking crash", async () => {
666
+ const logs = makeLogs([
667
+ { type: "error", text: "UnhandledRejection in module X" },
668
+ ]);
669
+ const result = await captureRuntimeErrors({
670
+ getProcesses: () => new Map(),
671
+ getConsoleLogs: () => logs,
672
+ });
673
+ assert.equal(result.length, 1);
674
+ assert.equal(result[0].severity, "crash");
675
+ assert.equal(result[0].blocking, true);
676
+ });
677
+
678
+ test("captureRuntimeErrors: browser console.error (general) → non-blocking error", async () => {
679
+ const logs = makeLogs([
680
+ { type: "error", text: "Failed to load resource: net::ERR_FAILED" },
681
+ ]);
682
+ const result = await captureRuntimeErrors({
683
+ getProcesses: () => new Map(),
684
+ getConsoleLogs: () => logs,
685
+ });
686
+ assert.equal(result.length, 1);
687
+ assert.equal(result[0].source, "browser");
688
+ assert.equal(result[0].severity, "error");
689
+ assert.equal(result[0].blocking, false);
690
+ });
691
+
692
+ test("captureRuntimeErrors: browser deprecation warning → non-blocking warning", async () => {
693
+ const logs = makeLogs([
694
+ { type: "warning", text: "Event.returnValue is deprecated. Use Event.preventDefault() instead." },
695
+ ]);
696
+ const result = await captureRuntimeErrors({
697
+ getProcesses: () => new Map(),
698
+ getConsoleLogs: () => logs,
699
+ });
700
+ assert.equal(result.length, 1);
701
+ assert.equal(result[0].source, "browser");
702
+ assert.equal(result[0].severity, "warning");
703
+ assert.equal(result[0].blocking, false);
704
+ assert.ok(result[0].message.includes("deprecated"));
705
+ });
706
+
707
+ test("captureRuntimeErrors: non-deprecation warning is ignored", async () => {
708
+ const logs = makeLogs([
709
+ { type: "warning", text: "Some general warning about performance" },
710
+ ]);
711
+ const result = await captureRuntimeErrors({
712
+ getProcesses: () => new Map(),
713
+ getConsoleLogs: () => logs,
714
+ });
715
+ assert.equal(result.length, 0, "non-deprecation warnings should be ignored");
716
+ });
717
+
718
+ test("captureRuntimeErrors: no processes, no browser logs → empty array", async () => {
719
+ const result = await captureRuntimeErrors({
720
+ getProcesses: () => new Map(),
721
+ getConsoleLogs: () => [],
722
+ });
723
+ assert.deepStrictEqual(result, []);
724
+ });
725
+
726
+ test("captureRuntimeErrors: dynamic import failure → graceful empty array", async () => {
727
+ const result = await captureRuntimeErrors({
728
+ getProcesses: () => { throw new Error("module not found"); },
729
+ getConsoleLogs: () => { throw new Error("module not found"); },
730
+ });
731
+ assert.deepStrictEqual(result, []);
732
+ });
733
+
734
+ test("captureRuntimeErrors: browser text truncated to 500 chars", async () => {
735
+ const longText = "x".repeat(600);
736
+ const logs = makeLogs([
737
+ { type: "error", text: longText },
738
+ ]);
739
+ const result = await captureRuntimeErrors({
740
+ getProcesses: () => new Map(),
741
+ getConsoleLogs: () => logs,
742
+ });
743
+ assert.equal(result.length, 1);
744
+ assert.ok(result[0].message.length <= 500 + 20, "message should be truncated near 500 chars");
745
+ assert.ok(result[0].message.includes("…[truncated]"), "should include truncation marker");
746
+ assert.ok(!result[0].message.includes("x".repeat(501)), "should not contain 501+ x's");
747
+ });
748
+
749
+ test("captureRuntimeErrors: bg-shell recentErrors limited to 3 in message", async () => {
750
+ const processes = new Map<string, unknown>([
751
+ ["p1", makeProc({
752
+ status: "crashed",
753
+ alive: false,
754
+ exitCode: 1,
755
+ recentErrors: ["err1", "err2", "err3", "err4", "err5"],
756
+ })],
757
+ ]);
758
+ const result = await captureRuntimeErrors({
759
+ getProcesses: () => processes,
760
+ getConsoleLogs: () => [],
761
+ });
762
+ assert.equal(result.length, 1);
763
+ assert.ok(result[0].message.includes("err1"));
764
+ assert.ok(result[0].message.includes("err2"));
765
+ assert.ok(result[0].message.includes("err3"));
766
+ assert.ok(!result[0].message.includes("err4"), "should only include first 3 errors");
767
+ });
768
+
769
+ test("captureRuntimeErrors: mixed bg-shell and browser errors", async () => {
770
+ const processes = new Map<string, unknown>([
771
+ ["p1", makeProc({ status: "crashed", alive: false, exitCode: 1 })],
772
+ ]);
773
+ const logs = makeLogs([
774
+ { type: "error", text: "Unhandled rejection: boom" },
775
+ { type: "error", text: "general error" },
776
+ { type: "warning", text: "deprecated API used" },
777
+ ]);
778
+ const result = await captureRuntimeErrors({
779
+ getProcesses: () => processes,
780
+ getConsoleLogs: () => logs,
781
+ });
782
+ // 1 bg-shell crash + 1 browser crash (unhandled) + 1 browser error + 1 browser warning
783
+ assert.equal(result.length, 4);
784
+ const blocking = result.filter(r => r.blocking);
785
+ const nonBlocking = result.filter(r => !r.blocking);
786
+ assert.equal(blocking.length, 2, "should have 2 blocking errors");
787
+ assert.equal(nonBlocking.length, 2, "should have 2 non-blocking errors");
788
+ });
789
+
790
+ // ─── Dependency Audit Tests (S05/T01) ─────────────────────────────────────────
791
+
792
+ /** Helper: build a realistic npm audit JSON stdout with vulnerabilities. */
793
+ function makeAuditJson(
794
+ vulns: Record<string, { severity: string; fixAvailable: boolean; via: unknown[] }>,
795
+ ): string {
796
+ return JSON.stringify({ vulnerabilities: vulns });
797
+ }
798
+
799
+ /** Sample npm audit JSON with a high-severity vuln. */
800
+ const SAMPLE_AUDIT_JSON = makeAuditJson({
801
+ "nth-check": {
802
+ severity: "high",
803
+ fixAvailable: true,
804
+ via: [
805
+ {
806
+ title: "Inefficient Regular Expression Complexity in nth-check",
807
+ url: "https://github.com/advisories/GHSA-rp65-9cf3-cjxr",
808
+ severity: "high",
809
+ },
810
+ ],
811
+ },
812
+ });
813
+
814
+ test("dependency-audit: package.json in git diff → runs npm audit and parses vulnerabilities", () => {
815
+ let npmAuditCalled = false;
816
+ const result = runDependencyAudit("/tmp/test", {
817
+ gitDiff: () => ["package.json", "src/index.ts"],
818
+ npmAudit: () => {
819
+ npmAuditCalled = true;
820
+ return { stdout: SAMPLE_AUDIT_JSON, exitCode: 0 };
821
+ },
822
+ });
823
+ assert.equal(npmAuditCalled, true, "npm audit should be called");
824
+ assert.equal(result.length, 1);
825
+ assert.equal(result[0].name, "nth-check");
826
+ assert.equal(result[0].severity, "high");
827
+ assert.equal(result[0].title, "Inefficient Regular Expression Complexity in nth-check");
828
+ assert.equal(result[0].url, "https://github.com/advisories/GHSA-rp65-9cf3-cjxr");
829
+ assert.equal(result[0].fixAvailable, true);
830
+ });
831
+
832
+ test("dependency-audit: package-lock.json change triggers audit", () => {
833
+ let npmAuditCalled = false;
834
+ const result = runDependencyAudit("/tmp/test", {
835
+ gitDiff: () => ["package-lock.json"],
836
+ npmAudit: () => {
837
+ npmAuditCalled = true;
838
+ return { stdout: SAMPLE_AUDIT_JSON, exitCode: 0 };
839
+ },
840
+ });
841
+ assert.equal(npmAuditCalled, true);
842
+ assert.equal(result.length, 1);
843
+ });
844
+
845
+ test("dependency-audit: pnpm-lock.yaml change triggers audit", () => {
846
+ let npmAuditCalled = false;
847
+ runDependencyAudit("/tmp/test", {
848
+ gitDiff: () => ["pnpm-lock.yaml"],
849
+ npmAudit: () => {
850
+ npmAuditCalled = true;
851
+ return { stdout: SAMPLE_AUDIT_JSON, exitCode: 0 };
852
+ },
853
+ });
854
+ assert.equal(npmAuditCalled, true);
855
+ });
856
+
857
+ test("dependency-audit: yarn.lock change triggers audit", () => {
858
+ let npmAuditCalled = false;
859
+ runDependencyAudit("/tmp/test", {
860
+ gitDiff: () => ["yarn.lock"],
861
+ npmAudit: () => {
862
+ npmAuditCalled = true;
863
+ return { stdout: SAMPLE_AUDIT_JSON, exitCode: 0 };
864
+ },
865
+ });
866
+ assert.equal(npmAuditCalled, true);
867
+ });
868
+
869
+ test("dependency-audit: bun.lockb change triggers audit", () => {
870
+ let npmAuditCalled = false;
871
+ runDependencyAudit("/tmp/test", {
872
+ gitDiff: () => ["bun.lockb"],
873
+ npmAudit: () => {
874
+ npmAuditCalled = true;
875
+ return { stdout: SAMPLE_AUDIT_JSON, exitCode: 0 };
876
+ },
877
+ });
878
+ assert.equal(npmAuditCalled, true);
879
+ });
880
+
881
+ test("dependency-audit: no dependency file changes → returns empty array, npm audit not called", () => {
882
+ let npmAuditCalled = false;
883
+ const result = runDependencyAudit("/tmp/test", {
884
+ gitDiff: () => ["src/index.ts", "README.md"],
885
+ npmAudit: () => {
886
+ npmAuditCalled = true;
887
+ return { stdout: "{}", exitCode: 0 };
888
+ },
889
+ });
890
+ assert.equal(npmAuditCalled, false, "npm audit should NOT be called when no dependency files changed");
891
+ assert.deepStrictEqual(result, []);
892
+ });
893
+
894
+ test("dependency-audit: git diff returns non-zero exit (not a git repo) → empty array", () => {
895
+ const result = runDependencyAudit("/tmp/test", {
896
+ gitDiff: () => { throw new Error("not a git repo"); },
897
+ npmAudit: () => { throw new Error("should not be called"); },
898
+ });
899
+ assert.deepStrictEqual(result, []);
900
+ });
901
+
902
+ test("dependency-audit: npm audit returns invalid JSON → empty array", () => {
903
+ const result = runDependencyAudit("/tmp/test", {
904
+ gitDiff: () => ["package.json"],
905
+ npmAudit: () => ({ stdout: "not json at all", exitCode: 1 }),
906
+ });
907
+ assert.deepStrictEqual(result, []);
908
+ });
909
+
910
+ test("dependency-audit: npm audit returns zero vulnerabilities → empty array", () => {
911
+ const result = runDependencyAudit("/tmp/test", {
912
+ gitDiff: () => ["package.json"],
913
+ npmAudit: () => ({
914
+ stdout: JSON.stringify({ vulnerabilities: {} }),
915
+ exitCode: 0,
916
+ }),
917
+ });
918
+ assert.deepStrictEqual(result, []);
919
+ });
920
+
921
+ test("dependency-audit: npm audit non-zero exit with valid JSON → parses correctly", () => {
922
+ // npm audit exits non-zero when vulnerabilities exist — this is expected, not an error
923
+ const result = runDependencyAudit("/tmp/test", {
924
+ gitDiff: () => ["package-lock.json"],
925
+ npmAudit: () => ({
926
+ stdout: SAMPLE_AUDIT_JSON,
927
+ exitCode: 1, // non-zero!
928
+ }),
929
+ });
930
+ assert.equal(result.length, 1);
931
+ assert.equal(result[0].name, "nth-check");
932
+ assert.equal(result[0].severity, "high");
933
+ });
934
+
935
+ test("dependency-audit: via entries with string-only values are skipped", () => {
936
+ const auditJson = makeAuditJson({
937
+ "postcss": {
938
+ severity: "moderate",
939
+ fixAvailable: false,
940
+ via: ["nth-check", "css-select"], // string-only via entries
941
+ },
942
+ });
943
+ const result = runDependencyAudit("/tmp/test", {
944
+ gitDiff: () => ["package.json"],
945
+ npmAudit: () => ({ stdout: auditJson, exitCode: 1 }),
946
+ });
947
+ assert.equal(result.length, 1);
948
+ // When no object via entry is found, title falls back to the package name
949
+ assert.equal(result[0].name, "postcss");
950
+ assert.equal(result[0].title, "postcss");
951
+ assert.equal(result[0].url, "");
952
+ });
953
+
954
+ test("dependency-audit: subdirectory package.json does not trigger audit", () => {
955
+ let npmAuditCalled = false;
956
+ const result = runDependencyAudit("/tmp/test", {
957
+ gitDiff: () => ["packages/foo/package.json", "libs/bar/package-lock.json"],
958
+ npmAudit: () => {
959
+ npmAuditCalled = true;
960
+ return { stdout: SAMPLE_AUDIT_JSON, exitCode: 0 };
961
+ },
962
+ });
963
+ assert.equal(npmAuditCalled, false, "subdirectory dependency files should not trigger audit");
964
+ assert.deepStrictEqual(result, []);
965
+ });