AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(api): return 404 when a new memo names a missing notebook
tianma-ifc1d92c75facc
diff --git a/apps/api/src/memo-create-notebook.test.mjs b/apps/api/src/memo-create-notebook.test.mjsnew file mode 100644index 00000000..bd783d93--- /dev/null+++ b/apps/api/src/memo-create-notebook.test.mjs@@ -0,0 +1,51 @@+import { afterEach, expect, test } from "bun:test";+import { Database } from "bun:sqlite";+import { globSync, readFileSync } from "node:fs";+import { AppError } from "./app-error.ts";+import { createMemoRecord } from "./memo-service.ts";+import { createSelfHostedStorageAdapter } from "./self-hosted-storage-adapter.ts";++const opened = [];+afterEach(() => opened.splice(0).forEach((db) => db.close()));++const setup = () => {+ const sqlite = new Database(":memory:");+ opened.push(sqlite);+ for (const file of globSync("migrations/*.sql").sort()) sqlite.exec(readFileSync(file, "utf8"));+ sqlite.exec(`INSERT INTO workspaces(id, name) VALUES ('paw', 'Paw');+ INSERT INTO notebooks(id, workspace_id, name) VALUES ('inbox', 'paw', 'Inbox');`);+ return { sqlite, db: createSelfHostedStorageAdapter(sqlite, "/tmp/memo-notebook-unused").db };+};++const actor = { actorType: "user", actorId: "user" };++test("a missing notebook is a 404 and leaves no memo rows", async () => {+ const { sqlite, db } = setup();+ const memosBefore = sqlite.query("SELECT COUNT(*) AS count FROM memos").get().count;+ const contentsBefore = sqlite.query("SELECT COUNT(*) AS count FROM memo_contents").get().count;+ const error = await createMemoRecord(+ db,+ "paw",+ { notebookId: "\u0001\u0005token1", title: "Video", contentMarkdown: "hello" },+ actor,+ "user",+ ).then(() => null, (caught) => caught);+ expect(error).toBeInstanceOf(AppError);+ expect(error.status).toBe(404);+ expect(error.message).toBe("Notebook not found");+ expect(sqlite.query("SELECT COUNT(*) AS count FROM memos").get().count).toBe(memosBefore);+ expect(sqlite.query("SELECT COUNT(*) AS count FROM memo_contents").get().count).toBe(contentsBefore);+});++test("a live notebook still creates the memo", async () => {+ const { db } = setup();+ const memo = await createMemoRecord(+ db,+ "paw",+ { notebookId: "inbox", title: "Video", contentMarkdown: "hello" },+ actor,+ "user",+ );+ expect(memo.notebookId).toBe("inbox");+ expect(memo.title).toBe("Video");+});diff --git a/apps/api/src/memo-service.ts b/apps/api/src/memo-service.tsindex 739e7b3e..2eea49d5 100644--- a/apps/api/src/memo-service.ts+++ b/apps/api/src/memo-service.ts@@ -946,6 +946,12 @@ export const createMemoRecord = async (actor: { actorType: "user" | "agent"; actorId: string | null },actorLabel: string): Promise<MemoDetail> => {+ // Reject a missing notebook before the batch. The memo insert is a SELECT+ // from notebooks, so a bad id used to write orphan content and then fail+ // the read-back as a generic 500.+ if (!(await getNotebook(db, workspaceId, input.notebookId))) {+ throw new AppError("not_found", "Notebook not found", 404);+ }const tags = normalizeTags(input.tags);const contentMarkdown = input.contentMarkdown ?? "";const contentJson = input.contentJson && typeof input.contentJson === "object"