@principal-ai/subsystems-react 0.56.20 → 0.56.22

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 (54) hide show
  1. package/dist/index.d.ts +4 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +2 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/subsystem/C4Graph.d.ts +2 -69
  6. package/dist/subsystem/C4Graph.d.ts.map +1 -1
  7. package/dist/subsystem/C4Graph.js +8 -154
  8. package/dist/subsystem/C4Graph.js.map +1 -1
  9. package/dist/subsystem/C4NodeCard.d.ts +1 -47
  10. package/dist/subsystem/C4NodeCard.d.ts.map +1 -1
  11. package/dist/subsystem/C4NodeCard.js +6 -141
  12. package/dist/subsystem/C4NodeCard.js.map +1 -1
  13. package/dist/subsystem/ContainersPanel.d.ts +33 -0
  14. package/dist/subsystem/ContainersPanel.d.ts.map +1 -0
  15. package/dist/subsystem/ContainersPanel.js +116 -0
  16. package/dist/subsystem/ContainersPanel.js.map +1 -0
  17. package/dist/subsystem/SubsystemComponentGraph.d.ts +27 -1
  18. package/dist/subsystem/SubsystemComponentGraph.d.ts.map +1 -1
  19. package/dist/subsystem/SubsystemComponentGraph.js +147 -21
  20. package/dist/subsystem/SubsystemComponentGraph.js.map +1 -1
  21. package/dist/subsystem/containerCard.d.ts +132 -0
  22. package/dist/subsystem/containerCard.d.ts.map +1 -0
  23. package/dist/subsystem/containerCard.js +167 -0
  24. package/dist/subsystem/containerCard.js.map +1 -0
  25. package/dist/subsystem/graphChrome.d.ts +11 -8
  26. package/dist/subsystem/graphChrome.d.ts.map +1 -1
  27. package/dist/subsystem/graphChrome.js +11 -9
  28. package/dist/subsystem/graphChrome.js.map +1 -1
  29. package/dist/subsystem/graphMorph.d.ts +142 -0
  30. package/dist/subsystem/graphMorph.d.ts.map +1 -0
  31. package/dist/subsystem/graphMorph.js +177 -0
  32. package/dist/subsystem/graphMorph.js.map +1 -0
  33. package/dist/subsystem/model.d.ts +89 -0
  34. package/dist/subsystem/model.d.ts.map +1 -1
  35. package/dist/subsystem/model.js +224 -15
  36. package/dist/subsystem/model.js.map +1 -1
  37. package/dist/subsystem/nodes.d.ts +9 -3
  38. package/dist/subsystem/nodes.d.ts.map +1 -1
  39. package/dist/subsystem/nodes.js +134 -82
  40. package/dist/subsystem/nodes.js.map +1 -1
  41. package/package.json +1 -1
  42. package/src/index.ts +5 -0
  43. package/src/stories/Canvases/Subsystem/ContainerFrames.stories.tsx +132 -0
  44. package/src/subsystem/C4Graph.tsx +20 -180
  45. package/src/subsystem/C4NodeCard.tsx +34 -202
  46. package/src/subsystem/ContainersPanel.tsx +239 -0
  47. package/src/subsystem/SubsystemComponentGraph.tsx +232 -25
  48. package/src/subsystem/containerCard.tsx +264 -0
  49. package/src/subsystem/graphChrome.tsx +11 -9
  50. package/src/subsystem/graphMorph.tsx +212 -0
  51. package/src/subsystem/model.collapse.test.ts +128 -0
  52. package/src/subsystem/model.ts +306 -16
  53. package/src/subsystem/nodes.group.test.tsx +125 -16
  54. package/src/subsystem/nodes.tsx +201 -91
@@ -25,6 +25,8 @@ import type {
25
25
  SubsystemConstructDeclaration,
26
26
  } from '@principal-ai/subsystems-core';
27
27
  import { purlOwnerName, purlRepoKey } from './paths';
28
+ import { technologyBrand } from './techIcons';
29
+ import { NODE_H, NODE_W, OPEN_HEADER_PAD, OPEN_SIDE_PAD } from './containerCard';
28
30
 
