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.
392 lines
18 KiB
JavaScript
392 lines
18 KiB
JavaScript
// The flowchart model on its own: flow-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", "flow-doc.js"),
|
|
"utf8",
|
|
);
|
|
const context = { window: {} };
|
|
vm.runInNewContext(source, context);
|
|
const flow = context.window.FlareFlowDoc;
|
|
// Values made in the model's context have that context's prototypes, so
|
|
// compare through JSON.
|
|
const plain = (value) => JSON.parse(JSON.stringify(value));
|
|
|
|
// A small deterministic generator (mulberry32), so a failing seed replays.
|
|
function random(seed) {
|
|
let a = seed >>> 0;
|
|
return () => {
|
|
a = (a + 0x6d2b79f5) >>> 0;
|
|
let t = a;
|
|
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
const pick = (rand, list) => list[Math.floor(rand() * list.length)];
|
|
|
|
test("roundtrip preserves own prototype-named document fields", () => {
|
|
const doc = JSON.parse('{"__proto__":{"inherited":"value"},"constructor":"ordinary","steps":[{"op":"wait","seconds":1}]}');
|
|
const graph = flow.toGraph(doc);
|
|
const result = flow.toDocument(graph, doc);
|
|
assert.equal(JSON.stringify(result), JSON.stringify(doc));
|
|
assert(Object.prototype.hasOwnProperty.call(result, "__proto__"));
|
|
assert.equal(result.inherited, undefined);
|
|
assert.equal(Object.getPrototypeOf(result).inherited, undefined);
|
|
});
|
|
|
|
test("prototype output names are checked as ordinary names", () => {
|
|
const context = { target: "warden", version: 2 };
|
|
const step = { op: "output", name: "constructor", value: 1 };
|
|
assert.deepEqual(plain(flow.documentErrors({ steps: [step] }, context)), { steps: {}, package: [] });
|
|
const errors = plain(flow.documentErrors({ steps: [step, { ...step }] }, context));
|
|
assert.deepEqual(errors.steps, { 1: ["Another output step already uses this name."] });
|
|
});
|
|
|
|
test("malformed flow fields return errors without aborting later steps", () => {
|
|
const context = { target: "warden", version: 2 };
|
|
const base = { op: "command", device: "rs485:1", type: "run_stop_cmd", value: "run" };
|
|
const malformed = ["constructor", { toString: 0 }, [], {}, null, 0, true];
|
|
for (const field of ["op", "device", "type"]) for (const value of malformed) {
|
|
const step = { ...base, [field]: value };
|
|
assert(flow.stepErrors(step, context).length > 0, JSON.stringify(step));
|
|
assert.strictEqual(typeof flow.label(step), "string");
|
|
assert.strictEqual(typeof flow.describe(step), "string");
|
|
const errors = flow.documentErrors({ steps: [step, { op: "wait", seconds: -1 }] }, context);
|
|
assert(errors.steps[0].length > 0);
|
|
assert(errors.steps[1].length > 0);
|
|
}
|
|
});
|
|
|
|
const POOL = [
|
|
{ op: "read", point: "io/state/relay/0#on", as: "on" },
|
|
{ op: "compare", left: "$on", cmp: "==", right: true },
|
|
{ op: "write", target: "relay:1", value: false },
|
|
{ op: "wait", seconds: 2.5 },
|
|
{ op: "loop_until", point: "modbus/state/readings/10#fields.level", cmp: ">=", value: 80, timeout_s: 60, poll_s: 5 },
|
|
{ op: "command", device: "rs485:10", type: "setpoint", of: "power_active", value: 250 },
|
|
{ op: "output", name: "dew_point", value: "$dp", unit: "degC", ttl_s: 180 },
|
|
{ op: "script", lang: "rhai", source: "40 + 2", note: { kept: [1, "two"] } },
|
|
{ op: "future_op", anything: [null, { deep: true }] },
|
|
{ op: "notify", channel: "sms", to: "+15550100", text: "x", extra: "kept" },
|
|
];
|
|
|
|
function document(rand, kind) {
|
|
const count = Math.floor(rand() * 12);
|
|
const steps = [];
|
|
for (let i = 0; i < count; i += 1) {
|
|
// A fresh copy with a marker, so steps are distinguishable.
|
|
steps.push({ ...structuredClone(pick(rand, POOL)), marker: i });
|
|
}
|
|
return {
|
|
wauto: 2, id: "flow", name: "Flow", kind, version: "1.0.0",
|
|
unknown_top: { kept: true }, requires: [], steps, after_steps: 1,
|
|
};
|
|
}
|
|
|
|
test("document to graph to document is the identity, byte for byte", () => {
|
|
for (let seed = 1; seed <= 500; seed += 1) {
|
|
const rand = random(seed);
|
|
const doc = document(rand, rand() < 0.5 ? "loop" : "procedure");
|
|
const back = flow.toDocument(flow.toGraph(doc), doc);
|
|
assert.equal(JSON.stringify(back), JSON.stringify(doc), `seed ${seed}`);
|
|
// The step objects themselves are carried, not copies.
|
|
back.steps.forEach((step, i) => assert.equal(step, doc.steps[i], `seed ${seed} step ${i}`));
|
|
}
|
|
});
|
|
|
|
// A reference implementation on a plain array: what each edit must do.
|
|
function reference(steps, edit) {
|
|
const out = steps.slice();
|
|
const at = (id) => out.indexOf(id);
|
|
switch (edit.type) {
|
|
case "insert": {
|
|
const index = edit.after === "start" ? 0 : at(edit.after) + 1;
|
|
if (out.length < 64) out.splice(index, 0, edit.id);
|
|
return out;
|
|
}
|
|
case "remove": out.splice(at(edit.id), 1); return out;
|
|
case "move": {
|
|
const item = out.splice(at(edit.id), 1)[0];
|
|
out.splice(Math.max(0, Math.min(out.length, edit.to)), 0, item);
|
|
return out;
|
|
}
|
|
case "connect": {
|
|
if (edit.to === "end") {
|
|
if (edit.from === "start") return out;
|
|
const item = out.splice(at(edit.from), 1)[0];
|
|
out.push(item);
|
|
return out;
|
|
}
|
|
if (edit.from === edit.to) return out;
|
|
const item = out.splice(at(edit.to), 1)[0];
|
|
out.splice(edit.from === "start" ? 0 : at(edit.from) + 1, 0, item);
|
|
return out;
|
|
}
|
|
default: throw new Error(edit.type);
|
|
}
|
|
}
|
|
|
|
test("random edits keep one chain and match the reference", () => {
|
|
for (let seed = 1; seed <= 300; seed += 1) {
|
|
const rand = random(seed * 7919);
|
|
let doc = document(rand, rand() < 0.5 ? "loop" : "procedure");
|
|
let graph = flow.toGraph(doc);
|
|
// Track identity by step object.
|
|
let expected = doc.steps.slice();
|
|
for (let n = 0; n < 40; n += 1) {
|
|
const ids = Array.from(flow.order(graph)).map((item) => item.id);
|
|
const byId = (id) => flow.order(graph).find((item) => item.id === id).step;
|
|
const kind = pick(rand, ids.length ? ["insert", "remove", "move", "connect"] : ["insert"]);
|
|
let result;
|
|
let edit;
|
|
if (kind === "insert") {
|
|
const step = { op: "wait", seconds: n, seed };
|
|
edit = { type: "insert", after: pick(rand, ["start", ...ids]) };
|
|
result = flow.insert(graph, edit.after, step);
|
|
if (!result.error) {
|
|
expected = reference(expected, {
|
|
...edit,
|
|
after: edit.after === "start" ? "start" : byId(edit.after),
|
|
id: step,
|
|
});
|
|
}
|
|
} else if (kind === "remove") {
|
|
edit = { type: "remove", id: pick(rand, ids) };
|
|
expected = reference(expected, { type: "remove", id: byId(edit.id) });
|
|
result = flow.remove(graph, edit.id);
|
|
} else if (kind === "move") {
|
|
edit = { type: "move", id: pick(rand, ids), to: Math.floor(rand() * (ids.length + 2)) - 1 };
|
|
expected = reference(expected, { ...edit, id: byId(edit.id) });
|
|
result = flow.move(graph, edit.id, edit.to);
|
|
} else {
|
|
const from = pick(rand, ["start", ...ids]);
|
|
const to = pick(rand, [...ids, "end"]);
|
|
edit = { type: "connect", from, to };
|
|
result = flow.connect(graph, from, to);
|
|
if (!result.error) {
|
|
expected = reference(expected, {
|
|
type: "connect",
|
|
from: from === "start" ? "start" : byId(from),
|
|
to: to === "end" ? "end" : byId(to),
|
|
});
|
|
}
|
|
}
|
|
graph = result.graph;
|
|
const steps = Array.from(flow.order(graph)).map((item) => item.step);
|
|
assert.deepEqual(steps, expected, `seed ${seed} edit ${n} ${JSON.stringify(edit)}`);
|
|
// And the edited graph survives its own round trip.
|
|
doc = flow.toDocument(graph, doc);
|
|
const again = flow.toGraph(doc);
|
|
assert.deepEqual(
|
|
Array.from(flow.order(again)).map((item) => item.step),
|
|
steps,
|
|
`seed ${seed} edit ${n} round trip`,
|
|
);
|
|
assert.deepEqual(
|
|
plain(again.edges).map((edge) => edge.kind),
|
|
plain(graph.edges).map((edge) => edge.kind),
|
|
`seed ${seed} edit ${n} edge kinds`,
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("derived edges follow what the engine does", () => {
|
|
const steps = [
|
|
{ op: "compare", left: "$x", cmp: ">", right: 1 },
|
|
{ op: "loop_until", point: "a#b", cmp: "==", right: true },
|
|
{ op: "wait", seconds: 1 },
|
|
];
|
|
const procedure = plain(flow.toGraph({ kind: "procedure", steps }));
|
|
assert.deepEqual(procedure.edges, [
|
|
{ from: "start", to: "s0", kind: "next" },
|
|
{ from: "s0", to: "s1", kind: "next" },
|
|
{ from: "s1", to: "s2", kind: "next" },
|
|
{ from: "s2", to: "end", kind: "next" },
|
|
{ from: "s0", to: "stop", kind: "false" },
|
|
{ from: "s1", to: "stop", kind: "timeout" },
|
|
]);
|
|
assert.deepEqual(procedure.nodes.map((node) => node.id), ["start", "s0", "s1", "s2", "end", "stop"]);
|
|
// A loop's guard ends the iteration, not the loop.
|
|
const loop = plain(flow.toGraph({ kind: "loop", steps }));
|
|
assert.deepEqual(loop.edges.slice(4), [
|
|
{ from: "s0", to: "end", kind: "false" },
|
|
{ from: "s1", to: "end", kind: "timeout" },
|
|
{ from: "end", to: "start", kind: "repeat" },
|
|
]);
|
|
assert.equal(loop.nodes.some((node) => node.id === "stop"), false);
|
|
// No step that can stop: no stop node.
|
|
const plainRun = plain(flow.toGraph({ kind: "procedure", steps: [{ op: "wait", seconds: 1 }] }));
|
|
assert.equal(plainRun.nodes.some((node) => node.id === "stop"), false);
|
|
});
|
|
|
|
const CONNECT = [
|
|
// [from, to, order after, refused]
|
|
["s0", "s2", ["s0", "s2", "s1"], false],
|
|
["s2", "s0", ["s1", "s2", "s0"], false],
|
|
["start", "s2", ["s2", "s0", "s1"], false],
|
|
["s0", "end", ["s1", "s2", "s0"], false],
|
|
["s0", "s1", ["s0", "s1", "s2"], false],
|
|
["s1", "s1", null, true],
|
|
["start", "end", null, true],
|
|
["end", "s0", null, true],
|
|
["stop", "s0", null, true],
|
|
["s0", "start", null, true],
|
|
["s0", "stop", null, true],
|
|
["s9", "s0", null, true],
|
|
["s0", "s9", null, true],
|
|
];
|
|
|
|
test("connect makes the target run next, and refuses what the format cannot say", () => {
|
|
const doc = { kind: "procedure", steps: [{ op: "wait", seconds: 0 }, { op: "wait", seconds: 1 }, { op: "wait", seconds: 2 }] };
|
|
for (const [from, to, after, refused] of CONNECT) {
|
|
const graph = flow.toGraph(doc);
|
|
const result = flow.connect(graph, from, to);
|
|
assert.equal(Boolean(result.error), refused, `${from} to ${to}`);
|
|
const ids = Array.from(flow.order(result.graph)).map((item) => item.id);
|
|
assert.deepEqual(ids, after ?? ["s0", "s1", "s2"], `${from} to ${to}`);
|
|
}
|
|
});
|
|
|
|
test("insert, remove and move at the edges", () => {
|
|
const doc = { kind: "procedure", steps: [{ op: "wait", seconds: 0 }] };
|
|
let { graph, id } = flow.insert(flow.toGraph(doc), "start", { op: "wait", seconds: 9 });
|
|
assert.equal(id, "s1");
|
|
assert.deepEqual(Array.from(flow.order(graph)).map((item) => item.id), ["s1", "s0"]);
|
|
({ graph } = flow.insert(graph, "end", { op: "wait", seconds: 8 }));
|
|
assert.deepEqual(Array.from(flow.order(graph)).map((item) => item.id), ["s1", "s0", "s2"]);
|
|
assert.equal(flow.indexOf(graph, "s2"), 2);
|
|
assert.ok(flow.remove(graph, "start").error);
|
|
assert.ok(flow.move(graph, "end", 0).error);
|
|
({ graph } = flow.move(graph, "s2", -5));
|
|
assert.deepEqual(Array.from(flow.order(graph)).map((item) => item.id), ["s2", "s1", "s0"]);
|
|
({ graph } = flow.remove(graph, "s1"));
|
|
assert.deepEqual(Array.from(flow.order(graph)).map((item) => item.id), ["s2", "s0"]);
|
|
});
|
|
|
|
test("a full package refuses one more step", () => {
|
|
const steps = Array.from({ length: flow.STEPS_MAX }, (_, i) => ({ op: "wait", seconds: i }));
|
|
const graph = flow.toGraph({ kind: "procedure", steps });
|
|
const result = flow.insert(graph, "end", { op: "wait", seconds: 1 });
|
|
assert.match(result.error, /at most 64/);
|
|
assert.equal(result.graph, graph);
|
|
});
|
|
|
|
test("order refuses a graph that is not one chain", () => {
|
|
const graph = plain(flow.toGraph({ kind: "procedure", steps: [{ op: "wait" }, { op: "wait" }] }));
|
|
const cycle = structuredClone(graph);
|
|
cycle.edges[1].to = "s0";
|
|
// Refused by the walk itself, not by running out of memory.
|
|
assert.throws(() => flow.order(cycle), /does not reach end/);
|
|
const branch = structuredClone(graph);
|
|
branch.edges.push({ from: "s0", to: "end", kind: "next" });
|
|
assert.throws(() => flow.order(branch), /Two next edges/);
|
|
const orphan = structuredClone(graph);
|
|
orphan.edges[0].to = "s1";
|
|
assert.throws(() => flow.order(orphan), /not on the chain/);
|
|
});
|
|
|
|
test("value fields keep strings strings", () => {
|
|
for (const value of [5, -1.5, 0, true, false, "5", "true", "rs485:10", "$x", "", "null", " "]) {
|
|
if (value === "") continue;
|
|
assert.deepEqual(flow.parseValue(flow.formatValue(value)), value, JSON.stringify(value));
|
|
}
|
|
assert.equal(flow.parseValue(""), undefined);
|
|
assert.equal(flow.parseValue("12"), 12);
|
|
assert.equal(flow.parseValue("on"), "on");
|
|
assert.equal(flow.parseValue("\"12\""), "12");
|
|
assert.equal(flow.formatValue(undefined), "");
|
|
});
|
|
|
|
const W2 = { target: "warden", version: 2 };
|
|
const W1 = { target: "warden", version: 1 };
|
|
const BE = { target: "backend", version: 1 };
|
|
const CHECKS = [
|
|
// [step, context, expected substring or null for valid]
|
|
[{ op: "read", point: "a#b", as: "x" }, W2, null],
|
|
[{ op: "read", point: "", as: "x" }, W2, "Point is required"],
|
|
[{ op: "compare", left: "$x", cmp: "~", right: 1 }, W2, "Comparison must be one of"],
|
|
[{ op: "compare", left: "$x", cmp: "<", right: null }, W2, "Against is required"],
|
|
[{ op: "compare", left: "$x", cmp: "<", right: false }, W2, null],
|
|
[{ op: "write", target: "relay:0" }, W2, "Value is required"],
|
|
[{ op: "wait", seconds: 3601 }, W2, "Seconds must be from 0 to 3600"],
|
|
[{ op: "wait", seconds: "1" }, W2, "Seconds must be a number"],
|
|
[{ op: "wait", seconds: 0 }, W2, null],
|
|
[{ op: "loop_until", point: "a#b", cmp: ">=", value: 1, poll_s: 1.5 }, W2, "whole number"],
|
|
[{ op: "loop_until", point: "a#b", cmp: ">=", value: 1 }, W2, null],
|
|
[{ op: "command", device: "rs485:248", type: "run_stop_cmd", value: "stop" }, W2, "rs485:<1-247>"],
|
|
[{ op: "command", device: "$gen", type: "run_stop_cmd", value: "go" }, W2, "Value must be run, stop"],
|
|
[{ op: "command", device: "rs485:1", type: "setpoint", value: 5 }, W2, "Sets is required"],
|
|
[{ op: "command", device: "rs485:1", type: "setpoint", of: "rpm", value: "x" }, W2, "number or a $name"],
|
|
[{ op: "command", device: "rs485:1", type: "outlet_cmd", value: "on" }, W2, "Channel is required"],
|
|
[{ op: "command", device: "rs485:1", type: "outlet_cmd", channel: 65, value: "on" }, W2, "from 1 to 64"],
|
|
[{ op: "command", device: "rs485:1", type: "reset_cmd", channel: 1, value: "reset" }, W2, "Only an outlet"],
|
|
[{ op: "command", device: "rs485:1", type: "outlet_cmd", channel: 3, value: "off" }, W2, null],
|
|
[{ op: "output", name: "Dew", value: 1 }, W2, "Name must be"],
|
|
[{ op: "output", name: "dew", value: "warm" }, W2, "Value must be a number"],
|
|
[{ op: "output", name: "dew", value: 1, unit: "deg C" }, W2, "Unit must be"],
|
|
[{ op: "output", name: "dew", value: 1, ttl_s: 0 }, W2, "from 1 to 3600"],
|
|
[{ op: "output", name: "dew", value: 1 }, W1, "version 2 package"],
|
|
[{ op: "output", name: "dew", value: true, unit: "degC" }, W2, null],
|
|
[{ op: "script", lang: "rhai", source: "" }, W2, "Rhai is required"],
|
|
[{ op: "script", lang: "lua", source: "1" }, W2, "Rhai only"],
|
|
[{ op: "ai", prompt: "x" }, W2, "does not run \"ai\""],
|
|
[{ op: "read", point: "a", as: "b" }, BE, "does not run \"read\""],
|
|
[{ op: "notify", channel: "sms", to: "555", text: "x" }, BE, "SMS recipient"],
|
|
[{ op: "notify", channel: "mail", to: "ops", text: "x" }, BE, "mail recipient"],
|
|
[{ op: "notify", channel: "mail", to: "ops@example.com", text: "" }, BE, "Message is required"],
|
|
[{ op: "notify", channel: "mail", to: "ops@example.com", text: "x", subject: "s".repeat(201) }, BE, "longer than 200"],
|
|
[{ op: "notify", channel: "mail", to: "ops@example.com", text: "x" }, BE, null],
|
|
[{ op: "ai", prompt: "Summarize" }, BE, null],
|
|
[[1], W2, "must be an object"],
|
|
];
|
|
|
|
test("step checks match the server's rules", () => {
|
|
for (const [step, ctx, expected] of CHECKS) {
|
|
const errors = Array.from(flow.stepErrors(step, ctx));
|
|
if (expected === null) assert.deepEqual(errors, [], JSON.stringify(step));
|
|
else assert.ok(errors.some((line) => line.includes(expected)), `${JSON.stringify(step)}: ${errors}`);
|
|
}
|
|
});
|
|
|
|
test("document checks find duplicate outputs and too many steps", () => {
|
|
const doc = { steps: [
|
|
{ op: "output", name: "a", value: 1 },
|
|
{ op: "wait", seconds: 1 },
|
|
{ op: "output", name: "a", value: 2 },
|
|
] };
|
|
const errors = plain(flow.documentErrors(doc, W2));
|
|
assert.deepEqual(Object.keys(errors.steps), ["2"]);
|
|
assert.match(errors.steps[2][0], /already uses this name/);
|
|
const many = { steps: Array.from({ length: 65 }, () => ({ op: "wait", seconds: 1 })) };
|
|
assert.match(flow.documentErrors(many, W2).package[0], /at most 64/);
|
|
});
|
|
|
|
test("the fields follow the step", () => {
|
|
const keys = (target, step) => Array.from(flow.fields(target, step)).map((field) => field.key);
|
|
assert.deepEqual(keys("warden", { op: "command", type: "setpoint" }), ["device", "type", "of", "value"]);
|
|
assert.deepEqual(keys("warden", { op: "command", type: "outlet_cmd" }), ["device", "type", "channel", "value"]);
|
|
assert.deepEqual(keys("warden", { op: "ai" }), []);
|
|
assert.deepEqual(keys("backend", { op: "toString" }), []);
|
|
assert.deepEqual(Array.from(flow.ops("warden", 1)), ["read", "compare", "write", "wait", "loop_until", "command", "script"]);
|
|
assert.deepEqual(Array.from(flow.ops("backend", 1)), ["ai", "notify", "script"]);
|
|
// loop_until reads `value` when the step uses it instead of `right`.
|
|
const until = flow.fields("warden", { op: "loop_until" })[2];
|
|
assert.equal(flow.keyOf({ op: "loop_until", value: 1 }, until), "value");
|
|
assert.equal(flow.keyOf({ op: "loop_until", right: 1 }, until), "right");
|
|
});
|
|
|
|
test("every step has a line that says what it does", () => {
|
|
assert.equal(flow.describe({ op: "compare", left: "$x", cmp: ">=", right: 5 }), "$x >= 5");
|
|
assert.equal(flow.describe({ op: "loop_until", point: "a#b", cmp: "==", value: true }), "a#b == true");
|
|
assert.equal(flow.describe({ op: "wait", seconds: 3 }), "3 seconds");
|
|
assert.equal(flow.label({ op: "loop_until" }), "Loop until");
|
|
assert.equal(flow.label({ op: "mystery" }), "mystery");
|
|
});
|