@principles/pd-cli 1.73.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 (298) hide show
  1. package/README.md +90 -0
  2. package/dist/commands/artifact.d.ts +14 -0
  3. package/dist/commands/artifact.d.ts.map +1 -0
  4. package/dist/commands/artifact.js +67 -0
  5. package/dist/commands/artifact.js.map +1 -0
  6. package/dist/commands/candidate.d.ts +83 -0
  7. package/dist/commands/candidate.d.ts.map +1 -0
  8. package/dist/commands/candidate.js +891 -0
  9. package/dist/commands/candidate.js.map +1 -0
  10. package/dist/commands/central-sync.d.ts +10 -0
  11. package/dist/commands/central-sync.d.ts.map +1 -0
  12. package/dist/commands/central-sync.js +32 -0
  13. package/dist/commands/central-sync.js.map +1 -0
  14. package/dist/commands/console.d.ts +9 -0
  15. package/dist/commands/console.d.ts.map +1 -0
  16. package/dist/commands/console.js +114 -0
  17. package/dist/commands/console.js.map +1 -0
  18. package/dist/commands/context.d.ts +7 -0
  19. package/dist/commands/context.d.ts.map +1 -0
  20. package/dist/commands/context.js +55 -0
  21. package/dist/commands/context.js.map +1 -0
  22. package/dist/commands/demo-story-a.d.ts +12 -0
  23. package/dist/commands/demo-story-a.d.ts.map +1 -0
  24. package/dist/commands/demo-story-a.js +175 -0
  25. package/dist/commands/demo-story-a.js.map +1 -0
  26. package/dist/commands/diagnose.d.ts +35 -0
  27. package/dist/commands/diagnose.d.ts.map +1 -0
  28. package/dist/commands/diagnose.js +390 -0
  29. package/dist/commands/diagnose.js.map +1 -0
  30. package/dist/commands/evolution-tasks-list.d.ts +15 -0
  31. package/dist/commands/evolution-tasks-list.d.ts.map +1 -0
  32. package/dist/commands/evolution-tasks-list.js +34 -0
  33. package/dist/commands/evolution-tasks-list.js.map +1 -0
  34. package/dist/commands/evolution-tasks-show.d.ts +14 -0
  35. package/dist/commands/evolution-tasks-show.d.ts.map +1 -0
  36. package/dist/commands/evolution-tasks-show.js +52 -0
  37. package/dist/commands/evolution-tasks-show.js.map +1 -0
  38. package/dist/commands/flow.d.ts +7 -0
  39. package/dist/commands/flow.d.ts.map +1 -0
  40. package/dist/commands/flow.js +57 -0
  41. package/dist/commands/flow.js.map +1 -0
  42. package/dist/commands/health.d.ts +16 -0
  43. package/dist/commands/health.d.ts.map +1 -0
  44. package/dist/commands/health.js +150 -0
  45. package/dist/commands/health.js.map +1 -0
  46. package/dist/commands/history.d.ts +11 -0
  47. package/dist/commands/history.d.ts.map +1 -0
  48. package/dist/commands/history.js +50 -0
  49. package/dist/commands/history.js.map +1 -0
  50. package/dist/commands/legacy-cleanup.d.ts +27 -0
  51. package/dist/commands/legacy-cleanup.d.ts.map +1 -0
  52. package/dist/commands/legacy-cleanup.js +171 -0
  53. package/dist/commands/legacy-cleanup.js.map +1 -0
  54. package/dist/commands/legacy-import.d.ts +7 -0
  55. package/dist/commands/legacy-import.d.ts.map +1 -0
  56. package/dist/commands/legacy-import.js +86 -0
  57. package/dist/commands/legacy-import.js.map +1 -0
  58. package/dist/commands/pain-record.d.ts +10 -0
  59. package/dist/commands/pain-record.d.ts.map +1 -0
  60. package/dist/commands/pain-record.js +162 -0
  61. package/dist/commands/pain-record.js.map +1 -0
  62. package/dist/commands/proven-channel-baseline.d.ts +12 -0
  63. package/dist/commands/proven-channel-baseline.d.ts.map +1 -0
  64. package/dist/commands/proven-channel-baseline.js +97 -0
  65. package/dist/commands/proven-channel-baseline.js.map +1 -0
  66. package/dist/commands/remediation-output.d.ts +40 -0
  67. package/dist/commands/remediation-output.d.ts.map +1 -0
  68. package/dist/commands/remediation-output.js +23 -0
  69. package/dist/commands/remediation-output.js.map +1 -0
  70. package/dist/commands/run.d.ts +10 -0
  71. package/dist/commands/run.d.ts.map +1 -0
  72. package/dist/commands/run.js +68 -0
  73. package/dist/commands/run.js.map +1 -0
  74. package/dist/commands/runtime-activation.d.ts +11 -0
  75. package/dist/commands/runtime-activation.d.ts.map +1 -0
  76. package/dist/commands/runtime-activation.js +150 -0
  77. package/dist/commands/runtime-activation.js.map +1 -0
  78. package/dist/commands/runtime-canary.d.ts +30 -0
  79. package/dist/commands/runtime-canary.d.ts.map +1 -0
  80. package/dist/commands/runtime-canary.js +343 -0
  81. package/dist/commands/runtime-canary.js.map +1 -0
  82. package/dist/commands/runtime-diagnostics-export.d.ts +20 -0
  83. package/dist/commands/runtime-diagnostics-export.d.ts.map +1 -0
  84. package/dist/commands/runtime-diagnostics-export.js +177 -0
  85. package/dist/commands/runtime-diagnostics-export.js.map +1 -0
  86. package/dist/commands/runtime-features.d.ts +26 -0
  87. package/dist/commands/runtime-features.d.ts.map +1 -0
  88. package/dist/commands/runtime-features.js +70 -0
  89. package/dist/commands/runtime-features.js.map +1 -0
  90. package/dist/commands/runtime-gfi-snapshot.d.ts +7 -0
  91. package/dist/commands/runtime-gfi-snapshot.d.ts.map +1 -0
  92. package/dist/commands/runtime-gfi-snapshot.js +101 -0
  93. package/dist/commands/runtime-gfi-snapshot.js.map +1 -0
  94. package/dist/commands/runtime-health-snapshot.d.ts +7 -0
  95. package/dist/commands/runtime-health-snapshot.d.ts.map +1 -0
  96. package/dist/commands/runtime-health-snapshot.js +93 -0
  97. package/dist/commands/runtime-health-snapshot.js.map +1 -0
  98. package/dist/commands/runtime-idle-trigger.d.ts +12 -0
  99. package/dist/commands/runtime-idle-trigger.d.ts.map +1 -0
  100. package/dist/commands/runtime-idle-trigger.js +102 -0
  101. package/dist/commands/runtime-idle-trigger.js.map +1 -0
  102. package/dist/commands/runtime-internalization-enqueue-successors.d.ts +9 -0
  103. package/dist/commands/runtime-internalization-enqueue-successors.d.ts.map +1 -0
  104. package/dist/commands/runtime-internalization-enqueue-successors.js +393 -0
  105. package/dist/commands/runtime-internalization-enqueue-successors.js.map +1 -0
  106. package/dist/commands/runtime-internalization-integrity-repair.d.ts +9 -0
  107. package/dist/commands/runtime-internalization-integrity-repair.d.ts.map +1 -0
  108. package/dist/commands/runtime-internalization-integrity-repair.js +54 -0
  109. package/dist/commands/runtime-internalization-integrity-repair.js.map +1 -0
  110. package/dist/commands/runtime-internalization-integrity.d.ts +7 -0
  111. package/dist/commands/runtime-internalization-integrity.d.ts.map +1 -0
  112. package/dist/commands/runtime-internalization-integrity.js +53 -0
  113. package/dist/commands/runtime-internalization-integrity.js.map +1 -0
  114. package/dist/commands/runtime-internalization-queue.d.ts +7 -0
  115. package/dist/commands/runtime-internalization-queue.d.ts.map +1 -0
  116. package/dist/commands/runtime-internalization-queue.js +85 -0
  117. package/dist/commands/runtime-internalization-queue.js.map +1 -0
  118. package/dist/commands/runtime-internalization-run-once.d.ts +12 -0
  119. package/dist/commands/runtime-internalization-run-once.d.ts.map +1 -0
  120. package/dist/commands/runtime-internalization-run-once.js +546 -0
  121. package/dist/commands/runtime-internalization-run-once.js.map +1 -0
  122. package/dist/commands/runtime-internalization-wake-once.d.ts +8 -0
  123. package/dist/commands/runtime-internalization-wake-once.d.ts.map +1 -0
  124. package/dist/commands/runtime-internalization-wake-once.js +72 -0
  125. package/dist/commands/runtime-internalization-wake-once.js.map +1 -0
  126. package/dist/commands/runtime-pain-flood-simulation.d.ts +10 -0
  127. package/dist/commands/runtime-pain-flood-simulation.d.ts.map +1 -0
  128. package/dist/commands/runtime-pain-flood-simulation.js +104 -0
  129. package/dist/commands/runtime-pain-flood-simulation.js.map +1 -0
  130. package/dist/commands/runtime-pruning.d.ts +45 -0
  131. package/dist/commands/runtime-pruning.d.ts.map +1 -0
  132. package/dist/commands/runtime-pruning.js +355 -0
  133. package/dist/commands/runtime-pruning.js.map +1 -0
  134. package/dist/commands/runtime-recovery.d.ts +9 -0
  135. package/dist/commands/runtime-recovery.d.ts.map +1 -0
  136. package/dist/commands/runtime-recovery.js +94 -0
  137. package/dist/commands/runtime-recovery.js.map +1 -0
  138. package/dist/commands/runtime-synthetic-baseline.d.ts +7 -0
  139. package/dist/commands/runtime-synthetic-baseline.d.ts.map +1 -0
  140. package/dist/commands/runtime-synthetic-baseline.js +59 -0
  141. package/dist/commands/runtime-synthetic-baseline.js.map +1 -0
  142. package/dist/commands/runtime-uat.d.ts +52 -0
  143. package/dist/commands/runtime-uat.d.ts.map +1 -0
  144. package/dist/commands/runtime-uat.js +274 -0
  145. package/dist/commands/runtime-uat.js.map +1 -0
  146. package/dist/commands/runtime.d.ts +20 -0
  147. package/dist/commands/runtime.d.ts.map +1 -0
  148. package/dist/commands/runtime.js +256 -0
  149. package/dist/commands/runtime.js.map +1 -0
  150. package/dist/commands/samples-list.d.ts +11 -0
  151. package/dist/commands/samples-list.d.ts.map +1 -0
  152. package/dist/commands/samples-list.js +37 -0
  153. package/dist/commands/samples-list.js.map +1 -0
  154. package/dist/commands/samples-review.d.ts +14 -0
  155. package/dist/commands/samples-review.d.ts.map +1 -0
  156. package/dist/commands/samples-review.js +22 -0
  157. package/dist/commands/samples-review.js.map +1 -0
  158. package/dist/commands/task.d.ts +14 -0
  159. package/dist/commands/task.d.ts.map +1 -0
  160. package/dist/commands/task.js +92 -0
  161. package/dist/commands/task.js.map +1 -0
  162. package/dist/commands/trace.d.ts +19 -0
  163. package/dist/commands/trace.d.ts.map +1 -0
  164. package/dist/commands/trace.js +154 -0
  165. package/dist/commands/trace.js.map +1 -0
  166. package/dist/commands/trajectory.d.ts +11 -0
  167. package/dist/commands/trajectory.d.ts.map +1 -0
  168. package/dist/commands/trajectory.js +47 -0
  169. package/dist/commands/trajectory.js.map +1 -0
  170. package/dist/index.d.ts +9 -0
  171. package/dist/index.d.ts.map +1 -0
  172. package/dist/index.js +736 -0
  173. package/dist/index.js.map +1 -0
  174. package/dist/legacy/legacy-import.d.ts +15 -0
  175. package/dist/legacy/legacy-import.d.ts.map +1 -0
  176. package/dist/legacy/legacy-import.js +141 -0
  177. package/dist/legacy/legacy-import.js.map +1 -0
  178. package/dist/legacy/session-history-import.d.ts +26 -0
  179. package/dist/legacy/session-history-import.d.ts.map +1 -0
  180. package/dist/legacy/session-history-import.js +151 -0
  181. package/dist/legacy/session-history-import.js.map +1 -0
  182. package/dist/principle-tree-ledger-adapter.d.ts +12 -0
  183. package/dist/principle-tree-ledger-adapter.d.ts.map +1 -0
  184. package/dist/principle-tree-ledger-adapter.js +12 -0
  185. package/dist/principle-tree-ledger-adapter.js.map +1 -0
  186. package/dist/resolve-workspace.d.ts +12 -0
  187. package/dist/resolve-workspace.d.ts.map +1 -0
  188. package/dist/resolve-workspace.js +20 -0
  189. package/dist/resolve-workspace.js.map +1 -0
  190. package/dist/services/demo-story-a-runner.d.ts +8 -0
  191. package/dist/services/demo-story-a-runner.d.ts.map +1 -0
  192. package/dist/services/demo-story-a-runner.js +369 -0
  193. package/dist/services/demo-story-a-runner.js.map +1 -0
  194. package/dist/services/feature-flag-loader.d.ts +6 -0
  195. package/dist/services/feature-flag-loader.d.ts.map +1 -0
  196. package/dist/services/feature-flag-loader.js +54 -0
  197. package/dist/services/feature-flag-loader.js.map +1 -0
  198. package/dist/services/pain-flood-simulation-runner.d.ts +10 -0
  199. package/dist/services/pain-flood-simulation-runner.d.ts.map +1 -0
  200. package/dist/services/pain-flood-simulation-runner.js +289 -0
  201. package/dist/services/pain-flood-simulation-runner.js.map +1 -0
  202. package/dist/services/proven-channel-baseline-runner.d.ts +12 -0
  203. package/dist/services/proven-channel-baseline-runner.d.ts.map +1 -0
  204. package/dist/services/proven-channel-baseline-runner.js +114 -0
  205. package/dist/services/proven-channel-baseline-runner.js.map +1 -0
  206. package/dist/services/synthetic-baseline-runner.d.ts +8 -0
  207. package/dist/services/synthetic-baseline-runner.d.ts.map +1 -0
  208. package/dist/services/synthetic-baseline-runner.js +251 -0
  209. package/dist/services/synthetic-baseline-runner.js.map +1 -0
  210. package/package.json +35 -0
  211. package/src/commands/artifact.ts +82 -0
  212. package/src/commands/candidate.ts +1117 -0
  213. package/src/commands/central-sync.ts +44 -0
  214. package/src/commands/console.ts +121 -0
  215. package/src/commands/context.ts +72 -0
  216. package/src/commands/demo-story-a.ts +195 -0
  217. package/src/commands/diagnose.ts +452 -0
  218. package/src/commands/evolution-tasks-list.ts +44 -0
  219. package/src/commands/evolution-tasks-show.ts +60 -0
  220. package/src/commands/flow.ts +60 -0
  221. package/src/commands/health.ts +189 -0
  222. package/src/commands/history.ts +63 -0
  223. package/src/commands/legacy-cleanup.ts +206 -0
  224. package/src/commands/legacy-import.ts +104 -0
  225. package/src/commands/pain-record.ts +167 -0
  226. package/src/commands/proven-channel-baseline.ts +113 -0
  227. package/src/commands/remediation-output.ts +66 -0
  228. package/src/commands/run.ts +89 -0
  229. package/src/commands/runtime-activation.ts +176 -0
  230. package/src/commands/runtime-canary.ts +371 -0
  231. package/src/commands/runtime-diagnostics-export.ts +229 -0
  232. package/src/commands/runtime-features.ts +103 -0
  233. package/src/commands/runtime-gfi-snapshot.ts +135 -0
  234. package/src/commands/runtime-health-snapshot.ts +106 -0
  235. package/src/commands/runtime-internalization-enqueue-successors.ts +479 -0
  236. package/src/commands/runtime-internalization-integrity-repair.ts +69 -0
  237. package/src/commands/runtime-internalization-integrity.ts +63 -0
  238. package/src/commands/runtime-internalization-queue.ts +106 -0
  239. package/src/commands/runtime-internalization-run-once.ts +658 -0
  240. package/src/commands/runtime-internalization-wake-once.ts +87 -0
  241. package/src/commands/runtime-pain-flood-simulation.ts +121 -0
  242. package/src/commands/runtime-pruning.ts +438 -0
  243. package/src/commands/runtime-recovery.ts +107 -0
  244. package/src/commands/runtime-synthetic-baseline.ts +70 -0
  245. package/src/commands/runtime-uat.ts +339 -0
  246. package/src/commands/runtime.ts +281 -0
  247. package/src/commands/samples-list.ts +43 -0
  248. package/src/commands/samples-review.ts +32 -0
  249. package/src/commands/task.ts +130 -0
  250. package/src/commands/trace.ts +174 -0
  251. package/src/commands/trajectory.ts +64 -0
  252. package/src/index.ts +829 -0
  253. package/src/legacy/legacy-import.ts +179 -0
  254. package/src/legacy/session-history-import.ts +231 -0
  255. package/src/principle-tree-ledger-adapter.ts +13 -0
  256. package/src/resolve-workspace.ts +20 -0
  257. package/src/services/demo-story-a-runner.ts +472 -0
  258. package/src/services/feature-flag-loader.ts +73 -0
  259. package/src/services/pain-flood-simulation-runner.ts +354 -0
  260. package/src/services/proven-channel-baseline-runner.ts +150 -0
  261. package/src/services/synthetic-baseline-runner.ts +291 -0
  262. package/tests/commands/candidate-audit-repair.test.ts +338 -0
  263. package/tests/commands/candidate-intake.test.ts +589 -0
  264. package/tests/commands/candidate-internalization-backfill.test.ts +480 -0
  265. package/tests/commands/candidate-internalize.test.ts +272 -0
  266. package/tests/commands/candidate-route.test.ts +328 -0
  267. package/tests/commands/candidate-show.test.ts +95 -0
  268. package/tests/commands/cli-command-tree.test.ts +64 -0
  269. package/tests/commands/context.test.ts +114 -0
  270. package/tests/commands/demo-story-a.test.ts +255 -0
  271. package/tests/commands/diagnose.test.ts +792 -0
  272. package/tests/commands/health.test.ts +330 -0
  273. package/tests/commands/pain-record.test.ts +316 -0
  274. package/tests/commands/plugin-config-resolution-cutover.test.ts +220 -0
  275. package/tests/commands/proven-channel-baseline.test.ts +441 -0
  276. package/tests/commands/runtime-activation.test.ts +168 -0
  277. package/tests/commands/runtime-canary.test.ts +369 -0
  278. package/tests/commands/runtime-diagnostics-export.test.ts +170 -0
  279. package/tests/commands/runtime-features.test.ts +114 -0
  280. package/tests/commands/runtime-health-snapshot.test.ts +357 -0
  281. package/tests/commands/runtime-internalization-enqueue-successors.test.ts +803 -0
  282. package/tests/commands/runtime-internalization-integrity-repair.test.ts +169 -0
  283. package/tests/commands/runtime-internalization-integrity.test.ts +102 -0
  284. package/tests/commands/runtime-internalization-queue.test.ts +252 -0
  285. package/tests/commands/runtime-internalization-run-once.test.ts +1318 -0
  286. package/tests/commands/runtime-internalization-wake-once.test.ts +170 -0
  287. package/tests/commands/runtime-internalization.test.ts +52 -0
  288. package/tests/commands/runtime-pain-flood-simulation.test.ts +418 -0
  289. package/tests/commands/runtime-pruning.test.ts +693 -0
  290. package/tests/commands/runtime-recovery.test.ts +96 -0
  291. package/tests/commands/runtime-synthetic-baseline.test.ts +249 -0
  292. package/tests/commands/runtime-uat.test.ts +397 -0
  293. package/tests/commands/runtime.test.ts +262 -0
  294. package/tests/commands/trace.test.ts +314 -0
  295. package/tests/e2e/candidate-intake-e2e.test.ts +316 -0
  296. package/tests/services/feature-flag-loader.test.ts +207 -0
  297. package/tests/services/proven-channel-baseline-runner.test.ts +30 -0
  298. package/tsconfig.json +26 -0
