Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

test(ui): guard the scope heights and cursor zoom of the architecture scenes

Bernhard Jackiewicz9bb07f5495e0
diff --git a/graph-ui/src/architecture/ArchitectureScene.tsx b/graph-ui/src/architecture/ArchitectureScene.tsx
index f2823823e..bf5cc74d4 100644
--- a/graph-ui/src/architecture/ArchitectureScene.tsx
+++ b/graph-ui/src/architecture/ArchitectureScene.tsx
@@ -4,7 +4,7 @@ import { Edges, Html, OrbitControls, OrthographicCamera } from '@react-three/dre
import { Box3, Color, CubicBezierCurve3, MOUSE, TOUCH, OrthographicCamera as ThreeOrthographicCamera, QuadraticBezierCurve3, Vector2, Vector3 } from 'three';
import type { OrbitControls as OrbitControlsImpl } from 'three-stdlib';
import type { SemanticEdge, SemanticGraph, SemanticNode, SemanticPlatform } from './semantic-graph';
-import { languageColor, measureSourceNode, sourceBrickHeight, sourceLanguage, sourceNodeSizePercent, type SourceCatalog, type SourceMeasure } from './source-metrics';
+import { languageColor, measureSourceNode, scopeBrickHeights, sourceLanguage, sourceNodeSizePercent, type SourceCatalog, type SourceMeasure } from './source-metrics';
import { gravityPercent, gravityStrength, hotspotsForNode, type HotspotCatalog, type HotspotGroup } from './hotspot-map';
import { labelEdges, labelInset, labelRect, labelsCollide, type LabelBox, type LabelRect } from './label-space';
import { edgeColor, isDirectedEdge } from '../graph/edge-style';
@@ -285,11 +285,7 @@ function FitArchitecture({ model, planar, resetKey, controls, active }: {
export function ArchitectureScene({ model: graphModel, selectedId, selectedEdgeId, onSelect, onSelectEdge, onOpen, onClearSelection, active = true, planar = false, resetKey = 0, catalog, heightMetric = 'uniform', colorMetric = 'kind', hotspots, showHotspots = true, adaptiveLabels = false }: ArchitectureSceneProps) {
const model: RenderGraph = useMemo(() => {
const measures = new Map(graphModel.nodes.map(node => [node.id, catalog ? measureSourceNode(node, catalog) : undefined]));
- // Heights compare the bricks of this scope with each other. Root files are
- // the flat baseline; outside areas are context and stay flat as well.
- const rootFiles = (node: SemanticNode) => node.kind === 'area' && node.areaPath === '(root)';
- const reference = Math.max(1, ...graphModel.nodes.filter(node => ['area', 'file'].includes(node.kind) && !node.external && !rootFiles(node))
- .map(node => measures.get(node.id)?.lines ?? 0));
+ const heights = catalog && heightMetric === 'lines' ? scopeBrickHeights(graphModel.nodes, node => measures.get(node.id)?.lines) : undefined;
return { ...graphModel, nodes: graphModel.nodes.map(node => {
const measure = measures.get(node.id);
const sizePercent = catalog ? sourceNodeSizePercent(node, catalog) : undefined;
@@ -297,7 +293,7 @@ export function ArchitectureScene({ model: graphModel, selectedId, selectedEdgeI
const hotspot = showHotspots && hotspots ? hotspotsForNode(node, hotspots) : undefined;
return { ...node, measure, sizePercent, hotspot, gravity: hotspot && hotspots ? gravityStrength(hotspot.maxFanIn, hotspots.maxFanIn) : 0,
gravityPercent: hotspot && hotspots ? gravityPercent(hotspot.maxFanIn, hotspots.maxFanIn) : undefined,
- height: catalog && heightMetric === 'lines' && ['area', 'file'].includes(node.kind) && !node.external ? rootFiles(node) ? 0.35 : sourceBrickHeight(measure?.lines, reference) : undefined,
+ height: heights?.get(node.id),
tint: node.tint ?? (colorMetric === 'language' && node.kind !== 'route' ? languageColor(language ?? 'Unknown') : undefined) };
}) };
}, [graphModel, catalog, heightMetric, colorMetric, hotspots, showHotspots]);
diff --git a/graph-ui/src/architecture/architecture-scenes.test.tsx b/graph-ui/src/architecture/architecture-scenes.test.tsx
new file mode 100644
index 000000000..9c38edd48
--- /dev/null
+++ b/graph-ui/src/architecture/architecture-scenes.test.tsx
@@ -0,0 +1,40 @@
+import { Children, isValidElement, type ReactElement, type ReactNode } from 'react';
+import { renderToStaticMarkup } from 'react-dom/server';
+import { OrbitControls } from '@react-three/drei';
+import { describe, expect, it, vi } from 'vitest';
+import type { SemanticGraph } from './semantic-graph';
+
+const canvas = vi.hoisted(() => ({ children: undefined as unknown }));
+vi.mock('@react-three/fiber', async (importOriginal) => ({
+ ...await importOriginal<typeof import('@react-three/fiber')>(),
+ Canvas: ({ children }: { children: unknown }) => { canvas.children = children; return null; },
+}));
+import { ArchitectureScene } from './ArchitectureScene';
+import SystemArchitectureScene from './SystemArchitectureScene';
+
+function elements(children: ReactNode): ReactElement<Record<string, unknown>>[] {
+ return Children.toArray(children).flatMap((child) => {
+ if (!isValidElement<Record<string, unknown>>(child)) return [];
+ return [child, ...elements(child.props['children'] as ReactNode)];
+ });
+}
+const orbit = () => elements(canvas.children as ReactNode).filter(element => element.type === OrbitControls);
+const model: SemanticGraph = { view: 'overview', scopeKey: 'overview', title: 'Repository structure', positionMeaning: '', nodes: [], edges: [],
+ totalNodes: 0, totalEdges: 0, omittedNodes: 0, omittedEdges: 0, warnings: [] };
+
+describe('architecture scene controls', () => {
+ it('zooms the repository map toward the cursor', () => {
+ renderToStaticMarkup(<ArchitectureScene model={model} onSelect={vi.fn()} onSelectEdge={vi.fn()} />);
+ expect(orbit()).toHaveLength(1);
+ expect(orbit()[0]!.props['zoomToCursor']).toBe(true);
+ });
+ it('zooms the system structure and behavior scenes toward the cursor', () => {
+ for (const presentation of ['system', 'journey'] as const) {
+ canvas.children = undefined;
+ renderToStaticMarkup(<SystemArchitectureScene model={{ nodes: [], edges: [], lanes: [], omittedNodes: 0, omittedEdges: 0 }} resetKey={0}
+ onSelectNode={vi.fn()} onSelectEdge={vi.fn()} presentation={presentation} />);
+ expect(orbit()).toHaveLength(1);
+ expect(orbit()[0]!.props['zoomToCursor']).toBe(true);
+ }
+ });
+});
diff --git a/graph-ui/src/architecture/source-metrics.test.ts b/graph-ui/src/architecture/source-metrics.test.ts
index 6251bfd68..a3d3a030c 100644
--- a/graph-ui/src/architecture/source-metrics.test.ts
+++ b/graph-ui/src/architecture/source-metrics.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import type { GraphData, GraphNode } from '../galaxy/types';
import { buildSemanticGraph } from './semantic-graph';
-import { collectSourceMetrics, measureSourceNode, sourceBrickHeight, sourceLanguage, languageColor, sourceNodeSizePercent } from './source-metrics';
+import { collectSourceMetrics, measureSourceNode, scopeBrickHeights, sourceBrickHeight, sourceLanguage, languageColor, sourceNodeSizePercent } from './source-metrics';
const node = (id: number, file_path: string, label: string, start_line?: number, end_line?: number): GraphNode => ({
id, file_path, label, start_line, end_line, name: `node${id}`, x: 0, y: 0, z: 0, size: 1, color: '',
@@ -78,6 +78,24 @@ describe('source size and language encoding', () => {
expect(heights[2] - heights[1]).toBeGreaterThan(8);
expect(sourceBrickHeight(0, 10000)).toBe(1.5);
});
+ it('scales brick heights within the opened scope, with root files and outside areas flat', () => {
+ const input: GraphData = { nodes: [node(1, 'django/db/models.py', 'Module', 1, 1000), node(2, 'django/utils/text.py', 'Module', 1, 250),
+ node(3, 'django/shortcuts.py', 'Module', 1, 50), node(4, 'tests/runtests.py', 'Module', 1, 90000), node(5, 'setup.py', 'Module', 1, 50000)],
+ edges: [{ source: 4, target: 1, type: 'CALLS' }, { source: 5, target: 2, type: 'IMPORTS' }], total_nodes: 5 };
+ const catalog = collectSourceMetrics(input);
+ const model = buildSemanticGraph(input, { view: 'overview', areaPath: 'django' });
+ const heights = scopeBrickHeights(model.nodes, item => measureSourceNode(item, catalog)?.lines);
+ // The largest part inside 'django' is full height, though the outside 'tests' area is far larger.
+ expect(heights.get('area:django/db')).toBe(14);
+ expect(heights.get('area:django/utils')).toBe(1.5 + 12.5 * Math.sqrt(0.25));
+ expect(heights.get('file:django/shortcuts.py')).toBe(1.5 + 12.5 * Math.sqrt(0.05));
+ expect(model.nodes.filter(item => item.external).map(item => [item.id, heights.get(item.id)])).toEqual([['area:(root)', undefined], ['area:tests', undefined]]);
+ const root = buildSemanticGraph(input, { view: 'overview' });
+ const rootHeights = scopeBrickHeights(root.nodes, item => measureSourceNode(item, catalog)?.lines);
+ expect(rootHeights.get('area:(root)')).toBe(0.35);
+ expect(rootHeights.get('area:tests')).toBe(14);
+ expect(rootHeights.get('area:django')).toBe(1.5 + 12.5 * Math.sqrt(1300 / 90000));
+ });
it('uses the largest non-root folder as 100 percent despite large root-direct files', () => {
const input: GraphData = { nodes: [node(1, 'root.ts', 'Module', 1, 1000000), node(2, 'other.ts', 'Module', 1, 2000000),
node(3, 'src/large/a.ts', 'Module', 1, 800), node(4, 'src/small/a.ts', 'Module', 1, 333)], edges: [], total_nodes: 4 };
diff --git a/graph-ui/src/architecture/source-metrics.ts b/graph-ui/src/architecture/source-metrics.ts
index be1399691..40704e994 100644
--- a/graph-ui/src/architecture/source-metrics.ts
+++ b/graph-ui/src/architecture/source-metrics.ts
@@ -150,3 +150,15 @@ export function sourceBrickHeight(lines: number | undefined, referenceLines: num
const reference = Number.isFinite(referenceLines) ? Math.max(1, referenceLines) : 1;
return 1.5 + 12.5 * Math.sqrt(Math.min(1, lines / reference));
}
+
+/**
+ * Brick heights compare the bricks of one scope with each other. Root files
+ * are the flat baseline; outside areas are context and stay flat as well, so
+ * neither sets the scale.
+ */
+export function scopeBrickHeights(nodes: SemanticNode[], lines: (node: SemanticNode) => number | undefined): Map<string, number> {
+ const rootFiles = (node: SemanticNode) => node.kind === 'area' && node.areaPath === '(root)';
+ const bricks = nodes.filter(node => (node.kind === 'area' || node.kind === 'file') && !node.external);
+ const reference = Math.max(1, ...bricks.filter(node => !rootFiles(node)).map(node => lines(node) ?? 0));
+ return new Map(bricks.map(node => [node.id, rootFiles(node) ? 0.35 : sourceBrickHeight(lines(node), reference)]));
+}