Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 2 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ RootStore
```typescript
BlockState {
$rawState: Signal<TBlock> // Raw block data
$state: computed(() => ...) // Full state + selection
$state: computed(() => ...) // Full block data; selection is not part of it, read $selected
$geometry: computed(() => ...) // x, y, width, height
$selected: computed(() => ...) // Selection state
$anchorStates: Signal<AnchorState[]>
Expand Down Expand Up @@ -1215,14 +1215,13 @@ export const MyStory = () => {

### Data Types

**Blocks must have required TBlock fields:**
**Blocks must have required TBlock fields** (selection is not block data; it lives in the selection buckets, read it through `$selected`, `selectedTag` or the selection API):
```typescript
const blocksData: TBlock[] = [
{
is: "Block" as const, // Required
id: "block-1", // Required
name: "Block 1", // Required
selected: false, // Required
x: 100,
y: 100,
width: 200,
Expand Down
3 changes: 0 additions & 3 deletions apps/e2e/tests/block/block-click.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test.describe("Block Click Selection", () => {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
{
id: "block-2",
Expand All @@ -30,7 +29,6 @@ test.describe("Block Click Selection", () => {
height: 100,
name: "Block 2",
anchors: [],
selected: false,
},
{
id: "block-3",
Expand All @@ -41,7 +39,6 @@ test.describe("Block Click Selection", () => {
height: 100,
name: "Block 3",
anchors: [],
selected: false,
},
],
connections: [],
Expand Down
4 changes: 0 additions & 4 deletions apps/e2e/tests/block/block-geometry-batching.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const TWO_BLOCKS = [
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
{
id: "block-2",
Expand All @@ -23,7 +22,6 @@ const TWO_BLOCKS = [
height: 100,
name: "Block 2",
anchors: [],
selected: false,
},
];

Expand Down Expand Up @@ -144,7 +142,6 @@ test.describe("block-change + coalesced blocks-geometry-change", () => {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
group: "group-1",
},
{
Expand All @@ -156,7 +153,6 @@ test.describe("block-change + coalesced blocks-geometry-change", () => {
height: 100,
name: "Block 2",
anchors: [],
selected: false,
group: "group-1",
},
];
Expand Down
2 changes: 0 additions & 2 deletions apps/e2e/tests/block/block-hover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test.describe("Block Hover", () => {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
{
id: "block-2",
Expand All @@ -30,7 +29,6 @@ test.describe("Block Hover", () => {
height: 100,
name: "Block 2",
anchors: [],
selected: false,
},
],
connections: [],
Expand Down
2 changes: 0 additions & 2 deletions apps/e2e/tests/block/block-render-delegated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const BLOCKS = [
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
{
id: "block-2",
Expand All @@ -41,7 +40,6 @@ const BLOCKS = [
height: 100,
name: "Block 2",
anchors: [],
selected: false,
},
];

Expand Down
46 changes: 46 additions & 0 deletions apps/e2e/tests/block/block-scene-order.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect, test } from "@playwright/test";

import { GraphPageObject } from "../../page-objects/GraphPageObject";

test.describe("Blocks in the scene: hit order", () => {
let graphPO: GraphPageObject;

test.beforeEach(async ({ page }) => {
graphPO = new GraphPageObject(page);
await graphPO.initialize({
blocks: [
{
id: "lower",
is: "Block",
x: 100,
y: 100,
width: 200,
height: 100,
name: "Lower",
anchors: [{ id: "out", blockId: "lower", type: "OUT" }],
},
{ id: "upper", is: "Block", x: 220, y: 140, width: 200, height: 100, name: "Upper", anchors: [] },
],
connections: [],
});
});

test("a click in the overlap selects the block drawn on top", async () => {
await graphPO.clickAt({ x: 260, y: 170 });
expect(await graphPO.getSelectedBlockIds()).toEqual(["upper"]);
});

test("a block raised by selection stays on top after deselection", async () => {
await graphPO.evaluate((graph) => graph.api.selectBlocks(["lower"], true));
await graphPO.evaluate((graph) => graph.api.selectBlocks(["lower"], false));
await graphPO.waitForFrames(3);
await graphPO.clickAt({ x: 260, y: 170 });
expect(await graphPO.getSelectedBlockIds()).toEqual(["lower"]);
});

test("an anchor of the lower block under the upper block is not hit", async () => {
// The OUT anchor of "lower" is at (300, 188), under "upper".
await graphPO.clickAt({ x: 300, y: 188 });
expect(await graphPO.getSelectedBlockIds()).toEqual(["upper"]);
});
});
2 changes: 0 additions & 2 deletions apps/e2e/tests/block/block-visibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const BLOCKS = [
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
{
id: "block-2",
Expand All @@ -35,7 +34,6 @@ const BLOCKS = [
height: 100,
name: "Block 2",
anchors: [],
selected: false,
},
];

Expand Down
165 changes: 165 additions & 0 deletions apps/e2e/tests/block/scene-webgl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { expect, test } from "@playwright/test";
import type { Page } from "@playwright/test";

import type { GraphConfig } from "../../page-objects/GraphPageObject";
import { GraphPageObject } from "../../page-objects/GraphPageObject";

/**
* The scene's WebGL backend in a real browser: blocks and anchors draw through it, and a change after the first frame
* (selection, raise) goes through `applyMany`. A backend failure falls back to Canvas2D silently, with the same picture,
* so the renderer state is checked after every step.
*/

/** Schematic: from SCALES[1] (0.225) to SCALES[2] (0.7); anchors draw there. */
const SCALE = 0.5;

const CONFIG: GraphConfig = {
blocks: [
{ id: "lower", is: "Block", x: 100, y: 100, width: 200, height: 100, name: "L", anchors: [] },
{ id: "upper", is: "Block", x: 220, y: 140, width: 200, height: 100, name: "U", anchors: [] },
{
id: "solo",
is: "Block",
x: 600,
y: 100,
width: 200,
height: 100,
name: "S",
anchors: [{ id: "out", blockId: "solo", type: "OUT" }],
},
],
connections: [],
settings: { renderer: "webgl", useBlocksAnchors: true },
};

const LOWER_BODY: [number, number, number] = [208, 32, 32];
const UPPER_BODY: [number, number, number] = [32, 80, 208];

type TWorldRect = { x: number; y: number; width: number; height: number };

async function hasWebGL2(page: Page): Promise<boolean> {
return page.evaluate(() => Boolean(document.createElement("canvas").getContext("webgl2")));
}

async function rendererState(page: Page): Promise<string> {
return page.evaluate(() => window.graph.scene.$renderer.value);
}

/** Counts the screen pixels of a world rect within `tolerance` of `rgb` on every channel, and the pixels checked. */
async function countColor(
page: Page,
rect: TWorldRect,
rgb: [number, number, number],
tolerance: number
): Promise<{ matched: number; total: number }> {
const clip = await page.evaluate((world) => {
const { scale, x, y } = window.graph.cameraService.getCameraState();
const origin = window.graph.getGraphCanvas()?.getBoundingClientRect() ?? { left: 0, top: 0 };
return {
x: Math.round(origin.left + world.x * scale + x),
y: Math.round(origin.top + world.y * scale + y),
width: Math.max(1, Math.round(world.width * scale)),
height: Math.max(1, Math.round(world.height * scale)),
};
}, rect);
const png = await page.screenshot({ clip, scale: "css" });
return page.evaluate(
async ({ base64, target, within }) => {
const image = new Image();
image.src = `data:image/png;base64,${base64}`;
await image.decode();
const canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
const ctx = canvas.getContext("2d");
if (!ctx) return { matched: 0, total: 0 };
ctx.drawImage(image, 0, 0);
const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
let matched = 0;
for (let i = 0; i < data.length; i += 4) {
if (target.every((value, k) => Math.abs(value - data[i + k]) <= within)) matched++;
}
return { matched, total: data.length / 4 };
},
{ base64: png.toString("base64"), target: rgb, within: tolerance }
);
}

/** The anchor's port point in world units: the anchor component is created lazily, the port always exists. */
async function anchorPoint(page: Page, blockId: string, anchorId: string): Promise<{ x: number; y: number }> {
return page.evaluate(
({ block, anchor }) => {
const component = window.graph.rootStore.blocksList.getBlockState(block)?.getViewComponent();
if (!component) throw new Error(`no block ${block}`);
return component.getAnchorPort(anchor).getPoint();
},
{ block: blockId, anchor: anchorId }
);
}

test.describe("Scene blocks with the WebGL renderer", () => {
test("draws blocks and a selected anchor's ring through WebGL, and a selection raise after the first frame", async ({
page,
}) => {
const graphPO = new GraphPageObject(page);
await graphPO.initialize(CONFIG);
test.skip(!(await hasWebGL2(page)), "Headless Chromium has no WebGL2");

await page.evaluate(
({ scale, lower, upper }) => {
const { CanvasBlock, defineTag } = window.GraphModule;
const graph = window.graph;
const lowerTag = defineTag("e2e-lower");
const upperTag = defineTag("e2e-upper");
const color = (rgb: number[]): string => `rgb(${rgb.join(", ")})`;
graph.scene.styles.add([
{ target: CanvasBlock, tags: [lowerTag], style: { parts: { body: { color: color(lower) } } } },
{ target: CanvasBlock, tags: [upperTag], style: { parts: { body: { color: color(upper) } } } },
]);
graph.scene.tags.add(lowerTag, { block: ["lower"] });
graph.scene.tags.add(upperTag, { block: ["upper"] });
graph.cameraService.set({ scale, x: 0, y: 0 });
},
{ scale: SCALE, lower: LOWER_BODY, upper: UPPER_BODY }
);
await expect.poll(() => rendererState(page), { timeout: 10_000 }).toBe("webgl");
await graphPO.waitForFrames(3);
expect(await page.evaluate(() => window.graph.scene.$level.value)).toBe("schematic");

// A block body with the theme's colour, in its lower left part (the title is centred at the top).
const body = await page.evaluate(() => window.graph.graphColors.block.background);
expect(body).toBe("#e0e0e0");
const soloBody = { x: 620, y: 160, width: 30, height: 20 };
const plain = await countColor(page, soloBody, [224, 224, 224], 3);
expect(plain.matched).toBe(plain.total);

// The overlap of "lower" and "upper": "upper" comes later and draws on top.
const overlap = { x: 235, y: 150, width: 40, height: 20 };
const before = await countColor(page, overlap, UPPER_BODY, 6);
expect(before.matched).toBe(before.total);

// The ring of a selected anchor: right of the OUT point, outside the block, between radius 6.5 and 11.5.
const out = await anchorPoint(page, "solo", "out");
const ringArea = { x: out.x - 14, y: out.y - 14, width: 28, height: 28 };
const ring = (await page.evaluate(() => window.graph.graphColors.anchor.selectedBorder)).toLowerCase();
expect(ring).toBe("#ffcc00");
const ringColor: [number, number, number] = [255, 204, 0];
expect((await countColor(page, ringArea, ringColor, 40)).matched).toBe(0);
await page.evaluate(() => window.graph.api.setAnchorSelection("solo", "out", true));
await graphPO.waitForFrames(3);
expect(await rendererState(page)).toBe("webgl");
const selected = await countColor(page, ringArea, ringColor, 40);
// The ring is about 2.5 CSS px wide at this scale: some 60 px of a 14 x 14 px area.
expect(selected.matched).toBeGreaterThan(20);

// Selecting "lower" raises it above "upper" (an update after the first frame, through applyMany).
await page.evaluate(() => window.graph.api.selectBlocks(["lower"], true));
await graphPO.waitForFrames(3);
expect(await rendererState(page)).toBe("webgl");
const after = await countColor(page, overlap, LOWER_BODY, 6);
expect(after.matched).toBe(after.total);
const stillPlain = await countColor(page, soloBody, [224, 224, 224], 3);
expect(stillPlain.matched).toBe(stillPlain.total);
expect(await rendererState(page)).toBe("webgl");
});
});
2 changes: 0 additions & 2 deletions apps/e2e/tests/camera-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test.describe("Camera Control", () => {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
{
id: "block-2",
Expand All @@ -30,7 +29,6 @@ test.describe("Camera Control", () => {
height: 100,
name: "Block 2",
anchors: [],
selected: false,
},
],
connections: [],
Expand Down
2 changes: 0 additions & 2 deletions apps/e2e/tests/camera/camera-block-scale-level.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const BLOCK = {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
};

test.describe("getCameraBlockScaleLevel setting", () => {
Expand Down Expand Up @@ -75,7 +74,6 @@ test.describe("getCameraBlockScaleLevel setting", () => {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
];
const graph = new Graph(
Expand Down
1 change: 0 additions & 1 deletion apps/e2e/tests/camera/camera-change-signal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ test.describe("camera-change event and $camera signal", () => {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
},
],
connections: [],
Expand Down
2 changes: 0 additions & 2 deletions apps/e2e/tests/camera/camera-mouse-emulation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const BLOCK_1 = {
height: 100,
name: "Block 1",
anchors: [],
selected: false,
};

const BLOCK_2 = {
Expand All @@ -25,7 +24,6 @@ const BLOCK_2 = {
height: 100,
name: "Block 2",
anchors: [],
selected: false,
};

test.describe("Camera mouse event emulation (emulateMouseEventsOnCameraChange)", () => {
Expand Down
Loading
Loading