Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): make empty trash idempotent

tianma-if62665863002c
diff --git a/apps/web/src/lib/local-mirror.test.mjs b/apps/web/src/lib/local-mirror.test.mjs
index bcd9f612..ecb48ac1 100644
--- a/apps/web/src/lib/local-mirror.test.mjs
+++ b/apps/web/src/lib/local-mirror.test.mjs
@@ -294,11 +294,32 @@ describe("local mirror", () => {
test("deletes and empties trash locally without waiting for the API", async () => {
const scope = createLocalDataScope("https://demo.edgeever.org", "user-1");
- const memo = await createLocalMemo(scope, { notebookId: "inbox" });
- expect(await applyLocalMemoDeleteBatch(scope, [memo.id])).toBe(1);
- expect((await getLocalMemo(scope, memo.id))?.isDeleted).toBe(true);
+ const activeMemo = await createLocalMemo(scope, { notebookId: "inbox", title: "Keep me" });
+ const trashedMemo = await createLocalMemo(scope, { notebookId: "inbox", title: "Delete me" });
+ expect(await applyLocalMemoDeleteBatch(scope, [trashedMemo.id])).toBe(1);
+ expect((await getLocalMemo(scope, trashedMemo.id))?.isDeleted).toBe(true);
expect(await applyLocalEmptyTrash(scope)).toBe(1);
- expect(await getLocalMemo(scope, memo.id)).toBeNull();
+ expect(await getLocalMemo(scope, trashedMemo.id)).toBeNull();
+ expect(await applyLocalEmptyTrash(scope)).toBe(0);
+ expect((await getLocalMemo(scope, activeMemo.id))?.title).toBe("Keep me");
+ });
+
+ test("treats empty batch selections as no-ops", async () => {
+ const scope = createLocalDataScope("https://demo.edgeever.org", "user-1");
+ const memo = await createLocalMemo(scope, { notebookId: "inbox", title: "Keep me", tags: ["old"] });
+
+ expect(await applyLocalMemoMove(scope, [], "archive")).toBe(0);
+ expect(await applyLocalMemoPin(scope, [], true)).toBe(0);
+ expect(await applyLocalMemoDeleteBatch(scope, [])).toBe(0);
+ expect(await applyLocalMemoDeleteBatch(scope, [], true)).toBe(0);
+ expect(await applyLocalTagRename(scope, "missing", "new")).toBe(0);
+
+ expect(await getLocalMemo(scope, memo.id)).toMatchObject({
+ notebookId: "inbox",
+ isPinned: false,
+ isDeleted: false,
+ tags: ["old"],
+ });
});
test("updates and removes notebooks in the local mirror immediately", async () => {
diff --git a/apps/web/src/lib/local-mirror.ts b/apps/web/src/lib/local-mirror.ts
index a0b1418e..5a2d57f3 100644
--- a/apps/web/src/lib/local-mirror.ts
+++ b/apps/web/src/lib/local-mirror.ts
@@ -644,11 +644,13 @@ export const listLocalTags = async (scope: string): Promise<{ tags: TagSummary[]
const updateLocalMemos = async (scope: string, memoIds: string[], update: (memo: MemoDetail) => MemoDetail | null) => {
const wanted = new Set(memoIds);
+ if (wanted.size === 0) return 0;
+
let updated = 0;
await localDb.transaction("rw", localDb.memos, async () => {
const memos = await localDb.memos.where("scope").equals(scope).toArray();
for (const stored of memos) {
- if (wanted.size > 0 && !wanted.has(stored.id)) continue;
+ if (!wanted.has(stored.id)) continue;
const { scope: _scope, ...memo } = stored;
const next = update(memo);
if (!next) {