29
31
  /**
30
32
  * Structured declaration shape — the document's own contract, re-exported from
@@ -463,6 +465,68 @@ export interface SubsystemProcessRegion {
463
465
  * membership still read the claim as a process region.
464
466
  */
465
467
  chrome?: 'package';
468
+ /**
469
+ * The technology the boundary is built with (Bun, React, Postgres…), for the
470
+ * frame's technology row — the same slot a C4 container card fills. Host-
471
+ * supplied through `BoundaryFrameOptions.regionDetails`, or derived from the
472
+ * members' `framework` when the host says nothing.
473
+ */
474
+ technology?: string;
475
+ /** One-line responsibility, the way C4 asks every container to state one. */
476
+ description?: string;
477
+ /**
478
+ * A process frame's sort — application or data store — which picks the kind
479
+ * glyph on its header. Defaults to `application`.
480
+ */
481
+ containerKind?: 'application' | 'data-store';
482
+ }
483
+
484
+ /**
485
+ * Host-supplied C4 facts about a boundary region, keyed by region key in
486
+ * `BoundaryFrameOptions.regionDetails`. Everything is optional: a host that
487
+ * accepted a C4 container for a process passes what the container states.
488
+ */
489
+ export type SubsystemRegionDetails = Pick<
490
+ SubsystemProcessRegion,
491
+ 'technology' | 'description' | 'containerKind'
492
+ >;
493
+
494
+ /**
495
+ * Whether a frame draws as a container *card* — header rows (label, kind glyph,
496
+ * technology, member count), filled body — rather than as a bare dashed frame
497
+ * with a path badge. Process and package boundaries are containers in the C4
498
+ * sense; module and directory frames are code folders and keep the badge.
499
+ */
500
+ export function isCardFrame(
501
+ region: Pick<SubsystemProcessRegion, 'kind' | 'chrome'>,
502
+ ): boolean {
503
+ return region.kind === 'process' || region.kind === 'package' || region.chrome === 'package';
504
+ }
505
+
506
+ /**
507
+ * The technology a region is built with, read off its members when the host
508
+ * states none: the most common member `framework` we hold an official mark for.
509
+ * A region whose members name no known framework stays unstated, so the header
510
+ * falls back to the kind rather than inventing a technology.
511
+ */
512
+ export function deriveRegionTechnology(
513
+ members: readonly Pick<SubsystemComponent, 'framework'>[],
514
+ ): string | undefined {
515
+ const counts = new Map<string, number>();
516
+ for (const m of members) {
517
+ const brand = technologyBrand(m.framework);
518
+ if (!brand) continue;
519
+ counts.set(brand.name, (counts.get(brand.name) ?? 0) + 1);
520
+ }
521
+ let best: string | undefined;
522
+ let bestCount = 0;
523
+ for (const [name, count] of counts) {
524
+ if (count > bestCount) {
525
+ best = name;
526
+ bestCount = count;
527
+ }
528
+ }
529
+ return best;
466
530
  }
467
531
 
468
532
  /** Options for which boundary frames are kept. */
@@ -497,8 +561,15 @@ export interface BoundaryFrameOptions {
497
561
  * only — region identity and issue targeting are unchanged.
498
562
  */
499
563
  codeProcessKeys?: ReadonlySet<string>;
564
+ /**
565
+ * C4 facts for boundary regions, keyed by region key (the `process` value or
566
+ * the package purl key): technology, description, container sort. A card
567
+ * frame draws them in its header the way a C4 container card does. Regions
568
+ * not named here derive a technology from their members' `framework` and
569
+ * otherwise state nothing.
570
+ */
571
+ regionDetails?: Readonly<Record<string, SubsystemRegionDetails>>;
500
572
  }
501
-
502
573
  /** React Flow id for a process boundary group node. */
