48 lines
1.8 KiB
JavaScript
48 lines
1.8 KiB
JavaScript
const { test } = require("node:test");
|
|
const assert = require("node:assert");
|
|
const {
|
|
PRESETS, DEFAULT_LAYOUT_TREE, listPresets, getPreset,
|
|
validateLayoutTree, normalizeLayoutTree,
|
|
} = require("../lib/layoutTree");
|
|
|
|
|
|
test("built-in presets are valid layout trees", () => {
|
|
assert.ok(PRESETS.length >= 2);
|
|
for (const p of PRESETS) {
|
|
const v = validateLayoutTree(p.tree);
|
|
assert.ok(v.ok, `${p.id} valid: ${v.errors.join("; ")}`);
|
|
}
|
|
});
|
|
|
|
|
|
test("listPresets / getPreset", () => {
|
|
const ids = listPresets().map((p) => p.id);
|
|
assert.ok(ids.includes("topnav") && ids.includes("sidebar"));
|
|
assert.equal(getPreset("topnav").type, "Root");
|
|
assert.equal(getPreset("nope"), null);
|
|
});
|
|
|
|
|
|
test("validateLayoutTree requires Root + exactly one Content", () => {
|
|
assert.equal(validateLayoutTree({ type: "Navbar" }).ok, false);
|
|
const noContent = { type: "Root", children: [{ type: "Navbar" }] };
|
|
assert.equal(validateLayoutTree(noContent).ok, false);
|
|
const twoContent = { type: "Root", children: [{ type: "Content" }, { type: "Content" }] };
|
|
assert.equal(validateLayoutTree(twoContent).ok, false);
|
|
assert.equal(validateLayoutTree({ type: "Root", children: [{ type: "Content" }] }).ok, true);
|
|
});
|
|
|
|
|
|
test("validateLayoutTree rejects unknown node types", () => {
|
|
const v = validateLayoutTree({ type: "Root", children: [{ type: "Content" }, { type: "Evil" }] });
|
|
assert.equal(v.ok, false);
|
|
assert.ok(v.errors.some((e) => e.includes("Evil")));
|
|
});
|
|
|
|
|
|
test("normalizeLayoutTree falls back to default for junk", () => {
|
|
assert.deepEqual(normalizeLayoutTree(null), DEFAULT_LAYOUT_TREE);
|
|
assert.deepEqual(normalizeLayoutTree({ type: "Bad" }), DEFAULT_LAYOUT_TREE);
|
|
const good = getPreset("sidebar");
|
|
assert.deepEqual(normalizeLayoutTree(good), good); // valid tree preserved
|
|
});
|