mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-09-02 04:00:40 +00:00
feat(explorer): add deterministic rendering E2E example and test (#1037) Adds a deterministic Explorer graph baseline and coverage for the full build -> persist -> API -> frontend hydration -> canvas rendering path, so a regression anywhere along that chain shows up in CI instead manually. examples/explorer_deterministic_rendering_example.py builds the canonical 4-node, 3-edge graph (Alice -WORKS_AT-> Acme, Bob -KNOWS-> Alice, Acme -LOCATED_IN-> New York) with ContextGraph.add_node()/ add_edge(), persists it with save_to_file() and reloads it with GraphSession.from_file(), printing the setup prerequisites and the expected node/edge/label checklist for anyone running it by hand. tests/explorer/test_explorer_deterministic_rendering_e2e.py covers graph construction, the serialize/deserialize round trip, GraphSession loading, and the Explorer API's /api/graph/* responses against the exact expected nodes, edges, and labels, plus all three auth modes (unconfigured, API-key required, anonymous opt-in). fix(explorer): address Qodo review findings for deterministic rendering e2e (#1037) - configure SEMANTICA_ALLOW_ANONYMOUS=true and document SEMANTICA_API_KEY as the alternative in the reproduction instructions, so the documented commands don't 503 on a clean checkout - add clean-checkout prerequisites and a visual verification checklist to the example - add edge-label (WORKS_AT, KNOWS, LOCATED_IN), zoom-tier, and hover-interaction coverage to the frontend test - add an explicit auth-enforcement integration test for the deterministic graph endpoints fix(explorer): connect deterministic rendering E2E path The frontend test built its own node/edge objects directly with batchMergeNodes()/batchMergeEdges(), bypassing the real loading path entirely -- it never went through useLoadGraph, never mounted the canvas, and its fixture didn't even carry the same fields the backend actually returns (e.g. no color values), so a break in API hydration, the edge.type -> edgeType mapping, or canvas label rendering could still pass. Adds deterministicExplorerRendering.e2e.ts, which mounts the real Explorer app in Chromium, serves API-shaped /api/graph/nodes and /api/graph/edges responses through route interception, drives the app through its actual useLoadGraph hydration path into a real Sigma canvas, and asserts on captured canvas fillText() calls that WORKS_AT, KNOWS, and LOCATED_IN are genuinely drawn, both after load and after Zoom In. fix(explorer): preserve upstream markdown dependencies ci(explorer): isolate deterministic backend test dependencies Wires the new Python test into ci.yml as its own focused step (it previously only ran manually), installs Playwright's Chromium browser before the frontend suite, and keeps the deterministic backend test's dependency install separate from the rest of the pipeline so it doesn't pull in unrelated optional extras during collection. fix(explorer): remove redundant edge label hydration An earlier commit in this PR added an explicit `label` field to hydrated edge attributes on the theory that it was needed for edge labels to render. Review traced through GraphCanvas.tsx's label resolution (`attrs.edgeType || data.label || ""`, from the earlier #1009 fix already on main) and found that `edgeType` is set unconditionally on every edge during hydration, so it always wins the `||` before `data.label` is ever consulted -- the added field and its plumbing in useLoadGraph.ts and graphStore.ts never did anything. Removed both; reran the real Chromium E2E test against the reverted code and confirmed all three labels still render identically, closing out the question of whether anything else was actually broken.
104 lines
4.4 KiB
TypeScript
104 lines
4.4 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { spawn, type ChildProcess } from "node:child_process";
|
|
import { existsSync } from "node:fs";
|
|
import { setTimeout as delay } from "node:timers/promises";
|
|
import test from "node:test";
|
|
import { chromium, type Page } from "playwright";
|
|
|
|
const PORT = 4173;
|
|
const BASE_URL = `http://127.0.0.1:${PORT}`;
|
|
|
|
const nodes = [
|
|
{ id: "alice", type: "Person", content: "Alice", properties: {} },
|
|
{ id: "bob", type: "Person", content: "Bob", properties: {} },
|
|
{ id: "acme", type: "Organization", content: "Acme", properties: {} },
|
|
{ id: "new_york", type: "Location", content: "New York", properties: {} },
|
|
];
|
|
|
|
const edges = [
|
|
{ id: "edge_alice_acme", familyId: "edge_alice_acme", source: "alice", target: "acme", type: "WORKS_AT", weight: 1, properties: {} },
|
|
{ id: "edge_bob_alice", familyId: "edge_bob_alice", source: "bob", target: "alice", type: "KNOWS", weight: 1, properties: {} },
|
|
{ id: "edge_acme_new_york", familyId: "edge_acme_new_york", source: "acme", target: "new_york", type: "LOCATED_IN", weight: 1, properties: {} },
|
|
];
|
|
|
|
let server: ChildProcess | undefined;
|
|
|
|
async function startVite(): Promise<void> {
|
|
server = spawn("npm", ["run", "dev", "--", "--host", "127.0.0.1", "--port", String(PORT)], {
|
|
cwd: process.cwd(),
|
|
stdio: "ignore",
|
|
});
|
|
|
|
for (let attempt = 0; attempt < 50; attempt += 1) {
|
|
try {
|
|
const response = await fetch(BASE_URL);
|
|
if (response.ok) return;
|
|
} catch {
|
|
// Vite is still starting.
|
|
}
|
|
await delay(100);
|
|
}
|
|
throw new Error("Vite did not become ready");
|
|
}
|
|
|
|
async function installApiFixture(page: Page): Promise<void> {
|
|
await page.route("**/api/graph/**", async (route) => {
|
|
const pathname = new URL(route.request().url()).pathname;
|
|
if (pathname === "/api/graph/stats") {
|
|
await route.fulfill({ json: { node_count: 4, edge_count: 3 } });
|
|
} else if (pathname === "/api/graph/nodes") {
|
|
await route.fulfill({ json: { nodes, total: nodes.length, skip: 0, limit: 1000, next_cursor: null } });
|
|
} else if (pathname === "/api/graph/edges") {
|
|
await route.fulfill({ json: { edges, total: edges.length, skip: 0, limit: 1000, next_cursor: null } });
|
|
} else {
|
|
await route.continue();
|
|
}
|
|
});
|
|
}
|
|
|
|
test("real Explorer loading path hydrates and renders API edge labels", async (t) => {
|
|
await startVite();
|
|
t.after(async () => {
|
|
server?.kill();
|
|
});
|
|
|
|
const browser = await chromium.launch({
|
|
headless: true,
|
|
executablePath: process.env.CHROMIUM_PATH || (existsSync("/usr/bin/chromium") ? "/usr/bin/chromium" : undefined),
|
|
});
|
|
t.after(() => browser.close());
|
|
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
|
|
|
|
await page.addInitScript(() => {
|
|
const captured = (window as Window & { __capturedCanvasText?: string[] }).__capturedCanvasText = [];
|
|
const originalFillText = CanvasRenderingContext2D.prototype.fillText;
|
|
CanvasRenderingContext2D.prototype.fillText = function (text: string, ...args: [number, number, number?, number?]) {
|
|
captured.push(String(text));
|
|
return originalFillText.call(this, text, ...args);
|
|
};
|
|
});
|
|
await installApiFixture(page);
|
|
await page.goto(BASE_URL);
|
|
await page.getByRole("button", { name: /Open Semantica Explorer/ }).click();
|
|
|
|
await page.locator("canvas").nth(0).waitFor({ state: "attached" });
|
|
await page.waitForFunction(() => document.querySelectorAll("canvas").length >= 2);
|
|
await page.waitForFunction(() => {
|
|
const labels = (window as Window & { __capturedCanvasText?: string[] }).__capturedCanvasText ?? [];
|
|
return ["WORKS_AT", "KNOWS", "LOCATED_IN"].every((label) => labels.includes(label));
|
|
}, undefined, { timeout: 10_000 });
|
|
|
|
const capturedLabels = await page.evaluate(() => (window as Window & { __capturedCanvasText?: string[] }).__capturedCanvasText ?? []);
|
|
for (const label of ["WORKS_AT", "KNOWS", "LOCATED_IN"]) {
|
|
assert.ok(capturedLabels.includes(label), `Expected rendered edge label ${label}`);
|
|
}
|
|
assert.ok(capturedLabels.includes("Alice"));
|
|
|
|
await page.getByRole("button", { name: "Zoom In" }).click();
|
|
await page.waitForTimeout(250);
|
|
const labelsAfterZoom = await page.evaluate(() => (window as Window & { __capturedCanvasText?: string[] }).__capturedCanvasText ?? []);
|
|
for (const label of ["WORKS_AT", "KNOWS", "LOCATED_IN"]) {
|
|
assert.ok(labelsAfterZoom.includes(label), `Expected edge label ${label} after zoom`);
|
|
}
|
|
});
|