import { test } from "node:test"; import assert from "node:assert/strict"; import { createBoard, loadWorkspaceHome } from "../src/lib/workspace.ts"; test("profile and workspace reads both start before either completes", async () => { const calls = []; const resolve = {}; const pending = loadWorkspaceHome((path) => { calls.push(path); return new Promise((done) => { resolve[path] = done; }); }); assert.deepEqual(calls, ["profile", "workspaces"]); resolve.profile({ id: "user" }); resolve.workspaces({ items: [{ id: "space", name: "个人空间" }] }); const result = await pending; assert.equal(result.profile.id, "user"); assert.equal(result.workspaces[0].id, "space"); }); test("a first board creates its personal workspace before posting the board", async () => { const calls = []; let selected = ""; const result = await createBoard( async (path, options) => { calls.push([path, JSON.parse(options.body)]); if (path === "workspaces") return { id: "space", name: "个人空间" }; assert.equal(selected, "space"); return { id: "board" }; }, " 周末的灵感 ", "", (w) => { selected = w.id; }, ); assert.equal(result.id, "board"); assert.deepEqual( calls.map(([p]) => p), ["workspaces", "workspaces/space/materials"], ); assert.equal(calls[1][1].title, "周末的灵感"); }); test("a board failure retains the new workspace, so retry creates no duplicate space", async () => { let selected = ""; let spaces = 0; let fail = true; const request = async (path) => { if (path === "workspaces") { spaces++; return { id: "space", name: "个人空间" }; } if (fail) throw new Error("upstream_unavailable"); return { id: "board" }; }; await assert.rejects( createBoard(request, "作品", selected, (w) => { selected = w.id; }), ); fail = false; await createBoard(request, "作品", selected, () => assert.fail("duplicate workspace"), ); assert.equal(spaces, 1); }); test("an existing workspace posts a board directly", async () => { const calls = []; await createBoard( async (path) => { calls.push(path); return { id: "board" }; }, "作品", "existing", () => assert.fail("unexpected workspace"), ); assert.deepEqual(calls, ["workspaces/existing/materials"]); });