@transitrix/diagrams 1.4.0 → 1.6.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.
- package/dist/activities/__tests__/gantt.test.js +39 -0
- package/dist/capability-map/CapabilityCardNode.d.ts +19 -0
- package/dist/capability-map/CapabilityCardNode.js +66 -0
- package/dist/capability-map/CapabilityMapView.d.ts +59 -0
- package/dist/capability-map/CapabilityMapView.js +204 -0
- package/dist/capability-map/CapabilityRootNode.d.ts +12 -0
- package/dist/capability-map/CapabilityRootNode.js +23 -0
- package/dist/capability-map/__tests__/CapabilityMapView.test.d.ts +1 -0
- package/dist/capability-map/__tests__/CapabilityMapView.test.js +217 -0
- package/dist/capability-map/__tests__/address.test.d.ts +1 -0
- package/dist/capability-map/__tests__/address.test.js +49 -0
- package/dist/capability-map/__tests__/dsm-layout.test.d.ts +1 -0
- package/dist/capability-map/__tests__/dsm-layout.test.js +46 -0
- package/dist/capability-map/__tests__/dsm-mutations.test.d.ts +1 -0
- package/dist/capability-map/__tests__/dsm-mutations.test.js +125 -0
- package/dist/capability-map/__tests__/dsm-validate.test.d.ts +1 -0
- package/dist/capability-map/__tests__/dsm-validate.test.js +56 -0
- package/dist/capability-map/address.d.ts +12 -0
- package/dist/capability-map/address.js +48 -0
- package/dist/capability-map/dsm-layout.d.ts +9 -0
- package/dist/capability-map/dsm-layout.js +125 -0
- package/dist/capability-map/dsm-mutations.d.ts +28 -0
- package/dist/capability-map/dsm-mutations.js +196 -0
- package/dist/capability-map/dsm-schema.d.ts +71 -0
- package/dist/capability-map/dsm-schema.js +14 -0
- package/dist/capability-map/dsm-theme.d.ts +20 -0
- package/dist/capability-map/dsm-theme.js +36 -0
- package/dist/capability-map/dsm-validate.d.ts +2 -0
- package/dist/capability-map/dsm-validate.js +93 -0
- package/dist/capability-map/index.d.ts +9 -0
- package/dist/capability-map/index.js +6 -0
- package/dist/compliance/__tests__/trace.test.d.ts +1 -0
- package/dist/compliance/__tests__/trace.test.js +144 -0
- package/dist/compliance/classify.js +7 -1
- package/dist/compliance/index.d.ts +2 -1
- package/dist/compliance/index.js +1 -0
- package/dist/compliance/reverse-index.js +4 -1
- package/dist/compliance/trace.d.ts +26 -0
- package/dist/compliance/trace.js +89 -0
- package/dist/compliance/types.d.ts +94 -1
- package/dist/goals/GoalNode.d.ts +18 -0
- package/dist/goals/GoalNode.js +91 -0
- package/dist/goals/GoalTreeView.d.ts +57 -0
- package/dist/goals/GoalTreeView.js +228 -0
- package/dist/goals/__tests__/GoalTreeView.test.d.ts +1 -0
- package/dist/goals/__tests__/GoalTreeView.test.js +296 -0
- package/dist/goals/index.d.ts +3 -0
- package/dist/goals/index.js +1 -0
- package/dist/goals/theme.d.ts +22 -0
- package/dist/goals/theme.js +32 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/webview/__tests__/render-blocks.test.js +48 -0
- package/dist/webview/notation-style.d.ts +11 -0
- package/dist/webview/notation-style.js +11 -0
- package/dist/webview/render-blocks.js +8 -4
- package/dist/webview/render-goals.js +2 -1
- package/package.json +4 -1
- package/src/activities/__tests__/gantt.test.ts +45 -0
- package/src/capability-map/CapabilityCardNode.tsx +146 -0
- package/src/capability-map/CapabilityMapView.tsx +331 -0
- package/src/capability-map/CapabilityRootNode.tsx +38 -0
- package/src/capability-map/__tests__/CapabilityMapView.test.tsx +232 -0
- package/src/capability-map/__tests__/address.test.ts +59 -0
- package/src/capability-map/__tests__/dsm-layout.test.ts +52 -0
- package/src/capability-map/__tests__/dsm-mutations.test.ts +141 -0
- package/src/capability-map/__tests__/dsm-validate.test.ts +65 -0
- package/src/capability-map/address.ts +48 -0
- package/src/capability-map/dsm-layout.ts +142 -0
- package/src/capability-map/dsm-mutations.ts +208 -0
- package/src/capability-map/dsm-schema.ts +80 -0
- package/src/capability-map/dsm-theme.ts +53 -0
- package/src/capability-map/dsm-validate.ts +103 -0
- package/src/capability-map/index.ts +35 -0
- package/src/compliance/__tests__/trace.test.ts +157 -0
- package/src/compliance/classify.ts +7 -1
- package/src/compliance/index.ts +6 -0
- package/src/compliance/reverse-index.ts +3 -1
- package/src/compliance/trace.ts +118 -0
- package/src/compliance/types.ts +101 -1
- package/src/goals/GoalNode.tsx +212 -0
- package/src/goals/GoalTreeView.tsx +350 -0
- package/src/goals/__tests__/GoalTreeView.test.tsx +313 -0
- package/src/goals/index.ts +4 -0
- package/src/goals/theme.ts +54 -0
- package/src/index.ts +29 -1
- package/src/webview/__tests__/render-blocks.test.ts +54 -0
- package/src/webview/notation-style.ts +12 -0
- package/src/webview/render-blocks.ts +8 -4
- package/src/webview/render-goals.ts +2 -1
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { render, screen, cleanup, waitFor } from '@testing-library/react';
|
|
5
|
+
import { CapabilityMapView, computeDragOutcome } from '../CapabilityMapView.js';
|
|
6
|
+
import type { CapabilityMap } from '../dsm-schema.js';
|
|
7
|
+
import type { CapabilityMapChange } from '../CapabilityMapView.js';
|
|
8
|
+
|
|
9
|
+
// See goals/__tests__/GoalTreeView.test.tsx for why these three shims are
|
|
10
|
+
// needed — reactflow keeps a node `visibility: hidden` (and inert to
|
|
11
|
+
// drag/measurement logic) until ResizeObserver reports its size, and reads
|
|
12
|
+
// the computed transform back out via DOMMatrixReadOnly; jsdom has neither.
|
|
13
|
+
class MockResizeObserver {
|
|
14
|
+
#callback: ResizeObserverCallback;
|
|
15
|
+
constructor(callback: ResizeObserverCallback) {
|
|
16
|
+
this.#callback = callback;
|
|
17
|
+
}
|
|
18
|
+
observe(target: Element) {
|
|
19
|
+
queueMicrotask(() => {
|
|
20
|
+
this.#callback([{ target, contentRect: target.getBoundingClientRect() } as ResizeObserverEntry], this as unknown as ResizeObserver);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
unobserve() {}
|
|
24
|
+
disconnect() {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class MockDOMMatrixReadOnly {
|
|
28
|
+
m41 = 0;
|
|
29
|
+
m42 = 0;
|
|
30
|
+
constructor(transform?: string) {
|
|
31
|
+
if (!transform || transform === 'none') return;
|
|
32
|
+
const t = /translate\((-?[\d.]+)px,\s*(-?[\d.]+)px\)/.exec(transform);
|
|
33
|
+
if (t) {
|
|
34
|
+
this.m41 = Number(t[1]);
|
|
35
|
+
this.m42 = Number(t[2]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function makeMap(): CapabilityMap {
|
|
41
|
+
return {
|
|
42
|
+
organisation: 'Acme Corp',
|
|
43
|
+
set_id: 'v1.0',
|
|
44
|
+
capabilities: [
|
|
45
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0', maturity: [{ date: '2026-04-01', level: 3 }] },
|
|
46
|
+
{ id: 2, name: 'Lead Qualification', address: '1.1.0' },
|
|
47
|
+
{ id: 3, name: 'Inbound Lead Scoring', address: '1.1.1' },
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
(global as unknown as { ResizeObserver: unknown }).ResizeObserver = MockResizeObserver;
|
|
54
|
+
(window as unknown as { DOMMatrixReadOnly: unknown }).DOMMatrixReadOnly = MockDOMMatrixReadOnly;
|
|
55
|
+
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) {
|
|
56
|
+
const isNode = this.classList?.contains('react-flow__node');
|
|
57
|
+
const width = isNode ? 250 : 1000;
|
|
58
|
+
const height = isNode ? 64 : 800;
|
|
59
|
+
return { width, height, top: 0, left: 0, right: width, bottom: height, x: 0, y: 0, toJSON() { return this; } } as DOMRect;
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
afterEach(() => {
|
|
64
|
+
cleanup();
|
|
65
|
+
vi.restoreAllMocks();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('CapabilityMapView', () => {
|
|
69
|
+
it('renders the virtual root and every on-diagram capability', async () => {
|
|
70
|
+
render(<CapabilityMapView map={makeMap()} />);
|
|
71
|
+
await waitFor(() => {
|
|
72
|
+
expect(screen.getByText('Acme Corp')).toBeTruthy();
|
|
73
|
+
expect(screen.getByText('Customer Acquisition')).toBeTruthy();
|
|
74
|
+
expect(screen.getByText('Lead Qualification')).toBeTruthy();
|
|
75
|
+
expect(screen.getByText('Inbound Lead Scoring')).toBeTruthy();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('renders a maturity dot only for capabilities with a maturity snapshot', async () => {
|
|
80
|
+
render(<CapabilityMapView map={makeMap()} />);
|
|
81
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
82
|
+
expect(screen.getByLabelText('Maturity 3')).toBeTruthy();
|
|
83
|
+
expect(screen.queryByLabelText(/Maturity/, { selector: '[aria-label="Maturity undefined"]' })).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('shows backlog capabilities in the sidebar, not on canvas', async () => {
|
|
87
|
+
const map: CapabilityMap = {
|
|
88
|
+
organisation: 'Acme Corp',
|
|
89
|
+
set_id: 'v1.0',
|
|
90
|
+
capabilities: [
|
|
91
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
92
|
+
{ id: 2, name: 'Parked idea', address: '0.0.0', backlog: true },
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
render(<CapabilityMapView map={map} showBacklog />);
|
|
96
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
97
|
+
expect(screen.getByText('Backlog')).toBeTruthy();
|
|
98
|
+
expect(screen.getByText('Parked idea')).toBeTruthy();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('does not render add/delete affordances when readOnly', async () => {
|
|
102
|
+
render(<CapabilityMapView map={makeMap()} readOnly />);
|
|
103
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
104
|
+
expect(screen.queryByLabelText('Add child capability')).toBeNull();
|
|
105
|
+
expect(screen.queryByLabelText('Delete capability')).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('fires onChange with kind "addChild" when the add-child button is clicked', async () => {
|
|
109
|
+
const onChange = vi.fn<(e: CapabilityMapChange) => void>();
|
|
110
|
+
render(<CapabilityMapView map={makeMap()} onChange={onChange} />);
|
|
111
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
112
|
+
const rootCard = document.querySelector('.react-flow__node[data-id="1"]')!;
|
|
113
|
+
rootCard.querySelector<HTMLElement>('[aria-label="Add child capability"]')!
|
|
114
|
+
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
115
|
+
|
|
116
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
117
|
+
const event = onChange.mock.calls[0][0];
|
|
118
|
+
expect(event.kind).toBe('addChild');
|
|
119
|
+
if (event.kind === 'addChild') {
|
|
120
|
+
expect(event.newCap.address).toBe('1.2.0');
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('fires onChange with kind "delete" when the delete button is clicked', async () => {
|
|
125
|
+
const onChange = vi.fn<(e: CapabilityMapChange) => void>();
|
|
126
|
+
render(<CapabilityMapView map={makeMap()} onChange={onChange} />);
|
|
127
|
+
await waitFor(() => expect(screen.getByText('Inbound Lead Scoring')).toBeTruthy());
|
|
128
|
+
const leafCard = document.querySelector('.react-flow__node[data-id="3"]')!;
|
|
129
|
+
leafCard.querySelector<HTMLElement>('[aria-label="Delete capability"]')!
|
|
130
|
+
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
131
|
+
|
|
132
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
133
|
+
expect(onChange.mock.calls[0][0].kind).toBe('delete');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('fires onEditRequest on double-click', async () => {
|
|
137
|
+
const onEditRequest = vi.fn();
|
|
138
|
+
render(<CapabilityMapView map={makeMap()} onEditRequest={onEditRequest} />);
|
|
139
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
140
|
+
document.querySelector('.react-flow__node[data-id="1"]')!
|
|
141
|
+
.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }));
|
|
142
|
+
|
|
143
|
+
await waitFor(() => expect(onEditRequest).toHaveBeenCalledTimes(1));
|
|
144
|
+
expect(onEditRequest.mock.calls[0][0]).toMatchObject({ id: 1, name: 'Customer Acquisition' });
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('fires onChange with kind "normaliseAddresses" from the toolbar button', async () => {
|
|
148
|
+
const onChange = vi.fn<(e: CapabilityMapChange) => void>();
|
|
149
|
+
render(<CapabilityMapView map={makeMap()} onChange={onChange} />);
|
|
150
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
151
|
+
screen.getByText('Normalise addresses').dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
152
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
153
|
+
expect(onChange.mock.calls[0][0].kind).toBe('normaliseAddresses');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('fires onChange with kind "restoreFromBacklog" when a backlog card is dropped onto a canvas card', async () => {
|
|
157
|
+
const map: CapabilityMap = {
|
|
158
|
+
organisation: 'Acme Corp',
|
|
159
|
+
set_id: 'v1.0',
|
|
160
|
+
capabilities: [
|
|
161
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
162
|
+
{ id: 2, name: 'Parked idea', address: '0.0.0', backlog: true },
|
|
163
|
+
],
|
|
164
|
+
};
|
|
165
|
+
const onChange = vi.fn<(e: CapabilityMapChange) => void>();
|
|
166
|
+
render(<CapabilityMapView map={map} onChange={onChange} showBacklog />);
|
|
167
|
+
await waitFor(() => expect(screen.getByText('Parked idea')).toBeTruthy());
|
|
168
|
+
|
|
169
|
+
const store = new Map<string, string>();
|
|
170
|
+
const dataTransfer = { setData: (k: string, v: string) => store.set(k, v), getData: (k: string) => store.get(k) ?? '', effectAllowed: 'move' };
|
|
171
|
+
function fireDnd(el: Element, type: string, coords: { clientX: number; clientY: number }) {
|
|
172
|
+
const event = new Event(type, { bubbles: true, cancelable: true });
|
|
173
|
+
Object.defineProperty(event, 'dataTransfer', { value: dataTransfer });
|
|
174
|
+
Object.assign(event, coords);
|
|
175
|
+
el.dispatchEvent(event);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const backlogItem = screen.getByText('Parked idea').closest('div[draggable]')!;
|
|
179
|
+
const rootCard = document.querySelector('.react-flow__node[data-id="1"]')!;
|
|
180
|
+
document.elementFromPoint = vi.fn().mockReturnValue(rootCard);
|
|
181
|
+
|
|
182
|
+
fireDnd(backlogItem, 'dragstart', { clientX: 0, clientY: 0 });
|
|
183
|
+
fireDnd(rootCard, 'drop', { clientX: 10, clientY: 10 });
|
|
184
|
+
|
|
185
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
186
|
+
const event = onChange.mock.calls[0][0];
|
|
187
|
+
expect(event.kind).toBe('restoreFromBacklog');
|
|
188
|
+
if (event.kind === 'restoreFromBacklog') {
|
|
189
|
+
expect(event.id).toBe(2);
|
|
190
|
+
expect(event.parentId).toBe(1);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Dragging a card is choreographed by reactflow's own pointer/DOM-measurement
|
|
196
|
+
// machinery — see the equivalent note in goals/__tests__/GoalTreeView.test.tsx.
|
|
197
|
+
// computeDragOutcome() is the pure decision this component's onNodeDragStop
|
|
198
|
+
// calls once reactflow reports a drag, tested directly here.
|
|
199
|
+
describe('computeDragOutcome (reparent / moveBranchToBacklog decision logic)', () => {
|
|
200
|
+
const map = makeMap();
|
|
201
|
+
|
|
202
|
+
it('fires a "reparent" change when dropped onto another card', () => {
|
|
203
|
+
const change = computeDragOutcome(map, 3, { kind: 'node', id: 1 });
|
|
204
|
+
expect(change?.kind).toBe('reparent');
|
|
205
|
+
if (change?.kind === 'reparent') {
|
|
206
|
+
expect(change.sourceId).toBe(3);
|
|
207
|
+
expect(change.targetId).toBe(1);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('fires a "reparent" change (promotion to L1) when dropped on the root', () => {
|
|
212
|
+
const change = computeDragOutcome(map, 2, { kind: 'root' });
|
|
213
|
+
expect(change?.kind).toBe('reparent');
|
|
214
|
+
if (change?.kind === 'reparent') {
|
|
215
|
+
expect(change.targetId).toBe(0);
|
|
216
|
+
expect(change.result.capabilities.find((c) => c.id === 2)?.address).toBe('2.0.0');
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('fires a "moveBranchToBacklog" change when dropped on the backlog zone', () => {
|
|
221
|
+
const change = computeDragOutcome(map, 2, { kind: 'backlog' });
|
|
222
|
+
expect(change?.kind).toBe('moveBranchToBacklog');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('is a no-op when dropped nowhere in particular', () => {
|
|
226
|
+
expect(computeDragOutcome(map, 2, null)).toBeNull();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('is a no-op when dropped on itself', () => {
|
|
230
|
+
expect(computeDragOutcome(map, 2, { kind: 'node', id: 2 })).toBeNull();
|
|
231
|
+
});
|
|
232
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { parseAddress, formatAddress, getLevel, getParentAddress, getFirstFreeAddress, isAddressTaken } from '../address.js';
|
|
3
|
+
import type { Capability } from '../dsm-schema.js';
|
|
4
|
+
|
|
5
|
+
function cap(id: number, address: string, backlog = false): Capability {
|
|
6
|
+
return { id, name: `Cap ${id}`, address, backlog };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('parseAddress / formatAddress', () => {
|
|
10
|
+
it('round-trips X.Y.Z', () => {
|
|
11
|
+
for (const addr of ['1.0.0', '1.2.0', '1.2.3', '0.0.0', '42.7.19']) {
|
|
12
|
+
expect(formatAddress(parseAddress(addr))).toBe(addr);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('throws on malformed input', () => {
|
|
17
|
+
expect(() => parseAddress('1.2')).toThrow();
|
|
18
|
+
expect(() => parseAddress('1.2.3.4')).toThrow();
|
|
19
|
+
expect(() => parseAddress('a.b.c')).toThrow();
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('getLevel', () => {
|
|
24
|
+
it('classifies each address shape', () => {
|
|
25
|
+
expect(getLevel('1.0.0')).toBe(1);
|
|
26
|
+
expect(getLevel('1.2.0')).toBe(2);
|
|
27
|
+
expect(getLevel('1.2.3')).toBe(3);
|
|
28
|
+
expect(getLevel('0.0.0')).toBe('backlog');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('getParentAddress', () => {
|
|
33
|
+
it('walks up one level at a time', () => {
|
|
34
|
+
expect(getParentAddress('1.2.3')).toBe('1.2.0');
|
|
35
|
+
expect(getParentAddress('1.2.0')).toBe('1.0.0');
|
|
36
|
+
expect(getParentAddress('1.0.0')).toBeNull();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('getFirstFreeAddress / isAddressTaken', () => {
|
|
41
|
+
it('finds the first unused L2 slot under an L1 parent', () => {
|
|
42
|
+
const caps = [cap(1, '1.0.0'), cap(2, '1.1.0'), cap(3, '1.2.0')];
|
|
43
|
+
expect(getFirstFreeAddress('1.0.0', caps)).toBe('1.3.0');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('finds the first unused L3 slot under an L2 parent', () => {
|
|
47
|
+
const caps = [cap(1, '1.0.0'), cap(2, '1.1.0'), cap(3, '1.1.1')];
|
|
48
|
+
expect(getFirstFreeAddress('1.1.0', caps)).toBe('1.1.2');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('ignores backlog entries when checking occupancy', () => {
|
|
52
|
+
const caps = [cap(1, '1.0.0'), cap(2, '0.0.0', true)];
|
|
53
|
+
expect(isAddressTaken('1.1.0', caps)).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('rejects an L3 address as a parent', () => {
|
|
57
|
+
expect(() => getFirstFreeAddress('1.1.1', [])).toThrow();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { layoutCapabilityMap } from '../dsm-layout.js';
|
|
3
|
+
import type { CapabilityMap } from '../dsm-schema.js';
|
|
4
|
+
|
|
5
|
+
function map(): CapabilityMap {
|
|
6
|
+
return {
|
|
7
|
+
organisation: 'Acme Corp',
|
|
8
|
+
set_id: 'v1.0',
|
|
9
|
+
capabilities: [
|
|
10
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
11
|
+
{ id: 2, name: 'Lead Qualification', address: '1.1.0' },
|
|
12
|
+
{ id: 3, name: 'Inbound Lead Scoring', address: '1.1.1' },
|
|
13
|
+
{ id: 4, name: 'Order Management', address: '2.0.0' },
|
|
14
|
+
{ id: 5, name: 'Parked idea', address: '0.0.0', backlog: true },
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('layoutCapabilityMap', () => {
|
|
20
|
+
it('lays out a virtual root plus every on-diagram capability, one column per level', () => {
|
|
21
|
+
const layout = layoutCapabilityMap(map());
|
|
22
|
+
expect(layout.rootNode.data).toBeNull();
|
|
23
|
+
expect(layout.nodes).toHaveLength(4); // backlog entry excluded
|
|
24
|
+
const byId = new Map(layout.nodes.map((n) => [n.id, n]));
|
|
25
|
+
expect(byId.get(1)!.x).toBe(byId.get(4)!.x); // both L1s share a column
|
|
26
|
+
expect(byId.get(2)!.x).toBeGreaterThan(byId.get(1)!.x); // L2 one column further right
|
|
27
|
+
expect(byId.get(3)!.x).toBeGreaterThan(byId.get(2)!.x); // L3 further still
|
|
28
|
+
expect(byId.get(1)!.x).toBeGreaterThan(layout.rootNode.x); // root sits left of the L1s
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('connects the root to every L1 and each parent to its children', () => {
|
|
32
|
+
const layout = layoutCapabilityMap(map());
|
|
33
|
+
const edgeSet = new Set(layout.edges.map((e) => `${e.source}->${e.target}`));
|
|
34
|
+
expect(edgeSet.has('root->1')).toBe(true);
|
|
35
|
+
expect(edgeSet.has('root->4')).toBe(true);
|
|
36
|
+
expect(edgeSet.has('1->2')).toBe(true);
|
|
37
|
+
expect(edgeSet.has('2->3')).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('marks a node as having hidden children when its subtree is collapsed', () => {
|
|
41
|
+
const layout = layoutCapabilityMap(map(), { hideCollapsed: [2] });
|
|
42
|
+
const cap2 = layout.nodes.find((n) => n.id === 2)!;
|
|
43
|
+
expect(cap2.hasHiddenChildren).toBe(true);
|
|
44
|
+
expect(layout.nodes.some((n) => n.id === 3)).toBe(false); // hidden subtree omitted
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('returns just the root when there are no on-diagram capabilities', () => {
|
|
48
|
+
const layout = layoutCapabilityMap({ organisation: 'Empty Co', set_id: 'v1', capabilities: [] });
|
|
49
|
+
expect(layout.nodes).toHaveLength(0);
|
|
50
|
+
expect(layout.rootNode.data).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { reparent, addChild, deleteWithDescendants, moveBranchToBacklog, restoreFromBacklog, normaliseAddresses } from '../dsm-mutations.js';
|
|
3
|
+
import type { CapabilityMap } from '../dsm-schema.js';
|
|
4
|
+
|
|
5
|
+
function baseMap(): CapabilityMap {
|
|
6
|
+
return {
|
|
7
|
+
organisation: 'Acme Corp',
|
|
8
|
+
set_id: 'v1.0',
|
|
9
|
+
capabilities: [
|
|
10
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
11
|
+
{ id: 2, name: 'Lead Qualification', address: '1.1.0' },
|
|
12
|
+
{ id: 3, name: 'Inbound Lead Scoring', address: '1.1.1' },
|
|
13
|
+
{ id: 4, name: 'Order Management', address: '2.0.0' },
|
|
14
|
+
],
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('reparent', () => {
|
|
19
|
+
it('moves an L3 under a different L2 (no cascade)', () => {
|
|
20
|
+
const map = baseMap();
|
|
21
|
+
const r = reparent(map, 3, 4); // Inbound Lead Scoring (L3) -> Order Management (L1)... newLevel would be 2
|
|
22
|
+
expect(r.ok).toBe(true);
|
|
23
|
+
const moved = r.result!.capabilities.find((c) => c.id === 3)!;
|
|
24
|
+
expect(moved.address).toBe('2.1.0'); // now an L2 under capability 4
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('promotes an L2 to L1 and cascades its L3 children to L2 (spec §9)', () => {
|
|
28
|
+
const map = baseMap();
|
|
29
|
+
const r = reparent(map, 2, 0); // Lead Qualification (L2, id 2) -> virtual root
|
|
30
|
+
expect(r.ok).toBe(true);
|
|
31
|
+
const promoted = r.result!.capabilities.find((c) => c.id === 2)!;
|
|
32
|
+
const child = r.result!.capabilities.find((c) => c.id === 3)!;
|
|
33
|
+
expect(promoted.address).toBe('3.0.0'); // first free L1 slot (1 and 2 taken)
|
|
34
|
+
expect(child.address).toBe('3.1.0'); // cascaded from L3 to L2 under the new L1
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('refuses a demotion that would push a grandchild past L3', () => {
|
|
38
|
+
const map = baseMap();
|
|
39
|
+
// Capability 1 (L1) has descendant 2 (L2) and 3 (L3, a grandchild).
|
|
40
|
+
// Demoting 1 under 4 (making 1 an L2) would need 3 to become L4.
|
|
41
|
+
const r = reparent(map, 1, 4);
|
|
42
|
+
expect(r.ok).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('refuses reparenting onto its own descendant (cycle)', () => {
|
|
46
|
+
const map = baseMap();
|
|
47
|
+
const r = reparent(map, 2, 3); // Lead Qualification onto its own child
|
|
48
|
+
expect(r.ok).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('refuses reparenting onto itself', () => {
|
|
52
|
+
const map = baseMap();
|
|
53
|
+
expect(reparent(map, 1, 1).ok).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('leaves the original map untouched', () => {
|
|
57
|
+
const map = baseMap();
|
|
58
|
+
reparent(map, 3, 4);
|
|
59
|
+
expect(map.capabilities.find((c) => c.id === 3)!.address).toBe('1.1.1');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('addChild', () => {
|
|
64
|
+
it('assigns the first free address under the parent', () => {
|
|
65
|
+
const map = baseMap();
|
|
66
|
+
const r = addChild(map, 1, { name: 'New L2' });
|
|
67
|
+
expect(r.ok).toBe(true);
|
|
68
|
+
const added = r.result!.capabilities.find((c) => c.name === 'New L2')!;
|
|
69
|
+
expect(added.address).toBe('1.2.0');
|
|
70
|
+
expect(added.backlog).toBe(false);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('assigns a new L1 address when parentId is 0 (root)', () => {
|
|
74
|
+
const map = baseMap();
|
|
75
|
+
const r = addChild(map, 0, { name: 'New L1' });
|
|
76
|
+
expect(r.ok).toBe(true);
|
|
77
|
+
expect(r.result!.capabilities.find((c) => c.name === 'New L1')!.address).toBe('3.0.0');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('refuses adding a child under an L3 (would exceed max depth)', () => {
|
|
81
|
+
const map = baseMap();
|
|
82
|
+
const r = addChild(map, 3, { name: 'Too deep' });
|
|
83
|
+
expect(r.ok).toBe(false);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('deleteWithDescendants', () => {
|
|
88
|
+
it('removes the capability and every descendant', () => {
|
|
89
|
+
const map = baseMap();
|
|
90
|
+
const r = deleteWithDescendants(map, 2); // Lead Qualification + its L3 child
|
|
91
|
+
expect(r.ok).toBe(true);
|
|
92
|
+
const ids = r.result!.capabilities.map((c) => c.id);
|
|
93
|
+
expect(ids).not.toContain(2);
|
|
94
|
+
expect(ids).not.toContain(3);
|
|
95
|
+
expect(ids).toContain(1);
|
|
96
|
+
expect(ids).toContain(4);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('moveBranchToBacklog / restoreFromBacklog', () => {
|
|
101
|
+
it('moves a branch to the backlog and back', () => {
|
|
102
|
+
const map = baseMap();
|
|
103
|
+
const toBacklog = moveBranchToBacklog(map, 2);
|
|
104
|
+
expect(toBacklog.ok).toBe(true);
|
|
105
|
+
const backlogged = toBacklog.result!.capabilities.filter((c) => c.id === 2 || c.id === 3);
|
|
106
|
+
expect(backlogged.every((c) => c.backlog && c.address === '0.0.0')).toBe(true);
|
|
107
|
+
|
|
108
|
+
const restored = restoreFromBacklog(toBacklog.result!, 2, 4);
|
|
109
|
+
expect(restored.ok).toBe(true);
|
|
110
|
+
const cap2 = restored.result!.capabilities.find((c) => c.id === 2)!;
|
|
111
|
+
expect(cap2.backlog).toBe(false);
|
|
112
|
+
expect(cap2.address).toBe('2.1.0');
|
|
113
|
+
// Restoring the parent doesn't automatically restore its backlogged
|
|
114
|
+
// children — matches deleteWithDescendants/addChild's scope (this
|
|
115
|
+
// mutation acts on the single named id only).
|
|
116
|
+
expect(restored.result!.capabilities.find((c) => c.id === 3)!.backlog).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('normaliseAddresses', () => {
|
|
121
|
+
it('moves a capability with an unresolvable parent address to the backlog', () => {
|
|
122
|
+
const map: CapabilityMap = {
|
|
123
|
+
organisation: 'Acme Corp',
|
|
124
|
+
set_id: 'v1.0',
|
|
125
|
+
capabilities: [
|
|
126
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
127
|
+
{ id: 2, name: 'Dangling L2', address: '9.1.0' }, // no capability at 9.0.0
|
|
128
|
+
],
|
|
129
|
+
};
|
|
130
|
+
const result = normaliseAddresses(map);
|
|
131
|
+
const dangling = result.capabilities.find((c) => c.id === 2)!;
|
|
132
|
+
expect(dangling.backlog).toBe(true);
|
|
133
|
+
expect(dangling.address).toBe('0.0.0');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('leaves a well-formed hierarchy unchanged', () => {
|
|
137
|
+
const map = baseMap();
|
|
138
|
+
const result = normaliseAddresses(map);
|
|
139
|
+
expect(result.capabilities).toEqual(map.capabilities);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { validateCapabilityMapData } from '../dsm-validate.js';
|
|
3
|
+
import type { CapabilityMap } from '../dsm-schema.js';
|
|
4
|
+
|
|
5
|
+
function validMap(): CapabilityMap {
|
|
6
|
+
return {
|
|
7
|
+
organisation: 'Acme Corp',
|
|
8
|
+
set_id: 'v1.0',
|
|
9
|
+
capabilities: [
|
|
10
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
11
|
+
{ id: 2, name: 'Lead Qualification', address: '1.1.0' },
|
|
12
|
+
{ id: 3, name: 'Inbound Lead Scoring', address: '1.1.1' },
|
|
13
|
+
],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('validateCapabilityMapData', () => {
|
|
18
|
+
it('accepts a well-formed map', () => {
|
|
19
|
+
expect(validateCapabilityMapData(validMap()).valid).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('flags a duplicate id', () => {
|
|
23
|
+
const map = validMap();
|
|
24
|
+
map.capabilities[1].id = 1;
|
|
25
|
+
const result = validateCapabilityMapData(map);
|
|
26
|
+
expect(result.valid).toBe(false);
|
|
27
|
+
expect(result.errors.some((e) => e.code === 'DUPLICATE_ID')).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('flags a malformed address', () => {
|
|
31
|
+
const map = validMap();
|
|
32
|
+
map.capabilities[0].address = 'not-an-address';
|
|
33
|
+
const result = validateCapabilityMapData(map);
|
|
34
|
+
expect(result.errors.some((e) => e.code === 'INVALID_ADDRESS_FORMAT')).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('flags a too-deep address as MAX_DEPTH_EXCEEDED, not INVALID_ADDRESS_FORMAT', () => {
|
|
38
|
+
const map = validMap();
|
|
39
|
+
map.capabilities[0].address = '1.2.3.4';
|
|
40
|
+
const result = validateCapabilityMapData(map);
|
|
41
|
+
expect(result.errors.some((e) => e.code === 'MAX_DEPTH_EXCEEDED')).toBe(true);
|
|
42
|
+
expect(result.errors.some((e) => e.code === 'INVALID_ADDRESS_FORMAT')).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('flags a duplicate on-diagram address', () => {
|
|
46
|
+
const map = validMap();
|
|
47
|
+
map.capabilities[1].address = '1.0.0';
|
|
48
|
+
const result = validateCapabilityMapData(map);
|
|
49
|
+
expect(result.errors.some((e) => e.code === 'DUPLICATE_ADDRESS')).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('warns on a missing parent by address', () => {
|
|
53
|
+
const map = validMap();
|
|
54
|
+
map.capabilities[1].address = '9.1.0'; // no capability at 9.0.0
|
|
55
|
+
const result = validateCapabilityMapData(map);
|
|
56
|
+
expect(result.warnings.some((w) => w.code === 'MISSING_PARENT_BY_ADDRESS')).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('does not flag a backlog entry\'s address as a duplicate or requiring a parent', () => {
|
|
60
|
+
const map = validMap();
|
|
61
|
+
map.capabilities.push({ id: 4, name: 'Parked idea', address: '0.0.0', backlog: true });
|
|
62
|
+
const result = validateCapabilityMapData(map);
|
|
63
|
+
expect(result.valid).toBe(true);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Capability } from './dsm-schema.js';
|
|
2
|
+
|
|
3
|
+
/** 'X.Y.Z' -> [X, Y, Z]. Throws on malformed input — callers on untrusted
|
|
4
|
+
* data should validate with validateCapabilityMapData first. */
|
|
5
|
+
export function parseAddress(address: string): [number, number, number] {
|
|
6
|
+
const m = /^(\d+)\.(\d+)\.(\d+)$/.exec(address.trim());
|
|
7
|
+
if (!m) throw new Error(`parseAddress: expected 'X.Y.Z', got "${address}"`);
|
|
8
|
+
return [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function formatAddress(triple: [number, number, number]): string {
|
|
12
|
+
return triple.join('.');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** X.0.0 -> L1, X.Y.0 -> L2, X.Y.Z -> L3, 0.0.0 -> backlog. */
|
|
16
|
+
export function getLevel(address: string): 1 | 2 | 3 | 'backlog' {
|
|
17
|
+
const [x, y, z] = parseAddress(address);
|
|
18
|
+
if (x === 0) return 'backlog';
|
|
19
|
+
if (y === 0) return 1;
|
|
20
|
+
if (z === 0) return 2;
|
|
21
|
+
return 3;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** '1.2.3' -> '1.2.0'; '1.2.0' -> '1.0.0'; '1.0.0' -> null (L1 has no parent). */
|
|
25
|
+
export function getParentAddress(address: string): string | null {
|
|
26
|
+
const [x, y, z] = parseAddress(address);
|
|
27
|
+
if (z !== 0) return formatAddress([x, y, 0]);
|
|
28
|
+
if (y !== 0) return formatAddress([x, 0, 0]);
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** First address under `parent` (an L1 or L2 address) not already taken. */
|
|
33
|
+
export function getFirstFreeAddress(parent: string, capabilities: Capability[]): string {
|
|
34
|
+
const [x, y] = parseAddress(parent);
|
|
35
|
+
const level = getLevel(parent);
|
|
36
|
+
if (level !== 1 && level !== 2) {
|
|
37
|
+
throw new Error(`getFirstFreeAddress: parent must be an L1 or L2 address, got "${parent}" (${level})`);
|
|
38
|
+
}
|
|
39
|
+
for (let n = 1; n < 1000; n++) {
|
|
40
|
+
const candidate = level === 1 ? formatAddress([x, n, 0]) : formatAddress([x, y, n]);
|
|
41
|
+
if (!isAddressTaken(candidate, capabilities)) return candidate;
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`getFirstFreeAddress: no free address under "${parent}" (searched 1..999)`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isAddressTaken(address: string, capabilities: Capability[]): boolean {
|
|
47
|
+
return capabilities.some((c) => !c.backlog && c.address === address);
|
|
48
|
+
}
|