@transitrix/diagrams 1.0.0 → 1.0.1

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 (267) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +47 -47
  3. package/dist/__tests__/edge-path.test.js +8 -0
  4. package/dist/activity-card/__tests__/example.test.js +20 -1
  5. package/dist/activity-card/__tests__/layout.test.js +102 -11
  6. package/dist/activity-card/__tests__/resolver.test.js +139 -6
  7. package/dist/activity-card/index.d.ts +1 -1
  8. package/dist/activity-card/layout.d.ts +1 -1
  9. package/dist/activity-card/layout.js +220 -103
  10. package/dist/activity-card/resolver.js +107 -26
  11. package/dist/activity-card/types.d.ts +150 -27
  12. package/dist/activity-card/validate.js +3 -0
  13. package/dist/actor/__tests__/validate.test.js +63 -31
  14. package/dist/actor/index.d.ts +1 -1
  15. package/dist/actor/index.js +1 -1
  16. package/dist/actor/types.d.ts +7 -3
  17. package/dist/actor/types.js +17 -3
  18. package/dist/actor/validate.js +55 -26
  19. package/dist/assertion/types.d.ts +3 -1
  20. package/dist/assertion/types.js +1 -1
  21. package/dist/change/__tests__/validate.test.js +40 -34
  22. package/dist/change/index.d.ts +1 -0
  23. package/dist/change/types.d.ts +1 -1
  24. package/dist/change/types.js +2 -2
  25. package/dist/change/validate.d.ts +6 -1
  26. package/dist/change/validate.js +68 -41
  27. package/dist/compliance/__tests__/classify.test.js +34 -0
  28. package/dist/compliance/__tests__/coverage-metric.test.js +146 -46
  29. package/dist/compliance/__tests__/html.test.js +2 -2
  30. package/dist/compliance/__tests__/impact.test.js +311 -6
  31. package/dist/compliance/classify.d.ts +6 -0
  32. package/dist/compliance/classify.js +9 -1
  33. package/dist/compliance/coverage-metric.d.ts +31 -11
  34. package/dist/compliance/coverage-metric.js +149 -39
  35. package/dist/compliance/html.js +77 -74
  36. package/dist/compliance/impact.d.ts +77 -16
  37. package/dist/compliance/impact.js +193 -25
  38. package/dist/compliance/index.d.ts +2 -2
  39. package/dist/compliance/index.js +1 -1
  40. package/dist/compliance/markdown.js +1 -0
  41. package/dist/compliance/types.d.ts +25 -6
  42. package/dist/edge-path.d.ts +8 -4
  43. package/dist/edge-path.js +11 -6
  44. package/dist/factor/__tests__/validate.test.js +74 -45
  45. package/dist/factor/index.d.ts +3 -2
  46. package/dist/factor/index.js +1 -1
  47. package/dist/factor/types.d.ts +4 -7
  48. package/dist/factor/types.js +10 -5
  49. package/dist/factor/validate.d.ts +6 -1
  50. package/dist/factor/validate.js +68 -50
  51. package/dist/fgca/__tests__/resolver.test.js +185 -0
  52. package/dist/fgca/index.d.ts +2 -0
  53. package/dist/fgca/index.js +1 -0
  54. package/dist/fgca/resolver.d.ts +49 -0
  55. package/dist/fgca/resolver.js +124 -0
  56. package/dist/goals/index.d.ts +1 -1
  57. package/dist/index.d.ts +5 -0
  58. package/dist/index.js +5 -0
  59. package/dist/process-blueprint/__tests__/layout.test.js +37 -0
  60. package/dist/process-blueprint/layout.js +5 -1
  61. package/dist/process-blueprint/types.d.ts +22 -1
  62. package/dist/stakeholder/__tests__/validate.test.js +49 -47
  63. package/dist/stakeholder/index.d.ts +1 -0
  64. package/dist/stakeholder/types.d.ts +3 -4
  65. package/dist/stakeholder/types.js +2 -3
  66. package/dist/stakeholder/validate.d.ts +6 -1
  67. package/dist/stakeholder/validate.js +71 -50
  68. package/dist/target-state/__tests__/validate.test.js +43 -27
  69. package/dist/target-state/index.d.ts +2 -0
  70. package/dist/target-state/index.js +1 -0
  71. package/dist/target-state/types.d.ts +6 -1
  72. package/dist/target-state/types.js +8 -7
  73. package/dist/target-state/validate.d.ts +6 -1
  74. package/dist/target-state/validate.js +69 -45
  75. package/dist/theme/themes.js +69 -69
  76. package/dist/webview/__tests__/entry.test.js +11 -11
  77. package/dist/webview/render-activities.d.ts +3 -1
  78. package/dist/webview/render-activities.js +2 -2
  79. package/dist/webview/render-activity-card.js +104 -77
  80. package/dist/webview/render-fgca.d.ts +4 -0
  81. package/dist/webview/render-fgca.js +2 -5
  82. package/dist/webview/render-goals.d.ts +1 -0
  83. package/dist/webview/render-goals.js +15 -15
  84. package/dist/webview/render-process-blueprint.js +10 -10
  85. package/package.json +40 -40
  86. package/src/__tests__/edge-path.test.ts +58 -46
  87. package/src/__tests__/scope.test.ts +27 -27
  88. package/src/__tests__/yaml-normalize.test.ts +158 -158
  89. package/src/activities/__tests__/cpm.test.ts +164 -164
  90. package/src/activities/__tests__/gantt.test.ts +245 -245
  91. package/src/activities/__tests__/layout.test.ts +112 -112
  92. package/src/activities/__tests__/validate.test.ts +409 -409
  93. package/src/activities/cpm.ts +95 -95
  94. package/src/activities/gantt.ts +276 -276
  95. package/src/activities/index.ts +28 -28
  96. package/src/activities/layout.ts +112 -112
  97. package/src/activities/types.ts +148 -148
  98. package/src/activities/validate.ts +357 -357
  99. package/src/activity-card/__tests__/example.test.ts +85 -66
  100. package/src/activity-card/__tests__/layout.test.ts +169 -63
  101. package/src/activity-card/__tests__/resolver.test.ts +307 -161
  102. package/src/activity-card/__tests__/validate.test.ts +131 -131
  103. package/src/activity-card/index.ts +38 -32
  104. package/src/activity-card/layout.ts +324 -194
  105. package/src/activity-card/resolver.ts +415 -337
  106. package/src/activity-card/types.ts +376 -237
  107. package/src/activity-card/validate.ts +162 -158
  108. package/src/actor/__tests__/validate.test.ts +70 -31
  109. package/src/actor/index.ts +1 -1
  110. package/src/actor/types.ts +21 -7
  111. package/src/actor/validate.ts +59 -27
  112. package/src/applications/__tests__/validate.test.ts +256 -256
  113. package/src/applications/index.ts +11 -11
  114. package/src/applications/types.ts +43 -43
  115. package/src/applications/validate.ts +131 -131
  116. package/src/assertion/__tests__/example.test.ts +37 -37
  117. package/src/assertion/__tests__/validate.test.ts +142 -142
  118. package/src/assertion/index.ts +12 -12
  119. package/src/assertion/types.ts +60 -58
  120. package/src/assertion/validate.ts +145 -145
  121. package/src/blocks/__tests__/layout.test.ts +169 -169
  122. package/src/blocks/__tests__/validate.test.ts +260 -260
  123. package/src/blocks/index.ts +17 -17
  124. package/src/blocks/layout.ts +186 -186
  125. package/src/blocks/types.ts +62 -62
  126. package/src/blocks/validate.ts +206 -206
  127. package/src/capability-map/__tests__/validate.test.ts +238 -238
  128. package/src/capability-map/index.ts +8 -8
  129. package/src/capability-map/types.ts +29 -29
  130. package/src/capability-map/validate.ts +127 -127
  131. package/src/change/__tests__/validate.test.ts +43 -34
  132. package/src/change/index.ts +1 -0
  133. package/src/change/types.ts +5 -3
  134. package/src/change/validate.ts +75 -43
  135. package/src/compliance/__tests__/classify.test.ts +64 -27
  136. package/src/compliance/__tests__/confidence.test.ts +98 -98
  137. package/src/compliance/__tests__/coverage-metric.test.ts +300 -188
  138. package/src/compliance/__tests__/demo-scenario.test.ts +273 -273
  139. package/src/compliance/__tests__/example.test.ts +57 -57
  140. package/src/compliance/__tests__/gap-report.test.ts +128 -128
  141. package/src/compliance/__tests__/html.test.ts +141 -141
  142. package/src/compliance/__tests__/impact.test.ts +840 -481
  143. package/src/compliance/__tests__/markdown.test.ts +94 -94
  144. package/src/compliance/__tests__/views.test.ts +64 -64
  145. package/src/compliance/classify.ts +100 -84
  146. package/src/compliance/confidence.ts +69 -69
  147. package/src/compliance/coverage-metric.ts +404 -263
  148. package/src/compliance/gap-report.ts +94 -94
  149. package/src/compliance/html.ts +298 -295
  150. package/src/compliance/impact.ts +861 -677
  151. package/src/compliance/index.ts +57 -56
  152. package/src/compliance/markdown.ts +136 -135
  153. package/src/compliance/reverse-index.ts +33 -33
  154. package/src/compliance/types.ts +136 -120
  155. package/src/compliance/views.ts +46 -46
  156. package/src/compliance-matrix/__tests__/build.test.ts +200 -200
  157. package/src/compliance-matrix/__tests__/example.test.ts +57 -57
  158. package/src/compliance-matrix/index.ts +11 -11
  159. package/src/compliance-matrix/types.ts +102 -102
  160. package/src/edge-path.ts +50 -44
  161. package/src/factor/__tests__/validate.test.ts +80 -45
  162. package/src/factor/index.ts +3 -7
  163. package/src/factor/types.ts +14 -18
  164. package/src/factor/validate.ts +77 -51
  165. package/src/fgca/__tests__/chain-table.test.ts +172 -172
  166. package/src/fgca/__tests__/layout.test.ts +179 -179
  167. package/src/fgca/__tests__/parse-canonical.test.ts +248 -248
  168. package/src/fgca/__tests__/preview-layout.test.ts +124 -124
  169. package/src/fgca/__tests__/resolver.test.ts +209 -0
  170. package/src/fgca/__tests__/validate.test.ts +142 -142
  171. package/src/fgca/chain-table.ts +225 -225
  172. package/src/fgca/index.ts +47 -45
  173. package/src/fgca/layout.ts +204 -204
  174. package/src/fgca/nodeTypes.ts +11 -11
  175. package/src/fgca/nodes/FGCAActivityNode.tsx +65 -65
  176. package/src/fgca/nodes/FGCAChangeNode.tsx +64 -64
  177. package/src/fgca/nodes/FGCAFactorNode.tsx +64 -64
  178. package/src/fgca/nodes/FGCAGoalNode.tsx +72 -72
  179. package/src/fgca/parse-canonical.ts +400 -400
  180. package/src/fgca/preview-layout.ts +217 -217
  181. package/src/fgca/resolver.ts +157 -0
  182. package/src/fgca/types.ts +50 -50
  183. package/src/fgca/validate.ts +126 -126
  184. package/src/geometry.ts +9 -9
  185. package/src/goals/__tests__/layout.test.ts +175 -175
  186. package/src/goals/__tests__/mutations.test.ts +111 -111
  187. package/src/goals/__tests__/parse-canonical.test.ts +145 -145
  188. package/src/goals/__tests__/validate.test.ts +202 -202
  189. package/src/goals/index.ts +20 -21
  190. package/src/goals/layout.ts +172 -172
  191. package/src/goals/mutations.ts +123 -123
  192. package/src/goals/parse-canonical.ts +286 -286
  193. package/src/goals/types.ts +85 -85
  194. package/src/goals/validate.ts +136 -136
  195. package/src/index.ts +28 -23
  196. package/src/process-blueprint/__tests__/compliance-lane.test.ts +320 -320
  197. package/src/process-blueprint/__tests__/layout.test.ts +240 -195
  198. package/src/process-blueprint/__tests__/validate.test.ts +310 -310
  199. package/src/process-blueprint/index.ts +31 -31
  200. package/src/process-blueprint/layout.ts +535 -531
  201. package/src/process-blueprint/types.ts +263 -242
  202. package/src/process-blueprint/validate.ts +181 -181
  203. package/src/process-map/__tests__/validate.test.ts +223 -223
  204. package/src/process-map/index.ts +10 -10
  205. package/src/process-map/types.ts +36 -36
  206. package/src/process-map/validate.ts +135 -135
  207. package/src/products/__tests__/validate.test.ts +239 -239
  208. package/src/products/index.ts +9 -9
  209. package/src/products/types.ts +31 -31
  210. package/src/products/validate.ts +112 -112
  211. package/src/repo-validate/__tests__/validate-repo.test.ts +149 -149
  212. package/src/repo-validate/index.ts +2 -2
  213. package/src/repo-validate/types.ts +50 -50
  214. package/src/repo-validate/validate-repo.ts +196 -196
  215. package/src/requirement/__tests__/example.test.ts +24 -24
  216. package/src/requirement/__tests__/validate.test.ts +96 -96
  217. package/src/requirement/index.ts +4 -4
  218. package/src/requirement/types.ts +35 -35
  219. package/src/requirement/validate.ts +92 -92
  220. package/src/scenarios/__tests__/validate.test.ts +209 -209
  221. package/src/scenarios/index.ts +15 -15
  222. package/src/scenarios/types.ts +37 -37
  223. package/src/scenarios/validate.ts +122 -122
  224. package/src/schema-version.ts +16 -16
  225. package/src/scope.ts +46 -46
  226. package/src/stakeholder/__tests__/validate.test.ts +51 -47
  227. package/src/stakeholder/index.ts +1 -0
  228. package/src/stakeholder/types.ts +7 -7
  229. package/src/stakeholder/validate.ts +79 -52
  230. package/src/target-state/__tests__/validate.test.ts +48 -27
  231. package/src/target-state/index.ts +2 -0
  232. package/src/target-state/types.ts +11 -7
  233. package/src/target-state/validate.ts +76 -46
  234. package/src/theme/index.ts +4 -4
  235. package/src/theme/themes.ts +237 -237
  236. package/src/theme/tokens.ts +133 -133
  237. package/src/typed-id.ts +56 -56
  238. package/src/validation-types.ts +36 -36
  239. package/src/webview/__tests__/entry-notations.test.ts +77 -77
  240. package/src/webview/__tests__/entry.test.ts +64 -64
  241. package/src/webview/__tests__/render-goals.test.ts +72 -72
  242. package/src/webview/entry.ts +292 -292
  243. package/src/webview/render-activities.ts +5 -3
  244. package/src/webview/render-activity-card.ts +266 -234
  245. package/src/webview/render-fgca.ts +6 -5
  246. package/src/webview/render-goals.ts +111 -110
  247. package/src/webview/render-process-blueprint.ts +207 -207
  248. package/src/webview/styles.css +322 -323
  249. package/src/yaml-normalize.ts +56 -56
  250. package/dist/element-envelope.d.ts +0 -15
  251. package/dist/element-envelope.js +0 -55
  252. package/dist/issues/__tests__/layout.test.js +0 -62
  253. package/dist/issues/__tests__/validate.test.d.ts +0 -1
  254. package/dist/issues/__tests__/validate.test.js +0 -82
  255. package/dist/issues/index.d.ts +0 -4
  256. package/dist/issues/index.js +0 -2
  257. package/dist/issues/layout.d.ts +0 -10
  258. package/dist/issues/layout.js +0 -82
  259. package/dist/issues/types.d.ts +0 -61
  260. package/dist/issues/types.js +0 -6
  261. package/dist/issues/validate.d.ts +0 -3
  262. package/dist/issues/validate.js +0 -143
  263. package/dist/webview/transitrix-render.css +0 -323
  264. package/dist/webview/transitrix-render.js +0 -6452
  265. package/dist/webview/transitrix-render.js.map +0 -7
  266. package/src/element-envelope.ts +0 -76
  267. /package/dist/{issues/__tests__/layout.test.d.ts → fgca/__tests__/resolver.test.d.ts} +0 -0
