forked from longtao.wu/droboticflow
108 lines
3.2 KiB
JavaScript
108 lines
3.2 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
dashboardPresentationSchema,
|
|
defaultDashboardPresentation,
|
|
dashboardPresentationKey,
|
|
readDashboardPresentation,
|
|
saveDashboardPresentation,
|
|
} from "../../src/features/annotation-output/presentation.ts";
|
|
|
|
test("layout document accepts order and tokens but rejects executable and business configuration", () => {
|
|
const base = defaultDashboardPresentation();
|
|
assert.equal(
|
|
dashboardPresentationSchema.safeParse({ ...base, order: ["tasks", "metrics", "suppliers"], accent: "blue" })
|
|
.success,
|
|
true,
|
|
);
|
|
for (const extra of [
|
|
{ role: "admin" },
|
|
{ supplierId: "other" },
|
|
{ endpoint: "/api/users" },
|
|
{ css: "body{display:none}" },
|
|
{ script: "alert(1)" },
|
|
{ onClick: "submit" },
|
|
]) {
|
|
assert.equal(dashboardPresentationSchema.safeParse({ ...base, ...extra }).success, false);
|
|
}
|
|
for (const order of [["tasks"], ["tasks", "tasks", "metrics"], ["metrics", "suppliers", "admin"]]) {
|
|
assert.equal(dashboardPresentationSchema.safeParse({ ...base, order }).success, false);
|
|
}
|
|
for (const extra of [
|
|
{ version: 2 },
|
|
{ page: "annotation-editing" },
|
|
{ accent: "url(https://example.com)" },
|
|
{ density: "hidden" },
|
|
]) {
|
|
assert.equal(dashboardPresentationSchema.safeParse({ ...base, ...extra }).success, false);
|
|
}
|
|
});
|
|
|
|
test("absent, corrupt, oversized and unsupported stored documents safely use the default", () => {
|
|
for (const value of [null, "{", "null", JSON.stringify({ version: 2 }), " ".repeat(5000)]) {
|
|
assert.deepEqual(readDashboardPresentation({ getItem: () => value }, "a"), defaultDashboardPresentation());
|
|
}
|
|
assert.deepEqual(
|
|
readDashboardPresentation(
|
|
{
|
|
getItem() {
|
|
throw new Error("blocked");
|
|
},
|
|
},
|
|
"a",
|
|
),
|
|
defaultDashboardPresentation(),
|
|
);
|
|
});
|
|
|
|
test("preferences are isolated by organization, account and role without carrying customer data", () => {
|
|
const data = new Map(),
|
|
storage = { getItem: (key) => data.get(key) ?? null, setItem: (key, value) => data.set(key, value) };
|
|
const scope = JSON.stringify(["o1", "u1", "admin"]);
|
|
saveDashboardPresentation(storage, scope, { ...defaultDashboardPresentation(), accent: "teal" });
|
|
assert.equal(readDashboardPresentation(storage, scope).accent, "teal");
|
|
for (const other of [
|
|
["o2", "u1", "admin"],
|
|
["o1", "u2", "admin"],
|
|
["o1", "u1", "annotator"],
|
|
]) {
|
|
assert.equal(readDashboardPresentation(storage, JSON.stringify(other)).accent, "orange");
|
|
}
|
|
assert.deepEqual(Object.keys(JSON.parse(data.get(dashboardPresentationKey(scope)))).sort(), [
|
|
"accent",
|
|
"density",
|
|
"order",
|
|
"page",
|
|
"version",
|
|
]);
|
|
});
|
|
|
|
test("invalid documents and failed persistence never report a saved preference", () => {
|
|
let writes = 0;
|
|
assert.throws(() =>
|
|
saveDashboardPresentation(
|
|
{
|
|
setItem() {
|
|
writes++;
|
|
},
|
|
},
|
|
"a",
|
|
{ ...defaultDashboardPresentation(), permission: true },
|
|
),
|
|
);
|
|
assert.equal(writes, 0);
|
|
assert.throws(
|
|
() =>
|
|
saveDashboardPresentation(
|
|
{
|
|
setItem() {
|
|
throw new Error("quota");
|
|
},
|
|
},
|
|
"a",
|
|
defaultDashboardPresentation(),
|
|
),
|
|
/quota/,
|
|
);
|
|
});
|