503
574
  export function processGroupNodeId(processKey: string): string {
504
575
  return `process:${processKey}`;
@@ -1213,6 +1284,13 @@ export interface SubsystemGroupNodeData extends Record<string, unknown> {
1213
1284
  color?: string;
1214
1285
  /** Boundary diagnostics badge — see `SubsystemRegionIssue`. */
1215
1286
  issue?: SubsystemRegionIssue;
1287
+ /**
1288
+ * Drawn as a closed container card: members folded away, lines rolled up onto
1289
+ * it. Set by the layout when the frame is in `collapsedGroupIds`.
1290
+ */
1291
+ collapsed?: boolean;
1292
+ /** Open or close this frame — the header's grow/shrink affordance. */
1293
+ onToggleCollapse?: () => void;
1216
1294
  }
1217
1295
 
1218
1296
  export type SubsystemGraphNode =
@@ -1234,6 +1312,14 @@ export interface SubsystemGraphEdgeData extends Record<string, unknown> {
1234
1312
  /** True while another edge is selected — render this edge (and its label)
1235
1313
  * dimmed to focus the selected relationship. */
1236
1314
  dimmed?: boolean;
1315
+ /**
1316
+ * How many edges this line stands for, when a collapsed frame rolled them up
1317
+ * (one line per source/target/mechanism). Absent on an ordinary edge.
1318
+ */
1319
+ rolledUp?: number;
1320
+ /** Ids of the edges a rolled-up line stands for, so trail-step highlighting
1321
+ * can still light the line when one of them is the focused step. */
1322
+ rolledUpIds?: string[];
1237
1323
  /** ELK-computed label midpoint (from the actual edge path, not node centers). */
1238
1324
  labelX?: number;
1239
1325
  labelY?: number;
@@ -1912,6 +1998,145 @@ export function elkDirectionFor(
1912
1998
  return layoutDirection === 'vertical' ? 'DOWN' : 'RIGHT';
1913
1999
  }
1914
2000
 
2001
+ /**
2002
+ * Fold the collapsed boundary frames down to cards.
2003
+ *
2004
+ * A collapsed frame stops being an ELK parent and becomes a leaf the size of a
2005
+ * container card (`NODE_W` × `NODE_H`). Its members — and every frame nested
2006
+ * inside it — leave the graph, and the edges that touched them are re-targeted
2007
+ * at the card: one line per (source, target, mechanism), so a frame with a
2008
+ * dozen calls into another reads as one `calls` line rather than a dozen. A
2009
+ * line that starts and ends inside the same collapsed frame folds into nothing.
2010
+ *
2011
+ * Only card frames (process / package) collapse; a module or directory id in
2012
+ * `collapsedIds` is ignored. A frame nested inside a collapsed frame is swallowed
2013
+ * by it, whatever its own state — the outer card is the only thing drawn.
2014
+ *
2015
+ * Pure: hands back new arrays and leaves the inputs alone.
2016
+ */
2017
+ export function collapseBoundaryGroups(
2018
+ nodes: readonly SubsystemGraphNode[],
2019
+ edges: readonly SubsystemGraphEdge[],
2020
+ groups: readonly BoundaryLayoutGroup[],
2021
+ collapsedIds: ReadonlySet<string>,
2022
+ ): {
2023
+ nodes: SubsystemGraphNode[];
2024
+ edges: SubsystemGraphEdge[];
2025
+ groups: BoundaryLayoutGroup[];
2026
+ /** The regions folded to cards, for callers that list every region drawn. */
2027
+ collapsedRegions: SubsystemProcessRegion[];
2028
+ } {
2029
+ const byId = new Map(groups.map((g) => [g.id, g]));
2030
+ const childrenOf = new Map<string, BoundaryLayoutGroup[]>();
2031
+ for (const g of groups) {
2032
+ if (!g.parentId) continue;
2033
+ const list = childrenOf.get(g.parentId) ?? [];
2034
+ list.push(g);
2035
+ childrenOf.set(g.parentId, list);
2036
+ }
2037
+ const isInsideCollapsed = (g: BoundaryLayoutGroup): boolean => {
2038
+ let p = g.parentId ? byId.get(g.parentId) : undefined;
2039
+ while (p) {
2040
+ if (collapsedIds.has(p.id) && isCardFrame(p.region)) return true;
2041
+ p = p.parentId ? byId.get(p.parentId) : undefined;
2042
+ }
2043
+ return false;
2044
+ };
2045
+ // The outermost collapsed card frames: the ones actually drawn as cards.
2046
+ const roots = groups.filter(
2047
+ (g) => collapsedIds.has(g.id) && isCardFrame(g.region) && !isInsideCollapsed(g),
2048
+ );
2049
+ if (roots.length === 0) {
2050
+ return { nodes: [...nodes], edges: [...edges], groups: [...groups], collapsedRegions: [] };
2051
+ }
2052
+
2053
+ // alias (or swallowed group id) → the card that now stands for it.
2054
+ const foldedInto = new Map<string, string>();
2055
+ const removedGroupIds = new Set<string>();
2056
+ for (const root of roots) {
2057
+ const stack = [root];
2058
+ while (stack.length > 0) {
2059
+ const g = stack.pop()!;
2060
+ removedGroupIds.add(g.id);
2061
+ foldedInto.set(g.id, root.id);
2062
+ for (const alias of g.memberAliases) foldedInto.set(alias, root.id);
2063
+ for (const child of childrenOf.get(g.id) ?? []) stack.push(child);
2064
+ }
2065
+ }
2066
+
2067
+ const keptGroups: BoundaryLayoutGroup[] = groups
2068
+ .filter((g) => !removedGroupIds.has(g.id))
2069
+ .map((g) => {
2070
+ // A kept frame that held members of a collapsed card now holds the card.
2071
+ const members: string[] = [];
2072
+ const seen = new Set<string>();
2073
+ for (const alias of g.memberAliases) {
2074
+ const id = foldedInto.get(alias) ?? alias;
2075
+ if (seen.has(id)) continue;
2076
+ seen.add(id);
2077
+ members.push(id);
2078
+ }
2079
+ return { ...g, memberAliases: members };
2080
+ });
2081
+
2082
+ const cards: SubsystemGraphNode[] = roots.map((root) => ({
2083
+ id: root.id,
2084
+ type: 'subsystem-group' as const,
2085
+ position: { x: 0, y: 0 },
2086
+ width: NODE_W,
2087
+ height: NODE_H,
2088
+ ...(root.parentId && !removedGroupIds.has(root.parentId) ? { parentId: root.parentId } : {}),
2089
+ data: { region: root.region, collapsed: true },
2090
+ draggable: false,
2091
+ }));
2092
+ const keptNodes = nodes.filter((n) => !foldedInto.has(n.id));
2093
+
2094
+ // Re-target and fold the edges. First occurrence keeps its id when neither
2095
+ // endpoint moved, so an untouched edge stays addressable by its original id
2096
+ // (trail-step highlighting keys off it).
2097
+ const folded = new Map<string, SubsystemGraphEdge>();
2098
+ const out: SubsystemGraphEdge[] = [];
2099
+ for (const e of edges) {
2100
+ const source = foldedInto.get(e.source) ?? e.source;
2101
+ const target = foldedInto.get(e.target) ?? e.target;
2102
+ if (source === target && (foldedInto.has(e.source) || foldedInto.has(e.target))) continue;
2103
+ const moved = source !== e.source || target !== e.target;
2104
+ if (!moved) {
2105
+ out.push(e);
2106
+ continue;
2107
+ }
2108
+ const mechanism = e.data?.mechanism ?? 'uses';
2109
+ const key = `${source}\0${target}\0${mechanism}`;
2110
+ const existing = folded.get(key);
2111
+ if (existing) {
2112
+ const refs = [...(existing.data?.refs ?? []), ...(e.data?.refs ?? [])];
2113
+ const prev = existing.data as SubsystemGraphEdgeData;
2114
+ existing.data = {
2115
+ ...prev,
2116
+ refs: refs.length > 0 ? [...new Set(refs)] : undefined,
2117
+ rolledUp: (prev.rolledUp ?? 1) + 1,
2118
+ rolledUpIds: [...(prev.rolledUpIds ?? []), e.id],
2119
+ };
2120
+ continue;
2121
+ }
2122
+ const rolled: SubsystemGraphEdge = {
2123
+ ...e,
2124
+ id: derivedGraphEdgeId(source, target, mechanism as SubsystemEdgeMechanism),
2125
+ source,
2126
+ target,
2127
+ data: { ...(e.data as SubsystemGraphEdgeData), rolledUp: 1, rolledUpIds: [e.id] },
2128
+ };
2129
+ folded.set(key, rolled);
2130
+ out.push(rolled);
2131
+ }
2132
+
2133
+ return {
2134
+ nodes: [...keptNodes, ...cards],
2135
+ edges: out,
2136
+ groups: keptGroups,
2137
+ collapsedRegions: roots.map((r) => r.region),
2138
+ };
2139
+ }
1915
2140
  /**
1916
2141
  * Build the full React Flow graph (nodes + edges) using **ELK auto-layout** to
1917
2142
  * position every node (including external stub targets), so the graph is
@@ -1943,6 +2168,15 @@ export async function buildSubsystemGraph(
1943
2168
  * @default 'horizontal'
1944
2169
  */
1945
2170
  layoutDirection?: SubsystemLayoutDirection;
2171
+ /**
2172
+ * Boundary group node ids (see `boundaryGroupNodeId`) drawn collapsed — as a
2173
+ * container card with its members folded away and their lines rolled up
2174
+ * onto it. Only card frames (process / package) collapse. With
2175
+ * `cardFramesCollapsed`, the set lists the frames drawn *open* instead.
2176
+ */
2177
+ collapsedGroupIds?: ReadonlySet<string>;
2178
+ /** Start every card frame collapsed; `collapsedGroupIds` then names the exceptions. */
2179
+ cardFramesCollapsed?: boolean;
1946
2180
  } & BoundaryFrameOptions = {},
1947
2181
  ): Promise<{
1948
2182
  nodes: SubsystemGraphNode[];
@@ -1967,6 +2201,9 @@ export async function buildSubsystemGraph(
1967
2201
  orderByLine = false,
1968
2202
  layoutDirection = 'horizontal',
1969
2203
  codeProcessKeys,
2204
+ regionDetails,
2205
+ collapsedGroupIds,
2206
+ cardFramesCollapsed = false,
1970
2207
  } = opts;
1971
2208
  const frameOpts: BoundaryFrameOptions = {
1972
2209
  showSingletonFrames,
@@ -1987,7 +2224,28 @@ export async function buildSubsystemGraph(
1987
2224
  }
1988
2225
  }
1989
2226
  }