@@ -1,482 +1,841 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { emptyCanon } from '../classify.js';
3
- import {
4
- buildImpactMatrix,
5
- renderImpactMarkdown,
6
- parseImpactViewConfig,
7
- COMPLIANCE_IMPACT_DEFAULTS,
8
- extractObjectDetails,
9
- computeDeadlineStatus,
10
- type ImpactViewConfig,
11
- } from '../impact.js';
12
- import type { ComplianceCanon } from '../classify.js';
13
-
14
- // Industry- and regime-agnostic synthetic fixture: opaque ids only, no real
15
- // regulator / product names, per #166 acceptance ("Output is aggregates only —
16
- // no subject/obligation names tied to any real corpus").
17
- function buildCanon(): ComplianceCanon {
18
- const canon = emptyCanon();
19
- canon.products.push({ id: 'PRODUCT-A-1', name: 'Product A' });
20
- canon.products.push({ id: 'PRODUCT-B-1', name: 'Product B' });
21
- canon.requirements.push(
22
- { id: 'REQUIREMENT-FOO-1', name: 'Foo', derived_from: ['LAW-X-1'] },
23
- { id: 'REQUIREMENT-BAR-1', name: 'Bar', derived_from: ['LAW-X-1', 'REGULATION-Y-1'] },
24
- { id: 'REQUIREMENT-BAZ-1', name: 'Baz', derived_from: ['REGULATION-Y-1'] },
25
- // Internal-only requirement, no codex derivation:
26
- { id: 'REQUIREMENT-INT-1', name: 'Internal-only' },
27
- );
28
- canon.assertions.push(
29
- // (PRODUCT-A-1, FOO) — two assertions; partial wins over compliant per §5.2 step 4.
30
- { id: 'ASSERTION-1', about: 'REQUIREMENT-FOO-1', subject: 'PRODUCT-A-1', status: 'compliant' },
31
- { id: 'ASSERTION-2', about: 'REQUIREMENT-FOO-1', subject: 'PRODUCT-A-1', status: 'partial' },
32
- // (PRODUCT-A-1, BAR) — single n_a → modelled fact ("No obligation applies").
33
- { id: 'ASSERTION-3', about: 'REQUIREMENT-BAR-1', subject: 'PRODUCT-A-1', status: 'n_a' },
34
- // (PRODUCT-B-1, BAR) — non_compliant.
35
- { id: 'ASSERTION-4', about: 'REQUIREMENT-BAR-1', subject: 'PRODUCT-B-1', status: 'non_compliant' },
36
- // (PRODUCT-B-1, BAZ) — under_review beats compliant.
37
- { id: 'ASSERTION-5', about: 'REQUIREMENT-BAZ-1', subject: 'PRODUCT-B-1', status: 'under_review' },
38
- { id: 'ASSERTION-6', about: 'REQUIREMENT-BAZ-1', subject: 'PRODUCT-B-1', status: 'compliant' },
39
- );
40
- return canon;
41
- }
42
-
43
- const baseConfig: ImpactViewConfig = {
44
- id: 'COMPLIANCE_IMPACT-FIXTURE-1',
45
- name: 'Fixture matrix',
46
- subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1'] },
47
- obligations: {},
48
- };
49
-
50
- describe('buildImpactMatrix', () => {
51
- it('aggregates statuses per §5.2 step 4 — non_compliant > partial > under_review > compliant', () => {
52
- const canon = buildCanon();
53
- const matrix = buildImpactMatrix(canon, {
54
- ...baseConfig,
55
- obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1'] },
56
- });
57
- expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
58
- expect(matrix.rows.map(r => r.id)).toEqual(['REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1', 'REQUIREMENT-FOO-1']);
59
-
60
- const cellFor = (req: string, subject: string) => {
61
- const r = matrix.rows.findIndex(x => x.id === req);
62
- const c = matrix.columns.findIndex(col => col.label === subject);
63
- return matrix.cells[r][c];
64
- };
65
-
66
- expect(cellFor('REQUIREMENT-FOO-1', 'PRODUCT-A-1').status).toBe('partial');
67
- expect(cellFor('REQUIREMENT-FOO-1', 'PRODUCT-A-1').kind).toBe('bound');
68
- expect(cellFor('REQUIREMENT-BAR-1', 'PRODUCT-A-1').kind).toBe('n_a_only');
69
- expect(cellFor('REQUIREMENT-BAR-1', 'PRODUCT-B-1').status).toBe('non_compliant');
70
- expect(cellFor('REQUIREMENT-BAZ-1', 'PRODUCT-B-1').status).toBe('under_review');
71
- expect(cellFor('REQUIREMENT-FOO-1', 'PRODUCT-B-1').kind).toBe('gap');
72
- expect(cellFor('REQUIREMENT-BAZ-1', 'PRODUCT-A-1').kind).toBe('gap');
73
- });
74
-
75
- it('selects obligations by `filter.derived_from_codex`', () => {
76
- const canon = buildCanon();
77
- const matrix = buildImpactMatrix(canon, {
78
- ...baseConfig,
79
- obligations: { filter: { derived_from_codex: ['REGULATION-Y-1'] } },
80
- });
81
- expect(matrix.rows.map(r => r.id)).toEqual(['REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1']);
82
- });
83
-
84
- it('`include` wins when both `include` and `filter` are present (COMPIMP-007)', () => {
85
- const canon = buildCanon();
86
- const matrix = buildImpactMatrix(canon, {
87
- ...baseConfig,
88
- obligations: {
89
- include: ['REQUIREMENT-INT-1'],
90
- filter: { derived_from_codex: ['LAW-X-1'] },
91
- },
92
- });
93
- expect(matrix.rows.map(r => r.id)).toEqual(['REQUIREMENT-INT-1']);
94
- });
95
-
96
- it('distinguishes modelling gap from n_a-only per §5.3', () => {
97
- const canon = buildCanon();
98
- const matrix = buildImpactMatrix(canon, {
99
- ...baseConfig,
100
- obligations: { include: ['REQUIREMENT-BAR-1'] },
101
- });
102
- const aCell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-A-1')];
103
- const bCell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-B-1')];
104
- expect(aCell.kind).toBe('n_a_only');
105
- expect(bCell.kind).toBe('bound');
106
- expect(bCell.status).toBe('non_compliant');
107
- });
108
-
109
- it('respects `status_display.show` — filtered-out statuses leave the cell as a gap', () => {
110
- const canon = buildCanon();
111
- const matrix = buildImpactMatrix(canon, {
112
- ...baseConfig,
113
- obligations: { include: ['REQUIREMENT-BAZ-1'] },
114
- status_display: { show: ['non_compliant'] }, // under_review + compliant filtered out
115
- });
116
- const cell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-B-1')];
117
- expect(cell.kind).toBe('gap');
118
- });
119
-
120
- it('keeps subjects with no binding obligation as columns so the gap is visible', () => {
121
- const canon = buildCanon();
122
- const matrix = buildImpactMatrix(canon, {
123
- ...baseConfig,
124
- subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1', 'PRODUCT-NONE-1'] },
125
- obligations: { include: ['REQUIREMENT-FOO-1'] },
126
- });
127
- expect(matrix.columns.map(c => c.label)).toContain('PRODUCT-NONE-1');
128
- const cell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-NONE-1')];
129
- expect(cell.kind).toBe('gap');
130
- });
131
-
132
- it('orders rows alphabetically by name when `order_rows_by: name`', () => {
133
- const canon = buildCanon();
134
- const matrix = buildImpactMatrix(canon, {
135
- ...baseConfig,
136
- obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1'] },
137
- order_rows_by: 'name',
138
- });
139
- expect(matrix.rows.map(r => r.name)).toEqual(['Bar', 'Baz', 'Foo']);
140
- });
141
- });
142
-
143
- describe('renderImpactMarkdown', () => {
144
- it('renders a header, table, and §5.3 legend', () => {
145
- const canon = buildCanon();
146
- const matrix = buildImpactMatrix(canon, {
147
- ...baseConfig,
148
- obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1'] },
149
- });
150
- const md = renderImpactMarkdown(matrix);
151
- expect(md).toContain('# Fixture matrix');
152
- expect(md).toContain('View ID: `COMPLIANCE_IMPACT-FIXTURE-1`');
153
- expect(md).toContain('| Obligation | PRODUCT-A-1 | PRODUCT-B-1 |');
154
- expect(md).toContain('PARTIAL');
155
- expect(md).toContain('FAIL');
156
- expect(md).toContain('No mapped obligation (current model)');
157
- expect(md).toContain('No obligation applies');
158
- expect(md).toContain('## Legend');
159
- });
160
-
161
- it('honours custom empty-cell labels', () => {
162
- const canon = buildCanon();
163
- const matrix = buildImpactMatrix(canon, {
164
- ...baseConfig,
165
- obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1'] },
166
- empty_cells: {
167
- no_obligation_label: 'GAP',
168
- no_obligation_applies_label: 'EXCLUDED',
169
- },
170
- });
171
- const md = renderImpactMarkdown(matrix);
172
- expect(md).toContain('GAP');
173
- expect(md).toContain('EXCLUDED');
174
- expect(md).not.toContain('No mapped obligation (current model)');
175
- });
176
-
177
- it('renders a placeholder when zero obligations are selected', () => {
178
- const canon = buildCanon();
179
- const matrix = buildImpactMatrix(canon, {
180
- ...baseConfig,
181
- obligations: { include: ['REQUIREMENT-DOES-NOT-EXIST-1'] },
182
- });
183
- const md = renderImpactMarkdown(matrix);
184
- expect(md).toContain('No obligations in scope');
185
- });
186
-
187
- it('propagates snapshot_at into the matrix and renders it', () => {
188
- const canon = buildCanon();
189
- const matrix = buildImpactMatrix(canon, { ...baseConfig, snapshot_at: '2026-06-09' });
190
- expect(matrix.snapshotAt).toBe('2026-06-09');
191
- const md = renderImpactMarkdown(matrix);
192
- expect(md).toContain('Report snapshot: 2026-06-09');
193
- });
194
- });
195
-
196
- // ── parseImpactViewConfig ───────────────────────────────────────────────────
197
-
198
- describe('parseImpactViewConfig', () => {
199
- it('returns ok:false for a non-object', () => {
200
- const r = parseImpactViewConfig(null);
201
- expect(r.ok).toBe(false);
202
- });
203
-
204
- it('returns ok:false when id is missing', () => {
205
- const r = parseImpactViewConfig({ name: 'X', subjects: {} });
206
- expect(r.ok).toBe(false);
207
- if (!r.ok) expect(r.errors.some(e => e.includes('id'))).toBe(true);
208
- });
209
-
210
- it('returns ok:false when name is missing', () => {
211
- const r = parseImpactViewConfig({ id: 'X', subjects: {} });
212
- expect(r.ok).toBe(false);
213
- if (!r.ok) expect(r.errors.some(e => e.includes('name'))).toBe(true);
214
- });
215
-
216
- it('unwraps the top-level view: wrapper', () => {
217
- const r = parseImpactViewConfig({ view: { id: 'V-1', name: 'My view', subjects: {} } });
218
- expect(r.ok).toBe(true);
219
- if (r.ok) expect(r.config.id).toBe('V-1');
220
- });
221
-
222
- it('fills all optional fields from COMPLIANCE_IMPACT_DEFAULTS', () => {
223
- const r = parseImpactViewConfig({ id: 'V-1', name: 'My view', subjects: {} });
224
- expect(r.ok).toBe(true);
225
- if (!r.ok) return;
226
- const c = r.config;
227
- expect(c.subjects.products).toEqual([]);
228
- expect(c.subjects.processes).toEqual([]);
229
- expect(c.status_display?.show).toEqual(COMPLIANCE_IMPACT_DEFAULTS.status_display.show);
230
- expect(c.order_rows_by).toBe('id');
231
- expect(c.empty_cells?.no_obligation_label).toBe(
232
- COMPLIANCE_IMPACT_DEFAULTS.empty_cells.no_obligation_label,
233
- );
234
- expect(c.empty_cells?.no_obligation_applies_label).toBe(
235
- COMPLIANCE_IMPACT_DEFAULTS.empty_cells.no_obligation_applies_label,
236
- );
237
- });
238
-
239
- it('preserves explicit subject lists and snapshot_at', () => {
240
- const r = parseImpactViewConfig({
241
- id: 'V-2',
242
- name: 'View 2',
243
- snapshot_at: '2026-01-01',
244
- subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1'] },
245
- });
246
- expect(r.ok).toBe(true);
247
- if (!r.ok) return;
248
- expect(r.config.subjects.products).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
249
- expect(r.config.snapshot_at).toBe('2026-01-01');
250
- });
251
-
252
- it('applies custom order_rows_by:name', () => {
253
- const r = parseImpactViewConfig({ id: 'V-3', name: 'V', subjects: {}, order_rows_by: 'name' });
254
- expect(r.ok).toBe(true);
255
- if (r.ok) expect(r.config.order_rows_by).toBe('name');
256
- });
257
-
258
- it('parses obligation filter correctly', () => {
259
- const r = parseImpactViewConfig({
260
- id: 'V-4',
261
- name: 'V',
262
- subjects: {},
263
- obligations: { filter: { derived_from_codex: ['LAW-X-1'] } },
264
- });
265
- expect(r.ok).toBe(true);
266
- if (r.ok) expect(r.config.obligations.filter?.derived_from_codex).toEqual(['LAW-X-1']);
267
- });
268
- });
269
-
270
- describe('CV-3a — stage grouping (ImpactColumn, extractObjectDetails, buildImpactMatrix stage grain)', () => {
271
- it('default product grain: columns are plain ImpactColumn with label=subjectId', () => {
272
- const canon = buildCanon();
273
- const matrix = buildImpactMatrix(canon, baseConfig);
274
- expect(matrix.columns.map(c => c.subjectId)).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
275
- expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
276
- expect(matrix.columns.every(c => !c.stageId)).toBe(true);
277
- });
278
-
279
- it('product-stage grain: expands columns by stage, label is subjectId:stageId', () => {
280
- const canon = buildCanon();
281
- const matrix = buildImpactMatrix(
282
- canon,
283
- { ...baseConfig, subjects: { products: ['PRODUCT-A-1'] }, grouping: { columns: 'object-details' } },
284
- [{ objectId: 'PRODUCT-A-1', details: [{ id: 'STAGE-1', name: 'Stage One' }, { id: 'STAGE-2', name: 'Stage Two' }] }],
285
- );
286
- expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1:STAGE-1', 'PRODUCT-A-1:STAGE-2']);
287
- expect(matrix.columns.map(c => c.stageId)).toEqual(['STAGE-1', 'STAGE-2']);
288
- });
289
-
290
- it('assertion without realised_via fills every stage column for its subject', () => {
291
- const canon = emptyCanon();
292
- canon.requirements.push({ id: 'REQ-1', name: 'R1' });
293
- // Assertion with no realised_via — covers the whole subject.
294
- canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
295
- const matrix = buildImpactMatrix(
296
- canon,
297
- {
298
- id: 'V', name: 'V',
299
- subjects: { products: ['PROD-1'] },
300
- obligations: {},
301
- grouping: { columns: 'object-details' },
302
- },
303
- [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 's1' }, { id: 'S2', name: 's2' }] }],
304
- );
305
- // Both stage columns should be bound (no realised_via means all stages inherit).
306
- expect(matrix.cells[0][0].kind).toBe('bound');
307
- expect(matrix.cells[0][1].kind).toBe('bound');
308
- });
309
-
310
- it('assertion with realised_via fills only the matching stage column', () => {
311
- const canon = emptyCanon();
312
- canon.requirements.push({ id: 'REQ-1', name: 'R1' });
313
- // Assertion only covers STAGE-A.
314
- canon.assertions.push({
315
- id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant',
316
- realised_via: ['STAGE-A'],
317
- });
318
- const matrix = buildImpactMatrix(
319
- canon,
320
- {
321
- id: 'V', name: 'V',
322
- subjects: { products: ['PROD-1'] },
323
- obligations: {},
324
- grouping: { columns: 'object-details' },
325
- },
326
- [{ objectId: 'PROD-1', details: [{ id: 'STAGE-A', name: 'a' }, { id: 'STAGE-B', name: 'b' }] }],
327
- );
328
- expect(matrix.cells[0][0].kind).toBe('bound'); // STAGE-A: covered
329
- expect(matrix.cells[0][1].kind).toBe('gap'); // STAGE-B: gap
330
- });
331
-
332
- it('subject with no stage mapping falls back to single product-grain column', () => {
333
- const canon = buildCanon();
334
- const matrix = buildImpactMatrix(
335
- canon,
336
- {
337
- ...baseConfig,
338
- subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1'] },
339
- grouping: { columns: 'object-details' },
340
- },
341
- // Only provide stages for A — B falls back to a single column.
342
- [{ objectId: 'PRODUCT-A-1', details: [{ id: 'S1', name: 's1' }] }],
343
- );
344
- expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1:S1', 'PRODUCT-B-1']);
345
- // PRODUCT-B-1 fallback column: no stageId.
346
- expect(matrix.columns[1].stageId).toBeUndefined();
347
- });
348
-
349
- it('extractObjectDetails — returns null for non-blueprint docs', () => {
350
- expect(extractObjectDetails(null)).toBeNull();
351
- expect(extractObjectDetails({ notation: 'requirement', id: 'R1' })).toBeNull();
352
- expect(extractObjectDetails({ process_blueprint: { details: [{ id: 'S1' }] } })).toBeNull(); // missing id
353
- });
354
-
355
- it('extractObjectDetails — extracts subjectId and stages from process_blueprint wrapper', () => {
356
- const doc = { process_blueprint: { id: 'PROC-1', notation: 'process_blueprint', stages: [{ id: 'S1', name: 'Stage One' }, { id: 'S2', name: 'Stage Two' }] } };
357
- const sg = extractObjectDetails(doc);
358
- expect(sg).not.toBeNull();
359
- expect(sg!.objectId).toBe('PROC-1');
360
- expect(sg!.details.map((s: { id: string }) => s.id)).toEqual(['S1', 'S2']);
361
- });
362
-
363
- it('renderImpactMarkdown uses column labels in the table header', () => {
364
- const canon = emptyCanon();
365
- canon.requirements.push({ id: 'REQ-1', name: 'Req' });
366
- canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
367
- const matrix = buildImpactMatrix(
368
- canon,
369
- {
370
- id: 'V', name: 'Stage Report',
371
- subjects: { products: ['PROD-1'] },
372
- obligations: {},
373
- grouping: { columns: 'object-details' },
374
- },
375
- [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 'Stage One' }] }],
376
- );
377
- const md = renderImpactMarkdown(matrix);
378
- expect(md).toContain('PROD-1:S1');
379
- });
380
- });
381
-
382
- describe('CV-3 -- blueprint-lane decorations + obligations lane', () => {
383
- it('computeDeadlineStatus function is available', () => {
384
- expect(typeof computeDeadlineStatus).toBe('function');
385
- });
386
-
387
- it('cell.decoration.isNew = false when no snapshot_at', () => {
388
- const canon = emptyCanon();
389
- canon.requirements.push({ id: 'REQ-1', name: 'R1', admitted_at: '2026-01-01' });
390
- canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
391
- const matrix = buildImpactMatrix(canon, {
392
- id: 'V', name: 'V',
393
- subjects: { products: ['PROD-1'] },
394
- obligations: {},
395
- });
396
- expect(matrix.cells[0][0].decoration.isNew).toBe(false);
397
- });
398
-
399
- it('cell.decoration.isNew = true when req admitted after snapshot_at', () => {
400
- const canon = emptyCanon();
401
- canon.requirements.push({ id: 'REQ-1', name: 'R1', admitted_at: '2026-06-01' });
402
- canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
403
- const matrix = buildImpactMatrix(canon, {
404
- id: 'V', name: 'V',
405
- snapshot_at: '2026-05-01',
406
- subjects: { products: ['PROD-1'] },
407
- obligations: {},
408
- });
409
- // admitted_at (2026-06-01) > snapshot_at (2026-05-01) -> isNew
410
- expect(matrix.cells[0][0].decoration.isNew).toBe(true);
411
- });
412
-
413
- it('cell.decoration.isUrgent = true when gap + past_due deadline', () => {
414
- const canon = emptyCanon();
415
- canon.requirements.push({ id: 'REQ-1', name: 'R1', deadline: '2000-01-01' });
416
- // No assertion -> gap cell
417
- const matrix = buildImpactMatrix(canon, {
418
- id: 'V', name: 'V',
419
- subjects: { products: ['PROD-1'] },
420
- obligations: {},
421
- });
422
- expect(matrix.cells[0][0].kind).toBe('gap');
423
- expect(matrix.cells[0][0].decoration.isUrgent).toBe(true);
424
- expect(matrix.cells[0][0].decoration.deadlineStatus).toBe('past_due');
425
- });
426
-
427
- it('cell.decoration.isUrgent = false on bound cell even with past deadline', () => {
428
- const canon = emptyCanon();
429
- canon.requirements.push({ id: 'REQ-1', name: 'R1', deadline: '2000-01-01' });
430
- canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
431
- const matrix = buildImpactMatrix(canon, {
432
- id: 'V', name: 'V',
433
- subjects: { products: ['PROD-1'] },
434
- obligations: {},
435
- });
436
- expect(matrix.cells[0][0].kind).toBe('bound');
437
- // Not urgent: cell is bound, not a gap
438
- expect(matrix.cells[0][0].decoration.isUrgent).toBe(false);
439
- });
440
-
441
- it('obligationsLane lists derived_from codex IDs for non-gap columns', () => {
442
- const canon = emptyCanon();
443
- canon.requirements.push({ id: 'REQ-1', name: 'R1', derived_from: ['LAW-X', 'REG-Y'] });
444
- canon.requirements.push({ id: 'REQ-2', name: 'R2', derived_from: ['LAW-X'] });
445
- // Only REQ-1 is bound in PROD-1 column; REQ-2 is a gap
446
- canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
447
- const matrix = buildImpactMatrix(canon, {
448
- id: 'V', name: 'V',
449
- subjects: { products: ['PROD-1'] },
450
- obligations: {},
451
- });
452
- // obligationsLane[0] (PROD-1 column): REQ-1 is bound -> derived_from -> LAW-X, REG-Y sorted
453
- expect(matrix.obligationsLane[0]).toEqual(['LAW-X', 'REG-Y']);
454
- });
455
-
456
- it('obligationsLane is empty for columns with only gap cells', () => {
457
- const canon = emptyCanon();
458
- canon.requirements.push({ id: 'REQ-1', name: 'R1', derived_from: ['LAW-X'] });
459
- // No assertions -> all gaps
460
- const matrix = buildImpactMatrix(canon, {
461
- id: 'V', name: 'V',
462
- subjects: { products: ['PROD-1'] },
463
- obligations: {},
464
- });
465
- expect(matrix.obligationsLane[0]).toEqual([]);
466
- });
467
- });
468
-
469
- describe('computeDeadlineStatus', () => {
470
- it('returns none for missing deadline', () => {
471
- expect(computeDeadlineStatus(undefined, '2026-06-01')).toBe('none');
472
- });
473
- it('returns past_due when deadline < today', () => {
474
- expect(computeDeadlineStatus('2026-01-01', '2026-06-01')).toBe('past_due');
475
- });
476
- it('returns in_force within 30 days', () => {
477
- expect(computeDeadlineStatus('2026-06-20', '2026-06-01')).toBe('in_force');
478
- });
479
- it('returns upcoming more than 30 days away', () => {
480
- expect(computeDeadlineStatus('2026-12-31', '2026-06-01')).toBe('upcoming');
481
- });
1
+ import { describe, it, expect } from 'vitest';
2
+ import { emptyCanon } from '../classify.js';
3
+ import {
4
+ buildImpactMatrix,
5
+ renderImpactMarkdown,
6
+ parseImpactViewConfig,
7
+ COMPLIANCE_IMPACT_DEFAULTS,
8
+ extractObjectDetails,
9
+ extractProcessFlowTasks,
10
+ mergeStageTaskDetails,
11
+ computeDeadlineStatus,
12
+ type ImpactViewConfig,
13
+ } from '../impact.js';
14
+ import type { ComplianceCanon } from '../classify.js';
15
+
16
+ // Industry- and regime-agnostic synthetic fixture: opaque ids only, no real
17
+ // regulator / product names, per #166 acceptance ("Output is aggregates only —
18
+ // no subject/obligation names tied to any real corpus").
19
+ function buildCanon(): ComplianceCanon {
20
+ const canon = emptyCanon();
21
+ canon.products.push({ id: 'PRODUCT-A-1', name: 'Product A' });
22
+ canon.products.push({ id: 'PRODUCT-B-1', name: 'Product B' });
23
+ canon.requirements.push(
24
+ { id: 'REQUIREMENT-FOO-1', name: 'Foo', derived_from: ['LAW-X-1'] },
25
+ { id: 'REQUIREMENT-BAR-1', name: 'Bar', derived_from: ['LAW-X-1', 'REGULATION-Y-1'] },
26
+ { id: 'REQUIREMENT-BAZ-1', name: 'Baz', derived_from: ['REGULATION-Y-1'] },
27
+ // Internal-only requirement, no codex derivation:
28
+ { id: 'REQUIREMENT-INT-1', name: 'Internal-only' },
29
+ );
30
+ canon.assertions.push(
31
+ // (PRODUCT-A-1, FOO) — two assertions; partial wins over compliant per §5.2 step 4.
32
+ { id: 'ASSERTION-1', about: 'REQUIREMENT-FOO-1', subject: 'PRODUCT-A-1', status: 'compliant' },
33
+ { id: 'ASSERTION-2', about: 'REQUIREMENT-FOO-1', subject: 'PRODUCT-A-1', status: 'partial' },
34
+ // (PRODUCT-A-1, BAR) — single n_a → modelled fact ("No obligation applies").
35
+ { id: 'ASSERTION-3', about: 'REQUIREMENT-BAR-1', subject: 'PRODUCT-A-1', status: 'n_a' },
36
+ // (PRODUCT-B-1, BAR) — non_compliant.
37
+ { id: 'ASSERTION-4', about: 'REQUIREMENT-BAR-1', subject: 'PRODUCT-B-1', status: 'non_compliant' },
38
+ // (PRODUCT-B-1, BAZ) — under_review beats compliant.
39
+ { id: 'ASSERTION-5', about: 'REQUIREMENT-BAZ-1', subject: 'PRODUCT-B-1', status: 'under_review' },
40
+ { id: 'ASSERTION-6', about: 'REQUIREMENT-BAZ-1', subject: 'PRODUCT-B-1', status: 'compliant' },
41
+ );
42
+ return canon;
43
+ }
44
+
45
+ const baseConfig: ImpactViewConfig = {
46
+ id: 'COMPLIANCE_IMPACT-FIXTURE-1',
47
+ name: 'Fixture matrix',
48
+ subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1'] },
49
+ obligations: {},
50
+ };
51
+
52
+ describe('buildImpactMatrix', () => {
53
+ it('aggregates statuses per §5.2 step 4 — non_compliant > partial > under_review > compliant', () => {
54
+ const canon = buildCanon();
55
+ const matrix = buildImpactMatrix(canon, {
56
+ ...baseConfig,
57
+ obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1'] },
58
+ });
59
+ expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
60
+ expect(matrix.rows.map(r => r.id)).toEqual(['REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1', 'REQUIREMENT-FOO-1']);
61
+
62
+ const cellFor = (req: string, subject: string) => {
63
+ const r = matrix.rows.findIndex(x => x.id === req);
64
+ const c = matrix.columns.findIndex(col => col.label === subject);
65
+ return matrix.cells[r][c];
66
+ };
67
+
68
+ expect(cellFor('REQUIREMENT-FOO-1', 'PRODUCT-A-1').status).toBe('partial');
69
+ expect(cellFor('REQUIREMENT-FOO-1', 'PRODUCT-A-1').kind).toBe('bound');
70
+ expect(cellFor('REQUIREMENT-BAR-1', 'PRODUCT-A-1').kind).toBe('n_a_only');
71
+ expect(cellFor('REQUIREMENT-BAR-1', 'PRODUCT-B-1').status).toBe('non_compliant');
72
+ expect(cellFor('REQUIREMENT-BAZ-1', 'PRODUCT-B-1').status).toBe('under_review');
73
+ expect(cellFor('REQUIREMENT-FOO-1', 'PRODUCT-B-1').kind).toBe('gap');
74
+ expect(cellFor('REQUIREMENT-BAZ-1', 'PRODUCT-A-1').kind).toBe('gap');
75
+ });
76
+
77
+ it('selects obligations by `filter.derived_from_codex`', () => {
78
+ const canon = buildCanon();
79
+ const matrix = buildImpactMatrix(canon, {
80
+ ...baseConfig,
81
+ obligations: { filter: { derived_from_codex: ['REGULATION-Y-1'] } },
82
+ });
83
+ expect(matrix.rows.map(r => r.id)).toEqual(['REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1']);
84
+ });
85
+
86
+ it('`include` wins when both `include` and `filter` are present (COMPIMP-007)', () => {
87
+ const canon = buildCanon();
88
+ const matrix = buildImpactMatrix(canon, {
89
+ ...baseConfig,
90
+ obligations: {
91
+ include: ['REQUIREMENT-INT-1'],
92
+ filter: { derived_from_codex: ['LAW-X-1'] },
93
+ },
94
+ });
95
+ expect(matrix.rows.map(r => r.id)).toEqual(['REQUIREMENT-INT-1']);
96
+ });
97
+
98
+ it('distinguishes modelling gap from n_a-only per §5.3', () => {
99
+ const canon = buildCanon();
100
+ const matrix = buildImpactMatrix(canon, {
101
+ ...baseConfig,
102
+ obligations: { include: ['REQUIREMENT-BAR-1'] },
103
+ });
104
+ const aCell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-A-1')];
105
+ const bCell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-B-1')];
106
+ expect(aCell.kind).toBe('n_a_only');
107
+ expect(bCell.kind).toBe('bound');
108
+ expect(bCell.status).toBe('non_compliant');
109
+ });
110
+
111
+ it('respects `status_display.show` — filtered-out statuses leave the cell as a gap', () => {
112
+ const canon = buildCanon();
113
+ const matrix = buildImpactMatrix(canon, {
114
+ ...baseConfig,
115
+ obligations: { include: ['REQUIREMENT-BAZ-1'] },
116
+ status_display: { show: ['non_compliant'] }, // under_review + compliant filtered out
117
+ });
118
+ const cell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-B-1')];
119
+ expect(cell.kind).toBe('gap');
120
+ });
121
+
122
+ it('keeps subjects with no binding obligation as columns so the gap is visible', () => {
123
+ const canon = buildCanon();
124
+ const matrix = buildImpactMatrix(canon, {
125
+ ...baseConfig,
126
+ subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1', 'PRODUCT-NONE-1'] },
127
+ obligations: { include: ['REQUIREMENT-FOO-1'] },
128
+ });
129
+ expect(matrix.columns.map(c => c.label)).toContain('PRODUCT-NONE-1');
130
+ const cell = matrix.cells[0][matrix.columns.findIndex(c => c.label === 'PRODUCT-NONE-1')];
131
+ expect(cell.kind).toBe('gap');
132
+ });
133
+
134
+ it('orders rows alphabetically by name when `order_rows_by: name`', () => {
135
+ const canon = buildCanon();
136
+ const matrix = buildImpactMatrix(canon, {
137
+ ...baseConfig,
138
+ obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1', 'REQUIREMENT-BAZ-1'] },
139
+ order_rows_by: 'name',
140
+ });
141
+ expect(matrix.rows.map(r => r.name)).toEqual(['Bar', 'Baz', 'Foo']);
142
+ });
143
+ });
144
+
145
+ describe('renderImpactMarkdown', () => {
146
+ it('renders a header, table, and §5.3 legend', () => {
147
+ const canon = buildCanon();
148
+ const matrix = buildImpactMatrix(canon, {
149
+ ...baseConfig,
150
+ obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1'] },
151
+ });
152
+ const md = renderImpactMarkdown(matrix);
153
+ expect(md).toContain('# Fixture matrix');
154
+ expect(md).toContain('View ID: `COMPLIANCE_IMPACT-FIXTURE-1`');
155
+ expect(md).toContain('| Obligation | PRODUCT-A-1 | PRODUCT-B-1 |');
156
+ expect(md).toContain('PARTIAL');
157
+ expect(md).toContain('FAIL');
158
+ expect(md).toContain('No mapped obligation (current model)');
159
+ expect(md).toContain('No obligation applies');
160
+ expect(md).toContain('## Legend');
161
+ });
162
+
163
+ it('honours custom empty-cell labels', () => {
164
+ const canon = buildCanon();
165
+ const matrix = buildImpactMatrix(canon, {
166
+ ...baseConfig,
167
+ obligations: { include: ['REQUIREMENT-FOO-1', 'REQUIREMENT-BAR-1'] },
168
+ empty_cells: {
169
+ no_obligation_label: 'GAP',
170
+ no_obligation_applies_label: 'EXCLUDED',
171
+ },
172
+ });
173
+ const md = renderImpactMarkdown(matrix);
174
+ expect(md).toContain('GAP');
175
+ expect(md).toContain('EXCLUDED');
176
+ expect(md).not.toContain('No mapped obligation (current model)');
177
+ });
178
+
179
+ it('renders a placeholder when zero obligations are selected', () => {
180
+ const canon = buildCanon();
181
+ const matrix = buildImpactMatrix(canon, {
182
+ ...baseConfig,
183
+ obligations: { include: ['REQUIREMENT-DOES-NOT-EXIST-1'] },
184
+ });
185
+ const md = renderImpactMarkdown(matrix);
186
+ expect(md).toContain('No obligations in scope');
187
+ });
188
+
189
+ it('propagates snapshot_at into the matrix and renders it', () => {
190
+ const canon = buildCanon();
191
+ const matrix = buildImpactMatrix(canon, { ...baseConfig, snapshot_at: '2026-06-09' });
192
+ expect(matrix.snapshotAt).toBe('2026-06-09');
193
+ const md = renderImpactMarkdown(matrix);
194
+ expect(md).toContain('Report snapshot: 2026-06-09');
195
+ });
196
+
197
+ it('resolves capability subject names from canon.subjects', () => {
198
+ const canon = emptyCanon();
199
+ canon.subjects.push({ id: 'CAPABILITY-CRM-1', name: 'CRM Platform' });
200
+ canon.requirements.push({ id: 'REQUIREMENT-FOO-1', name: 'Foo' });
201
+ canon.assertions.push({
202
+ id: 'ASSERTION-1',
203
+ about: 'REQUIREMENT-FOO-1',
204
+ subject: 'CAPABILITY-CRM-1',
205
+ status: 'compliant',
206
+ });
207
+ const matrix = buildImpactMatrix(canon, {
208
+ id: 'V-1', name: 'Test',
209
+ subjects: { capabilities: ['CAPABILITY-CRM-1'] },
210
+ obligations: {},
211
+ });
212
+ expect(matrix.columns[0].subjectId).toBe('CAPABILITY-CRM-1');
213
+ expect(matrix.columns[0].subjectName).toBe('CRM Platform');
214
+ expect(matrix.cells[0][0].status).toBe('compliant');
215
+ });
216
+
217
+ it('resolves process subject names from canon.subjects', () => {
218
+ const canon = emptyCanon();
219
+ canon.subjects.push({ id: 'PROCESS-CS-1', name: 'Customer Support' });
220
+ canon.requirements.push({ id: 'REQUIREMENT-BAR-1', name: 'Bar' });
221
+ canon.assertions.push({
222
+ id: 'ASSERTION-P1',
223
+ about: 'REQUIREMENT-BAR-1',
224
+ subject: 'PROCESS-CS-1',
225
+ status: 'partial',
226
+ });
227
+ const matrix = buildImpactMatrix(canon, {
228
+ id: 'V-2', name: 'Test',
229
+ subjects: { processes: ['PROCESS-CS-1'] },
230
+ obligations: {},
231
+ });
232
+ expect(matrix.columns[0].subjectName).toBe('Customer Support');
233
+ expect(matrix.cells[0][0].status).toBe('partial');
234
+ });
235
+
236
+ it('mixed product + capability columns — names resolved from respective buckets', () => {
237
+ const canon = buildCanon();
238
+ canon.subjects.push({ id: 'CAPABILITY-CRM-1', name: 'CRM' });
239
+ canon.requirements.push({ id: 'REQUIREMENT-CRM-1', name: 'CRM req' });
240
+ canon.assertions.push({
241
+ id: 'ASSERTION-CRM-1',
242
+ about: 'REQUIREMENT-CRM-1',
243
+ subject: 'CAPABILITY-CRM-1',
244
+ status: 'under_review',
245
+ });
246
+ const matrix = buildImpactMatrix(canon, {
247
+ id: 'V-3', name: 'Mixed',
248
+ subjects: { products: ['PRODUCT-A-1'], capabilities: ['CAPABILITY-CRM-1'] },
249
+ obligations: {},
250
+ });
251
+ expect(matrix.columns.map(c => c.subjectId)).toEqual(['PRODUCT-A-1', 'CAPABILITY-CRM-1']);
252
+ expect(matrix.columns[0].subjectName).toBe('Product A');
253
+ expect(matrix.columns[1].subjectName).toBe('CRM');
254
+ });
255
+
256
+ it('pending_owner cell renders as bound with PENDING status', () => {
257
+ const canon = emptyCanon();
258
+ canon.products.push({ id: 'PRODUCT-A-1', name: 'Product A' });
259
+ canon.requirements.push({ id: 'REQUIREMENT-PO-1', name: 'PO req' });
260
+ canon.assertions.push({
261
+ id: 'ASSERTION-PO-1',
262
+ about: 'REQUIREMENT-PO-1',
263
+ subject: 'PRODUCT-A-1',
264
+ status: 'pending_owner',
265
+ owner_to_confirm: 'alice@example.com',
266
+ });
267
+ const matrix = buildImpactMatrix(canon, {
268
+ id: 'V-PO', name: 'PO Test',
269
+ subjects: { products: ['PRODUCT-A-1'] },
270
+ obligations: {},
271
+ });
272
+ expect(matrix.cells[0][0].kind).toBe('bound');
273
+ expect(matrix.cells[0][0].status).toBe('pending_owner');
274
+ expect(matrix.cells[0][0].assertions[0].owner_to_confirm).toBe('alice@example.com');
275
+ });
276
+
277
+ it('pending_owner loses to partial in aggregation', () => {
278
+ const canon = emptyCanon();
279
+ canon.products.push({ id: 'PRODUCT-A-1', name: 'Product A' });
280
+ canon.requirements.push({ id: 'REQUIREMENT-PO-2', name: 'PO req 2' });
281
+ canon.assertions.push(
282
+ { id: 'ASSERTION-PO-2a', about: 'REQUIREMENT-PO-2', subject: 'PRODUCT-A-1', status: 'partial' },
283
+ { id: 'ASSERTION-PO-2b', about: 'REQUIREMENT-PO-2', subject: 'PRODUCT-A-1', status: 'pending_owner' },
284
+ );
285
+ const matrix = buildImpactMatrix(canon, {
286
+ id: 'V-PO2', name: 'PO Agg',
287
+ subjects: { products: ['PRODUCT-A-1'] },
288
+ obligations: {},
289
+ });
290
+ expect(matrix.cells[0][0].status).toBe('partial');
291
+ });
292
+ });
293
+
294
+ // ── parseImpactViewConfig ───────────────────────────────────────────────────
295
+
296
+ describe('parseImpactViewConfig', () => {
297
+ it('returns ok:false for a non-object', () => {
298
+ const r = parseImpactViewConfig(null);
299
+ expect(r.ok).toBe(false);
300
+ });
301
+
302
+ it('returns ok:false when id is missing', () => {
303
+ const r = parseImpactViewConfig({ name: 'X', subjects: {} });
304
+ expect(r.ok).toBe(false);
305
+ if (!r.ok) expect(r.errors.some(e => e.includes('id'))).toBe(true);
306
+ });
307
+
308
+ it('returns ok:false when name is missing', () => {
309
+ const r = parseImpactViewConfig({ id: 'X', subjects: {} });
310
+ expect(r.ok).toBe(false);
311
+ if (!r.ok) expect(r.errors.some(e => e.includes('name'))).toBe(true);
312
+ });
313
+
314
+ it('unwraps the top-level view: wrapper', () => {
315
+ const r = parseImpactViewConfig({ view: { id: 'V-1', name: 'My view', subjects: {} } });
316
+ expect(r.ok).toBe(true);
317
+ if (r.ok) expect(r.config.id).toBe('V-1');
318
+ });
319
+
320
+ it('fills all optional fields from COMPLIANCE_IMPACT_DEFAULTS', () => {
321
+ const r = parseImpactViewConfig({ id: 'V-1', name: 'My view', subjects: {} });
322
+ expect(r.ok).toBe(true);
323
+ if (!r.ok) return;
324
+ const c = r.config;
325
+ expect(c.subjects.products).toEqual([]);
326
+ expect(c.subjects.processes).toEqual([]);
327
+ expect(c.subjects.capabilities).toEqual([]);
328
+ expect(c.status_display?.show).toEqual(COMPLIANCE_IMPACT_DEFAULTS.status_display.show);
329
+ expect(c.order_rows_by).toBe('id');
330
+ expect(c.empty_cells?.no_obligation_label).toBe(
331
+ COMPLIANCE_IMPACT_DEFAULTS.empty_cells.no_obligation_label,
332
+ );
333
+ expect(c.empty_cells?.no_obligation_applies_label).toBe(
334
+ COMPLIANCE_IMPACT_DEFAULTS.empty_cells.no_obligation_applies_label,
335
+ );
336
+ });
337
+
338
+ it('preserves explicit subject lists and snapshot_at', () => {
339
+ const r = parseImpactViewConfig({
340
+ id: 'V-2',
341
+ name: 'View 2',
342
+ snapshot_at: '2026-01-01',
343
+ subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1'] },
344
+ });
345
+ expect(r.ok).toBe(true);
346
+ if (!r.ok) return;
347
+ expect(r.config.subjects.products).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
348
+ expect(r.config.snapshot_at).toBe('2026-01-01');
349
+ });
350
+
351
+ it('applies custom order_rows_by:name', () => {
352
+ const r = parseImpactViewConfig({ id: 'V-3', name: 'V', subjects: {}, order_rows_by: 'name' });
353
+ expect(r.ok).toBe(true);
354
+ if (r.ok) expect(r.config.order_rows_by).toBe('name');
355
+ });
356
+
357
+ it('parses obligation filter correctly', () => {
358
+ const r = parseImpactViewConfig({
359
+ id: 'V-4',
360
+ name: 'V',
361
+ subjects: {},
362
+ obligations: { filter: { derived_from_codex: ['LAW-X-1'] } },
363
+ });
364
+ expect(r.ok).toBe(true);
365
+ if (r.ok) expect(r.config.obligations.filter?.derived_from_codex).toEqual(['LAW-X-1']);
366
+ });
367
+ });
368
+
369
+ describe('CV-3a — stage grouping (ImpactColumn, extractObjectDetails, buildImpactMatrix stage grain)', () => {
370
+ it('default product grain: columns are plain ImpactColumn with label=subjectId', () => {
371
+ const canon = buildCanon();
372
+ const matrix = buildImpactMatrix(canon, baseConfig);
373
+ expect(matrix.columns.map(c => c.subjectId)).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
374
+ expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1', 'PRODUCT-B-1']);
375
+ expect(matrix.columns.every(c => !c.stageId)).toBe(true);
376
+ });
377
+
378
+ it('product-stage grain: expands columns by stage, label is subjectId:stageId', () => {
379
+ const canon = buildCanon();
380
+ const matrix = buildImpactMatrix(
381
+ canon,
382
+ { ...baseConfig, subjects: { products: ['PRODUCT-A-1'] }, grouping: { columns: 'product-stage' } },
383
+ [{ objectId: 'PRODUCT-A-1', details: [{ id: 'STAGE-1', name: 'Stage One' }, { id: 'STAGE-2', name: 'Stage Two' }] }],
384
+ );
385
+ expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1:STAGE-1', 'PRODUCT-A-1:STAGE-2']);
386
+ expect(matrix.columns.map(c => c.stageId)).toEqual(['STAGE-1', 'STAGE-2']);
387
+ });
388
+
389
+ it('assertion without realised_via fills every stage column for its subject', () => {
390
+ const canon = emptyCanon();
391
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
392
+ // Assertion with no realised_via — covers the whole subject.
393
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
394
+ const matrix = buildImpactMatrix(
395
+ canon,
396
+ {
397
+ id: 'V', name: 'V',
398
+ subjects: { products: ['PROD-1'] },
399
+ obligations: {},
400
+ grouping: { columns: 'product-stage' },
401
+ },
402
+ [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 's1' }, { id: 'S2', name: 's2' }] }],
403
+ );
404
+ // Both stage columns should be bound (no realised_via means all stages inherit).
405
+ expect(matrix.cells[0][0].kind).toBe('bound');
406
+ expect(matrix.cells[0][1].kind).toBe('bound');
407
+ });
408
+
409
+ it('assertion with realised_via fills only the matching stage column', () => {
410
+ const canon = emptyCanon();
411
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
412
+ // Assertion only covers STAGE-A.
413
+ canon.assertions.push({
414
+ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant',
415
+ realised_via: ['STAGE-A'],
416
+ });
417
+ const matrix = buildImpactMatrix(
418
+ canon,
419
+ {
420
+ id: 'V', name: 'V',
421
+ subjects: { products: ['PROD-1'] },
422
+ obligations: {},
423
+ grouping: { columns: 'product-stage' },
424
+ },
425
+ [{ objectId: 'PROD-1', details: [{ id: 'STAGE-A', name: 'a' }, { id: 'STAGE-B', name: 'b' }] }],
426
+ );
427
+ expect(matrix.cells[0][0].kind).toBe('bound'); // STAGE-A: covered
428
+ expect(matrix.cells[0][1].kind).toBe('gap'); // STAGE-B: gap
429
+ });
430
+
431
+ it('subject with no stage mapping falls back to single product-grain column', () => {
432
+ const canon = buildCanon();
433
+ const matrix = buildImpactMatrix(
434
+ canon,
435
+ {
436
+ ...baseConfig,
437
+ subjects: { products: ['PRODUCT-A-1', 'PRODUCT-B-1'] },
438
+ grouping: { columns: 'product-stage' },
439
+ },
440
+ // Only provide stages for A — B falls back to a single column.
441
+ [{ objectId: 'PRODUCT-A-1', details: [{ id: 'S1', name: 's1' }] }],
442
+ );
443
+ expect(matrix.columns.map(c => c.label)).toEqual(['PRODUCT-A-1:S1', 'PRODUCT-B-1']);
444
+ // PRODUCT-B-1 fallback column: no stageId.
445
+ expect(matrix.columns[1].stageId).toBeUndefined();
446
+ });
447
+
448
+ it('extractObjectDetails — returns null for non-blueprint docs', () => {
449
+ expect(extractObjectDetails(null)).toBeNull();
450
+ expect(extractObjectDetails({ notation: 'requirement', id: 'R1' })).toBeNull();
451
+ expect(extractObjectDetails({ process_blueprint: { details: [{ id: 'S1' }] } })).toBeNull(); // missing id
452
+ });
453
+
454
+ it('extractObjectDetails — extracts subjectId and stages from process_blueprint wrapper', () => {
455
+ const doc = { process_blueprint: { id: 'PROC-1', notation: 'process_blueprint', stages: [{ id: 'S1', name: 'Stage One' }, { id: 'S2', name: 'Stage Two' }] } };
456
+ const sg = extractObjectDetails(doc);
457
+ expect(sg).not.toBeNull();
458
+ expect(sg!.objectId).toBe('PROC-1');
459
+ expect(sg!.details.map((s: { id: string }) => s.id)).toEqual(['S1', 'S2']);
460
+ });
461
+
462
+ it('renderImpactMarkdown uses column labels in the table header', () => {
463
+ const canon = emptyCanon();
464
+ canon.requirements.push({ id: 'REQ-1', name: 'Req' });
465
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
466
+ const matrix = buildImpactMatrix(
467
+ canon,
468
+ {
469
+ id: 'V', name: 'Stage Report',
470
+ subjects: { products: ['PROD-1'] },
471
+ obligations: {},
472
+ grouping: { columns: 'product-stage' },
473
+ },
474
+ [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 'Stage One' }] }],
475
+ );
476
+ const md = renderImpactMarkdown(matrix);
477
+ expect(md).toContain('PROD-1:S1');
478
+ });
479
+ });
480
+
481
+ describe('CV-3 -- blueprint-lane decorations + obligations lane', () => {
482
+ it('computeDeadlineStatus function is available', () => {
483
+ expect(typeof computeDeadlineStatus).toBe('function');
484
+ });
485
+
486
+ it('cell.decoration.isNew = false when no snapshot_at', () => {
487
+ const canon = emptyCanon();
488
+ canon.requirements.push({ id: 'REQ-1', name: 'R1', admitted_at: '2026-01-01' });
489
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
490
+ const matrix = buildImpactMatrix(canon, {
491
+ id: 'V', name: 'V',
492
+ subjects: { products: ['PROD-1'] },
493
+ obligations: {},
494
+ });
495
+ expect(matrix.cells[0][0].decoration.isNew).toBe(false);
496
+ });
497
+
498
+ it('cell.decoration.isNew = true when req admitted after snapshot_at', () => {
499
+ const canon = emptyCanon();
500
+ canon.requirements.push({ id: 'REQ-1', name: 'R1', admitted_at: '2026-06-01' });
501
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
502
+ const matrix = buildImpactMatrix(canon, {
503
+ id: 'V', name: 'V',
504
+ snapshot_at: '2026-05-01',
505
+ subjects: { products: ['PROD-1'] },
506
+ obligations: {},
507
+ });
508
+ // admitted_at (2026-06-01) > snapshot_at (2026-05-01) -> isNew
509
+ expect(matrix.cells[0][0].decoration.isNew).toBe(true);
510
+ });
511
+
512
+ it('cell.decoration.isUrgent = true when gap + past_due deadline', () => {
513
+ const canon = emptyCanon();
514
+ canon.requirements.push({ id: 'REQ-1', name: 'R1', deadline: '2000-01-01' });
515
+ // No assertion -> gap cell
516
+ const matrix = buildImpactMatrix(canon, {
517
+ id: 'V', name: 'V',
518
+ subjects: { products: ['PROD-1'] },
519
+ obligations: {},
520
+ });
521
+ expect(matrix.cells[0][0].kind).toBe('gap');
522
+ expect(matrix.cells[0][0].decoration.isUrgent).toBe(true);
523
+ expect(matrix.cells[0][0].decoration.deadlineStatus).toBe('past_due');
524
+ });
525
+
526
+ it('cell.decoration.isUrgent = false on bound cell even with past deadline', () => {
527
+ const canon = emptyCanon();
528
+ canon.requirements.push({ id: 'REQ-1', name: 'R1', deadline: '2000-01-01' });
529
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
530
+ const matrix = buildImpactMatrix(canon, {
531
+ id: 'V', name: 'V',
532
+ subjects: { products: ['PROD-1'] },
533
+ obligations: {},
534
+ });
535
+ expect(matrix.cells[0][0].kind).toBe('bound');
536
+ // Not urgent: cell is bound, not a gap
537
+ expect(matrix.cells[0][0].decoration.isUrgent).toBe(false);
538
+ });
539
+
540
+ it('obligationsLane lists derived_from codex IDs for non-gap columns', () => {
541
+ const canon = emptyCanon();
542
+ canon.requirements.push({ id: 'REQ-1', name: 'R1', derived_from: ['LAW-X', 'REG-Y'] });
543
+ canon.requirements.push({ id: 'REQ-2', name: 'R2', derived_from: ['LAW-X'] });
544
+ // Only REQ-1 is bound in PROD-1 column; REQ-2 is a gap
545
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
546
+ const matrix = buildImpactMatrix(canon, {
547
+ id: 'V', name: 'V',
548
+ subjects: { products: ['PROD-1'] },
549
+ obligations: {},
550
+ });
551
+ // obligationsLane[0] (PROD-1 column): REQ-1 is bound -> derived_from -> LAW-X, REG-Y sorted
552
+ expect(matrix.obligationsLane[0]).toEqual(['LAW-X', 'REG-Y']);
553
+ });
554
+
555
+ it('obligationsLane is empty for columns with only gap cells', () => {
556
+ const canon = emptyCanon();
557
+ canon.requirements.push({ id: 'REQ-1', name: 'R1', derived_from: ['LAW-X'] });
558
+ // No assertions -> all gaps
559
+ const matrix = buildImpactMatrix(canon, {
560
+ id: 'V', name: 'V',
561
+ subjects: { products: ['PROD-1'] },
562
+ obligations: {},
563
+ });
564
+ expect(matrix.obligationsLane[0]).toEqual([]);
565
+ });
566
+ });
567
+
568
+ describe('computeDeadlineStatus', () => {
569
+ it('returns none for missing deadline', () => {
570
+ expect(computeDeadlineStatus(undefined, '2026-06-01')).toBe('none');
571
+ });
572
+ it('returns past_due when deadline < today', () => {
573
+ expect(computeDeadlineStatus('2026-01-01', '2026-06-01')).toBe('past_due');
574
+ });
575
+ it('returns in_force within 30 days', () => {
576
+ expect(computeDeadlineStatus('2026-06-20', '2026-06-01')).toBe('in_force');
577
+ });
578
+ it('returns upcoming more than 30 days away', () => {
579
+ expect(computeDeadlineStatus('2026-12-31', '2026-06-01')).toBe('upcoming');
580
+ });
581
+ });
582
+
583
+ // ── parseImpactViewConfig — grouping field ────────────────────────────────────
584
+
585
+ describe('parseImpactViewConfig — grouping field', () => {
586
+ it('parses grouping.columns: product-stage', () => {
587
+ const r = parseImpactViewConfig({ id: 'V', name: 'V', subjects: {}, grouping: { columns: 'product-stage' } });
588
+ expect(r.ok).toBe(true);
589
+ if (r.ok) expect(r.config.grouping?.columns).toBe('product-stage');
590
+ });
591
+
592
+ it('parses grouping.columns: product-stage-task', () => {
593
+ const r = parseImpactViewConfig({ id: 'V', name: 'V', subjects: {}, grouping: { columns: 'product-stage-task' } });
594
+ expect(r.ok).toBe(true);
595
+ if (r.ok) expect(r.config.grouping?.columns).toBe('product-stage-task');
596
+ });
597
+
598
+ it('parses grouping.columns: product (coarsest grain)', () => {
599
+ const r = parseImpactViewConfig({ id: 'V', name: 'V', subjects: {}, grouping: { columns: 'product' } });
600
+ expect(r.ok).toBe(true);
601
+ if (r.ok) expect(r.config.grouping?.columns).toBe('product');
602
+ });
603
+
604
+ it('normalises backward-compat alias object-details → product-stage', () => {
605
+ const r = parseImpactViewConfig({ id: 'V', name: 'V', subjects: {}, grouping: { columns: 'object-details' } });
606
+ expect(r.ok).toBe(true);
607
+ if (r.ok) expect(r.config.grouping?.columns).toBe('product-stage');
608
+ });
609
+
610
+ it('normalises backward-compat alias object → product', () => {
611
+ const r = parseImpactViewConfig({ id: 'V', name: 'V', subjects: {}, grouping: { columns: 'object' } });
612
+ expect(r.ok).toBe(true);
613
+ if (r.ok) expect(r.config.grouping?.columns).toBe('product');
614
+ });
615
+
616
+ it('leaves grouping undefined when the field is absent', () => {
617
+ const r = parseImpactViewConfig({ id: 'V', name: 'V', subjects: {} });
618
+ expect(r.ok).toBe(true);
619
+ if (r.ok) expect(r.config.grouping).toBeUndefined();
620
+ });
621
+
622
+ it('leaves grouping undefined for an unrecognised columns value', () => {
623
+ const r = parseImpactViewConfig({ id: 'V', name: 'V', subjects: {}, grouping: { columns: 'unknown-grain' } });
624
+ expect(r.ok).toBe(true);
625
+ if (r.ok) expect(r.config.grouping).toBeUndefined();
626
+ });
627
+ });
628
+
629
+ // ── CV-3a — product-stage-task grain ─────────────────────────────────────────
630
+
631
+ describe('CV-3a — product-stage-task grain (buildImpactMatrix, buildTaskColumns)', () => {
632
+ it('expands columns to (subject, stage, task) triples; label is subjectId:stageId:taskId', () => {
633
+ const canon = emptyCanon();
634
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
635
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
636
+ const matrix = buildImpactMatrix(
637
+ canon,
638
+ {
639
+ id: 'V', name: 'V',
640
+ subjects: { products: ['PROD-1'] },
641
+ obligations: {},
642
+ grouping: { columns: 'product-stage-task' },
643
+ },
644
+ [{
645
+ objectId: 'PROD-1',
646
+ details: [
647
+ { id: 'STAGE-A', name: 'Stage A', tasks: [{ id: 'TASK-1', name: 'Task 1' }, { id: 'TASK-2', name: 'Task 2' }] },
648
+ ],
649
+ }],
650
+ );
651
+ expect(matrix.columns.map(c => c.label)).toEqual(['PROD-1:STAGE-A:TASK-1', 'PROD-1:STAGE-A:TASK-2']);
652
+ expect(matrix.columns[0]).toMatchObject({ subjectId: 'PROD-1', stageId: 'STAGE-A', taskId: 'TASK-1' });
653
+ expect(matrix.columns[1]).toMatchObject({ subjectId: 'PROD-1', stageId: 'STAGE-A', taskId: 'TASK-2' });
654
+ });
655
+
656
+ it('assertion without realised_via fills every task column for its subject', () => {
657
+ const canon = emptyCanon();
658
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
659
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant' });
660
+ const matrix = buildImpactMatrix(
661
+ canon,
662
+ {
663
+ id: 'V', name: 'V',
664
+ subjects: { products: ['PROD-1'] },
665
+ obligations: {},
666
+ grouping: { columns: 'product-stage-task' },
667
+ },
668
+ [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 's1', tasks: [{ id: 'T1', name: 't1' }, { id: 'T2', name: 't2' }] }] }],
669
+ );
670
+ expect(matrix.cells[0][0].kind).toBe('bound');
671
+ expect(matrix.cells[0][1].kind).toBe('bound');
672
+ });
673
+
674
+ it('assertion with realised_via: [taskId] fills only the matching task column', () => {
675
+ const canon = emptyCanon();
676
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
677
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant', realised_via: ['T1'] });
678
+ const matrix = buildImpactMatrix(
679
+ canon,
680
+ {
681
+ id: 'V', name: 'V',
682
+ subjects: { products: ['PROD-1'] },
683
+ obligations: {},
684
+ grouping: { columns: 'product-stage-task' },
685
+ },
686
+ [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 's1', tasks: [{ id: 'T1', name: 't1' }, { id: 'T2', name: 't2' }] }] }],
687
+ );
688
+ expect(matrix.cells[0][0].kind).toBe('bound'); // T1: covered
689
+ expect(matrix.cells[0][1].kind).toBe('gap'); // T2: not covered
690
+ });
691
+
692
+ it('assertion with realised_via: [stageId] fills all task columns in that stage', () => {
693
+ const canon = emptyCanon();
694
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
695
+ canon.assertions.push({ id: 'ASS-1', about: 'REQ-1', subject: 'PROD-1', status: 'compliant', realised_via: ['S1'] });
696
+ const matrix = buildImpactMatrix(
697
+ canon,
698
+ {
699
+ id: 'V', name: 'V',
700
+ subjects: { products: ['PROD-1'] },
701
+ obligations: {},
702
+ grouping: { columns: 'product-stage-task' },
703
+ },
704
+ [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 's1', tasks: [{ id: 'T1', name: 't1' }, { id: 'T2', name: 't2' }] }] }],
705
+ );
706
+ expect(matrix.cells[0][0].kind).toBe('bound');
707
+ expect(matrix.cells[0][1].kind).toBe('bound');
708
+ });
709
+
710
+ it('stage with no tasks falls back to stage-grain column', () => {
711
+ const canon = emptyCanon();
712
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
713
+ const matrix = buildImpactMatrix(
714
+ canon,
715
+ {
716
+ id: 'V', name: 'V',
717
+ subjects: { products: ['PROD-1'] },
718
+ obligations: {},
719
+ grouping: { columns: 'product-stage-task' },
720
+ },
721
+ [{ objectId: 'PROD-1', details: [{ id: 'S1', name: 's1' }] }],
722
+ );
723
+ expect(matrix.columns.map(c => c.label)).toEqual(['PROD-1:S1']);
724
+ expect(matrix.columns[0].taskId).toBeUndefined();
725
+ expect(matrix.columns[0].stageId).toBe('S1');
726
+ });
727
+
728
+ it('subject with no objectDetails falls back to product-grain column', () => {
729
+ const canon = emptyCanon();
730
+ canon.requirements.push({ id: 'REQ-1', name: 'R1' });
731
+ const matrix = buildImpactMatrix(
732
+ canon,
733
+ {
734
+ id: 'V', name: 'V',
735
+ subjects: { products: ['PROD-1'] },
736
+ obligations: {},
737
+ grouping: { columns: 'product-stage-task' },
738
+ },
739
+ );
740
+ expect(matrix.columns.map(c => c.label)).toEqual(['PROD-1']);
741
+ expect(matrix.columns[0].stageId).toBeUndefined();
742
+ });
743
+ });
744
+
745
+ // ── extractProcessFlowTasks ───────────────────────────────────────────────────
746
+
747
+ describe('extractProcessFlowTasks', () => {
748
+ it('returns null for non-process documents', () => {
749
+ expect(extractProcessFlowTasks(null)).toBeNull();
750
+ expect(extractProcessFlowTasks({ notation: 'requirement', id: 'R1' })).toBeNull();
751
+ expect(extractProcessFlowTasks({ notation: 'process_blueprint', id: 'BP-1' })).toBeNull();
752
+ });
753
+
754
+ it('returns null when process has no flow.steps', () => {
755
+ expect(extractProcessFlowTasks({ notation: 'process', id: 'PROC-1' })).toBeNull();
756
+ expect(extractProcessFlowTasks({ notation: 'process', id: 'PROC-1', flow: { sequence: [] } })).toBeNull();
757
+ });
758
+
759
+ it('returns null when no task-type steps exist', () => {
760
+ const doc = {
761
+ notation: 'process', id: 'PROC-1',
762
+ flow: { steps: [
763
+ { id: 'S-1', type: 'startEvent' },
764
+ { id: 'G-1', type: 'exclusiveGateway' },
765
+ { id: 'E-1', type: 'endEvent' },
766
+ ] },
767
+ };
768
+ expect(extractProcessFlowTasks(doc)).toBeNull();
769
+ });
770
+
771
+ it('extracts task-type steps and skips events/gateways', () => {
772
+ const doc = {
773
+ notation: 'process', id: 'PROC-1',
774
+ flow: { steps: [
775
+ { id: 'S-1', type: 'startEvent' },
776
+ { id: 'STEP-T1', type: 'task', name: 'Task One' },
777
+ { id: 'G-1', type: 'exclusiveGateway', name: 'Branch?' },
778
+ { id: 'STEP-T2', type: 'userTask', name: 'User Task' },
779
+ { id: 'E-1', type: 'endEvent' },
780
+ ] },
781
+ };
782
+ const result = extractProcessFlowTasks(doc);
783
+ expect(result).not.toBeNull();
784
+ expect(result!.objectId).toBe('PROC-1');
785
+ expect(result!.details.map(d => d.id)).toEqual(['STEP-T1', 'STEP-T2']);
786
+ expect(result!.details[0].name).toBe('Task One');
787
+ });
788
+
789
+ it('falls back to step id as name when name is absent', () => {
790
+ const doc = {
791
+ notation: 'process', id: 'PROC-1',
792
+ flow: { steps: [{ id: 'STEP-T1', type: 'serviceTask' }] },
793
+ };
794
+ const result = extractProcessFlowTasks(doc);
795
+ expect(result!.details[0].name).toBe('STEP-T1');
796
+ });
797
+ });
798
+
799
+ // ── mergeStageTaskDetails ─────────────────────────────────────────────────────
800
+
801
+ describe('mergeStageTaskDetails', () => {
802
+ const stages = {
803
+ objectId: 'PROC-1',
804
+ details: [
805
+ { id: 'S1', name: 'Stage 1' },
806
+ { id: 'S2', name: 'Stage 2' },
807
+ ],
808
+ };
809
+
810
+ it('returns stageInput unchanged when taskInput is null', () => {
811
+ expect(mergeStageTaskDetails(stages, null)).toBe(stages);
812
+ });
813
+
814
+ it('returns stageInput unchanged when objectIds do not match', () => {
815
+ const other = { objectId: 'PROC-99', details: [{ id: 'T1', name: 't1' }] };
816
+ expect(mergeStageTaskDetails(stages, other)).toBe(stages);
817
+ });
818
+
819
+ it('embeds all tasks into every stage when no stageOwner map is provided', () => {
820
+ const tasks = { objectId: 'PROC-1', details: [{ id: 'T1', name: 't1' }, { id: 'T2', name: 't2' }] };
821
+ const merged = mergeStageTaskDetails(stages, tasks);
822
+ expect(merged.objectId).toBe('PROC-1');
823
+ expect(merged.details[0].tasks?.map(t => t.id)).toEqual(['T1', 'T2']);
824
+ expect(merged.details[1].tasks?.map(t => t.id)).toEqual(['T1', 'T2']);
825
+ });
826
+
827
+ it('distributes tasks by stageOwner map', () => {
828
+ const tasks = { objectId: 'PROC-1', details: [{ id: 'T1', name: 't1' }, { id: 'T2', name: 't2' }] };
829
+ const merged = mergeStageTaskDetails(stages, tasks, { S1: ['T1'], S2: ['T2'] });
830
+ expect(merged.details[0].tasks?.map(t => t.id)).toEqual(['T1']);
831
+ expect(merged.details[1].tasks?.map(t => t.id)).toEqual(['T2']);
832
+ });
833
+
834
+ it('leaves stages without matching stageOwner entries task-free', () => {
835
+ const tasks = { objectId: 'PROC-1', details: [{ id: 'T1', name: 't1' }] };
836
+ const merged = mergeStageTaskDetails(stages, tasks, { S1: ['T1'], S2: [] });
837
+ expect(merged.details[0].tasks?.map(t => t.id)).toEqual(['T1']);
838
+ // S2 has no tasks in stageOwner, filter returns [] → no tasks embedded
839
+ expect(merged.details[1].tasks).toBeUndefined();
840
+ });
482
841
  });