// Synthetic scope projections exercise only local preferences and shared quick-assignment UI. // Server authorisation is independently covered by RelationshipWorkspaceTests. const { chromium } = require("playwright"), fs = require("fs"), path = require("path"), assert = require("assert/strict"); (async () => { const b = await chromium.launch(); try { const c = await b.newContext({ viewport: { width: 1920, height: 1080 } }), p = await c.newPage(), errors = [], values = new Map(); p.on("pageerror", (e) => errors.push(e.message)); await c.route("https://cdn.jsdelivr.net/**", (r) => r.fulfill({ contentType: "application/javascript", body: fs.readFileSync( path.join( __dirname, "node_modules", r.request().url().includes("cytoscape") ? "cytoscape/dist/cytoscape.min.js" : r.request().url().includes("chart.umd") ? "chart.js/dist/chart.umd.js" : "@microsoft/signalr/dist/browser/signalr.min.js", ), ), }), ); await c.route("**/NarrativeFocus/Shared?*", (r) => r.fulfill({ json: { projectID: 7, bookID: 12, chapterID: 21, sceneID: 1, version: 1, isValid: true, }, }), ); await c.route("**/CharacterAttributes/Directory?*", (r) => r.fulfill({ json: { canEdit: true, characters: Array.from({ length: 20 }, (_, i) => ({ id: i + 1, version: "v1", values: { importance: values.get(i + 1) || "6" }, })), }, }), ); await c.route("**/CharacterAttributes/Assign", (r) => { const body = r.request().postDataJSON(); assert.equal(body.projectId, 8); assert.equal(body.attribute, "importance"); assert.deepEqual(body.changes, [{ id: 1, value: "10", version: "v1" }]); values.set(1, "10"); return r.fulfill({ json: { saved: true, undoToken: "scope-fixture", characters: [{ id: 1, version: "v2", values: { importance: "10" } }], }, }); }); await c.route("**/CharacterAttributes/Undo", (r) => { assert.equal(r.request().postDataJSON().token, "scope-fixture"); values.delete(1); return r.fulfill({ json: { saved: true, characters: [{ id: 1, version: "v3", values: { importance: "6" } }], }, }); }); await c.route("**/fixture?**", async (r) => { if (new URL(r.request().url()).searchParams.get("scope") !== "8") return r.continue(); const response = await r.fetch(), html = (await response.text()) .replace( 'data-project-id="7" data-book-id="" id="rwApp"', 'data-project-id="8" data-book-id="" id="rwApp"', ) .replace("projectId: 7,", "projectId: 8,"); return r.fulfill({ response, body: html }); }); const ready = () => p.waitForFunction(() => { const m = window.PlotDirectorRelationshipWorkspace?.mainCommands; return m?.initialised && !m.busy && !m.routing; }), open = async (scope) => { await p.goto( "http://127.0.0.1:5238/fixture?page=relationships&layout=full&cast=20&network=isolated&scope=" + scope, ); await ready(); }, view = () => p.evaluate(() => { const w = window.PlotDirectorRelationshipWorkspace, m = w.mainCommands; return { key: m.storageKey, members: m.members, pins: m.pins, zoom: w.cy.zoom(), pan: w.cy.pan(), positions: Object.fromEntries( w.cy.nodes(":visible").map((n) => [n.id(), n.position()]), ), left: document .querySelector(".rw-navigation") .getBoundingClientRect().width, }; }); await open(7); assert.equal( (await view()).members.length, 0, "fresh Connected with no relationships remains empty", ); await p.evaluate(() => { const w = window.PlotDirectorRelationshipWorkspace; w.mainCommands.focusCharacter(1); w.cy.$id("c-1").position({ x: 305, y: 207 }); w.mainCommands.pin(1); w.cy.zoom(1.1); w.cy.pan({ x: 60, y: 75 }); }); await p .getByRole("separator", { name: "Resize left panel" }) .press("ArrowRight"); await p.waitForTimeout(350); const expected = await view(); assert(expected.key.includes(".4.7.")); await open(8); assert((await view()).key.includes(".4.8.")); assert.equal((await view()).members.length, 0); assert.notEqual( (await view()).left, expected.left, "independent project divider width", ); assert.equal( await p.locator('[data-rw-tier="10"] [data-rw-character]').count(), 0, ); await p.evaluate(() => { const source = document.querySelector('[data-rw-character="1"]'), target = document.querySelector('[data-rw-tier="10"]'), dataTransfer = new DataTransfer(); source.dispatchEvent( new DragEvent("dragstart", { bubbles: true, dataTransfer }), ); target.dispatchEvent( new DragEvent("drop", { bubbles: true, cancelable: true, dataTransfer, }), ); source.dispatchEvent( new DragEvent("dragend", { bubbles: true, dataTransfer }), ); }); await p .getByRole("button", { name: "Undo importance", exact: true }) .waitFor(); assert.equal( await p.locator('[data-rw-tier="10"] [data-rw-character="1"]').count(), 1, "empty Major remains a drop target", ); assert.match( await p.locator('[data-rw-tier="10"]').getAttribute("aria-label"), /Major: 1/, ); await p .getByRole("button", { name: "Undo importance", exact: true }) .click(); await p.waitForFunction(() => document.querySelector('[data-rw-tier="6"] [data-rw-character="1"]'), ); assert.equal( await p.locator('[data-rw-tier="10"] [data-rw-character]').count(), 0, ); await p.locator("#rwEmptyFull").click(); await ready(); assert( await p.evaluate(() => { const nodes = PlotDirectorRelationshipWorkspace.cy.nodes(":visible"), bs = nodes.map((n) => n.boundingBox({ includeOverlays: false })); return ( nodes.length === 20 && bs.every((a, i) => bs .slice(i + 1) .every( (b) => Math.min(a.x2, b.x2) <= Math.max(a.x1, b.x1) || Math.min(a.y2, b.y2) <= Math.max(a.y1, b.y1), ), ) ); }), "Full cast from an empty view places each new member separately before any automatic arrangement", ); await p.locator('.rw-menu[data-pwa-context="Layout"] > summary').click(); await p.locator("#rwLayout").selectOption("radial"); await p.locator("#rwCentreDialog[open]").waitFor(); await p.getByLabel("Central character", { exact: true }).selectOption("3"); await p .getByRole("button", { name: "Arrange radial", exact: true }) .click(); await ready(); assert.equal( await p.evaluate( () => window.PlotDirectorRelationshipWorkspace.lastLayout.result.focus, ), 3, "radial centre is chosen through an accessible dialog", ); await p.screenshot({ path: "/tmp/pd-main-relationships-after/empty-group-and-centre.png", }); await open(7); assert.deepEqual( await view(), expected, "project-specific membership, positions, pin, viewport and divider restored", ); // Rejected family reads cannot release pins, change the preset or overwrite a saved view. await c.route("**/RelationshipMap/Family?*", (r) => r.fulfill({ status: 403, json: { message: "Synthetic denied family read" }, }), ); await p.locator(".rw-display > summary").click(); await p.locator("#rwPreset").selectOption("family"); await p.keyboard.press("Escape"); await ready(); assert.deepEqual(await view(), expected); assert.equal(await p.locator("#rwPreset").inputValue(), "custom"); const original = await p.evaluate( (key) => localStorage.getItem(key), expected.key, ), unavailable = JSON.stringify({ ...JSON.parse(original), preset: "family", layout: "family", }); await p.evaluate(({ key, data }) => localStorage.setItem(key, data), { key: expected.key, data: unavailable, }); await p.addInitScript( ({ key, data }) => { if (sessionStorage.getItem("main-test-family-seed") !== "done") { localStorage.setItem(key, data); sessionStorage.setItem("main-test-family-seed", "done"); } }, { key: expected.key, data: unavailable }, ); await p.reload(); await ready(); await p.waitForTimeout(350); assert.equal( await p.evaluate((key) => localStorage.getItem(key), expected.key), unavailable, "failed restore retains exact saved preferences", ); assert.equal( await p.evaluate( () => window.PlotDirectorRelationshipWorkspace.cy.nodes(":visible").length, ), 0, "unavailable family data is not shown as a Story tree", ); assert.match( await p.locator("#rwStatus").innerText(), /could not be loaded/, ); await p.goto("http://127.0.0.1:5238/fixture?page=calendar-test"); assert.equal( await p.evaluate((key) => localStorage.getItem(key), expected.key), unavailable, "pagehide cannot overwrite failed restore", ); await p.evaluate(({ key, data }) => localStorage.setItem(key, data), { key: expected.key, data: original, }); await open(7); assert.deepEqual(await view(), expected); await c.addCookies([ { name: "fixture-user", value: "5", url: "http://127.0.0.1:5238" }, ]); await open(7); assert((await view()).key.includes(".5.7.")); assert.equal( (await view()).members.length, 0, "different author has independent preferences", ); await c.addCookies([ { name: "fixture-user", value: "4", url: "http://127.0.0.1:5238" }, ]); await open(7); assert.deepEqual( await view(), expected, "return to original author restores only that author state", ); assert.deepEqual(errors, []); console.log( "PASS empty connected network, empty importance target/count/Undo, radial chooser, independent author/project membership, positions, pins, viewport and divider preferences", ); } finally { await b.close(); } })().catch((e) => { console.error(e.stack); process.exitCode = 1; });