1990
- const regions = layoutGroups.map((g) => g.region);
2227
+ // Card frames carry C4 facts in their header. The host's word wins; a region
2228
+ // it says nothing about reads its technology off its members.
2229
+ {
2230
+ const componentByAlias = new Map(doc.components.map((c) => [c.alias, c]));
2231
+ for (const g of layoutGroups) {
2232
+ if (!isCardFrame(g.region)) continue;
2233
+ const details = regionDetails?.[g.region.key];
2234
+ const technology =
2235
+ details?.technology ??
2236
+ deriveRegionTechnology(
2237
+ g.region.memberAliases
2238
+ .map((alias) => componentByAlias.get(alias))
2239
+ .filter((c): c is SubsystemComponent => c != null),
2240
+ );
2241
+ g.region = {
2242
+ ...g.region,
2243
+ ...(technology != null && { technology }),
2244
+ ...(details?.description != null && { description: details.description }),
2245
+ ...(details?.containerKind != null && { containerKind: details.containerKind }),
2246
+ };
2247
+ }
2248
+ }
1991
2249
  const regionIds = new Set(layoutGroups.map((g) => g.id));
1992
2250
  const byAlias = new Map(doc.components.map((c) => [c.alias, c]));
1993
2251
 
@@ -2061,6 +2319,24 @@ export async function buildSubsystemGraph(
2061
2319
  }
