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 type { EntityChange, EntityChangeRecord, EntityRow } from "@triliumnext/commons";
import { afterEach, describe, expect, it, vi } from "vitest";
import * as cls from "./context.js";
import events from "./events.js";
import notes from "./notes.js";
import { getSql } from "./sql/index.js";
import syncUpdateService from "./sync_update.js";
import { encodeBase64 } from "./utils/binary.js";
import dateUtils from "./utils/date.js";
import ws from "./ws.js";
const FUTURE = "2099-01-01 00:00:00.000Z";
const PAST = "2000-01-01 00:00:00.000Z";
let counter = 0;
function createNote(title = "sync-update"): string {
counter++;
return cls.init(() => notes.createNewNote({
parentNoteId: "root",
title: `${title}-${counter}`,
content: "<p>x</p>",
type: "text"
}).note.noteId);
}
function noteRow(noteId: string): EntityRow {
const row = getSql().getRowOrNull<EntityRow>("SELECT * FROM notes WHERE noteId = ?", [noteId]);
if (!row) throw new Error(`no note row ${noteId}`);
return row;
}
/** A note row with a few columns overridden, cast to the permissive EntityRow shape. */
function rowWith(noteId: string, extra: Record<string, unknown>): EntityRow {
return { ...noteRow(noteId), ...extra } as unknown as EntityRow;
}
function localHash(entityName: string, entityId: string): string {
return getSql().getValue<string>("SELECT hash FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]) ?? "";
}
function buildEC(overrides: Partial<EntityChange>): EntityChange {
return {
entityName: "notes",
entityId: "x",
hash: "remote-hash",
isErased: false,
isSynced: true,
utcDateChanged: FUTURE,
...overrides
} as EntityChange;
}
function apply(records: EntityChangeRecord[], instanceId = "remoteInst") {
return cls.init(() => syncUpdateService.updateEntities(records, instanceId));
}
describe("sync_update service (real DB)", () => {
afterEach(() => vi.restoreAllMocks());
it("returns immediately for an empty change set without signalling a pull", () => {
const pullSpy = vi.spyOn(ws, "syncPullInProgress");
apply([]);
expect(pullSpy).not.toHaveBeenCalled();
});
it("skips changes whose changeId has already been applied", () => {
const pullSpy = vi.spyOn(ws, "syncPullInProgress");
const noteId = createNote();
const changeId = localChangeId("notes", noteId);
apply([{ entityChange: buildEC({ entityId: noteId, changeId }), entity: noteRow(noteId) }]);
// Already-applied change -> no pull signalled.
expect(pullSpy).not.toHaveBeenCalled();
});
it("applies a newer remote note, signals a pull and emits ENTITY_CHANGE_SYNCED", () => {
const pullSpy = vi.spyOn(ws, "syncPullInProgress");
const emitSpy = vi.spyOn(events, "emit");
const noteId = createNote();
const row = rowWith(noteId, { title: "updated-by-sync" });
apply([{ entityChange: buildEC({ entityId: noteId, hash: "changed" }), entity: row }], "remoteInstA");
expect(pullSpy).toHaveBeenCalledOnce();
expect(getSql().getValue("SELECT title FROM notes WHERE noteId = ?", [noteId])).toBe("updated-by-sync");
expect(getSql().getValue("SELECT instanceId FROM entity_changes WHERE entityName='notes' AND entityId=?", [noteId])).toBe("remoteInstA");
expect(emitSpy).toHaveBeenCalledWith(events.ENTITY_CHANGE_SYNCED, expect.objectContaining({ entityName: "notes" }));
});
it("emits ENTITY_DELETE_SYNCED when the synced row is marked deleted", () => {
const emitSpy = vi.spyOn(events, "emit");
const noteId = createNote();
const row = rowWith(noteId, { isDeleted: 1 });
apply([{ entityChange: buildEC({ entityId: noteId, hash: "changed" }), entity: row }]);
expect(emitSpy).toHaveBeenCalledWith(events.ENTITY_DELETE_SYNCED, expect.objectContaining({ entityName: "notes", entityId: noteId }));
});
it("erases a note when the remote change is erased and was not erased locally", () => {
const noteId = createNote();
apply([{ entityChange: buildEC({ entityId: noteId, isErased: true }), entity: undefined }]);
expect(getSql().getRowOrNull("SELECT 1 FROM notes WHERE noteId = ?", [noteId])).toBeNull();
});
it("re-erases a note that was already erased locally", () => {
const noteId = createNote();
cls.init(() => getSql().execute("UPDATE entity_changes SET isErased = 1 WHERE entityName='notes' AND entityId=?", [noteId]));
apply([{ entityChange: buildEC({ entityId: noteId, isErased: true }), entity: undefined }]);
expect(getSql().getRowOrNull("SELECT 1 FROM notes WHERE noteId = ?", [noteId])).toBeNull();
});
it("requeues the change for other instances when the local copy is newer (conflict)", () => {
const emitSpy = vi.spyOn(events, "emit");
const noteId = createNote();
const before = getSql().getValue("SELECT title FROM notes WHERE noteId = ?", [noteId]);
apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: PAST }), entity: rowWith(noteId, { title: "should-not-apply" }) }]);
// Older remote change is rejected, local row untouched.
expect(getSql().getValue("SELECT title FROM notes WHERE noteId = ?", [noteId])).toBe(before);
expect(emitSpy).not.toHaveBeenCalledWith(events.ENTITY_CHANGE_SYNCED, expect.anything());
});
it("requeues the local blob with a newer note whose blob the remote does not reference", () => {
const noteId = createNote();
const localBlobId = String(noteRow(noteId).blobId);
const maxIdBefore = maxEntityChangeId();
apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: PAST }), entity: rowWith(noteId, { blobId: "su_remote_blob" }) }]);
expect(noteRow(noteId).blobId).toBe(localBlobId);
expect(requeuedSince(maxIdBefore, "notes", noteId)).toBe(true);
expect(requeuedSince(maxIdBefore, "blobs", localBlobId)).toBe(true);
});
it("does not requeue the blob when a newer local note shares it with the remote row", () => {
const noteId = createNote();
const localBlobId = String(noteRow(noteId).blobId);
const maxIdBefore = maxEntityChangeId();
apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: PAST }), entity: rowWith(noteId, { title: "remote-title" }) }]);
expect(requeuedSince(maxIdBefore, "notes", noteId)).toBe(true);
expect(requeuedSince(maxIdBefore, "blobs", localBlobId)).toBe(false);
});
it("does not requeue a sync stub with a newer note, so the remote keeps the full content", () => {
const noteId = createNote();
const stubBlobId = "suStubbedBlob";
cls.init(() => {
const sql = getSql();
const now = dateUtils.utcNowDateTime();
sql.execute("INSERT INTO blobs (blobId, content, dateModified, utcDateModified) VALUES (?, '', ?, ?)", [stubBlobId, now, now]);
sql.execute("UPDATE notes SET blobId = ? WHERE noteId = ?", [stubBlobId, noteId]);
});
const maxIdBefore = maxEntityChangeId();
apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: PAST }), entity: rowWith(noteId, { blobId: "su_remote_blob" }) }]);
expect(requeuedSince(maxIdBefore, "notes", noteId)).toBe(true);
expect(requeuedSince(maxIdBefore, "blobs", stubBlobId)).toBe(false);
});
it("lets a remote change win against a local change dated in the future", () => {
const noteId = createNote();
const clockAhead = dateUtils.utcDateTimeStr(new Date(Date.now() + 3600 * 1000));
cls.init(() => getSql().execute("UPDATE entity_changes SET utcDateChanged = ? WHERE entityName = 'notes' AND entityId = ?", [clockAhead, noteId]));
apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: dateUtils.utcNowDateTime() }), entity: rowWith(noteId, { title: "remote-edit" }) }]);
expect(getSql().getValue("SELECT title FROM notes WHERE noteId = ?", [noteId])).toBe("remote-edit");
});
it("stores a remote change dated in the future at the current time", () => {
const noteId = createNote();
apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: FUTURE }), entity: rowWith(noteId, { title: "remote-edit" }) }]);
expect(getSql().getValue("SELECT title FROM notes WHERE noteId = ?", [noteId])).toBe("remote-edit");
expect(localDateChanged("notes", noteId) <= dateUtils.utcNowDateTime()).toBe(true);
});
it("is a no-op when local and remote are identical but remote is older", () => {
const noteId = createNote();
const sameHash = localHash("notes", noteId);
// hash equal + isErased equal (stored as integer 0) + remote older -> falls
// through to the final `return false` without re-queuing or applying anything.
apply([{ entityChange: buildEC({ entityId: noteId, hash: sameHash, isErased: 0 as unknown as boolean, utcDateChanged: PAST }), entity: noteRow(noteId) }]);
expect(getSql().getRowOrNull("SELECT 1 FROM notes WHERE noteId = ?", [noteId])).not.toBeNull();
});
it("applies a note reordering by updating branch positions", () => {
const parentNoteId = createNote("reorder-parent");
const childBranchId = cls.init(() =>
notes.createNewNote({ parentNoteId, title: "child", content: "<p/>", type: "text" }).branch.branchId
);
if (!childBranchId) throw new Error("child branch missing");
apply([{ entityChange: buildEC({ entityName: "note_reordering", entityId: parentNoteId }), entity: { [childBranchId]: 77 } as unknown as EntityRow }]);
expect(getSql().getValue("SELECT notePosition FROM branches WHERE branchId = ?", [childBranchId])).toBe(77);
});
it("decodes base64 blob content (including the empty-buffer special case)", () => {
apply([
{ entityChange: buildEC({ entityName: "blobs", entityId: "su_blob_full" }), entity: blobRow("su_blob_full", encodeBase64("hello")) },
{ entityChange: buildEC({ entityName: "blobs", entityId: "su_blob_empty" }), entity: blobRow("su_blob_empty", "") }
]);
const full = getSql().getValue<Buffer | Uint8Array>("SELECT content FROM blobs WHERE blobId = ?", ["su_blob_full"]);
expect(Buffer.from(full ?? []).toString()).toBe("hello");
const empty = getSql().getValue("SELECT content FROM blobs WHERE blobId = ?", ["su_blob_empty"]);
expect(empty === "" || (empty && Buffer.from(empty as Uint8Array).length === 0)).toBeTruthy();
});
it("stores distinct content for sequential blobs of different sizes (decode pool reuse must not cross-contaminate)", () => {
// The decode pool reuses one scratch buffer across blobs on platforms with an in-place
// decoder. A longer blob followed by a shorter one exercises the case where stale bytes
// from the previous decode sit beyond the new content's end in the same buffer.
const longContent = "long-blob-content-".repeat(50);
const shortContent = "tiny";
apply([
{ entityChange: buildEC({ entityName: "blobs", entityId: "su_blob_pool_a" }), entity: blobRow("su_blob_pool_a", encodeBase64(longContent)) },
{ entityChange: buildEC({ entityName: "blobs", entityId: "su_blob_pool_b" }), entity: blobRow("su_blob_pool_b", encodeBase64(shortContent)) }
]);
const a = getSql().getValue<Buffer | Uint8Array>("SELECT content FROM blobs WHERE blobId = ?", ["su_blob_pool_a"]);
const b = getSql().getValue<Buffer | Uint8Array>("SELECT content FROM blobs WHERE blobId = ?", ["su_blob_pool_b"]);
expect(Buffer.from(a ?? []).toString()).toBe(longContent);
expect(Buffer.from(b ?? []).toString()).toBe(shortContent);
});
it("ignores option changes that have no entity row", () => {
// entityName 'options' + no row -> early return, no throw.
expect(() => apply([{ entityChange: buildEC({ entityName: "options", entityId: "su_missing_option" }), entity: undefined }])).not.toThrow();
});
it("logs but does not crash when asked to erase an unsupported entity type", () => {
// 'options' is not in the eraseable entity list.
expect(() => apply([{ entityChange: buildEC({ entityName: "options", entityId: "su_unknown_erase", isErased: true }), entity: { name: "su_unknown_erase", value: "v" } as unknown as EntityRow }])).not.toThrow();
});
it("throws on a normal change missing its entity row", () => {
expect(() => apply([{ entityChange: buildEC({ entityId: "su_no_row_note" }), entity: undefined }])).toThrow(/Empty entity row/);
});
it("throws on a note_reordering change missing its body", () => {
expect(() => apply([{ entityChange: buildEC({ entityName: "note_reordering", entityId: "su_no_reorder" }), entity: undefined }])).toThrow(/Empty note_reordering body/);
});
});
function localChangeId(entityName: string, entityId: string): string {
return getSql().getValue<string>("SELECT changeId FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]) ?? "";
}
function localDateChanged(entityName: string, entityId: string): string {
return getSql().getValue<string>("SELECT utcDateChanged FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]) ?? "";
}
function maxEntityChangeId(): number {
return getSql().getValue<number>("SELECT COALESCE(MAX(id), 0) FROM entity_changes") ?? 0;
}
/** Whether the entity's change was written after `id`, which is what puts it in the next push. */
function requeuedSince(id: number, entityName: string, entityId: string): boolean {
return !!getSql().getValue("SELECT 1 FROM entity_changes WHERE entityName = ? AND entityId = ? AND id > ?", [entityName, entityId, id]);
}
function blobRow(blobId: string, content: string): EntityRow {
const now = dateUtils.utcNowDateTime();
return { blobId, content, dateModified: now, utcDateModified: now } as unknown as EntityRow;
}