PlotDirector/PlotLine.Tests/Browser/relationship-protected-character-capture.cjs

175 lines
5.8 KiB
JavaScript

// Controlled synthetic capture matching the pre-change baseline; no author data.
const { chromium } = require("playwright"),
assert = require("node:assert/strict"),
fs = require("node:fs"),
path = require("node:path");
const base = process.env.PD_BROWSER_BASE || "http://127.0.0.1:5238";
(async () => {
const b = await chromium.launch();
let step = "setup";
try {
const c = await b.newContext({ viewport: { width: 1366, height: 1000 } });
await c.route("**/cdn.jsdelivr.net/**signalr*", (r) =>
r.fulfill({
contentType: "application/javascript",
body: fs.readFileSync(
path.join(
__dirname,
"node_modules/@microsoft/signalr/dist/browser/signalr.min.js",
),
),
}),
);
await c.route("**/native-companion/status", (r) => r.fulfill({ json: [] }));
await c.route("**/native-companion/connection-state", (r) =>
r.fulfill({ json: { known: false } }),
);
let removed = false,
notes = null,
fail = false,
viewer = false,
tabReads = [],
familyReads = 0;
for (const [endpoint, fixture] of [
["Tab", "tab"],
["Family", "family"],
["Operation", "operation"],
])
await c.route("**/Characters/Workspace/" + endpoint + "/*", async (r) => {
const u = new URL(r.request().url());
if (endpoint === "Operation" && r.request().method() === "POST") {
if (fail) {
fail = false;
await r.fulfill({
response: await r.fetch({
method: "GET",
url:
base +
"/character-workspace-operation-fixture?operation=definition&relationshipId=9&invalid=true&notes=" +
encodeURIComponent("Draft retained"),
}),
});
return;
}
if (r.request().postData()?.includes("archive")) removed = true;
else notes = "Saved fixture definition";
await r.fulfill({
json: {
saved: true,
message: removed
? "Relationship archived; history retained."
: "Relationship saved.",
},
});
return;
}
if (endpoint === "Tab") {
tabReads.push(u.searchParams.get("Tab"));
u.searchParams.set("id", u.pathname.split("/").at(-1));
}
if (endpoint === "Family") familyReads++;
u.searchParams.set("viewer", String(viewer));
u.searchParams.set("removed", String(removed));
if (notes) u.searchParams.set("notes", notes);
await r.fulfill({
response: await r.fetch({
url:
base + "/character-workspace-" + fixture + "-fixture" + u.search,
}),
});
});
await c.route("**/Characters/Workspace/Lookup/*", (r) =>
r.fulfill({ json: [{ id: 6, name: "Another established character" }] }),
);
await c.route("**/Characters/Workspace/Story/*", (r) =>
r.fulfill({
json: {
lens: "story",
focusID: 5,
povID: null,
sceneID: 22,
canEdit: !viewer,
truncated: false,
nodes: [
{ id: 5, name: "Current character" },
{ id: 7, name: "Recorded colleague" },
],
links: [
{
id: 12,
a: 5,
b: 7,
type: "Friend",
category: "Friendship",
state: "Earlier recorded state",
reciprocal: false,
initial: true,
recent: [],
},
],
},
}),
);
await c.route("**/Characters/Details/*", async (r) => {
const u = new URL(r.request().url());
u.searchParams.set("id", u.pathname.split("/").at(-1));
await r.fulfill({
response: await r.fetch({
url: base + "/character-workspace-fixture" + u.search,
}),
});
});
const p = await c.newPage(),
errors = [];
await p.addLocatorHandler(
p.locator("[data-desktop-continue]"),
async (locator) => locator.click(),
);
let native = 0,
si = 0;
p.on("pageerror", (e) => errors.push(e.message));
p.on("request", (r) => {
if (r.url().includes("/hubs/native-companion/negotiate")) native++;
if (r.url().includes("/hubs/story-intelligence/negotiate")) si++;
});
await p.goto(base + "/character-workspace-fixture");
await p.waitForSelector('[data-character-tab-content="overview"]');
assert.equal(
await p.locator('script[src*="/relationship-main"]').count(),
0,
"protected view must not load main enhancement code",
);
const output = process.env.PD_CAPTURE_OUTPUT;
fs.mkdirSync(output, { recursive: true });
await p.evaluate(() => {
document.documentElement.dataset.appTheme = "modern-light";
document.documentElement.dataset.bsTheme = "light";
});
await p.click('[data-character-tab="relationships"]');
await p.waitForSelector('[data-character-node="5"]');
await p.evaluate(() => document.fonts.ready);
await p.waitForTimeout(300);
await p
.locator('[data-character-tab-content="relationships"]')
.screenshot({
path: output + "/character-relationships.png",
animations: "disabled",
});
fs.writeFileSync(
output + "/character-relationships.svg",
await p
.locator(
'[data-character-tab-content="relationships"] [data-character-graph] svg',
)
.evaluate((n) => n.outerHTML),
);
console.log("PASS captured protected Character relationships baseline");
} catch (e) {
console.error(e.name + ": " + e.message.split("Call log:")[0]);
process.exitCode = 1;
} finally {
await b.close();
}
})();