Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import becca from "../../becca/becca";
import { load } from "../../becca/becca_loader";
import { getContext } from "../../services/context";
import eraseService from "../../services/erase";
import noteService from "../../services/notes";
import { getSql } from "../../services/sql/index";
import { registerVirtualNoteProvider, unregisterVirtualNoteProvider } from "../../services/virtual_notes";
import ws from "../../services/ws";
import { createTextNote } from "../../test/api_fixtures";
import { CoreApiTester } from "../../test/api_tester";
/**
* Drives the shared core note routes through {@link CoreApiTester} (no Express),
* so this spec runs under both the node and standalone (WASM) suites.
*/
let api: CoreApiTester;
function noteIsDeleted(noteId: string): number | null {
const row = getSql().getRowOrNull<{ isDeleted: number }>(
"SELECT isDeleted FROM notes WHERE noteId = ?",
[ noteId ]
);
return row ? row.isDeleted : null;
}
describe("Notes API (core)", () => {
beforeAll(() => {
api = CoreApiTester.build();
});
describe("reading", () => {
it("returns note metadata for an existing note", async () => {
const res = await api.get<{ noteId: string; type: string }>("/api/notes/root");
expect(res.status).toBe(200);
expect(res.body.noteId).toBe("root");
expect(res.body.type).toBeTruthy();
});
it("returns timestamp metadata", async () => {
const res = await api.get("/api/notes/root/metadata");
expect(res.status).toBe(200);
expect(res.body).toMatchObject({
dateCreated: expect.any(String),
utcDateCreated: expect.any(String),
dateModified: expect.any(String),
utcDateModified: expect.any(String)
});
});
it("returns the timestamps of several notes at once, skipping the ones it cannot find", async () => {
const note = await createTextNote(api, { title: "Bulk metadata" });
const res = await api.post<Record<string, { utcDateCreated: string }>>(
"/api/notes/metadata",
{ body: { noteIds: [ "root", note.noteId, "missingNote123" ] } });
expect(res.status).toBe(200);
expect(Object.keys(res.body).sort()).toEqual([ note.noteId, "root" ].sort());
expect(res.body[note.noteId]).toMatchObject({
dateCreated: expect.any(String),
utcDateCreated: expect.any(String),
dateModified: expect.any(String),
utcDateModified: expect.any(String)
});
});
it("400s a bulk metadata request whose noteIds are not a list of strings", async () => {
expect((await api.post("/api/notes/metadata", { body: {} })).status).toBe(400);
expect((await api.post("/api/notes/metadata", { body: { noteIds: "root" } })).status)
.toBe(400);
expect((await api.post("/api/notes/metadata", { body: { noteIds: [ 1 ] } })).status)
.toBe(400);
});
it("returns the note blob", async () => {
const res = await api.get<{ blobId: string; content: string }>("/api/notes/root/blob");
expect(res.status).toBe(200);
expect(res.body.blobId).toBeTruthy();
expect(typeof res.body.content).toBe("string");
});
it("404s for a missing note", async () => {
const res = await api.get("/api/notes/missingNote123");
expect(res.status).toBe(404);
});
});
describe("creating", () => {
it("creates a child note under root", async () => {
interface CreateResponse {
note: { noteId: string; title: string };
branch: { parentNoteId: string };
}
const res = await api.post<CreateResponse>(
"/api/notes/root/children?target=into",
{ body: { title: "Created via API", type: "text", content: "<p>body</p>" } }
);
expect(res.status).toBe(200);
expect(res.body.note.noteId).toBeTruthy();
expect(res.body.note.title).toBe("Created via API");
expect(res.body.branch.parentNoteId).toBe("root");
});
// What "cut selection into sub-note" does: the new note is handed HTML carrying the source
// note's pictures, and the source then drops them from its own content, scheduling those
// attachments for erasure. The new note has to end up owning copies, not pointing at them.
it("copies a foreign attachment referenced by the content it is created with", async () => {
const source = await createTextNote(api, { content: "<p>source</p>" });
const save = await api.post(`/api/notes/${source.noteId}/attachments`, {
body: { role: "image", mime: "image/png", title: "picture.png", content: "picture bytes" }
});
expect(save.status).toBe(204);
const [ original ] = (await api.get<{ attachmentId: string }[]>(
`/api/notes/${source.noteId}/attachments`
)).body;
const created = await api.post<{ note: { noteId: string } }>(
"/api/notes/root/children?target=into",
{
body: {
title: "Cut selection",
type: "text",
content: `<p><img src="api/attachments/${original.attachmentId}/image/picture.png"></p>`
}
}
);
expect(created.status).toBe(200);
const { noteId } = created.body.note;
const copies = await api.get<{ attachmentId: string; ownerId: string; title: string }[]>(
`/api/notes/${noteId}/attachments`
);
expect(copies.body).toHaveLength(1);
expect(copies.body[0].ownerId).toBe(noteId);
expect(copies.body[0].attachmentId).not.toBe(original.attachmentId);
const blob = await api.get<{ content: string }>(`/api/notes/${noteId}/blob`);
expect(blob.body.content).toContain(`api/attachments/${copies.body[0].attachmentId}/image/`);
expect(blob.body.content).not.toContain(original.attachmentId);
});
it("400s when the target query param is invalid", async () => {
const res = await api.post("/api/notes/root/children", {
body: { title: "no target", type: "text" }
});
expect(res.status).toBe(400);
});
});
describe("updating", () => {
it("changes a note title and returns the updated note", async () => {
const { noteId } = await createTextNote(api, { title: "Before" });
const res = await api.put<{ title: string }>(`/api/notes/${noteId}/title`, {
body: { title: "After" }
});
expect(res.status).toBe(200);
expect(res.body.title).toBe("After");
});
it("updates note content", async () => {
const { noteId } = await createTextNote(api, { content: "<p>old</p>" });
const update = await api.put(`/api/notes/${noteId}/data`, {
body: { content: "<p>new</p>" }
});
expect(update.status).toBe(204);
const blob = await api.get<{ content: string }>(`/api/notes/${noteId}/blob`);
expect(blob.body.content).toContain("new");
});
});
describe("editing blocks", () => {
const blocks = `<p data-trilium-block-id="a">A</p><p data-trilium-block-id="b">B</p>`;
it("reads and replaces the blocks of a text note", async () => {
const { noteId } = await createTextNote(api, {
content: `<p>Before</p>${blocks}<p>After</p>`
});
const read = await api.get<{ content: string }>(`/api/notes/${noteId}/blocks`, {
query: { block: "a:b" }
});
expect(read.status).toBe(200);
expect(read.body.content).toBe(blocks);
const update = await api.put(`/api/notes/${noteId}/blocks`, {
query: { block: "a:b" },
body: { content: `<p data-trilium-block-id="a">New</p>` }
});
expect(update.status).toBe(204);
const blob = await api.get<{ content: string }>(`/api/notes/${noteId}/blob`);
expect(blob.body.content)
.toBe(`<p>Before</p><p data-trilium-block-id="a">New</p><p>After</p>`);
});
it("rejects missing blocks and malformed requests", async () => {
const { noteId } = await createTextNote(api, { content: blocks });
const url = `/api/notes/${noteId}/blocks`;
const codeNote = await api.post<{ note: { noteId: string } }>(
"/api/notes/root/children?target=into",
{ body: { title: "Code", type: "code", mime: "text/plain", content: blocks } }
);
const codeUrl = `/api/notes/${codeNote.body.note.noteId}/blocks`;
expect((await api.get(url, { query: { block: "a:x" } })).status).toBe(404);
expect((await api.get(url)).status).toBe(400);
expect((await api.get(codeUrl, { query: { block: "a" } })).status).toBe(400);
expect((await api.put(url, { query: { block: "a:x" }, body: { content: "" } })).status)
.toBe(404);
expect((await api.put(url, { query: { block: "a" }, body: {} })).status).toBe(400);
});
});
describe("deleting and undeleting", () => {
it("soft-deletes a note, then undeletes it", async () => {
const { noteId } = await createTextNote(api, { title: "To delete" });
expect(noteIsDeleted(noteId)).toBe(0);
const del = await api.delete(`/api/notes/${noteId}`, {
query: { taskId: "test-delete", last: "true" }
});
expect(del.status).toBe(204);
expect(noteIsDeleted(noteId)).toBe(1);
const undel = await api.put<{ undeleted: boolean }>(`/api/notes/${noteId}/undelete`);
expect(undel.status).toBe(200);
expect(undel.body.undeleted).toBe(true);
expect(noteIsDeleted(noteId)).toBe(0);
});
it("reports failure (and restores nothing) when the note's parent is still deleted", async () => {
const parent = await createTextNote(api, { title: "Deleted parent" });
const child = await createTextNote(api, { parentNoteId: parent.noteId, title: "Orphaned child" });
// Deleting the parent soft-deletes the whole subtree, including the child.
const del = await api.delete(`/api/notes/${parent.noteId}`, {
query: { taskId: "test-parent-delete", last: "true" }
});
expect(del.status).toBe(204);
expect(noteIsDeleted(child.noteId)).toBe(1);
// Undeleting the child directly can't succeed — its only parent is itself still deleted.
const undel = await api.put<{ undeleted: boolean }>(`/api/notes/${child.noteId}/undelete`);
expect(undel.status).toBe(200);
expect(undel.body.undeleted).toBe(false);
expect(noteIsDeleted(child.noteId)).toBe(1);
});
it("restores an orphaned note under the fallback parent when one is given", async () => {
const parent = await createTextNote(api, { title: "Doomed parent" });
const child = await createTextNote(api, { parentNoteId: parent.noteId, title: "Rehomed child" });
const newHome = await createTextNote(api, { title: "Inbox stand-in" });
// Deleting the parent orphans the child: its only original location is gone.
const del = await api.delete(`/api/notes/${parent.noteId}`, {
query: { taskId: "test-fallback-delete", last: "true" }
});
expect(del.status).toBe(204);
expect(noteIsDeleted(child.noteId)).toBe(1);
const undel = await api.put<{ undeleted: boolean; restoredToFallbackParent: boolean }>(
`/api/notes/${child.noteId}/undelete`,
{ body: { fallbackParentNoteId: newHome.noteId } }
);
expect(undel.status).toBe(200);
expect(undel.body.undeleted).toBe(true);
expect(undel.body.restoredToFallbackParent).toBe(true);
// The child is alive again under its new home; the original parent stays deleted.
expect(noteIsDeleted(child.noteId)).toBe(0);
expect(noteIsDeleted(parent.noteId)).toBe(1);
expect(becca.getNote(child.noteId)?.getParentNotes().map((note) => note.noteId))
.toContain(newHome.noteId);
});
it("re-links an inbox-restored orphan under its original parent when that parent is later restored", async () => {
const parent = await createTextNote(api, { title: "Parent restored later" });
const child = await createTextNote(api, { parentNoteId: parent.noteId, title: "Orphan rehomed" });
const inbox = await createTextNote(api, { title: "Inbox stand-in 2" });
// Deleting the parent orphans the child.
await api.delete(`/api/notes/${parent.noteId}`, {
query: { taskId: "test-relink-delete", last: "true" }
});
// The user restores just the child, into the inbox.
const undelChild = await api.put<{ restoredToFallbackParent: boolean }>(
`/api/notes/${child.noteId}/undelete`,
{ body: { fallbackParentNoteId: inbox.noteId } }
);
expect(undelChild.body.restoredToFallbackParent).toBe(true);
// Later, the user restores the whole parent.
const undelParent = await api.put<{ undeleted: boolean }>(`/api/notes/${parent.noteId}/undelete`);
expect(undelParent.body.undeleted).toBe(true);
expect(noteIsDeleted(parent.noteId)).toBe(0);
// The child is the SAME note (not a copy), now cloned under both its new home and its
// original parent — so restoring the parent brings back a complete subtree.
const parentIds = becca.getNote(child.noteId)?.getParentNotes().map((note) => note.noteId);
expect(parentIds).toContain(inbox.noteId);
expect(parentIds).toContain(parent.noteId);
});
it("reports failure (instead of crashing) when undeleting an already-erased note", async () => {
const { noteId } = await createTextNote(api, { title: "Erase then undelete" });
const del = await api.delete(`/api/notes/${noteId}`, {
query: { taskId: "test-erase-undelete", eraseNotes: "true", last: "true" }
});
expect(del.status).toBe(204);
expect(noteIsDeleted(noteId)).toBeNull(); // row is gone entirely
const undel = await api.put<{ undeleted: boolean }>(`/api/notes/${noteId}/undelete`);
expect(undel.status).toBe(200);
expect(undel.body.undeleted).toBe(false);
});
it("400s when deleting without a taskId", async () => {
const { noteId } = await createTextNote(api, { title: "Needs taskId" });
const res = await api.delete(`/api/notes/${noteId}`);
expect(res.status).toBe(400);
});
it("erases a note immediately when eraseNotes is set", async () => {
const { noteId } = await createTextNote(api, { title: "Erase now" });
const del = await api.delete(`/api/notes/${noteId}`, {
query: { taskId: "test-erase", eraseNotes: "true", last: "true" }
});
expect(del.status).toBe(204);
// Erasing removes the row entirely rather than just flagging it deleted.
expect(noteIsDeleted(noteId)).toBeNull();
});
it("holds a multi-note erase back until the last request of the task group", async () => {
const first = await createTextNote(api, { title: "Batch erase note 1" });
const second = await createTextNote(api, { title: "Batch erase note 2" });
const taskId = "test-note-erase-batch";
const firstRes = await api.delete(`/api/notes/${first.noteId}`, {
query: { taskId, last: "false", eraseNotes: "true" }
});
expect(firstRes.status).toBe(204);
// Soft-deleted, but not erased: the client is still sending the rest of the batch, and
// an erase here would reload it out from under them.
expect(noteIsDeleted(first.noteId)).toBe(1);
const secondRes = await api.delete(`/api/notes/${second.noteId}`, {
query: { taskId, last: "true", eraseNotes: "true" }
});
expect(secondRes.status).toBe(204);
// The last request erases everything the group deleted, in one go.
expect(noteIsDeleted(first.noteId)).toBeNull();
expect(noteIsDeleted(second.noteId)).toBeNull();
});
});
describe("deleting a selection", () => {
it("deletes every branch of the selection, and erases them when asked", async () => {
const first = await createTextNote(api, { title: "Selection note 1" });
const second = await createTextNote(api, { title: "Selection note 2" });
const child = await createTextNote(api,
{ parentNoteId: first.noteId, title: "Selection child" });
const res = await api.post("/api/delete-notes", {
body: {
branchIdsToDelete: [ first.branchId, second.branchId ],
taskId: "test-batch-delete"
}
});
expect(res.status).toBe(204);
expect(noteIsDeleted(first.noteId)).toBe(1);
expect(noteIsDeleted(second.noteId)).toBe(1);
// Deleting a note takes its subtree with it, as the per-note route does.
expect(noteIsDeleted(child.noteId)).toBe(1);
const toErase = await createTextNote(api, { title: "Selection to erase" });
const eraseRes = await api.post("/api/delete-notes", {
body: {
branchIdsToDelete: [ toErase.branchId ],
eraseNotes: true,
taskId: "test-batch-erase"
}
});
expect(eraseRes.status).toBe(204);
// One request covers the whole selection, so nothing holds the erase back.
expect(noteIsDeleted(toErase.noteId)).toBeNull();
});
it("removes only the selected branch of a clone, unless all clones are asked", async () => {
const otherParent = await createTextNote(api, { title: "Other clone parent" });
const cloned = await createTextNote(api, { title: "Cloned note" });
const clone = await api.put<{ branchId: string }>(
`/api/notes/${cloned.noteId}/clone-to-note/${otherParent.noteId}`,
{ body: {} }
);
expect(clone.status).toBe(200);
const res = await api.post("/api/delete-notes", {
body: {
branchIdsToDelete: [ cloned.branchId ],
taskId: "test-batch-clone-delete"
}
});
expect(res.status).toBe(204);
// The note outlives the branch: it still hangs under the other parent.
expect(noteIsDeleted(cloned.noteId)).toBe(0);
expect(becca.getNote(cloned.noteId)?.getParentNotes().map((note) => note.noteId))
.toEqual([ otherParent.noteId ]);
const allClones = await createTextNote(api, { title: "Cloned note, all clones" });
await api.put(
`/api/notes/${allClones.noteId}/clone-to-note/${otherParent.noteId}`, { body: {} });
const allRes = await api.post("/api/delete-notes", {
body: {
branchIdsToDelete: [ allClones.branchId ],
deleteAllClones: true,
taskId: "test-batch-all-clones-delete"
}
});
expect(allRes.status).toBe(204);
expect(noteIsDeleted(allClones.noteId)).toBe(1);
});
it("skips a branch an earlier one of the selection already took down", async () => {
const parent = await createTextNote(api, { title: "Selection ancestor" });
const child = await createTextNote(api,
{ parentNoteId: parent.noteId, title: "Selection descendant" });
// The tree only ever selects top-most nodes, but other callers (a note title menu,
// the table view) can name both.
const res = await api.post("/api/delete-notes", {
body: {
branchIdsToDelete: [ parent.branchId, child.branchId ],
taskId: "test-batch-nested-delete"
}
});
expect(res.status).toBe(204);
expect(noteIsDeleted(parent.noteId)).toBe(1);
expect(noteIsDeleted(child.noteId)).toBe(1);
});
it("reports the caller's total, and announces the erasing phase", async () => {
const progress = vi.spyOn(ws, "sendMessageToAllClients").mockImplementation(() => {});
try {
const { branchId } = await createTextNote(api, { title: "Progress-reported" });
const res = await api.post("/api/delete-notes", {
body: {
branchIdsToDelete: [ branchId ],
eraseNotes: true,
totalCount: 4,
taskId: "test-batch-progress"
}
});
expect(res.status).toBe(204);
const counts = progress.mock.calls
.map(([ message ]) => message)
.filter((message) => message.type === "taskProgressCount");
// The first message leaves the constructor before the total is known; every later
// one carries it, so the client can draw a bar.
expect(counts.length).toBeGreaterThan(1);
expect(counts[0].totalCount).toBeUndefined();
expect(counts.at(-1)).toMatchObject({ totalCount: 4, phase: "erasing" });
} finally {
progress.mockRestore();
}
});
it("tells the task system when it fails, and keeps the selection", async () => {
const { noteId, branchId } = await createTextNote(api, { title: "Delete that fails" });
const messages = vi.spyOn(ws, "sendMessageToAllClients").mockImplementation(() => {});
const erase = vi.spyOn(eraseService, "eraseNotesWithDeleteIds")
.mockImplementation(() => {
throw new Error("erase blew up");
});
try {
const res = await api.post("/api/delete-notes", {
body: {
branchIdsToDelete: [ branchId ],
eraseNotes: true,
taskId: "test-batch-failure"
}
});
expect(res.status).toBe(500);
const sent = messages.mock.calls.map(([ message ]) => message);
// Without a terminal message the client's progress toast stays up, and it has no
// close button of its own.
expect(sent.filter((message) => message.type === "taskError")).toEqual([
expect.objectContaining({
taskId: "test-batch-failure",
taskType: "deleteNotes",
message: expect.stringContaining("erase blew up")
})
]);
expect(sent.filter((message) => message.type === "taskSucceeded")).toEqual([]);
// The throw rolls the transaction back, so nothing of the selection is gone.
expect(noteIsDeleted(noteId)).toBe(0);
} finally {
erase.mockRestore();
messages.mockRestore();
}
});
it("400s without a taskId or a branch id array", async () => {
const { noteId, branchId } = await createTextNote(api, { title: "Needs a task id" });
const noTaskId = await api.post("/api/delete-notes", {
body: { branchIdsToDelete: [ branchId ] }
});
expect(noTaskId.status).toBe(400);
const noBranchIds = await api.post("/api/delete-notes", {
body: { taskId: "test-batch-no-branches" }
});
expect(noBranchIds.status).toBe(400);
// Neither one deleted anything.
expect(noteIsDeleted(noteId)).toBe(0);
});
});
describe("creating with targets", () => {
it("creates a sibling note after a target branch", async () => {
const anchor = await createTextNote(api, { title: "Anchor" });
interface CreateResponse {
note: { noteId: string };
branch: { parentNoteId: string };
}
const res = await api.post<CreateResponse>(
"/api/notes/root/children",
{ query: { target: "after", targetBranchId: anchor.branchId }, body: { title: "After sibling", type: "text", content: "" } }
);
expect(res.status).toBe(200);
expect(res.body.note.noteId).toBeTruthy();
expect(res.body.branch.parentNoteId).toBe("root");
});
});
describe("revisions and conversion", () => {
it("force-saves a revision and returns its id", async () => {
const { noteId } = await createTextNote(api, { title: "Snapshot me" });
const res = await api.post<{ revisionId: string }>(`/api/notes/${noteId}/revision`, {
body: { description: "manual" }
});
expect(res.status).toBe(200);
expect(res.body.revisionId).toBeTruthy();
});
it("400s force-saving a revision of a protected note without a protected session", async () => {
const { noteId } = await createTextNote(api, { title: "Protected revision" });
becca.notes[noteId].isProtected = true;
const res = await api.post(`/api/notes/${noteId}/revision`, { body: {} });
expect(res.status).toBe(400);
becca.notes[noteId].isProtected = false;
});
it("returns a null attachment for a note ineligible for conversion", async () => {
const { noteId } = await createTextNote(api, { title: "Not an image" });
const res = await api.post<{ attachment: unknown }>(`/api/notes/${noteId}/convert-to-attachment`);
expect(res.status).toBe(200);
expect(res.body.attachment).toBeNull();
});
});
describe("title and type", () => {
it("400s changing the title of a protected note without a protected session", async () => {
const { noteId } = await createTextNote(api, { title: "Locked" });
becca.notes[noteId].isProtected = true;
const res = await api.put(`/api/notes/${noteId}/title`, { body: { title: "Nope" } });
expect(res.status).toBe(400);
becca.notes[noteId].isProtected = false;
});
it("leaves the note untouched when the title is unchanged", async () => {
const { noteId } = await createTextNote(api, { title: "Same title" });
const res = await api.put<{ title: string }>(`/api/notes/${noteId}/title`, {
body: { title: "Same title" }
});
expect(res.status).toBe(200);
expect(res.body.title).toBe("Same title");
});
it("sets the note type and mime", async () => {
const { noteId } = await createTextNote(api, { title: "Type me" });
const res = await api.put(`/api/notes/${noteId}/type`, {
body: { type: "code", mime: "text/x-python" }
});
expect(res.status).toBe(204);
const note = await api.get<{ type: string; mime: string }>(`/api/notes/${noteId}`);
expect(note.body.type).toBe("code");
expect(note.body.mime).toBe("text/x-python");
});
it("rejects a type outside the note type set and leaves the note unchanged", async () => {
const { noteId } = await createTextNote(api, { title: "Bad type" });
for (const type of ["EVILTYPE", "html", undefined, 42]) {
const res = await api.put(`/api/notes/${noteId}/type`, {
body: { type, mime: "application/x-anything" }
});
expect(res.status).toBe(400);
}
expect(becca.getNoteOrThrow(noteId).type).toBe("text");
expect(becca.getNoteOrThrow(noteId).mime).toBe("text/html");
});
});
describe("structural operations", () => {
it("sorts child notes", async () => {
const parent = await createTextNote(api, { title: "Sort parent" });
await createTextNote(api, { parentNoteId: parent.noteId, title: "Zebra" });
await createTextNote(api, { parentNoteId: parent.noteId, title: "Alpha" });
const res = await api.put(`/api/notes/${parent.noteId}/sort-children`, {
body: { sortBy: "title", sortDirection: "asc", foldersFirst: false, sortNatural: false, sortLocale: "" }
});
expect(res.status).toBe(204);
const children = becca.notes[parent.noteId].getChildNotes();
expect(children[0].title).toBe("Alpha");
});
it("protects a note recursively", async () => {
const { noteId } = await createTextNote(api, { title: "Protect target" });
// The actual (un)protect requires an active protected session, which the
// in-process tester does not have, so stub the recursive worker and assert
// the handler drives it (with subtree) and reports task success.
const spy = vi.spyOn(noteService, "protectNoteRecursively").mockImplementation(() => {});
try {
const res = await api.put(`/api/notes/${noteId}/protect/1`, { query: { subtree: "1" } });
expect(res.status).toBe(204);
expect(spy).toHaveBeenCalledWith(becca.notes[noteId], true, true, expect.anything());
} finally {
spy.mockRestore();
}
});
it("duplicates a subtree under a parent", async () => {
const original = await createTextNote(api, { title: "Original subtree" });
await createTextNote(api, { parentNoteId: original.noteId, title: "Child" });
interface DuplicateResponse {
note: { noteId: string; title: string };
}
const res = await api.post<DuplicateResponse>(
`/api/notes/${original.noteId}/duplicate/root`
);
expect(res.status).toBe(200);
expect(res.body.note.noteId).not.toBe(original.noteId);
expect(res.body.note.title).toContain("Original subtree");
expect(becca.getNoteOrThrow(res.body.note.noteId).getChildNotes()).toHaveLength(1);
});
it("duplicates only the note when the body asks for no children", async () => {
const original = await createTextNote(api, { title: "Original note only" });
await createTextNote(api, { parentNoteId: original.noteId, title: "Child" });
const res = await api.post<{ note: { noteId: string } }>(
`/api/notes/${original.noteId}/duplicate/root`, { body: { withChildren: false } }
);
expect(res.status).toBe(200);
expect(becca.getNoteOrThrow(res.body.note.noteId).getChildNotes()).toHaveLength(0);
});
});
describe("erase and preview operations", () => {
it("erases deleted notes now", async () => {
const { noteId } = await createTextNote(api, { title: "Will be erased" });
await api.delete(`/api/notes/${noteId}`, { query: { taskId: "erase-deleted", last: "true" } });
const res = await api.post("/api/notes/erase-deleted-notes-now");
expect(res.status).toBe(204);
expect(noteIsDeleted(noteId)).toBeNull();
});
it("erases unused attachments now", async () => {
const res = await api.post("/api/notes/erase-unused-attachments-now");
expect(res.status).toBe(204);
});
it("previews a subtree to be deleted and surfaces broken relations", async () => {
// parent → child, with an outside note holding a relation to the child;
// deleting the parent recurses into the child and breaks the outside relation.
const parent = await createTextNote(api, { title: "Preview parent" });
const child = await createTextNote(api, { parentNoteId: parent.noteId, title: "Preview child" });
const outside = await createTextNote(api, { title: "Preview outside" });
const rel = await api.put(
`/api/notes/${outside.noteId}/relations/refToChild/to/${child.noteId}`,
{ body: {} }
);
expect(rel.status).toBeLessThan(300);
const res = await api.post<{ noteIdsToBeDeleted: string[]; brokenRelations: Array<{ noteId: string }> }>(
"/api/delete-notes-preview",
{ body: { branchIdsToDelete: [ parent.branchId ], deleteAllClones: true } }
);
expect(res.status).toBe(200);
expect(res.body.noteIdsToBeDeleted).toEqual(expect.arrayContaining([ parent.noteId, child.noteId ]));
expect(res.body.brokenRelations.some((attr) => attr.noteId === outside.noteId)).toBe(true);
});
it("skips missing branches in the delete preview", async () => {
const res = await api.post<{ noteIdsToBeDeleted: string[] }>(
"/api/delete-notes-preview",
{ body: { branchIdsToDelete: [ "missingBranch123" ], deleteAllClones: false } }
);
expect(res.status).toBe(200);
expect(res.body.noteIdsToBeDeleted).toEqual([]);
});
it("skips weak branches (e.g. bookmarks) in the delete preview", async () => {
const note = await createTextNote(api, { title: "Weakly linked" });
// A clone under _lbBookmarks produces a weak branch, which the preview ignores.
interface CloneResponse { branchId: string }
const clone = await api.put<CloneResponse>(
`/api/notes/${note.noteId}/clone-to-note/_lbBookmarks`,
{ body: {} }
);
expect(clone.status).toBe(200);
expect(clone.body.branchId).toBeTruthy();
const res = await api.post<{ noteIdsToBeDeleted: string[] }>(
"/api/delete-notes-preview",
{ body: { branchIdsToDelete: [ clone.body.branchId ], deleteAllClones: true } }
);
expect(res.status).toBe(200);
expect(res.body.noteIdsToBeDeleted).toEqual([]);
});
});
describe("read-only virtual notes", () => {
beforeAll(() => {
registerVirtualNoteProvider({
namespace: "_vapi",
parentNoteId: "_hidden",
getSubtree: () => [{ id: "_vapiPage", title: "Page", type: "text" }],
getContent: () => "<p>shipped</p>"
});
getContext().init(() => load());
});
afterAll(() => {
unregisterVirtualNoteProvider("_vapi");
getContext().init(() => load());
});
it("refuses content and revision writes as a client error", async () => {
expect((await api.put("/api/notes/_vapiPage/data", { body: { content: "<p>edited</p>" } })).status).toBe(400);
expect((await api.post("/api/notes/_vapiPage/revision")).status).toBe(400);
expect(becca.getNoteOrThrow("_vapiPage").getContent()).toBe("<p>shipped</p>");
});
});
});