Files
edgeever/apps/api/src/workspace-provisioning.test.mjs
T
tianma-if cc874cd3f9 fix(sync): keep desktop pull working without a slug inbox
Identify the workspace inbox by stable id, restore it when missing, and apply notebooks before memos so a renamed or deleted inbox cannot abort the whole pull.

Fixes #378
2026-09-10 11:52:01 +08:00

76 lines
2.3 KiB
JavaScript

import { describe, expect, test } from "bun:test";
import {
createDefaultNotebookRows,
createWorkspaceDefaultSeedStatements,
ensureUserWorkspace,
isInboxNotebook,
workspaceInboxId,
} from "./workspace-provisioning.ts";
const statement = (sql, calls) => ({
bind(...values) {
calls.push({ sql, values });
return this;
},
async first() {
return sql.includes("FROM workspace_members WHERE user_id")
? { workspace_id: "ws_existing", role: "member" }
: null;
},
async run() {
return { success: true };
},
});
describe("workspace provisioning", () => {
test("uses a stable workspace-scoped inbox id", () => {
expect(workspaceInboxId("ws_1")).toBe("ws_1_inbox");
expect(createDefaultNotebookRows("ws_1")[0]).toMatchObject({
id: "ws_1_inbox",
slug: "inbox",
});
expect(isInboxNotebook({ id: "ws_1_inbox", slug: "shou-ji-xiang" }, "ws_1")).toBe(true);
});
test("does not restore defaults while resolving an existing workspace", async () => {
const calls = [];
let batchCount = 0;
const db = {
prepare: (sql) => statement(sql, calls),
batch: async () => {
batchCount += 1;
return [];
},
};
expect(await ensureUserWorkspace(db, "usr_existing", "writer")).toEqual({
workspaceId: "ws_existing",
role: "member",
});
expect(batchCount).toBe(0);
expect(calls).toHaveLength(1);
expect(calls[0].sql).toContain("FROM workspace_members WHERE user_id");
});
test("builds editable localized template and AI prompt statements for new workspaces", () => {
const calls = [];
const db = {
prepare: (sql) => statement(sql, calls),
};
const statements = createWorkspaceDefaultSeedStatements(
db,
"ws_new",
"2026-08-14T00:00:00.000Z",
"en-US",
);
expect(statements.length).toBeGreaterThan(6);
expect(calls[0].sql).toContain("INSERT OR IGNORE INTO memo_templates");
expect(calls[0].values).toContain("ws_new_template_quick-note");
expect(calls[0].values).toContain("Quick Spark");
expect(calls.filter((call) => call.sql.includes("INSERT OR IGNORE INTO memo_templates"))).toHaveLength(5);
expect(calls.some((call) => call.sql.includes("INSERT OR IGNORE INTO ai_prompt_templates"))).toBe(true);
});
});