An agent that built a graph through MCP left every new node in one row. In the first real test that put nine nodes on a line 2080 px long, with the Owner's links running straight through all the others. Placing nodes by hand cost nine move_node calls and guessed heights. The agent now gives no coordinates, and the editor lays the nodes out. - arrange.ts is a pure function. It puts the flow into columns from left to right and sets an Owner or Get right before the first node it feeds. It orders each column for the fewest crossings and lines nodes up by their linked ports without overlaps. - Node sizes come from the drawn NodeView in the browser and from an estimate in unit tests. Both share the constants in nodeBox.ts. - run_commands arranges the nodes that came without x and y when the call ends, inside the same undo step. Nodes that were there stay. - New command arrange_graph and new tool lint_graph. The lint counts overlaps, crossings, links through foreign nodes, backward links and the longest links, with example ids. - The relay instructions tell the agent not to give coordinates and to fetch far values with a second Get or Owner node. - An Arrange button next to the snap magnet in all eight graph tabs. It arranges the selected nodes, or all of them, as one undo step. Found while testing on a real project: - lint_graph missed a crossing where two links swap rows exactly mirrored. Both curves then share a sample point in the middle, and only a crossing between two sample points was counted. Links that meet on a point count now (segmentsMeet). - The Arrow-key graph with one Owner for four Moves cannot get below 8 crossings. The Owner feeds object in row 2 of Move, between exec and dt of the key, so it always crosses one of them. Tests: - graph-arrange, agent-edit, snap-toggle: 33/33. The swapped-rows test was red before the fix (0 instead of 1). - Four counter-checks that bend arrange or the edit plan, all red. - e2e 71 on melektaus-atx: 18/18. Lint after the build shows 0 overlaps, 0 links through nodes, 0 backward. Arrange takes a tangled graph from 3 backward links and 2 crossings to 0, and one Ctrl+Z restores it. - Gate on melektaus-atx: CHECK EXIT 0, 318 files, 4917 tests, measured before the lint fix. - Live through MCP on the user's project: lint_graph reports 8 crossings after Arrange, matching the screenshot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
3.0 KiB
TypeScript
79 lines
3.0 KiB
TypeScript
/**
|
||
* The magnet in the tab headers (#194): one icon toggle for every tab that snaps, graph canvas and
|
||
* vector art alike. Driven through the button's own `onClick`, the path a click takes.
|
||
*/
|
||
import { beforeAll, describe, expect, it, vi } from 'vitest';
|
||
import { CanvasTextMetrics } from 'pixi.js';
|
||
import { GRAPH_SNAPS_AT_FIRST, SnapToggle } from '../../src/editor/graph/SnapToggle';
|
||
import { Theme } from '../../src/ui';
|
||
|
||
// Node has no DOMParser to read the file with; what the magnet looks like is the icon tests' job.
|
||
vi.mock('../../src/ui/svg', () => ({ drawSvg: () => undefined, tintSvg: (svg: string) => svg }));
|
||
|
||
beforeAll(() => {
|
||
CanvasTextMetrics.measureText = (() => ({ width: 10, height: 10, lines: [], lineWidths: [] })) as never;
|
||
});
|
||
|
||
function target() {
|
||
const calls: boolean[] = [];
|
||
return { calls, setSnapping: (on: boolean) => calls.push(on) };
|
||
}
|
||
|
||
describe('SnapToggle', () => {
|
||
it('is a square icon button without a caption', () => {
|
||
const snap = new SnapToggle(() => target());
|
||
expect(snap.widgets).toEqual([snap.button]);
|
||
expect(snap.button.measure().w).toBe(Theme.controlHeight);
|
||
expect(snap.button.text).toBe('');
|
||
});
|
||
|
||
it('starts off and switches the target on and off again, lit while on', () => {
|
||
const canvas = target();
|
||
const snap = new SnapToggle(() => canvas);
|
||
expect(snap.button.toggled).toBe(false);
|
||
|
||
snap.button.onClick?.();
|
||
expect(snap.button.toggled).toBe(true);
|
||
|
||
snap.button.onClick?.();
|
||
expect(snap.button.toggled).toBe(false);
|
||
expect(canvas.calls).toEqual([true, false]);
|
||
});
|
||
|
||
it('starts lit where its target snaps from the start, as every graph does (#391)', () => {
|
||
const canvas = target();
|
||
const snap = new SnapToggle(() => canvas, GRAPH_SNAPS_AT_FIRST);
|
||
expect(GRAPH_SNAPS_AT_FIRST).toBe(true);
|
||
expect(snap.button.toggled).toBe(true);
|
||
expect(snap.snapping).toBe(true);
|
||
// Nothing is asked of the target before a click: it snaps by itself from the start.
|
||
expect(canvas.calls).toEqual([]);
|
||
|
||
snap.button.onClick?.();
|
||
expect(snap.button.toggled).toBe(false);
|
||
expect(canvas.calls).toEqual([false]);
|
||
});
|
||
|
||
it('has Arrange beside it in a graph’s tab, greyed out with it, arranging the target of the click (#429)', () => {
|
||
let arranged = 0;
|
||
const canvas = { ...target(), arrange: () => (arranged += 1) };
|
||
const snap = new SnapToggle(() => canvas, GRAPH_SNAPS_AT_FIRST, true);
|
||
expect(snap.widgets).toEqual([snap.button, snap.arrangeButton]);
|
||
expect(snap.arrangeButton!.text).toBe('Arrange');
|
||
snap.arrangeButton!.onClick?.();
|
||
expect(arranged).toBe(1);
|
||
snap.setEnabled(false);
|
||
expect(snap.arrangeButton!.enabled).toBe(false);
|
||
expect(new SnapToggle(() => canvas).arrangeButton).toBeNull();
|
||
});
|
||
|
||
it('asks for the target at the click, so a canvas mounted later is the one switched', () => {
|
||
let canvas = target();
|
||
const snap = new SnapToggle(() => canvas);
|
||
const later = target();
|
||
canvas = later;
|
||
snap.set(true);
|
||
expect(later.calls).toEqual([true]);
|
||
});
|
||
});
|