Files
ComposeLauncher/frontend/test-layout.mjs
T

70 lines
2.8 KiB
JavaScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { runInNewContext } from "node:vm";
import { alignmentGuide, builtInLayout, deviceMapper, normalizeLayout, validate } from "./src/layout.ts";
const fixtures = JSON.parse(readFileSync(new URL("../docs/layout-cases.json", import.meta.url), "utf8"));
const offline = readFileSync(new URL("./canvas.html", import.meta.url), "utf8");
const offlineFunction = name => {
const start = offline.indexOf("function " + name + "(");
const end = offline.indexOf("\n}", start) + 2;
assert.ok(start >= 0 && end > start, name);
return offline.slice(start, end);
};
for (const { name, accepted, layout } of fixtures) {
let editorAccepted;
try {
editorAccepted = !validate(normalizeLayout(layout)).some(issue => issue.level === "error");
} catch {
editorAccepted = false;
}
assert.equal(editorAccepted, accepted, "Vue editor: " + name);
const offlineAccepted = runInNewContext(`
let state, activePage, selectedId;
function pushHistory() {}
function syncControls() {}
function fitCanvas() {}
function render() {}
${offlineFunction("importLayout")}
${offlineFunction("validate")}
try {
importLayout(layout);
validate().errors === 0;
} catch {
false;
}
`, { layout });
assert.equal(offlineAccepted, accepted, "Offline editor: " + name);
}
console.log(`${fixtures.length} shared cases passed in both editors`);
const layout = builtInLayout();
const { canvas } = layout;
const items = layout.pages[0].items;
const tall = { w: 1080, h: 2160 };
const wide = { w: 1536, h: 2048 };
const centered = { mode: "contain", anchorY: 0.5 };
assert.deepEqual(deviceMapper(canvas, centered, items, tall).insets,
{ top: 120, bottom: 120, left: 0, right: 0 });
assert.deepEqual(deviceMapper(canvas, { ...centered, anchorY: 0 }, items, tall).insets,
{ top: 0, bottom: 240, left: 0, right: 0 });
assert.deepEqual(deviceMapper(canvas, centered, items, wide).insets,
{ top: 0, bottom: 0, left: 192, right: 192 });
const elastic = deviceMapper(canvas, { ...centered, mode: "elastic" }, items, tall);
assert.equal(elastic.mode, "elastic");
assert.ok(elastic.gapScale > 1);
assert.equal(elastic.insets, undefined);
assert.equal(deviceMapper(canvas, { ...centered, mode: "elastic" }, items, wide).mode, "contain");
const stretch = deviceMapper(canvas, { ...centered, mode: "stretch" }, items, tall);
assert.equal(stretch.insets, undefined);
assert.ok(stretch.sy > stretch.sx);
console.log("Tall and wide preview mappings passed");
assert.equal(alignmentGuide(82, 36, [100, 120], 4), 100);
assert.equal(alignmentGuide(82, 36, [120, 118], 4), 118);
assert.equal(alignmentGuide(82, 36, [122], 3), undefined);
assert.equal(alignmentGuide(0, 36, [0], 0), 0);
console.log("Drag alignment guides passed");