@@ -0,0 +1,693 @@
1
+ /**
2
+ * pd runtime pruning CLI unit tests — report, explain, review.
3
+ */
4
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
5
+
6
+ type Never = never;
7
+
8
+ const { MockPruningReadModel } = vi.hoisted(() => {
9
+ class MockPruningReadModel {
10
+ getPrincipleSignals() {
11
+ return [
12
+ {
13
+ principleId: 'p_watch',
14
+ status: 'active' as const,
15
+ createdAt: '2026-01-01T00:00:00.000Z',
16
+ updatedAt: '2026-01-01T00:00:00.000Z',
17
+ derivedCandidateIds: [] as string[],
18
+ derivedPainCount: 0,
19
+ matchedCandidateCount: 0,
20
+ recentCandidateCount: 0,
21
+ orphanCandidateCount: 0,
22
+ ageDays: 45,
23
+ riskLevel: 'watch' as const,
24
+ reasons: ['watch: principle older than 30 days with no recent derived pain signals [source: createdAt + derivedFromPainIds]'],
25
+ },
26
+ {
27
+ principleId: 'p_review',
28
+ status: 'active' as const,
29
+ createdAt: '2025-01-01T00:00:00.000Z',
30
+ updatedAt: '2025-01-01T00:00:00.000Z',
31
+ derivedCandidateIds: [] as string[],
32
+ derivedPainCount: 0,
33
+ matchedCandidateCount: 0,
34
+ recentCandidateCount: 0,
35
+ orphanCandidateCount: 0,
36
+ ageDays: 120,
37
+ riskLevel: 'review' as const,
38
+ reasons: ['review: principle older than 90 days with no derived pain signals [source: createdAt + derivedFromPainIds]'],
39
+ },
40
+ ];
41
+ }
42
+ getHealthSummary() {
43
+ return {
44
+ totalPrinciples: 2,
45
+ byStatus: { active: 2 },
46
+ watchCount: 1,
47
+ reviewCount: 1,
48
+ orphanDerivedCandidateCount: 0,
49
+ averageAgeDays: 82,
50
+ generatedAt: '2026-05-02T00:00:00.000Z',
51
+ };
52
+ }
53
+ getOrphanDerivedCandidates() {
54
+ return { candidates: [], dbReadable: true };
55
+ }
56
+ }
57
+ return { MockPruningReadModel };
58
+ }, { validateType: false });
59
+
60
+ const mockAppendPruningReview = vi.hoisted(() => vi.fn());
61
+ const mockListPruningReviews = vi.hoisted(() => vi.fn());
62
+ const mockBuildMaskedPrincipleSet = vi.hoisted(() => vi.fn());
63
+ const mockRemoveOrphanReferencesFromLedger = vi.hoisted(() => vi.fn());
64
+
65
+ vi.mock('../../src/resolve-workspace.js', () => ({
66
+ resolveWorkspaceDir: vi.fn().mockReturnValue('/tmp/test-workspace'),
67
+ }));
68
+
69
+ vi.mock('@principles/core/runtime-v2', () => ({
70
+ PruningReadModel: vi.fn().mockImplementation(function () {
71
+ return new MockPruningReadModel();
72
+ }),
73
+ createRemediationResult: vi.fn((input) => ({
74
+ mode: input.mode,
75
+ status: input.status ?? (input.mode === 'dry_run'
76
+ ? (input.actions?.length > 0 ? 'would_change' : 'no_op')
77
+ : (input.repairedCount > 0 ? 'changed' : 'no_op')),
78
+ safeToConfirm: input.safeToConfirm ?? false,
79
+ repairedCount: input.repairedCount ?? 0,
80
+ skippedCount: input.skippedCount ?? 0,
81
+ actions: input.actions ?? [],
82
+ warnings: input.warnings ?? [],
83
+ ...(input.includeLegacyDryRun ? { dryRun: input.mode === 'dry_run' } : {}),
84
+ })),
85
+ remediationAction: vi.fn((input) => input),
86
+ appendPruningReview: mockAppendPruningReview,
87
+ listPruningReviews: mockListPruningReviews,
88
+ buildMaskedPrincipleSet: mockBuildMaskedPrincipleSet,
89
+ removeOrphanReferencesFromLedger: mockRemoveOrphanReferencesFromLedger,
90
+ }));
91
+
92
+ import { handlePruningReport, handlePruningExplain, handlePruningReview, handlePruningRollback, handlePruningOrphans } from '../../src/commands/runtime-pruning.js';
93
+ import { PruningReadModel } from '@principles/core/runtime-v2';
94
+
95
+ // ── pd runtime pruning report ───────────────────────────────────────────────
96
+
97
+ describe('pd runtime pruning report', () => {
98
+ beforeEach(() => {
99
+ vi.clearAllMocks();
100
+ });
101
+
102
+ it('text output contains read-only note', () => {
103
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
104
+ handlePruningReport({ json: false });
105
+ expect(consoleSpy).toHaveBeenCalledWith(
106
+ expect.stringContaining('NOTE: This report is read-only. No principles are modified or deleted.')
107
+ );
108
+ consoleSpy.mockRestore();
109
+ });
110
+
111
+ it('text output includes watch and review sections', () => {
112
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
113
+ handlePruningReport({ json: false });
114
+ const output = consoleSpy.mock.calls.map((c) => c[0]).join('\n');
115
+ expect(output).toContain('Principles flagged WATCH');
116
+ expect(output).toContain('Principles flagged REVIEW');
117
+ expect(output).toContain('p_watch');
118
+ expect(output).toContain('p_review');
119
+ consoleSpy.mockRestore();
120
+ });
121
+
122
+ it('--json flag outputs full shape', () => {
123
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
124
+ handlePruningReport({ json: true });
125
+ expect(consoleSpy).toHaveBeenCalledTimes(1);
126
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
127
+ expect(output).toHaveProperty('generatedAt');
128
+ expect(output).toHaveProperty('workspace');
129
+ expect(output).toHaveProperty('summary');
130
+ expect(output).toHaveProperty('signals');
131
+ expect(output.summary.watchCount).toBe(1);
132
+ expect(output.summary.reviewCount).toBe(1);
133
+ consoleSpy.mockRestore();
134
+ });
135
+
136
+ it('--workspace passes explicit path to PruningReadModel constructor', () => {
137
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
138
+ handlePruningReport({ workspace: '/custom/workspace', json: false });
139
+ const calls = (PruningReadModel as ReturnType<typeof vi.fn>).mock.calls;
140
+ expect(calls.length).toBeGreaterThan(0);
141
+ expect(String(calls[calls.length - 1][0].workspaceDir)).toMatch(/custom.*workspace/);
142
+ consoleSpy.mockRestore();
143
+ });
144
+
145
+ it('error-path: propagates errors from PruningReadModel', () => {
146
+ class MockErrorReadModel {
147
+ getPrincipleSignals() { throw new Error('DB query failed'); }
148
+ getHealthSummary() { return {}; }
149
+ }
150
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
151
+ return new MockErrorReadModel() as unknown as InstanceType<typeof PruningReadModel>;
152
+ });
153
+ expect(() => handlePruningReport({ json: false })).toThrow('DB query failed');
154
+ });
155
+
156
+ it('healthy-path: no watch or review signals', () => {
157
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
158
+ class MockHealthyReadModel {
159
+ getPrincipleSignals() { return []; }
160
+ getHealthSummary() {
161
+ return {
162
+ totalPrinciples: 5,
163
+ byStatus: { active: 5 },
164
+ watchCount: 0,
165
+ reviewCount: 0,
166
+ orphanDerivedCandidateCount: 0,
167
+ averageAgeDays: 10,
168
+ generatedAt: '2026-05-02T00:00:00.000Z',
169
+ };
170
+ }
171
+ }
172
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
173
+ return new MockHealthyReadModel() as unknown as InstanceType<typeof PruningReadModel>;
174
+ });
175
+ handlePruningReport({ json: false });
176
+ const output = consoleSpy.mock.calls.map((c) => c[0]).join('\n');
177
+ expect(output).toContain('No watch or review signals. System is healthy.');
178
+ expect(output).not.toContain('WATCH');
179
+ consoleSpy.mockRestore();
180
+ });
181
+ });
182
+
183
+ // ── pd runtime pruning explain ───────────────────────────────────────────────
184
+
185
+ describe('pd runtime pruning explain', () => {
186
+ beforeEach(() => {
187
+ vi.clearAllMocks();
188
+ });
189
+
190
+ it('explain --json outputs matching signal for p_watch', () => {
191
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
192
+ handlePruningExplain({ principleId: 'p_watch', json: true });
193
+ expect(consoleSpy).toHaveBeenCalledTimes(1);
194
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
195
+ expect(output.principleId).toBe('p_watch');
196
+ expect(output.signal).toBeDefined();
197
+ expect(output.workspace).toBeDefined();
198
+ expect(output.generatedAt).toBeDefined();
199
+ consoleSpy.mockRestore();
200
+ });
201
+
202
+ it('explain text output includes reason lines and read-only note', () => {
203
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
204
+ handlePruningExplain({ principleId: 'p_watch', json: false });
205
+ const output = consoleSpy.mock.calls.map((c) => c[0]).join('\n');
206
+ expect(output).toContain('p_watch');
207
+ expect(output).toContain('watch');
208
+ expect(output).toContain('NOTE: This report is read-only.');
209
+ consoleSpy.mockRestore();
210
+ });
211
+
212
+ it('explain missing principle exits 1', () => {
213
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
214
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
215
+ handlePruningExplain({ principleId: 'nonexistent', json: false });
216
+ expect(processSpy).toHaveBeenCalledWith(1);
217
+ consoleSpy.mockRestore();
218
+ processSpy.mockRestore();
219
+ });
220
+
221
+ it('explain passes explicit workspace to PruningReadModel', () => {
222
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
223
+ handlePruningExplain({ principleId: 'p_watch', workspace: '/custom/workspace', json: false });
224
+ const calls = (PruningReadModel as ReturnType<typeof vi.fn>).mock.calls;
225
+ expect(calls.length).toBeGreaterThan(0);
226
+ expect(String(calls[calls.length - 1][0].workspaceDir)).toMatch(/custom.*workspace/);
227
+ consoleSpy.mockRestore();
228
+ });
229
+ });
230
+
231
+ // ── pd runtime pruning review ───────────────────────────────────────────────
232
+
233
+ describe('pd runtime pruning review', () => {
234
+ beforeEach(() => {
235
+ vi.clearAllMocks();
236
+ mockAppendPruningReview.mockReset();
237
+ mockAppendPruningReview.mockReturnValue({
238
+ reviewId: 'review-uuid-123',
239
+ principleId: 'p_watch',
240
+ decision: 'keep',
241
+ note: '',
242
+ reviewer: 'operator',
243
+ reviewedAt: '2026-05-02T00:00:00.000Z',
244
+ signalSnapshot: {
245
+ principleId: 'p_watch',
246
+ status: 'active',
247
+ createdAt: '2026-01-01T00:00:00.000Z',
248
+ updatedAt: '2026-01-01T00:00:00.000Z',
249
+ derivedCandidateIds: [],
250
+ derivedPainCount: 0,
251
+ matchedCandidateCount: 0,
252
+ recentCandidateCount: 0,
253
+ orphanCandidateCount: 0,
254
+ ageDays: 45,
255
+ riskLevel: 'watch',
256
+ reasons: ['watch: principle older than 30 days'],
257
+ },
258
+ });
259
+ });
260
+
261
+ it('review --json writes review record and outputs reviewId', () => {
262
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
263
+ handlePruningReview({ principleId: 'p_watch', decision: 'keep', json: true });
264
+ expect(mockAppendPruningReview).toHaveBeenCalledTimes(1);
265
+ const callInput = mockAppendPruningReview.mock.calls[0]![1];
266
+ expect(callInput.principleId).toBe('p_watch');
267
+ expect(callInput.decision).toBe('keep');
268
+ expect(callInput.signalSnapshot).toBeDefined();
269
+ expect(callInput.signalSnapshot.principleId).toBe('p_watch');
270
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
271
+ expect(output.reviewId).toBe('review-uuid-123');
272
+ expect(output.principleId).toBe('p_watch');
273
+ expect(output.decision).toBe('keep');
274
+ expect(output.reviewedAt).toBeDefined();
275
+ consoleSpy.mockRestore();
276
+ });
277
+
278
+ it('review text output includes audit-only / no mutation note', () => {
279
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
280
+ handlePruningReview({ principleId: 'p_watch', decision: 'keep', note: 'looks fine', json: false });
281
+ const output = consoleSpy.mock.calls.map((c) => c[0]).join('\n');
282
+ expect(output).toContain('reviewId');
283
+ expect(output).toContain('reviewer');
284
+ expect(output).toContain('NOTE: This audit record does not modify the principle.');
285
+ consoleSpy.mockRestore();
286
+ });
287
+
288
+ it('review missing principle exits 1 and does not append', () => {
289
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
290
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
291
+ handlePruningReview({ principleId: 'nonexistent', decision: 'keep', json: false });
292
+ expect(mockAppendPruningReview).not.toHaveBeenCalled();
293
+ expect(processSpy).toHaveBeenCalledWith(1);
294
+ consoleSpy.mockRestore();
295
+ processSpy.mockRestore();
296
+ });
297
+
298
+ it('review invalid decision exits 1 and does not append', () => {
299
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
300
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
301
+ // @ts-expect-error — testing invalid input
302
+ handlePruningReview({ principleId: 'p_watch', decision: 'invalid', json: false });
303
+ expect(mockAppendPruningReview).not.toHaveBeenCalled();
304
+ expect(processSpy).toHaveBeenCalledWith(1);
305
+ consoleSpy.mockRestore();
306
+ processSpy.mockRestore();
307
+ });
308
+
309
+ it('archive-candidate without note exits 1', () => {
310
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
311
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
312
+ handlePruningReview({ principleId: 'p_watch', decision: 'archive-candidate', note: undefined, json: false });
313
+ expect(mockAppendPruningReview).not.toHaveBeenCalled();
314
+ expect(processSpy).toHaveBeenCalledWith(1);
315
+ consoleSpy.mockRestore();
316
+ processSpy.mockRestore();
317
+ });
318
+
319
+ it('review passes workspace to PruningReadModel and appendPruningReview', () => {
320
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
321
+ handlePruningReview({ principleId: 'p_watch', decision: 'keep', workspace: '/custom/workspace', json: false });
322
+ const modelCalls = (PruningReadModel as ReturnType<typeof vi.fn>).mock.calls;
323
+ expect(modelCalls.length).toBeGreaterThan(0);
324
+ expect(modelCalls[modelCalls.length - 1][0].workspaceDir).toMatch(/custom[\\/]workspace/);
325
+ expect(mockAppendPruningReview).toHaveBeenCalledWith(expect.stringMatching(/custom[\\/]workspace/), expect.any(Object));
326
+ consoleSpy.mockRestore();
327
+ });
328
+
329
+ it('review captures signalSnapshot from matching signal', () => {
330
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
331
+ handlePruningReview({ principleId: 'p_watch', decision: 'defer', note: 'deferring', json: true });
332
+ const callInput = mockAppendPruningReview.mock.calls[0]![1];
333
+ expect(callInput.signalSnapshot).toMatchObject({
334
+ principleId: 'p_watch',
335
+ riskLevel: 'watch',
336
+ });
337
+ consoleSpy.mockRestore();
338
+ });
339
+ });
340
+
341
+ // ── pd runtime pruning rollback ───────────────────────────────────────────────
342
+
343
+ describe('pd runtime pruning rollback', () => {
344
+ beforeEach(() => {
345
+ vi.clearAllMocks();
346
+ mockAppendPruningReview.mockReset();
347
+ mockAppendPruningReview.mockReturnValue({
348
+ reviewId: 'rollback-uuid-456',
349
+ principleId: 'p_watch',
350
+ decision: 'keep',
351
+ note: 'Rollback: restore principle injection',
352
+ reviewer: 'operator',
353
+ reviewedAt: '2026-05-04T00:00:00.000Z',
354
+ signalSnapshot: {
355
+ principleId: 'p_watch',
356
+ status: 'active',
357
+ createdAt: '2026-01-01T00:00:00.000Z',
358
+ updatedAt: '2026-01-01T00:00:00.000Z',
359
+ derivedCandidateIds: [],
360
+ derivedPainCount: 0,
361
+ matchedCandidateCount: 0,
362
+ recentCandidateCount: 0,
363
+ orphanCandidateCount: 0,
364
+ ageDays: 45,
365
+ riskLevel: 'watch',
366
+ reasons: ['watch: principle older than 30 days'],
367
+ },
368
+ });
369
+ mockListPruningReviews.mockReset();
370
+ mockBuildMaskedPrincipleSet.mockReset();
371
+ });
372
+
373
+ it('rollback --json writes keep record and outputs reviewId', () => {
374
+ mockListPruningReviews.mockReturnValueOnce([
375
+ {
376
+ reviewId: 'rv-1',
377
+ principleId: 'p_watch',
378
+ decision: 'archive-candidate',
379
+ note: 'pruning candidate',
380
+ reviewer: 'operator',
381
+ reviewedAt: '2026-05-01T00:00:00.000Z',
382
+ signalSnapshot: {
383
+ principleId: 'p_watch',
384
+ status: 'active',
385
+ createdAt: '2026-01-01T00:00:00.000Z',
386
+ updatedAt: '2026-01-01T00:00:00.000Z',
387
+ derivedCandidateIds: [],
388
+ derivedPainCount: 0,
389
+ matchedCandidateCount: 0,
390
+ recentCandidateCount: 0,
391
+ orphanCandidateCount: 0,
392
+ ageDays: 45,
393
+ riskLevel: 'watch',
394
+ reasons: ['watch: principle older than 30 days'],
395
+ },
396
+ },
397
+ ]);
398
+ mockBuildMaskedPrincipleSet.mockReturnValueOnce(new Set(['p_watch']));
399
+
400
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
401
+ handlePruningRollback({ principleId: 'p_watch', json: true });
402
+ expect(mockAppendPruningReview).toHaveBeenCalledTimes(1);
403
+ const callInput = mockAppendPruningReview.mock.calls[0]![1];
404
+ expect(callInput.principleId).toBe('p_watch');
405
+ expect(callInput.decision).toBe('keep');
406
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
407
+ expect(output.reviewId).toBe('rollback-uuid-456');
408
+ expect(output.decision).toBe('keep');
409
+ consoleSpy.mockRestore();
410
+ });
411
+
412
+ it('rollback exits 1 when no reviews found', () => {
413
+ mockListPruningReviews.mockReturnValueOnce([]);
414
+
415
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
416
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
417
+ handlePruningRollback({ principleId: 'nonexistent', json: false });
418
+ expect(processSpy).toHaveBeenCalledWith(1);
419
+ consoleSpy.mockRestore();
420
+ processSpy.mockRestore();
421
+ });
422
+
423
+ it('rollback exits 1 when principle is not currently masked', () => {
424
+ mockListPruningReviews.mockReturnValueOnce([
425
+ {
426
+ reviewId: 'rv-1',
427
+ principleId: 'p_watch',
428
+ decision: 'keep',
429
+ note: 'looks fine',
430
+ reviewer: 'operator',
431
+ reviewedAt: '2026-05-01T00:00:00.000Z',
432
+ signalSnapshot: {
433
+ principleId: 'p_watch',
434
+ status: 'active',
435
+ createdAt: '2026-01-01T00:00:00.000Z',
436
+ updatedAt: '2026-01-01T00:00:00.000Z',
437
+ derivedCandidateIds: [],
438
+ derivedPainCount: 0,
439
+ matchedCandidateCount: 0,
440
+ recentCandidateCount: 0,
441
+ orphanCandidateCount: 0,
442
+ ageDays: 45,
443
+ riskLevel: 'watch',
444
+ reasons: [],
445
+ },
446
+ },
447
+ ]);
448
+ mockBuildMaskedPrincipleSet.mockReturnValueOnce(new Set());
449
+
450
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
451
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
452
+ handlePruningRollback({ principleId: 'p_watch', json: false });
453
+ expect(processSpy).toHaveBeenCalledWith(1);
454
+ expect(mockAppendPruningReview).not.toHaveBeenCalled();
455
+ consoleSpy.mockRestore();
456
+ processSpy.mockRestore();
457
+ });
458
+
459
+ it('rollback succeeds even when principle is not in pruning signals (degraded signalSnapshot)', () => {
460
+ // p_orphan has a review + mask but no live signal in MockPruningReadModel
461
+ mockListPruningReviews.mockReturnValueOnce([
462
+ {
463
+ reviewId: 'rv-1',
464
+ principleId: 'p_orphan',
465
+ decision: 'archive-candidate',
466
+ note: 'pruning candidate',
467
+ reviewer: 'operator',
468
+ reviewedAt: '2026-05-01T00:00:00.000Z',
469
+ signalSnapshot: {
470
+ principleId: 'p_orphan',
471
+ status: 'active',
472
+ createdAt: '2026-01-01T00:00:00.000Z',
473
+ updatedAt: '2026-01-01T00:00:00.000Z',
474
+ derivedCandidateIds: [],
475
+ derivedPainCount: 0,
476
+ matchedCandidateCount: 0,
477
+ recentCandidateCount: 0,
478
+ orphanCandidateCount: 0,
479
+ ageDays: 45,
480
+ riskLevel: 'watch',
481
+ reasons: [],
482
+ },
483
+ },
484
+ ]);
485
+ mockBuildMaskedPrincipleSet.mockReturnValueOnce(new Set(['p_orphan']));
486
+
487
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
488
+ handlePruningRollback({ principleId: 'p_orphan', json: true });
489
+ expect(mockAppendPruningReview).toHaveBeenCalledTimes(1);
490
+ const callInput = mockAppendPruningReview.mock.calls[0]![1];
491
+ // signal is undefined (p_orphan not in MockPruningReadModel) — real appendPruningReview applies fallback
492
+ expect(callInput.principleId).toBe('p_orphan');
493
+ expect(callInput.decision).toBe('keep');
494
+ expect(callInput.signalSnapshot).toBeUndefined();
495
+ consoleSpy.mockRestore();
496
+ });
497
+ });
498
+
499
+ // ── pd runtime pruning orphans ───────────────────────────────────────────────
500
+
501
+ describe('pd runtime pruning orphans', () => {
502
+ beforeEach(() => {
503
+ vi.clearAllMocks();
504
+ mockRemoveOrphanReferencesFromLedger.mockReset();
505
+ });
506
+
507
+ it('dry-run outputs orphan list with count (JSON)', () => {
508
+ class MockOrphanReadModel {
509
+ getOrphanDerivedCandidates() {
510
+ return {
511
+ candidates: [
512
+ { candidateId: 'c_orphan1', principleId: 'p1', reason: 'candidate not found in state.db', sourceRef: 'derivedFromPainIds', status: 'active' },
513
+ { candidateId: 'c_orphan2', principleId: 'p1', reason: 'candidate not found in state.db', sourceRef: 'derivedFromPainIds', status: 'active' },
514
+ ],
515
+ dbReadable: true,
516
+ };
517
+ }
518
+ }
519
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
520
+ return new MockOrphanReadModel() as unknown as InstanceType<typeof PruningReadModel>;
521
+ });
522
+
523
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
524
+ handlePruningOrphans({ json: true, dryRun: true, confirm: false });
525
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
526
+ expect(output.mode).toBe('dry_run');
527
+ expect(output.status).toBe('would_change');
528
+ expect(output.safeToConfirm).toBe(true);
529
+ expect(output.orphanDerivedCandidateCount).toBe(2);
530
+ expect(output.dryRun).toBe(true);
531
+ expect(output.dbReadable).toBe(true);
532
+ expect(output.candidates).toHaveLength(2);
533
+ expect(output.candidates[0].candidateId).toBe('c_orphan1');
534
+ consoleSpy.mockRestore();
535
+ });
536
+
537
+ it('default is dry-run (no modifications)', () => {
538
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
539
+ handlePruningOrphans({ json: true });
540
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
541
+ expect(output.mode).toBe('dry_run');
542
+ expect(output.status).toBe('no_op');
543
+ expect(output.dryRun).toBe(true);
544
+ expect(mockRemoveOrphanReferencesFromLedger).not.toHaveBeenCalled();
545
+ consoleSpy.mockRestore();
546
+ });
547
+
548
+ it('--confirm removes orphan IDs from ledger derivedFromPainIds', () => {
549
+ mockRemoveOrphanReferencesFromLedger.mockReturnValue([
550
+ { principleId: 'p1', removedIds: ['c_orphan1'] },
551
+ ]);
552
+
553
+ class MockOrphanReadModel {
554
+ getOrphanDerivedCandidates() {
555
+ return {
556
+ candidates: [
557
+ { candidateId: 'c_orphan1', principleId: 'p1', reason: 'candidate not found in state.db', sourceRef: 'derivedFromPainIds', status: 'active' },
558
+ ],
559
+ dbReadable: true,
560
+ };
561
+ }
562
+ }
563
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
564
+ return new MockOrphanReadModel() as unknown as InstanceType<typeof PruningReadModel>;
565
+ });
566
+
567
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
568
+ handlePruningOrphans({ json: true, confirm: true });
569
+ expect(mockRemoveOrphanReferencesFromLedger).toHaveBeenCalledTimes(1);
570
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
571
+ expect(output.mode).toBe('confirm');
572
+ expect(output.status).toBe('changed');
573
+ expect(output.repairedCount).toBe(1);
574
+ expect(output.dryRun).toBe(false);
575
+ expect(output.dbReadable).toBe(true);
576
+ expect(output.removedFromPrinciples).toHaveLength(1);
577
+ expect(output.removedFromPrinciples[0].principleId).toBe('p1');
578
+ expect(output.removedFromPrinciples[0].removedIds).toContain('c_orphan1');
579
+ consoleSpy.mockRestore();
580
+ });
581
+
582
+ it('--confirm does not touch non-orphan candidates', () => {
583
+ mockRemoveOrphanReferencesFromLedger.mockReturnValue([]);
584
+
585
+ class MockOrphanReadModel {
586
+ getOrphanDerivedCandidates() {
587
+ return {
588
+ candidates: [
589
+ { candidateId: 'c_orphan1', principleId: 'p2', reason: 'candidate not found in state.db', sourceRef: 'derivedFromPainIds', status: 'active' },
590
+ ],
591
+ dbReadable: true,
592
+ };
593
+ }
594
+ }
595
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
596
+ return new MockOrphanReadModel() as unknown as InstanceType<typeof PruningReadModel>;
597
+ });
598
+
599
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
600
+ handlePruningOrphans({ json: true, confirm: true });
601
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
602
+ expect(output.removedFromPrinciples).toHaveLength(0);
603
+ consoleSpy.mockRestore();
604
+ });
605
+
606
+ it('dry-run text output includes note about no modifications', () => {
607
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
608
+ handlePruningOrphans({ json: false, dryRun: true, confirm: false });
609
+ const output = consoleSpy.mock.calls.map((c) => c[0]).join('\n');
610
+ expect(output).toContain('dryRun: true');
611
+ expect(output).toContain('No orphan derived candidates found.');
612
+ consoleSpy.mockRestore();
613
+ });
614
+
615
+ it('--confirm REFUSED when DB is unreadable, does not call saveLedger', () => {
616
+ class MockDegradedReadModel {
617
+ getOrphanDerivedCandidates() {
618
+ return {
619
+ candidates: [
620
+ { candidateId: 'c_maybe_orphan', principleId: 'p1', reason: 'candidate not verifiable: state.db unreadable', sourceRef: 'derivedFromPainIds', status: 'active' },
621
+ ],
622
+ dbReadable: false,
623
+ };
624
+ }
625
+ }
626
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
627
+ return new MockDegradedReadModel() as unknown as InstanceType<typeof PruningReadModel>;
628
+ });
629
+
630
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
631
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
632
+ handlePruningOrphans({ json: false, confirm: true });
633
+ expect(processSpy).toHaveBeenCalledWith(1);
634
+ expect(mockRemoveOrphanReferencesFromLedger).not.toHaveBeenCalled();
635
+ const errOutput = consoleSpy.mock.calls.map((c) => c[0]).join('\n');
636
+ expect(errOutput).toContain('REFUSED');
637
+ consoleSpy.mockRestore();
638
+ processSpy.mockRestore();
639
+ });
640
+
641
+ it('--confirm REFUSED when DB is unreadable (JSON mode)', () => {
642
+ class MockDegradedReadModel {
643
+ getOrphanDerivedCandidates() {
644
+ return {
645
+ candidates: [
646
+ { candidateId: 'c_maybe_orphan', principleId: 'p1', reason: 'candidate not verifiable: state.db unreadable', sourceRef: 'derivedFromPainIds', status: 'active' },
647
+ ],
648
+ dbReadable: false,
649
+ };
650
+ }
651
+ }
652
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
653
+ return new MockDegradedReadModel() as unknown as InstanceType<typeof PruningReadModel>;
654
+ });
655
+
656
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
657
+ const processSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as (code: number) => never);
658
+ handlePruningOrphans({ json: true, confirm: true });
659
+ expect(processSpy).toHaveBeenCalledWith(1);
660
+ expect(mockRemoveOrphanReferencesFromLedger).not.toHaveBeenCalled();
661
+ const output = JSON.parse(consoleSpy.mock.calls[0]![0] as string);
662
+ expect(output.status).toBe('refused');
663
+ expect(output.safeToConfirm).toBe(false);
664
+ expect(output.dbReadable).toBe(false);
665
+ expect(output.dryRun).toBe(false); // operation was refused, not a dry-run
666
+ consoleSpy.mockRestore();
667
+ processSpy.mockRestore();
668
+ });
669
+
670
+ it('dry-run shows degraded warning when DB is unreadable', () => {
671
+ class MockDegradedReadModel {
672
+ getOrphanDerivedCandidates() {
673
+ return {
674
+ candidates: [
675
+ { candidateId: 'c_maybe_orphan', principleId: 'p1', reason: 'candidate not verifiable: state.db unreadable', sourceRef: 'derivedFromPainIds', status: 'active' },
676
+ ],
677
+ dbReadable: false,
678
+ };
679
+ }
680
+ }
681
+ (PruningReadModel as unknown as ReturnType<typeof vi.fn>).mockImplementationOnce(function () {
682
+ return new MockDegradedReadModel() as unknown as InstanceType<typeof PruningReadModel>;
683
+ });
684
+
685
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
686
+ handlePruningOrphans({ json: false, dryRun: true, confirm: false });
687
+ const output = consoleSpy.mock.calls.map((c) => c[0]).join('\n');
688
+ expect(output).toContain('dbReadable: false');
689
+ expect(output).toContain('not verifiable');
690
+ expect(output).toContain('--confirm will be refused');
691
+ consoleSpy.mockRestore();
692
+ });
693
+ });