PlotDirector/PlotLine.Tests/Browser/relationship-main-layout.cjs

190 lines
5.7 KiB
JavaScript

const vm = require("vm"),
fs = require("fs"),
assert = require("assert/strict");
const box = {};
box.window = box;
vm.createContext(box);
for (const file of [
"context-relationship-layout.js",
"relationship-main-layout.js",
])
vm.runInContext(fs.readFileSync("PlotLine/wwwroot/js/" + file, "utf8"), box);
const engine = box.PlotDirectorMainRelationshipLayout;
const nodes = Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
name: "Person " + (i + 1),
w: 184 + (i % 3) * 12,
h: 70 + (i % 2) * 12,
importance: [10, 6, 2, null][i % 4],
}));
const overlap = (a, b, ps) =>
engine.intersects(engine.rect(a, ps[a.id]), engine.rect(b, ps[b.id]));
const results = [];
for (const count of [20, 75, 100, 200])
for (const network of ["sparse", "dense", "clusters", "isolated"]) {
const ns = nodes.slice(0, count),
edges = [];
for (let id = 1; id <= count; id++)
for (
let step = 1;
step <= (network === "dense" ? 6 : network === "clusters" ? 3 : 1);
step++
)
if (
network !== "isolated" &&
id + step <= count &&
(network !== "sparse" || id % 4 === 0) &&
(network !== "clusters" ||
Math.floor(id / 12) === Math.floor((id + step) / 12) ||
(id % 12 === 0 && step === 1))
)
edges.push({
id: edges.length + 1,
a: id,
b: id + step,
type: "Friend",
reciprocal: true,
});
const input = {
nodes: ns,
edges,
layout: "clusters",
viewport: { width: 1400, height: 650 },
},
start = performance.now(),
result = engine.layout(input),
ms = performance.now() - start;
assert.equal(Object.keys(result.positions).length, count);
for (let i = 0; i < count; i++)
for (let j = i + 1; j < count; j++)
assert(
!overlap(ns[i], ns[j], result.positions),
`${count}/${network}: overlap ${i}/${j}`,
);
assert.equal(
JSON.stringify(result.positions),
JSON.stringify(engine.layout(input).positions),
"deterministic",
);
results.push({
count,
network,
layoutMs: ms,
communities: result.communities?.length,
aspect: result.bounds.w / result.bounds.h,
});
}
const clique = nodes.slice(0, 12),
edges = [];
for (let a = 1; a <= 12; a++)
for (let b = a + 1; b <= 12; b++)
if (a <= 6 === b <= 6 || (a === 6 && b === 7))
edges.push({ id: edges.length + 1, a, b });
const clustered = engine.layout({ nodes: clique, edges, layout: "clusters" });
assert(
clustered.communities.length >= 2,
"connected graph reveals recorded subgroups",
);
assert(clustered.communities.some((g) => g.length >= 4));
const radial = engine.layout({
nodes: nodes.slice(0, 8),
edges: [
{ id: 1, a: 1, b: 2 },
{ id: 2, a: 2, b: 3 },
{ id: 3, a: 3, b: 4 },
{ id: 4, a: 1, b: 5 },
],
focus: 1,
depth: 3,
layout: "radial",
});
assert.equal(radial.distances[2], 1);
assert.equal(radial.distances[3], 2);
assert.equal(radial.distances[4], 3);
assert.equal(radial.distances[6], undefined);
assert(
Math.hypot(radial.positions[2].x, radial.positions[2].y) <
Math.hypot(radial.positions[3].x, radial.positions[3].y),
);
const family = engine.layout({
nodes: nodes.slice(0, 8).map((n) => ({ ...n, w: 180, h: 66 })),
edges: [
{ id: 1, a: 1, b: 2, type: "Spouse", reciprocal: true },
{ id: 2, a: 1, b: 3, type: "Parent" },
{ id: 3, a: 2, b: 3, type: "Parent" },
{ id: 4, a: 3, b: 4, type: "Adoptive Parent" },
{ id: 5, a: 3, b: 5, type: "Parent" },
{ id: 6, a: 6, b: 5, type: "Parent" },
],
layout: "family",
});
assert(
family.positions[1].y < family.positions[3].y &&
family.positions[3].y < family.positions[4].y,
);
assert.equal(family.positions[1].y, family.positions[2].y);
assert.equal(family.positions[4].y, family.positions[5].y);
assert(family.routes.some((r) => r.kind === "adoptive"));
assert(family.junctions.length);
const pinned = engine.layout({
nodes: nodes.slice(0, 20),
edges: [],
pins: { 1: { x: 10, y: 15 } },
layout: "importance",
});
assert.deepEqual(JSON.parse(JSON.stringify(pinned.positions[1])), {
x: 10,
y: 15,
});
for (let i = 1; i < 20; i++)
assert(!overlap(nodes[0], nodes[i], pinned.positions));
const route = engine.route(
[
{ id: 1, x: 0, y: 0, w: 100, h: 50 },
{ id: 2, x: 500, y: 0, w: 100, h: 50 },
{ id: 3, x: 250, y: 0, w: 120, h: 100 },
],
{ id: 1, a: 1, b: 2 },
"orthogonal",
);
assert(route.points.some((p) => Math.abs(p.y) > 60 || p.x < 180 || p.x > 320));
const curve = engine.route(
[
{ id: 1, x: 0, y: 0, w: 100, h: 50 },
{ id: 2, x: 500, y: 0, w: 100, h: 50 },
{ id: 3, x: 250, y: 0, w: 120, h: 100 },
],
{ id: 1, a: 1, b: 2 },
"curved",
);
assert(Math.abs(curve.distance) > 100);
fs.mkdirSync("/tmp/pd-main-relationships-after", { recursive: true });
fs.writeFileSync(
"/tmp/pd-main-relationships-after/layout-performance.json",
JSON.stringify(results, null, 2),
);
console.log(
"PASS main-only deterministic clustered/subgroup, distance-ring radial, shared canonical family, importance/pins, collision separation and obstacle routing fixtures",
results,
);
const parallelNodes = [
{ id: 1, x: 0, y: 0, w: 100, h: 50 },
{ id: 2, x: 500, y: 0, w: 100, h: 50 },
{ id: 3, x: 250, y: 0, w: 120, h: 100 },
];
for (const style of ["straight", "curved", "orthogonal"])
assert.notDeepEqual(
JSON.parse(
JSON.stringify(
engine.route(parallelNodes, { id: 1, a: 1, b: 2 }, style, -16),
),
),
JSON.parse(
JSON.stringify(
engine.route(parallelNodes, { id: 2, a: 1, b: 2 }, style, 16),
),
),
style + " parallel paths remain distinct around an obstacle",
);