2062
2320
  }
2063
2321
 
2322
+ // Fold the collapsed frames to cards before layout: their members leave the
2323
+ // graph and their lines roll up onto the card, so ELK lays out what is drawn.
2324
+ let collapsedRegions: SubsystemProcessRegion[] = [];
2325
+ const toggled = collapsedGroupIds ?? new Set<string>();
2326
+ const collapsedSet = new Set(
2327
+ layoutGroups
2328
+ .filter((g) => isCardFrame(g.region) && toggled.has(g.id) !== cardFramesCollapsed)
2329
+ .map((g) => g.id),
2330
+ );
2331
+ if (collapsedSet.size > 0) {
2332
+ const folded = collapseBoundaryGroups(nodes, edges, layoutGroups, collapsedSet);
2333
+ nodes.splice(0, nodes.length, ...folded.nodes);
2334
+ edges.splice(0, edges.length, ...folded.edges);
2335
+ layoutGroups.splice(0, layoutGroups.length, ...folded.groups);
2336
+ collapsedRegions = folded.collapsedRegions;
2337
+ }
2338
+ const regions = [...layoutGroups.map((g) => g.region), ...collapsedRegions];
2339
+
2064
2340
  // Same-layer order follows the trails: a component ranks by the first step
2065
2341
  // that mentions it, so a trail reads top-to-bottom (or left-to-right) as it
