import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import test from "node:test"; import { moveWorkspaceOrder, resolveWorkspaceOrder } from "../app/static/workspace-order.js"; const staticRoot = new URL("../app/static/", import.meta.url); const definitions = [ { id: "device" }, { id: "settings" }, { id: "canvas" }, { id: "text" }, { id: "animations" }, { id: "templates" }, ]; test("workspace order resolves saved ids and appends new registrations", () => { const resolved = resolveWorkspaceOrder( definitions, ["removed", "templates", "device", "templates", "settings"], ); assert.deepEqual( resolved.map((definition) => definition.id), ["templates", "device", "settings", "canvas", "text", "animations"], ); assert.deepEqual(definitions.map((definition) => definition.id), [ "device", "settings", "canvas", "text", "animations", "templates", ]); }); test("workspace order movement is immutable and respects both boundaries", () => { const original = definitions.map((definition) => definition.id); assert.deepEqual(moveWorkspaceOrder(original, 1, -1), [ "settings", "device", "canvas", "text", "animations", "templates", ]); assert.equal(moveWorkspaceOrder(original, 0, -1), null); assert.equal(moveWorkspaceOrder(original, original.length - 1, 1), null); assert.deepEqual(original, ["device", "settings", "canvas", "text", "animations", "templates"]); }); test("drawer exposes one editable list and commits only from the save action", async () => { const [html, app, core, style] = await Promise.all([ readFile(new URL("index.html", staticRoot), "utf8"), readFile(new URL("app.js", staticRoot), "utf8"), readFile(new URL("core.js", staticRoot), "utf8"), readFile(new URL("style.css", staticRoot), "utf8"), ]); assert.ok(html.indexOf('id="workspace-order-toggle"') < html.indexOf('id="workspace-nav"')); assert.match(html, /id="workspace-order-toggle"[^>]*>自定义项目位置<\/button>/); assert.doesNotMatch(app, /nav-group|groupOrder/); assert.doesNotMatch(core, /"group"|"groupOrder"/); assert.match(app, /copy\.dynamic\.workspace_order\.move_up/); assert.match(app, /copy\.dynamic\.workspace_order\.move_down/); assert.match(app, /method:\s*"PUT"[\s\S]*workspace_order:\s*draftWorkspaceOrder/); assert.match(app, /workspaceOrderEditing \|\| workspaceOrderBusy/); assert.match(app, /requestCloseDrawer\(true\)/); assert.match(app, /event\.key === "Escape"[\s\S]*requestCloseDrawer\(true\)/); assert.doesNotMatch(app, /beforeunload|localStorage/); assert.match(style, /\.workspace-nav\.is-order-editing \.nav-item-row/); });