@transitrix/diagrams 1.5.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/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/package.json +4 -1
- 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
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { Capability } from './dsm-schema.js';
|
|
3
|
+
import type { ThemeTokens } from './dsm-theme.js';
|
|
4
|
+
export interface CapabilityCardNodeData {
|
|
5
|
+
capability: Capability;
|
|
6
|
+
theme: Required<ThemeTokens>;
|
|
7
|
+
maturityColours: Record<number, string>;
|
|
8
|
+
readOnly: boolean;
|
|
9
|
+
isDropTarget: boolean;
|
|
10
|
+
hasHiddenChildren: boolean;
|
|
11
|
+
isCollapsed: boolean;
|
|
12
|
+
onAddChild: (parentId: number) => void;
|
|
13
|
+
onDelete: (id: number) => void;
|
|
14
|
+
onToggleCollapse: (id: number) => void;
|
|
15
|
+
}
|
|
16
|
+
declare const CapabilityCardNode: React.MemoExoticComponent<({ data }: {
|
|
17
|
+
data: CapabilityCardNodeData;
|
|
18
|
+
}) => import("react/jsx-runtime").JSX.Element>;
|
|
19
|
+
export default CapabilityCardNode;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { memo } from 'react';
|
|
3
|
+
import { Handle, Position } from 'reactflow';
|
|
4
|
+
const CARD_WIDTH = 250;
|
|
5
|
+
const CARD_HEIGHT = 64;
|
|
6
|
+
function latestMaturityLevel(cap) {
|
|
7
|
+
if (!cap.maturity || cap.maturity.length === 0)
|
|
8
|
+
return undefined;
|
|
9
|
+
return [...cap.maturity].sort((a, b) => b.date.localeCompare(a.date))[0].level;
|
|
10
|
+
}
|
|
11
|
+
const CapabilityCardNode = memo(({ data }) => {
|
|
12
|
+
const { capability, theme, maturityColours, readOnly, isDropTarget, hasHiddenChildren, isCollapsed } = data;
|
|
13
|
+
const maturity = latestMaturityLevel(capability);
|
|
14
|
+
const dotColour = maturity !== undefined ? maturityColours[maturity] : undefined;
|
|
15
|
+
return (_jsxs("div", { style: {
|
|
16
|
+
position: 'relative',
|
|
17
|
+
width: CARD_WIDTH,
|
|
18
|
+
height: CARD_HEIGHT,
|
|
19
|
+
background: theme.cardFill,
|
|
20
|
+
borderRadius: 8,
|
|
21
|
+
borderStyle: 'solid',
|
|
22
|
+
borderWidth: isDropTarget ? 2 : theme.cardBorderWidth,
|
|
23
|
+
borderColor: isDropTarget ? theme.dropTargetBorderColor : theme.cardBorderColor,
|
|
24
|
+
boxShadow: '0 1px 4px rgba(0,0,0,0.12)',
|
|
25
|
+
padding: '6px 10px',
|
|
26
|
+
display: 'flex',
|
|
27
|
+
flexDirection: 'column',
|
|
28
|
+
justifyContent: 'center',
|
|
29
|
+
}, children: [_jsx(Handle, { type: "target", position: Position.Left, style: { opacity: 0 } }), _jsx(Handle, { type: "source", position: Position.Right, style: { opacity: 0 } }), dotColour && (_jsx("span", { title: `Maturity ${maturity}`, "aria-label": `Maturity ${maturity}`, style: {
|
|
30
|
+
position: 'absolute', top: 6, left: 6, width: 8, height: 8, borderRadius: '50%', background: dotColour,
|
|
31
|
+
} })), _jsx("div", { title: capability.name, style: {
|
|
32
|
+
fontWeight: 600,
|
|
33
|
+
fontSize: 12,
|
|
34
|
+
color: theme.cardTextColor,
|
|
35
|
+
marginLeft: dotColour ? 12 : 0,
|
|
36
|
+
overflow: 'hidden',
|
|
37
|
+
display: '-webkit-box',
|
|
38
|
+
WebkitLineClamp: 2,
|
|
39
|
+
WebkitBoxOrient: 'vertical',
|
|
40
|
+
wordBreak: 'break-word',
|
|
41
|
+
lineHeight: 1.2,
|
|
42
|
+
}, children: capability.name }), _jsx("div", { style: { fontSize: 10, color: theme.cardMetaColor, marginTop: 2, marginLeft: dotColour ? 12 : 0 }, children: capability.address }), !readOnly && (_jsx("button", { type: "button", "aria-label": "Add child capability", title: "Add child capability", onClick: (e) => {
|
|
43
|
+
e.stopPropagation();
|
|
44
|
+
data.onAddChild(capability.id);
|
|
45
|
+
}, style: {
|
|
46
|
+
position: 'absolute', top: -8, right: -8, width: 16, height: 16,
|
|
47
|
+
borderRadius: '50%', border: '1px solid #86efac', background: '#ffffff',
|
|
48
|
+
color: '#16a34a', fontSize: 11, lineHeight: '14px', padding: 0, cursor: 'pointer',
|
|
49
|
+
}, children: "+" })), !readOnly && (_jsx("button", { type: "button", "aria-label": "Delete capability", title: "Delete capability and descendants", onClick: (e) => {
|
|
50
|
+
e.stopPropagation();
|
|
51
|
+
data.onDelete(capability.id);
|
|
52
|
+
}, style: {
|
|
53
|
+
position: 'absolute', bottom: -8, right: -8, width: 16, height: 16,
|
|
54
|
+
borderRadius: '50%', border: '1px solid #fca5a5', background: '#ffffff',
|
|
55
|
+
color: '#dc2626', fontSize: 11, lineHeight: '14px', padding: 0, cursor: 'pointer',
|
|
56
|
+
}, children: "\u00D7" })), hasHiddenChildren && (_jsx("button", { type: "button", "aria-label": isCollapsed ? 'Expand branch' : 'Collapse branch', title: isCollapsed ? 'Expand branch' : 'Collapse branch', onClick: (e) => {
|
|
57
|
+
e.stopPropagation();
|
|
58
|
+
data.onToggleCollapse(capability.id);
|
|
59
|
+
}, style: {
|
|
60
|
+
position: 'absolute', top: '50%', right: -8, transform: 'translateY(-50%)',
|
|
61
|
+
width: 16, height: 16, borderRadius: '50%', border: `1px solid ${theme.cardBorderColor}`,
|
|
62
|
+
background: '#ffffff', color: theme.cardBorderColor, fontSize: 11, lineHeight: '14px', padding: 0, cursor: 'pointer',
|
|
63
|
+
}, children: isCollapsed ? '+' : '−' }))] }));
|
|
64
|
+
});
|
|
65
|
+
CapabilityCardNode.displayName = 'CapabilityCardNode';
|
|
66
|
+
export default CapabilityCardNode;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import 'reactflow/dist/style.css';
|
|
3
|
+
import type { Capability, CapabilityMap, CapabilityMapLayout, LayoutOptions } from './dsm-schema.js';
|
|
4
|
+
import type { ThemeTokens } from './dsm-theme.js';
|
|
5
|
+
export type { ThemeTokens } from './dsm-theme.js';
|
|
6
|
+
export interface CapabilityMapViewProps {
|
|
7
|
+
map: CapabilityMap;
|
|
8
|
+
layout?: CapabilityMapLayout;
|
|
9
|
+
layoutOptions?: LayoutOptions;
|
|
10
|
+
theme?: ThemeTokens;
|
|
11
|
+
readOnly?: boolean;
|
|
12
|
+
showBacklog?: boolean;
|
|
13
|
+
showMiniMap?: boolean;
|
|
14
|
+
maturityColours?: Record<number, string>;
|
|
15
|
+
selectedSet?: string;
|
|
16
|
+
onChange?: (event: CapabilityMapChange) => void;
|
|
17
|
+
onEditRequest?: (cap: Capability) => void;
|
|
18
|
+
}
|
|
19
|
+
export type CapabilityMapChange = {
|
|
20
|
+
kind: 'reparent';
|
|
21
|
+
sourceId: number;
|
|
22
|
+
targetId: number;
|
|
23
|
+
result: CapabilityMap;
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'addChild';
|
|
26
|
+
parentId: number;
|
|
27
|
+
newCap: Capability;
|
|
28
|
+
result: CapabilityMap;
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'delete';
|
|
31
|
+
id: number;
|
|
32
|
+
result: CapabilityMap;
|
|
33
|
+
} | {
|
|
34
|
+
kind: 'moveBranchToBacklog';
|
|
35
|
+
id: number;
|
|
36
|
+
result: CapabilityMap;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'restoreFromBacklog';
|
|
39
|
+
id: number;
|
|
40
|
+
parentId: number;
|
|
41
|
+
result: CapabilityMap;
|
|
42
|
+
} | {
|
|
43
|
+
kind: 'normaliseAddresses';
|
|
44
|
+
result: CapabilityMap;
|
|
45
|
+
};
|
|
46
|
+
/** Where a card was dropped, as classified from the pointer position at
|
|
47
|
+
* drag-stop — mirrors goals' computeDragOutcome split (see GoalTreeView.tsx)
|
|
48
|
+
* so the reparent/moveBranchToBacklog decision is unit-testable without a
|
|
49
|
+
* live drag through reactflow's DOM-measurement-gated dragging. */
|
|
50
|
+
export type DropTarget = {
|
|
51
|
+
kind: 'node';
|
|
52
|
+
id: number;
|
|
53
|
+
} | {
|
|
54
|
+
kind: 'root';
|
|
55
|
+
} | {
|
|
56
|
+
kind: 'backlog';
|
|
57
|
+
} | null;
|
|
58
|
+
export declare function computeDragOutcome(map: CapabilityMap, sourceId: number, dropTarget: DropTarget): CapabilityMapChange | null;
|
|
59
|
+
export declare function CapabilityMapView(props: CapabilityMapViewProps): React.ReactElement;
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useMemo, useState, useEffect } from 'react';
|
|
3
|
+
import ReactFlow, { Controls, MiniMap, Background, Panel, ConnectionLineType, useNodesState, useEdgesState, } from 'reactflow';
|
|
4
|
+
import 'reactflow/dist/style.css';
|
|
5
|
+
import { layoutCapabilityMap } from './dsm-layout.js';
|
|
6
|
+
import { reparent, addChild, deleteWithDescendants, moveBranchToBacklog, restoreFromBacklog, normaliseAddresses } from './dsm-mutations.js';
|
|
7
|
+
import { resolveTheme, DEFAULT_MATURITY_COLOURS } from './dsm-theme.js';
|
|
8
|
+
import CapabilityCardNode from './CapabilityCardNode.js';
|
|
9
|
+
import CapabilityRootNode from './CapabilityRootNode.js';
|
|
10
|
+
const BACKLOG_DRAG_MIME = 'application/x-transitrix-capability-id';
|
|
11
|
+
const ROOT_NODE_ID = '__root__';
|
|
12
|
+
const nodeTypes = { capabilityCard: CapabilityCardNode, capabilityRoot: CapabilityRootNode };
|
|
13
|
+
export function computeDragOutcome(map, sourceId, dropTarget) {
|
|
14
|
+
if (!dropTarget)
|
|
15
|
+
return null;
|
|
16
|
+
if (dropTarget.kind === 'backlog') {
|
|
17
|
+
const mutation = moveBranchToBacklog(map, sourceId);
|
|
18
|
+
if (!mutation.ok || !mutation.result)
|
|
19
|
+
return null;
|
|
20
|
+
return { kind: 'moveBranchToBacklog', id: sourceId, result: mutation.result };
|
|
21
|
+
}
|
|
22
|
+
const targetId = dropTarget.kind === 'root' ? 0 : dropTarget.id;
|
|
23
|
+
if (targetId === sourceId)
|
|
24
|
+
return null;
|
|
25
|
+
const mutation = reparent(map, sourceId, targetId);
|
|
26
|
+
if (!mutation.ok || !mutation.result)
|
|
27
|
+
return null;
|
|
28
|
+
return { kind: 'reparent', sourceId, targetId, result: mutation.result };
|
|
29
|
+
}
|
|
30
|
+
function CapabilityMapViewInner({ map, layout: layoutProp, layoutOptions, theme: themeProp, readOnly = false, showBacklog = false, showMiniMap = false, maturityColours = DEFAULT_MATURITY_COLOURS, onChange, onEditRequest, }) {
|
|
31
|
+
const theme = useMemo(() => resolveTheme(themeProp), [themeProp]);
|
|
32
|
+
const [collapsedIds, setCollapsedIds] = useState(new Set());
|
|
33
|
+
const [hoveredId, setHoveredId] = useState(null);
|
|
34
|
+
const backlogCaps = useMemo(() => map.capabilities.filter((c) => c.backlog), [map.capabilities]);
|
|
35
|
+
const layout = useMemo(() => {
|
|
36
|
+
if (layoutProp)
|
|
37
|
+
return layoutProp;
|
|
38
|
+
const hideCollapsed = [...(layoutOptions?.hideCollapsed ?? []), ...collapsedIds];
|
|
39
|
+
return layoutCapabilityMap(map, { ...layoutOptions, hideCollapsed });
|
|
40
|
+
}, [layoutProp, layoutOptions, collapsedIds, map]);
|
|
41
|
+
const rootLabel = layoutOptions?.organisationLabel ?? map.organisation;
|
|
42
|
+
const onAddChild = useCallback((parentId) => {
|
|
43
|
+
if (readOnly)
|
|
44
|
+
return;
|
|
45
|
+
const before = new Set(map.capabilities.map((c) => c.id));
|
|
46
|
+
const mutation = addChild(map, parentId, { name: 'New capability' });
|
|
47
|
+
if (!mutation.ok || !mutation.result)
|
|
48
|
+
return;
|
|
49
|
+
const newCap = mutation.result.capabilities.find((c) => !before.has(c.id));
|
|
50
|
+
if (!newCap)
|
|
51
|
+
return;
|
|
52
|
+
onChange?.({ kind: 'addChild', parentId, newCap, result: mutation.result });
|
|
53
|
+
}, [map, readOnly, onChange]);
|
|
54
|
+
const onDeleteNode = useCallback((id) => {
|
|
55
|
+
if (readOnly)
|
|
56
|
+
return;
|
|
57
|
+
const mutation = deleteWithDescendants(map, id);
|
|
58
|
+
if (!mutation.ok || !mutation.result)
|
|
59
|
+
return;
|
|
60
|
+
onChange?.({ kind: 'delete', id, result: mutation.result });
|
|
61
|
+
}, [map, readOnly, onChange]);
|
|
62
|
+
const onRestoreFromBacklog = useCallback((id, parentId) => {
|
|
63
|
+
if (readOnly)
|
|
64
|
+
return;
|
|
65
|
+
const mutation = restoreFromBacklog(map, id, parentId);
|
|
66
|
+
if (!mutation.ok || !mutation.result)
|
|
67
|
+
return;
|
|
68
|
+
onChange?.({ kind: 'restoreFromBacklog', id, parentId, result: mutation.result });
|
|
69
|
+
}, [map, readOnly, onChange]);
|
|
70
|
+
const onNormalise = useCallback(() => {
|
|
71
|
+
if (readOnly)
|
|
72
|
+
return;
|
|
73
|
+
onChange?.({ kind: 'normaliseAddresses', result: normaliseAddresses(map) });
|
|
74
|
+
}, [map, readOnly, onChange]);
|
|
75
|
+
const onToggleCollapse = useCallback((id) => {
|
|
76
|
+
setCollapsedIds((prev) => {
|
|
77
|
+
const next = new Set(prev);
|
|
78
|
+
if (next.has(id))
|
|
79
|
+
next.delete(id);
|
|
80
|
+
else
|
|
81
|
+
next.add(id);
|
|
82
|
+
return next;
|
|
83
|
+
});
|
|
84
|
+
}, []);
|
|
85
|
+
const buildNodes = useCallback(() => {
|
|
86
|
+
const rootNode = {
|
|
87
|
+
id: ROOT_NODE_ID,
|
|
88
|
+
type: 'capabilityRoot',
|
|
89
|
+
position: { x: layout.rootNode.x, y: layout.rootNode.y },
|
|
90
|
+
draggable: false,
|
|
91
|
+
data: { label: rootLabel, theme },
|
|
92
|
+
};
|
|
93
|
+
const cardNodes = layout.nodes.map((n) => ({
|
|
94
|
+
id: String(n.id),
|
|
95
|
+
type: 'capabilityCard',
|
|
96
|
+
position: { x: n.x, y: n.y },
|
|
97
|
+
draggable: !readOnly,
|
|
98
|
+
data: {
|
|
99
|
+
capability: n.data,
|
|
100
|
+
theme,
|
|
101
|
+
maturityColours,
|
|
102
|
+
readOnly,
|
|
103
|
+
isDropTarget: hoveredId === n.id,
|
|
104
|
+
hasHiddenChildren: n.hasHiddenChildren,
|
|
105
|
+
isCollapsed: collapsedIds.has(n.id),
|
|
106
|
+
onAddChild,
|
|
107
|
+
onDelete: onDeleteNode,
|
|
108
|
+
onToggleCollapse,
|
|
109
|
+
},
|
|
110
|
+
}));
|
|
111
|
+
return [rootNode, ...cardNodes];
|
|
112
|
+
}, [layout, theme, rootLabel, readOnly, hoveredId, collapsedIds, maturityColours, onAddChild, onDeleteNode, onToggleCollapse]);
|
|
113
|
+
const buildEdges = useCallback(() => {
|
|
114
|
+
return layout.edges.map((e) => ({
|
|
115
|
+
id: `e${e.source}-${e.target}`,
|
|
116
|
+
source: e.source === 'root' ? ROOT_NODE_ID : String(e.source),
|
|
117
|
+
target: String(e.target),
|
|
118
|
+
type: 'default',
|
|
119
|
+
style: { stroke: theme.edgeColor, strokeWidth: theme.edgeWidth },
|
|
120
|
+
}));
|
|
121
|
+
}, [layout.edges, theme]);
|
|
122
|
+
const [nodes, setNodes, onNodesChange] = useNodesState(buildNodes());
|
|
123
|
+
const [edges, setEdges, onEdgesChange] = useEdgesState(buildEdges());
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
setNodes(buildNodes());
|
|
126
|
+
setEdges(buildEdges());
|
|
127
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
128
|
+
}, [layout, theme, readOnly, hoveredId, collapsedIds, maturityColours]);
|
|
129
|
+
const checkIntersection = useCallback((a, b) => {
|
|
130
|
+
const midX = a.position.x + 125;
|
|
131
|
+
const midY = a.position.y + 32;
|
|
132
|
+
return midX > b.position.x && midX < b.position.x + 250 && midY > b.position.y && midY < b.position.y + 64;
|
|
133
|
+
}, []);
|
|
134
|
+
const onNodeDrag = useCallback((_event, node) => {
|
|
135
|
+
if (readOnly)
|
|
136
|
+
return;
|
|
137
|
+
const hit = nodes.find((n) => n.id !== node.id && checkIntersection(node, n));
|
|
138
|
+
if (!hit) {
|
|
139
|
+
setHoveredId(null);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
setHoveredId(hit.id === ROOT_NODE_ID ? 'root' : Number(hit.id));
|
|
143
|
+
}
|
|
144
|
+
}, [readOnly, nodes, checkIntersection]);
|
|
145
|
+
const onNodeDragStop = useCallback((event, node) => {
|
|
146
|
+
if (readOnly) {
|
|
147
|
+
setHoveredId(null);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const sourceId = Number(node.id);
|
|
151
|
+
const mouseEvent = event;
|
|
152
|
+
const dropEl = typeof document !== 'undefined' && 'clientX' in mouseEvent
|
|
153
|
+
? document.elementFromPoint(mouseEvent.clientX, mouseEvent.clientY)
|
|
154
|
+
: null;
|
|
155
|
+
let dropTarget;
|
|
156
|
+
if (dropEl?.closest('[data-capmap-backlog-zone]'))
|
|
157
|
+
dropTarget = { kind: 'backlog' };
|
|
158
|
+
else if (hoveredId === 'root')
|
|
159
|
+
dropTarget = { kind: 'root' };
|
|
160
|
+
else if (hoveredId != null)
|
|
161
|
+
dropTarget = { kind: 'node', id: hoveredId };
|
|
162
|
+
else
|
|
163
|
+
dropTarget = null;
|
|
164
|
+
const change = computeDragOutcome(map, sourceId, dropTarget);
|
|
165
|
+
if (change)
|
|
166
|
+
onChange?.(change);
|
|
167
|
+
setHoveredId(null);
|
|
168
|
+
}, [readOnly, hoveredId, map, onChange]);
|
|
169
|
+
const onNodeDoubleClick = useCallback((_event, node) => {
|
|
170
|
+
const cap = map.capabilities.find((c) => c.id === Number(node.id));
|
|
171
|
+
if (cap)
|
|
172
|
+
onEditRequest?.(cap);
|
|
173
|
+
}, [map.capabilities, onEditRequest]);
|
|
174
|
+
const onBacklogItemDragStart = useCallback((event, capId) => {
|
|
175
|
+
event.dataTransfer.setData(BACKLOG_DRAG_MIME, String(capId));
|
|
176
|
+
event.dataTransfer.effectAllowed = 'move';
|
|
177
|
+
}, []);
|
|
178
|
+
const onCanvasDragOver = useCallback((event) => {
|
|
179
|
+
if (readOnly)
|
|
180
|
+
return;
|
|
181
|
+
event.preventDefault();
|
|
182
|
+
}, [readOnly]);
|
|
183
|
+
const onCanvasDrop = useCallback((event) => {
|
|
184
|
+
if (readOnly)
|
|
185
|
+
return;
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
const idStr = event.dataTransfer.getData(BACKLOG_DRAG_MIME);
|
|
188
|
+
if (!idStr)
|
|
189
|
+
return;
|
|
190
|
+
const capId = Number(idStr);
|
|
191
|
+
const targetEl = document.elementFromPoint(event.clientX, event.clientY);
|
|
192
|
+
const nodeEl = targetEl?.closest('.react-flow__node');
|
|
193
|
+
const targetIdAttr = nodeEl?.getAttribute('data-id');
|
|
194
|
+
if (targetIdAttr == null)
|
|
195
|
+
return;
|
|
196
|
+
const parentId = targetIdAttr === ROOT_NODE_ID ? 0 : Number(targetIdAttr);
|
|
197
|
+
onRestoreFromBacklog(capId, parentId);
|
|
198
|
+
}, [readOnly, onRestoreFromBacklog]);
|
|
199
|
+
const showBacklogPanel = showBacklog && backlogCaps.length > 0;
|
|
200
|
+
return (_jsxs("div", { style: { display: 'flex', width: '100%', height: '100%' }, children: [showBacklogPanel && (_jsxs("div", { "data-capmap-backlog-zone": true, style: { width: 220, flexShrink: 0, borderRight: '1px solid #e2e8f0', padding: 10, overflowY: 'auto', background: '#f8fafc' }, children: [_jsx("div", { style: { fontWeight: 700, fontSize: 12, color: '#334155', marginBottom: 8 }, children: "Backlog" }), backlogCaps.map((cap) => (_jsx("div", { draggable: !readOnly, onDragStart: (e) => onBacklogItemDragStart(e, cap.id), onDoubleClick: () => onEditRequest?.(cap), title: "Drag onto a card (or the root) to set its parent", style: { padding: 8, marginBottom: 6, background: '#ffffff', border: '1px solid #cbd5e1', borderRadius: 6, fontSize: 11, cursor: readOnly ? 'default' : 'grab' }, children: _jsx("div", { style: { fontWeight: 600 }, children: cap.name }) }, cap.id)))] })), _jsx("div", { style: { flex: 1, position: 'relative' }, onDragOver: onCanvasDragOver, onDrop: onCanvasDrop, children: _jsxs(ReactFlow, { nodes: nodes, edges: edges, onNodesChange: readOnly ? undefined : onNodesChange, onEdgesChange: readOnly ? undefined : onEdgesChange, nodeTypes: nodeTypes, nodesDraggable: !readOnly, nodesConnectable: false, onNodeDrag: onNodeDrag, onNodeDragStop: onNodeDragStop, onNodeDoubleClick: onNodeDoubleClick, connectionLineType: ConnectionLineType.Bezier, fitView: true, children: [_jsx(Background, {}), _jsx(Controls, {}), showMiniMap && _jsx(MiniMap, { pannable: true, zoomable: true }), !readOnly && (_jsx(Panel, { position: "top-right", children: _jsx("button", { type: "button", onClick: onNormalise, style: { fontSize: 11, padding: '4px 8px', cursor: 'pointer' }, children: "Normalise addresses" }) }))] }) })] }));
|
|
201
|
+
}
|
|
202
|
+
export function CapabilityMapView(props) {
|
|
203
|
+
return _jsx(CapabilityMapViewInner, { ...props });
|
|
204
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { ThemeTokens } from './dsm-theme.js';
|
|
3
|
+
export interface CapabilityRootNodeData {
|
|
4
|
+
label: string;
|
|
5
|
+
theme: Required<ThemeTokens>;
|
|
6
|
+
}
|
|
7
|
+
/** The virtual organisation root — single node, no maturity dot, no
|
|
8
|
+
* add/delete/collapse affordances (it isn't a mutable Capability). */
|
|
9
|
+
declare const CapabilityRootNode: React.MemoExoticComponent<({ data }: {
|
|
10
|
+
data: CapabilityRootNodeData;
|
|
11
|
+
}) => import("react/jsx-runtime").JSX.Element>;
|
|
12
|
+
export default CapabilityRootNode;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { memo } from 'react';
|
|
3
|
+
import { Handle, Position } from 'reactflow';
|
|
4
|
+
/** The virtual organisation root — single node, no maturity dot, no
|
|
5
|
+
* add/delete/collapse affordances (it isn't a mutable Capability). */
|
|
6
|
+
const CapabilityRootNode = memo(({ data }) => (_jsxs("div", { style: {
|
|
7
|
+
width: 200,
|
|
8
|
+
height: 64,
|
|
9
|
+
background: data.theme.rootFill,
|
|
10
|
+
color: data.theme.rootTextColor,
|
|
11
|
+
borderRadius: 8,
|
|
12
|
+
border: `1px solid ${data.theme.cardBorderColor}`,
|
|
13
|
+
boxShadow: '0 1px 4px rgba(0,0,0,0.12)',
|
|
14
|
+
display: 'flex',
|
|
15
|
+
alignItems: 'center',
|
|
16
|
+
justifyContent: 'center',
|
|
17
|
+
textAlign: 'center',
|
|
18
|
+
fontWeight: 700,
|
|
19
|
+
fontSize: 12,
|
|
20
|
+
padding: '4px 10px',
|
|
21
|
+
}, children: [_jsx(Handle, { type: "source", position: Position.Right, style: { opacity: 0 } }), data.label] })));
|
|
22
|
+
CapabilityRootNode.displayName = 'CapabilityRootNode';
|
|
23
|
+
export default CapabilityRootNode;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _MockResizeObserver_callback;
|
|
13
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
+
// @vitest-environment jsdom
|
|
15
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
16
|
+
import { render, screen, cleanup, waitFor } from '@testing-library/react';
|
|
17
|
+
import { CapabilityMapView, computeDragOutcome } from '../CapabilityMapView.js';
|
|
18
|
+
// See goals/__tests__/GoalTreeView.test.tsx for why these three shims are
|
|
19
|
+
// needed — reactflow keeps a node `visibility: hidden` (and inert to
|
|
20
|
+
// drag/measurement logic) until ResizeObserver reports its size, and reads
|
|
21
|
+
// the computed transform back out via DOMMatrixReadOnly; jsdom has neither.
|
|
22
|
+
class MockResizeObserver {
|
|
23
|
+
constructor(callback) {
|
|
24
|
+
_MockResizeObserver_callback.set(this, void 0);
|
|
25
|
+
__classPrivateFieldSet(this, _MockResizeObserver_callback, callback, "f");
|
|
26
|
+
}
|
|
27
|
+
observe(target) {
|
|
28
|
+
queueMicrotask(() => {
|
|
29
|
+
__classPrivateFieldGet(this, _MockResizeObserver_callback, "f").call(this, [{ target, contentRect: target.getBoundingClientRect() }], this);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
unobserve() { }
|
|
33
|
+
disconnect() { }
|
|
34
|
+
}
|
|
35
|
+
_MockResizeObserver_callback = new WeakMap();
|
|
36
|
+
class MockDOMMatrixReadOnly {
|
|
37
|
+
constructor(transform) {
|
|
38
|
+
this.m41 = 0;
|
|
39
|
+
this.m42 = 0;
|
|
40
|
+
if (!transform || transform === 'none')
|
|
41
|
+
return;
|
|
42
|
+
const t = /translate\((-?[\d.]+)px,\s*(-?[\d.]+)px\)/.exec(transform);
|
|
43
|
+
if (t) {
|
|
44
|
+
this.m41 = Number(t[1]);
|
|
45
|
+
this.m42 = Number(t[2]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function makeMap() {
|
|
50
|
+
return {
|
|
51
|
+
organisation: 'Acme Corp',
|
|
52
|
+
set_id: 'v1.0',
|
|
53
|
+
capabilities: [
|
|
54
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0', maturity: [{ date: '2026-04-01', level: 3 }] },
|
|
55
|
+
{ id: 2, name: 'Lead Qualification', address: '1.1.0' },
|
|
56
|
+
{ id: 3, name: 'Inbound Lead Scoring', address: '1.1.1' },
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
global.ResizeObserver = MockResizeObserver;
|
|
62
|
+
window.DOMMatrixReadOnly = MockDOMMatrixReadOnly;
|
|
63
|
+
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function () {
|
|
64
|
+
const isNode = this.classList?.contains('react-flow__node');
|
|
65
|
+
const width = isNode ? 250 : 1000;
|
|
66
|
+
const height = isNode ? 64 : 800;
|
|
67
|
+
return { width, height, top: 0, left: 0, right: width, bottom: height, x: 0, y: 0, toJSON() { return this; } };
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
afterEach(() => {
|
|
71
|
+
cleanup();
|
|
72
|
+
vi.restoreAllMocks();
|
|
73
|
+
});
|
|
74
|
+
describe('CapabilityMapView', () => {
|
|
75
|
+
it('renders the virtual root and every on-diagram capability', async () => {
|
|
76
|
+
render(_jsx(CapabilityMapView, { map: makeMap() }));
|
|
77
|
+
await waitFor(() => {
|
|
78
|
+
expect(screen.getByText('Acme Corp')).toBeTruthy();
|
|
79
|
+
expect(screen.getByText('Customer Acquisition')).toBeTruthy();
|
|
80
|
+
expect(screen.getByText('Lead Qualification')).toBeTruthy();
|
|
81
|
+
expect(screen.getByText('Inbound Lead Scoring')).toBeTruthy();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
it('renders a maturity dot only for capabilities with a maturity snapshot', async () => {
|
|
85
|
+
render(_jsx(CapabilityMapView, { map: makeMap() }));
|
|
86
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
87
|
+
expect(screen.getByLabelText('Maturity 3')).toBeTruthy();
|
|
88
|
+
expect(screen.queryByLabelText(/Maturity/, { selector: '[aria-label="Maturity undefined"]' })).toBeNull();
|
|
89
|
+
});
|
|
90
|
+
it('shows backlog capabilities in the sidebar, not on canvas', async () => {
|
|
91
|
+
const map = {
|
|
92
|
+
organisation: 'Acme Corp',
|
|
93
|
+
set_id: 'v1.0',
|
|
94
|
+
capabilities: [
|
|
95
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
96
|
+
{ id: 2, name: 'Parked idea', address: '0.0.0', backlog: true },
|
|
97
|
+
],
|
|
98
|
+
};
|
|
99
|
+
render(_jsx(CapabilityMapView, { map: map, showBacklog: true }));
|
|
100
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
101
|
+
expect(screen.getByText('Backlog')).toBeTruthy();
|
|
102
|
+
expect(screen.getByText('Parked idea')).toBeTruthy();
|
|
103
|
+
});
|
|
104
|
+
it('does not render add/delete affordances when readOnly', async () => {
|
|
105
|
+
render(_jsx(CapabilityMapView, { map: makeMap(), readOnly: true }));
|
|
106
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
107
|
+
expect(screen.queryByLabelText('Add child capability')).toBeNull();
|
|
108
|
+
expect(screen.queryByLabelText('Delete capability')).toBeNull();
|
|
109
|
+
});
|
|
110
|
+
it('fires onChange with kind "addChild" when the add-child button is clicked', async () => {
|
|
111
|
+
const onChange = vi.fn();
|
|
112
|
+
render(_jsx(CapabilityMapView, { map: makeMap(), onChange: onChange }));
|
|
113
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
114
|
+
const rootCard = document.querySelector('.react-flow__node[data-id="1"]');
|
|
115
|
+
rootCard.querySelector('[aria-label="Add child capability"]')
|
|
116
|
+
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
117
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
118
|
+
const event = onChange.mock.calls[0][0];
|
|
119
|
+
expect(event.kind).toBe('addChild');
|
|
120
|
+
if (event.kind === 'addChild') {
|
|
121
|
+
expect(event.newCap.address).toBe('1.2.0');
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
it('fires onChange with kind "delete" when the delete button is clicked', async () => {
|
|
125
|
+
const onChange = vi.fn();
|
|
126
|
+
render(_jsx(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('[aria-label="Delete capability"]')
|
|
130
|
+
.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
131
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
132
|
+
expect(onChange.mock.calls[0][0].kind).toBe('delete');
|
|
133
|
+
});
|
|
134
|
+
it('fires onEditRequest on double-click', async () => {
|
|
135
|
+
const onEditRequest = vi.fn();
|
|
136
|
+
render(_jsx(CapabilityMapView, { map: makeMap(), onEditRequest: onEditRequest }));
|
|
137
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
138
|
+
document.querySelector('.react-flow__node[data-id="1"]')
|
|
139
|
+
.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }));
|
|
140
|
+
await waitFor(() => expect(onEditRequest).toHaveBeenCalledTimes(1));
|
|
141
|
+
expect(onEditRequest.mock.calls[0][0]).toMatchObject({ id: 1, name: 'Customer Acquisition' });
|
|
142
|
+
});
|
|
143
|
+
it('fires onChange with kind "normaliseAddresses" from the toolbar button', async () => {
|
|
144
|
+
const onChange = vi.fn();
|
|
145
|
+
render(_jsx(CapabilityMapView, { map: makeMap(), onChange: onChange }));
|
|
146
|
+
await waitFor(() => expect(screen.getByText('Customer Acquisition')).toBeTruthy());
|
|
147
|
+
screen.getByText('Normalise addresses').dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
148
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
149
|
+
expect(onChange.mock.calls[0][0].kind).toBe('normaliseAddresses');
|
|
150
|
+
});
|
|
151
|
+
it('fires onChange with kind "restoreFromBacklog" when a backlog card is dropped onto a canvas card', async () => {
|
|
152
|
+
const map = {
|
|
153
|
+
organisation: 'Acme Corp',
|
|
154
|
+
set_id: 'v1.0',
|
|
155
|
+
capabilities: [
|
|
156
|
+
{ id: 1, name: 'Customer Acquisition', address: '1.0.0' },
|
|
157
|
+
{ id: 2, name: 'Parked idea', address: '0.0.0', backlog: true },
|
|
158
|
+
],
|
|
159
|
+
};
|
|
160
|
+
const onChange = vi.fn();
|
|
161
|
+
render(_jsx(CapabilityMapView, { map: map, onChange: onChange, showBacklog: true }));
|
|
162
|
+
await waitFor(() => expect(screen.getByText('Parked idea')).toBeTruthy());
|
|
163
|
+
const store = new Map();
|
|
164
|
+
const dataTransfer = { setData: (k, v) => store.set(k, v), getData: (k) => store.get(k) ?? '', effectAllowed: 'move' };
|
|
165
|
+
function fireDnd(el, type, coords) {
|
|
166
|
+
const event = new Event(type, { bubbles: true, cancelable: true });
|
|
167
|
+
Object.defineProperty(event, 'dataTransfer', { value: dataTransfer });
|
|
168
|
+
Object.assign(event, coords);
|
|
169
|
+
el.dispatchEvent(event);
|
|
170
|
+
}
|
|
171
|
+
const backlogItem = screen.getByText('Parked idea').closest('div[draggable]');
|
|
172
|
+
const rootCard = document.querySelector('.react-flow__node[data-id="1"]');
|
|
173
|
+
document.elementFromPoint = vi.fn().mockReturnValue(rootCard);
|
|
174
|
+
fireDnd(backlogItem, 'dragstart', { clientX: 0, clientY: 0 });
|
|
175
|
+
fireDnd(rootCard, 'drop', { clientX: 10, clientY: 10 });
|
|
176
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
177
|
+
const event = onChange.mock.calls[0][0];
|
|
178
|
+
expect(event.kind).toBe('restoreFromBacklog');
|
|
179
|
+
if (event.kind === 'restoreFromBacklog') {
|
|
180
|
+
expect(event.id).toBe(2);
|
|
181
|
+
expect(event.parentId).toBe(1);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
// Dragging a card is choreographed by reactflow's own pointer/DOM-measurement
|
|
186
|
+
// machinery — see the equivalent note in goals/__tests__/GoalTreeView.test.tsx.
|
|
187
|
+
// computeDragOutcome() is the pure decision this component's onNodeDragStop
|
|
188
|
+
// calls once reactflow reports a drag, tested directly here.
|
|
189
|
+
describe('computeDragOutcome (reparent / moveBranchToBacklog decision logic)', () => {
|
|
190
|
+
const map = makeMap();
|
|
191
|
+
it('fires a "reparent" change when dropped onto another card', () => {
|
|
192
|
+
const change = computeDragOutcome(map, 3, { kind: 'node', id: 1 });
|
|
193
|
+
expect(change?.kind).toBe('reparent');
|
|
194
|
+
if (change?.kind === 'reparent') {
|
|
195
|
+
expect(change.sourceId).toBe(3);
|
|
196
|
+
expect(change.targetId).toBe(1);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
it('fires a "reparent" change (promotion to L1) when dropped on the root', () => {
|
|
200
|
+
const change = computeDragOutcome(map, 2, { kind: 'root' });
|
|
201
|
+
expect(change?.kind).toBe('reparent');
|
|
202
|
+
if (change?.kind === 'reparent') {
|
|
203
|
+
expect(change.targetId).toBe(0);
|
|
204
|
+
expect(change.result.capabilities.find((c) => c.id === 2)?.address).toBe('2.0.0');
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
it('fires a "moveBranchToBacklog" change when dropped on the backlog zone', () => {
|
|
208
|
+
const change = computeDragOutcome(map, 2, { kind: 'backlog' });
|
|
209
|
+
expect(change?.kind).toBe('moveBranchToBacklog');
|
|
210
|
+
});
|
|
211
|
+
it('is a no-op when dropped nowhere in particular', () => {
|
|
212
|
+
expect(computeDragOutcome(map, 2, null)).toBeNull();
|
|
213
|
+
});
|
|
214
|
+
it('is a no-op when dropped on itself', () => {
|
|
215
|
+
expect(computeDragOutcome(map, 2, { kind: 'node', id: 2 })).toBeNull();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { parseAddress, formatAddress, getLevel, getParentAddress, getFirstFreeAddress, isAddressTaken } from '../address.js';
|
|
3
|
+
function cap(id, address, backlog = false) {
|
|
4
|
+
return { id, name: `Cap ${id}`, address, backlog };
|
|
5
|
+
}
|
|
6
|
+
describe('parseAddress / formatAddress', () => {
|
|
7
|
+
it('round-trips X.Y.Z', () => {
|
|
8
|
+
for (const addr of ['1.0.0', '1.2.0', '1.2.3', '0.0.0', '42.7.19']) {
|
|
9
|
+
expect(formatAddress(parseAddress(addr))).toBe(addr);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
it('throws on malformed input', () => {
|
|
13
|
+
expect(() => parseAddress('1.2')).toThrow();
|
|
14
|
+
expect(() => parseAddress('1.2.3.4')).toThrow();
|
|
15
|
+
expect(() => parseAddress('a.b.c')).toThrow();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
describe('getLevel', () => {
|
|
19
|
+
it('classifies each address shape', () => {
|
|
20
|
+
expect(getLevel('1.0.0')).toBe(1);
|
|
21
|
+
expect(getLevel('1.2.0')).toBe(2);
|
|
22
|
+
expect(getLevel('1.2.3')).toBe(3);
|
|
23
|
+
expect(getLevel('0.0.0')).toBe('backlog');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe('getParentAddress', () => {
|
|
27
|
+
it('walks up one level at a time', () => {
|
|
28
|
+
expect(getParentAddress('1.2.3')).toBe('1.2.0');
|
|
29
|
+
expect(getParentAddress('1.2.0')).toBe('1.0.0');
|
|
30
|
+
expect(getParentAddress('1.0.0')).toBeNull();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe('getFirstFreeAddress / isAddressTaken', () => {
|
|
34
|
+
it('finds the first unused L2 slot under an L1 parent', () => {
|
|
35
|
+
const caps = [cap(1, '1.0.0'), cap(2, '1.1.0'), cap(3, '1.2.0')];
|
|
36
|
+
expect(getFirstFreeAddress('1.0.0', caps)).toBe('1.3.0');
|
|
37
|
+
});
|
|
38
|
+
it('finds the first unused L3 slot under an L2 parent', () => {
|
|
39
|
+
const caps = [cap(1, '1.0.0'), cap(2, '1.1.0'), cap(3, '1.1.1')];
|
|
40
|
+
expect(getFirstFreeAddress('1.1.0', caps)).toBe('1.1.2');
|
|
41
|
+
});
|
|
42
|
+
it('ignores backlog entries when checking occupancy', () => {
|
|
43
|
+
const caps = [cap(1, '1.0.0'), cap(2, '0.0.0', true)];
|
|
44
|
+
expect(isAddressTaken('1.1.0', caps)).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
it('rejects an L3 address as a parent', () => {
|
|
47
|
+
expect(() => getFirstFreeAddress('1.1.1', [])).toThrow();
|
|
48
|
+
});
|
|
49
|
+
});
|