2066
2342
  // fans out. Components no trail touches follow in declaration order.
@@ -2106,18 +2382,31 @@ export async function buildSubsystemGraph(
2106
2382
  preserveNodePositions: false,
2107
2383
  orderByLine,
2108
2384
  edgeLabels: showEdgeLabels === false ? { enabled: false } : { enabled: true, placement: 'CENTER' },
2109
- groups: layoutGroups.map((g) => ({
2110
- id: g.id,
2111
- memberIds: g.memberAliases,
2112
- parentId: g.parentId,
2113
- // Modules collapse a long path to `first/…/last`, so they only need
2114
- // room for the collapsed badge. Processes/packages never collapse:
2115
- // size the frame to hold the full centered label plus padding.
2116
- minWidth:
2117
- g.region.kind === 'module' || g.region.kind === 'directory'
2118
- ? moduleMinWidthForBadge(g.region.label)
2119
- : boundaryMinWidthForBadge(g.region.label),
2120
- })),
2385
+ groups: layoutGroups.map((g) =>
2386
+ isCardFrame(g.region)
2387
+ ? {
2388
+ id: g.id,
2389
+ memberIds: g.memberAliases,
2390
+ parentId: g.parentId,
2391
+ // A card frame wraps its label to two lines, so it needs no more
2392
+ // width than the container card it is drawn as.
2393
+ minWidth: NODE_W,
2394
+ // Reserve the header rows above the members and the card inset
2395
+ // around them — the same chrome a grown C4 container reserves,
2396
+ // so ELK's fit already clears it.
2397
+ padTop: OPEN_HEADER_PAD,
2398
+ padBottom: OPEN_SIDE_PAD,
2399
+ padX: OPEN_SIDE_PAD,
2400
+ }
2401
+ : {
2402
+ id: g.id,
2403
+ memberIds: g.memberAliases,
2404
+ parentId: g.parentId,
2405
+ // Modules collapse a long path to `first/…/last`, so they only
2406
+ // need room for the collapsed badge.
2407
+ minWidth: moduleMinWidthForBadge(g.region.label),
2408
+ },
2409
+ ),
2121
2410
  // Honour `showSingletonFrames`: a one-member process or module is
2122
2411
  // still a real boundary. `buildBoundaryLayoutGroups` already made
2123
2412
  // those regions claim their members, so keeping them here cannot
@@ -2147,9 +2436,10 @@ export async function buildSubsystemGraph(
2147
2436
  else if (g.region.kind === 'process') processGroupNodes.push(group);
2148
2437
  else moduleGroupNodes.push(group);
2149
2438
  }
2150
- // Clear leaf parentIds that point at groups ELK dropped.
2439
+ // Clear leaf parentIds that point at groups ELK dropped (components and
2440
+ // collapsed cards alike).
2151
2441
  for (const n of result.nodes as SubsystemGraphNode[]) {
2152
- if (n.type !== 'subsystem-component') continue;
2442
+ if (n.type !== 'subsystem-component' && !(n.data as SubsystemGroupNodeData).collapsed) continue;
2153
2443
  const parentId = (n as { parentId?: string }).parentId;
2154
2444
  if (parentId && !builtGroupIds.has(parentId)) {
2155
2445
  delete (n as { parentId?: string }).parentId;
@@ -1,7 +1,8 @@
1
1
  import { afterEach, describe, expect, test } from 'bun:test';
2
2
  import { createElement } from 'react';
3
3
  import { Window } from 'happy-dom';
4
- import { cleanup, render } from '@testing-library/react/pure';
4
+ import { cleanup, fireEvent, render } from '@testing-library/react/pure';
5
+ import { ReactFlowProvider } from '@xyflow/react';
5
6
  import { ThemeProvider, defaultEditorTheme } from '@principal-ade/industry-theme';
6
7
  import { SubsystemGroupNode } from './nodes';
7
8
 
@@ -32,16 +33,21 @@ const region = {
32
33
  };
33
34
 
34
35
  function renderGroup(data: Record<string, unknown>) {
36
+ // A collapsed card mounts React Flow handles, which need the flow store.
35
37
  return render(
36
38
  createElement(
37
- ThemeProvider,
38
- { theme: defaultEditorTheme },
39
- createElement(SubsystemGroupNode as never, {
40
- data,
41
- width: 200,
42
- height: 120,
43
- selected: false,
44
- } as never),
39
+ ReactFlowProvider,
40
+ null,
41
+ createElement(
42
+ ThemeProvider,
43
+ { theme: defaultEditorTheme },
44
+ createElement(SubsystemGroupNode as never, {
45
+ data,
46
+ width: 200,
47
+ height: 120,
48
+ selected: false,
49
+ } as never),
50
+ ),
45
51
  ),
46
52
  );
47
53
  }
@@ -64,15 +70,16 @@ describe('SubsystemGroupNode frame color', () => {
64
70
  });
65
71
 
66
72
  describe('SubsystemGroupNode chrome', () => {
67
- // Runtime chrome asserts "deployment unit": square, solid, tinted. A
68
- // process region the host marked `chrome: 'package'` groups code
69
- // organisation instead — a library, an area, a proposal — and draws as a
70
- // package boundary: rounded, dashed, transparent.
73
+ // A process or package is a container in the C4 sense, so it draws as a
74
+ // container card: filled body, header rows. Runtime chrome (a plain process)
75
+ // is solid with a slight radius; a process the host marked `chrome: 'package'`
76
+ // groups code organisation — a library, an area, a proposal — and draws as a
77
+ // package boundary: rounded, dashed.
71
78
  test('a plain process region wears runtime chrome', () => {
72
79
  const { container } = renderGroup({ region });
73
80
  const frame = container.querySelector('div') as HTMLElement;
74
81
  expect(frame.style.border).toContain('solid');
75
- expect(frame.style.borderRadius).toBe('0px');
82
+ expect(frame.style.borderRadius).toBe('4px');
76
83
  expect(frame.style.background).not.toBe('transparent');
77
84
  });
78
85
 
@@ -83,18 +90,89 @@ describe('SubsystemGroupNode chrome', () => {
83
90
  const frame = container.querySelector('div') as HTMLElement;
84
91
  expect(frame.style.border).toContain('dashed');
85
92
  expect(frame.style.borderRadius).toBe('12px');
86
- expect(frame.style.background).toBe('transparent');
87
93
  });
88
94
 
89
95
  test('chrome does not change the region identity the frame renders', () => {
90
96
  const { container } = renderGroup({
91
97
  region: { ...region, chrome: 'package' },
92
98
  });
93
- // The badge still reads the label — kind is chrome's business alone.
99
+ // The header still reads the label — kind is chrome's business alone.
94
100
  expect(container.textContent).toContain('booking-web/server');
95
101
  });
102
+
103
+ test('a module region keeps the bare dashed frame and path badge', () => {
104
+ const { container } = renderGroup({
105
+ region: {
106
+ kind: 'module',
107
+ key: 'src/bun/index.ts',
108
+ label: 'src/bun/index.ts',
109
+ memberAliases: ['a', 'b'],
110
+ },
111
+ });
112
+ const frame = container.querySelector('div') as HTMLElement;
113
+ expect(frame.style.border).toContain('dashed');
114
+ expect(frame.style.background).toBe('transparent');
115
+ // The badge may collapse a long path to `first/…/last` at this width.
116
+ expect(container.textContent).toContain('index.ts');
117
+ });
96
118
  });
97
119
 
120
+ describe('SubsystemGroupNode card header', () => {
121
+ // The header is the C4 container card's: label, kind glyph, technology row
122
+ // with the member count. A frame with no stated technology falls back to its
123
+ // kind so the row is never blank.
124
+ test('a process header carries the member count and the application glyph', () => {
125
+ const { container } = renderGroup({ region });
126
+ expect(container.textContent).toContain('2');
127
+ expect(container.querySelector('.lucide-app-window')).not.toBeNull();
128
+ expect(container.textContent).toContain('application');
129
+ });
130
+
131
+ test('a data-store process wears the database glyph', () => {
132
+ const { container } = renderGroup({ region: { ...region, containerKind: 'data-store' } });
133
+ expect(container.querySelector('.lucide-database')).not.toBeNull();
134
+ expect(container.querySelector('.lucide-app-window')).toBeNull();
135
+ });
136
+
137
+ test('a stated technology fills the technology row and colours the frame', () => {
138
+ const { container } = renderGroup({ region: { ...region, technology: 'Bun' } });
139
+ expect(container.textContent).toContain('Bun');
140
+ const frame = container.querySelector('div') as HTMLElement;
141
+ // Bun's brand hue, not the key hash.
142
+ expect(frame.style.border).toContain('#fbf0df');
143
+ // The official mark rides the row.
144
+ expect(container.querySelector('[aria-label="Bun"]')).not.toBeNull();
145
+ });
146
+
147
+ test('an unknown technology keeps the hashed frame colour', () => {
148
+ const { container } = renderGroup({
149
+ region: { ...region, key: 'booking-web/client', technology: 'COBOL' },
150
+ });
151
+ const frame = container.querySelector('div') as HTMLElement;
152
+ expect(frame.style.border).toContain('#ff6b35');
153
+ expect(container.textContent).toContain('COBOL');
154
+ });
155
+
156
+ test('an explicit data.color still beats the technology hue', () => {
157
+ const { container } = renderGroup({ region: { ...region, technology: 'Bun' }, color: '#d9a441' });
158
+ const frame = container.querySelector('div') as HTMLElement;
159
+ expect(frame.style.border).toContain('#d9a441');
160
+ });
161
+
162
+ test('a package header falls back to "package" and wears the glyph', () => {
163
+ const { container } = renderGroup({
164
+ region: {
165
+ kind: 'package',
166
+ key: 'pkg:github/acme/app',
167
+ label: 'acme/app',
168
+ memberAliases: ['a', 'b', 'c'],
169
+ },
170
+ });
171
+ expect(container.textContent).toContain('package');
172
+ expect(container.textContent).toContain('3');
173
+ expect(container.querySelector('.lucide-package')).not.toBeNull();
174
+ });
175
+ });
98
176
  describe('SubsystemGroupNode package glyph', () => {
99
177
  // A frame that renders as a package boundary wears the Package glyph: a
100
178
  // real package (purl repo identity) or a process claim the host demoted to
@@ -211,3 +289,34 @@ describe('SubsystemGroupNode boundary badge', () => {
211
289
  expect(container.querySelector('.lucide-circle-ellipsis')).not.toBeNull();
212
290
  });
213
291
  });
292
+
293
+ describe('SubsystemGroupNode collapse toggle', () => {
294
+ test('no toggle without a callback', () => {
295
+ const { container } = renderGroup({ region });
296
+ expect(container.querySelector('button')).toBeNull();
297
+ });
298
+
299
+ test('open frame offers "Hide members" and fires the toggle', () => {
300
+ let toggled = 0;
301
+ const { container } = renderGroup({ region, onToggleCollapse: () => { toggled += 1; } });
302
+ const btn = container.querySelector('button') as HTMLButtonElement;
303
+ expect(btn.getAttribute('aria-label')).toBe('Hide members');
304
+ expect(btn.className).toContain('nodrag');
305
+ fireEvent.click(btn);
306
+ expect(toggled).toBe(1);
307
+ });
308
+
309
+ test('collapsed card offers "Show members" and grows handles for its lines', () => {
310
+ const { container } = renderGroup({ region, collapsed: true, onToggleCollapse: () => {} });
311
+ const btn = container.querySelector('button') as HTMLButtonElement;
312
+ expect(btn.getAttribute('aria-label')).toBe('Show members');
313
+ expect(btn.getAttribute('aria-pressed')).toBe('true');
314
+ expect(container.querySelectorAll('.react-flow__handle').length).toBe(2);
315
+ expect(container.querySelector('.lucide-maximize-2')).not.toBeNull();
316
+ });
317
+
318
+ test('an open card has no handles — its members carry the lines', () => {
319
+ const { container } = renderGroup({ region });
320
+ expect(container.querySelectorAll('.react-flow__handle').length).toBe(0);
321
+ });
322
+ });