Files
alkane-os/third-party/flare-ui/dash-editor/tests/model.test.mjs
T
x ad31cfbafa Vendor flare-ui v0.2.9 and prove the schema-driven UI in an image
Spike, not the final UI: it answers whether flare-ui can drive Alkane OS's
on-box pages, and what it costs.

What works:

- schema/alkane-bb.json authors the sidebar as data (3 sections, 14 tabs);
- src/bin/ui-spike.rs renders it through flare_ui::nav + flare_ui::shell, so
  the unified sidebar, ordering and surface filtering come from the schema
  rather than from hand-written HTML;
- the render uses the shared bf- design tokens, the same ones the rest of the
  suite uses.

Size, measured rather than assumed (armv7, release, stripped):

  ui-baseline   323 KiB   (maud + serde_json, no flare-ui)
  ui-spike      771 KiB   (flare-ui + wschema + sha2)
  marginal      448 KiB

Vendoring: third-party/flare-ui is the full v0.2.9 tree pinned in
flare-ui/flare-ui.lock (a partial archive does not compile, since the crate
include_str!s assets from dash-editor/). vendor/ is re-generated as a cargo
directory source with the new registry dependencies (maud, sha2, typenum,
digest, generic-array, block-buffer, crypto-common, cfg-if, etc.) so the build
still works offline. third-party/README.md records how to re-pin.
2026-11-04 14:06:25 -06:00

98 lines
4.5 KiB
JavaScript

// The document model on its own: dashboard-doc.js touches no element, so it
// runs here with nothing but a `window` to hang its global on.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import vm from "node:vm";
const source = fs.readFileSync(
path.join(import.meta.dirname, "..", "editor", "dashboard-doc.js"),
"utf8",
);
const context = { window: {} };
vm.runInNewContext(source, context);
const model = context.window.FlareDashboardDoc;
test("the model names the table kind in the server's order", () => {
// FLARE pins this list to its own KINDS, element for element, so the order
// is part of the contract and not just the membership.
assert.deepEqual(Array.from(model.KINDS), [
"metric", "status", "text", "heading", "separator", "card", "gauge",
"chart", "status_grid", "table", "group", "legacy_grid",
]);
});
test("a table carries no value, no unit and no block thresholds", () => {
// Its readings are its cells and its units and bounds are its columns', so
// a value, a unit or a block-level threshold on one is a key the server
// refuses. The panel offers the frame fields only.
assert.equal(model.VALUELESS_KINDS.indexOf("table") >= 0, true);
for (const name of ["value", "source", "unit", "caution", "critical", "rising", "cells"]) {
assert.equal(model.fieldApplies(name, "table", null), false, name);
}
for (const name of ["min_rows", "space_before"]) {
assert.equal(model.fieldApplies(name, "table", null), true, name);
}
});
test("a new table is one the server takes", () => {
// At least one column and one row, every row as long as the header.
const block = model.blockOf("table", []);
assert.equal(block.body.columns.length, 1);
assert.equal(block.body.rows.length, 1);
assert.equal(block.body.rows[0].cells.length, 1);
assert.equal(model.fieldApplies("columns", "table", null), true);
assert.equal(model.fieldApplies("rows", "table", null), true);
assert.equal(model.fieldApplies("columns", "status_grid", null), false);
assert.equal(model.fieldApplies("rows", "metric", null), false);
});
test("the row editor's lines round-trip through the model", () => {
const body = { kind: "table", columns: [], rows: [] };
model.applyColumns(body, "Oil temp | C\nSuction | kPa");
body.rows = model.parseRows(
'Engine 1 | engine1.oil | engine1.suction\nEngine 2 | "offline" |', 2);
const json = (v) => JSON.parse(JSON.stringify(v));
assert.deepEqual(json(body.columns), [
{ label: "Oil temp", unit: "C" },
{ label: "Suction", unit: "kPa" },
]);
assert.deepEqual(json(body.rows), [
{ label: "Engine 1", cells: [{ source: "engine1.oil" }, { source: "engine1.suction" }] },
{ label: "Engine 2", cells: [{ literal: "offline" }, { literal: "" }] },
]);
const block = { id: "t", title: "T", body };
assert.equal(model.fieldValue(block, "columns"), "Oil temp | C\nSuction | kPa");
assert.equal(model.fieldValue(block, "rows"),
'Engine 1 | engine1.oil | engine1.suction\nEngine 2 | "offline" | ');
// And back again, unchanged.
assert.deepEqual(json(model.parseRows(model.fieldValue(block, "rows"), 2)), json(body.rows));
});
test("rows follow the column count and the caps hold", () => {
const body = { kind: "table", columns: [], rows: [] };
model.applyColumns(body, "A\nB\nC");
body.rows = model.parseRows("R | x | y | z | extra\nS | x", 3);
assert.deepEqual(Array.from(body.rows, (r) => r.cells.length), [3, 3]);
// Dropping a column cuts every row to match.
model.applyColumns(body, "A");
assert.deepEqual(Array.from(body.rows, (r) => r.cells.length), [1, 1]);
const many = Array.from({ length: 40 }, (_, i) => `L${i}`).join("\n");
model.applyColumns(body, many);
assert.equal(body.columns.length, model.TABLE_COLUMN_CAP);
assert.equal(model.parseRows(many, 1).length, model.TABLE_ROW_CAP);
// Empty text keeps a placeholder rather than a table the server refuses.
model.applyColumns(body, " \n");
assert.equal(body.columns.length, 1);
assert.equal(model.parseRows("", 1).length, 1);
});
test("a column keeps the thresholds of the one it replaces", () => {
// Thresholds are set in the raw pane; renaming a column must not drop them.
const limits = { caution: 80, critical: 95 };
const body = { kind: "table", columns: [{ label: "Oil", unit: "C", thresholds: limits }], rows: [] };
model.applyColumns(body, "Oil temp | C");
assert.deepEqual(JSON.parse(JSON.stringify(body.columns[0].thresholds)), limits);
});