Files
alkane-os/third-party/flare-ui/dash-editor/tests/file.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

180 lines
7.7 KiB
JavaScript

// dashboard-file.js against a stub page: what it sends, what it writes into
// the raw pane, and what it says. The consumers drive the same script in real
// browsers; this holds the contract README.md states without one.
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-file.js"), "utf8");
class Element {
constructor(attrs = {}) {
this.attrs = attrs;
this.hidden = false;
this.disabled = false;
this.type = attrs.type || "";
this.value = attrs.value || "";
this.textContent = "";
this.listeners = {};
this.events = [];
}
getAttribute(name) { return this.attrs[name] ?? null; }
addEventListener(type, fn) { (this.listeners[type] ||= []).push(fn); }
dispatchEvent(event) {
this.events.push(event);
for (const fn of this.listeners[event.type] || []) fn(event);
}
click() { this.clicked = (this.clicked || 0) + 1; }
}
// One page: the editor root and its hooks, plus whichever host forms exist.
function page({ importForm = true, exportForm = true, csrf = "token-1", reply, timeout } = {}) {
const hooks = {
"[data-dashboard-file-input]": new Element({ form: "dashboard_import_form" }),
"[data-dashboard-import]": new Element({ type: "submit", form: "dashboard_import_form" }),
"[data-dashboard-export]": new Element({ type: "submit", form: "dashboard_export_form" }),
"[data-dashboard-file-error]": new Element(),
"[data-dashboard-file-notice]": new Element(),
"[data-dashboard-raw]": new Element({ value: "{}" }),
};
const root = new Element(timeout ? { "data-file-timeout-ms": String(timeout) } : {});
root.querySelector = (selector) => hooks[selector] || null;
const forms = {};
if (importForm) {
forms.dashboard_import_form = {
getAttribute: (name) => (name === "action" ? "https://host.test/dashboard/import" : null),
elements: { namedItem: (name) => (name === "csrf" && csrf ? { value: csrf } : null) },
};
}
if (exportForm) forms.dashboard_export_form = { getAttribute: (name) => (name === "action" ? "https://host.test/dashboard/export" : null) };
const requests = [];
const context = {
document: {
querySelector: (selector) => (selector === "[data-dashboard-editor]" ? root : null),
getElementById: (id) => forms[id] || null,
},
fetch: (url, options) => {
requests.push({ url, options });
return reply ? reply(options) : Promise.reject(new Error("offline"));
},
Event: class { constructor(type) { this.type = type; } },
CustomEvent: class { constructor(type, init) { this.type = type; this.detail = init.detail; } },
JSON,
Promise,
AbortController,
setTimeout,
clearTimeout,
};
vm.runInNewContext(source, context);
return { hooks, root, requests };
}
function answer(status, body) {
return () => Promise.resolve({
ok: status >= 200 && status < 300,
status,
json: () => (body === undefined ? Promise.reject(new Error("not JSON")) : Promise.resolve(body)),
});
}
// Choose a file and wait for the import it starts to settle.
async function choose(hooks, text = "{\"wdash\":1}") {
const input = hooks["[data-dashboard-file-input]"];
input.files = [{ name: "plant.wdash", text: () => Promise.resolve(text) }];
input.dispatchEvent({ type: "change" });
for (let i = 0; i < 10; i += 1) await new Promise((resolve) => setImmediate(resolve));
}
test("Import opens the picker, which stays hidden", () => {
const { hooks } = page();
const input = hooks["[data-dashboard-file-input]"];
const button = hooks["[data-dashboard-import]"];
assert.equal(input.hidden, true);
assert.equal(button.type, "button");
button.dispatchEvent({ type: "click" });
assert.equal(input.clicked, 1);
});
test("a control whose form the host does not supply is hidden", () => {
const { hooks } = page({ importForm: false, exportForm: false });
assert.equal(hooks["[data-dashboard-import]"].hidden, true);
assert.equal(hooks["[data-dashboard-export]"].hidden, true);
assert.equal(hooks["[data-dashboard-file-input]"].hidden, true);
});
test("an import posts the file's text with the form's CSRF token", async () => {
const document = { version: 3, blocks: [] };
const { hooks, requests } = page({ reply: answer(200, { name: "Plant", document }) });
await choose(hooks, "FILE TEXT");
assert.equal(requests.length, 1);
assert.equal(requests[0].url, "https://host.test/dashboard/import");
assert.equal(requests[0].options.method, "POST");
assert.equal(requests[0].options.body, "FILE TEXT");
assert.equal(requests[0].options.headers["X-CSRF-Token"], "token-1");
assert.equal(requests[0].options.headers.Accept, "application/json");
});
test("no CSRF header when the form carries no token", async () => {
const { hooks, requests } = page({ csrf: "", reply: answer(200, { name: "P", document: { version: 3, blocks: [] } }) });
await choose(hooks);
assert.equal("X-CSRF-Token" in requests[0].options.headers, false);
});
test("an accepted file lands in the raw pane as an input, unsaved", async () => {
const document = { version: 3, blocks: [{ id: "a" }] };
const { hooks, root } = page({ reply: answer(200, { name: "Plant", document }) });
await choose(hooks);
const raw = hooks["[data-dashboard-raw]"];
assert.deepEqual(JSON.parse(raw.value), document);
assert.deepEqual(raw.events.map((e) => e.type), ["input"]);
assert.equal(hooks["[data-dashboard-file-error]"].textContent, "");
assert.match(hooks["[data-dashboard-file-notice]"].textContent, /^Imported Plant\. It is not saved yet/);
const event = root.events.find((e) => e.type === "dashboard-import");
assert.equal(event.detail.name, "Plant");
assert.equal(hooks["[data-dashboard-import]"].disabled, false);
assert.equal(hooks["[data-dashboard-file-input]"].value, "");
});
test("a refused file says the reader's reason and leaves the pane alone", async () => {
const { hooks } = page({ reply: answer(400, { ok: false, error: "checksum does not match the document" }) });
await choose(hooks);
assert.equal(hooks["[data-dashboard-raw]"].value, "{}");
assert.equal(hooks["[data-dashboard-raw]"].events.length, 0);
assert.equal(hooks["[data-dashboard-file-error]"].textContent,
"The file was not imported: checksum does not match the document");
assert.equal(hooks["[data-dashboard-file-notice]"].textContent, "");
});
test("an answer that is not JSON names the status", async () => {
const { hooks } = page({ reply: answer(413) });
await choose(hooks);
assert.equal(hooks["[data-dashboard-file-error]"].textContent,
"The file was not imported: the server answered 413");
});
test("a server that does not answer is said so", async () => {
const { hooks } = page();
await choose(hooks);
assert.equal(hooks["[data-dashboard-file-error]"].textContent,
"The file was not imported: the server did not answer.");
assert.equal(hooks["[data-dashboard-import]"].disabled, false);
});
test("an import that gets no answer ends after the timeout", async () => {
// A server that holds the request until the editor gives up on it.
const held = (options) => new Promise((_, reject) => {
options.signal.addEventListener("abort", () => reject(new Error("aborted")));
});
const { hooks } = page({ reply: held, timeout: 30 });
await choose(hooks);
assert.equal(hooks["[data-dashboard-import]"].disabled, true);
await new Promise((resolve) => setTimeout(resolve, 80));
for (let i = 0; i < 10; i += 1) await new Promise((resolve) => setImmediate(resolve));
assert.equal(hooks["[data-dashboard-file-error]"].textContent,
"The file was not imported: the server did not answer.");
assert.equal(hooks["[data-dashboard-import]"].disabled, false);
});