Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
test(sync): add SyncService orchestration tests
Alcahest1c7431bfe1a8
diff --git a/test/helpers/syncServiceFolders.test.js b/test/helpers/syncServiceFolders.test.jsnew file mode 100644index 000000000..f1306921c--- /dev/null+++ b/test/helpers/syncServiceFolders.test.js@@ -0,0 +1,485 @@+// Folder half of the SyncService orchestration: real SyncService over the real+// cloudApi/FoldersService stack, a real DatabaseManager, and the fake cloud.+// Assertions are on the wire (cloud.log) as well as on the resulting rows.++const test = require("node:test");+const assert = require("node:assert/strict");++const { createDb } = require("./harness/db.js");+const {+ installBrowserGlobals,+ resetBrowserGlobals,+ enableSync,+ establishValidatedAuth,+ stopSyncTimers,+ window: browserWindow,+ localStorage: browserLocalStorage,+} = require("./harness/browserGlobals.js");+const { createElectronApi } = require("./harness/electronApiBridge.js");+const { createFakeCloud } = require("./harness/fakeCloud.js");++installBrowserGlobals();++const loadSync = () => import("../../src/services/SyncService.ts");++async function setup(t, cloudConfig) {+ resetBrowserGlobals();+ const db = createDb(t);+ if (!db) return null;+ enableSync();+ const cloud = createFakeCloud(cloudConfig);+ browserWindow.electronAPI = createElectronApi(db, { cloud });+ await establishValidatedAuth();+ return { db, cloud };+}++async function newSyncService(t) {+ const { SyncService } = await loadSync();+ const service = new SyncService();+ if (t) t.after(() => stopSyncTimers(service));+ return service;+}++function folderByName(db, name) {+ return db.db.prepare("SELECT * FROM folders WHERE name = ?").get(name) ?? null;+}++function folderByClientId(db, clientFolderId) {+ return db.db.prepare("SELECT * FROM folders WHERE client_folder_id = ?").get(clientFolderId);+}++function allFolders(db) {+ return db.db.prepare("SELECT * FROM folders ORDER BY id").all();+}++function noteByClientId(db, clientNoteId) {+ return db.db.prepare("SELECT * FROM notes WHERE client_note_id = ?").get(clientNoteId);+}++// Deterministic timestamps come from SQL, never from a faked clock: SQLite's+// CURRENT_TIMESTAMP is second-granularity and the gate under test is textual.+function setFolderRow(db, id, columns) {+ const keys = Object.keys(columns);+ db.db+ .prepare(`UPDATE folders SET ${keys.map((k) => `${k} = ?`).join(", ")} WHERE id = ?`)+ .run(...keys.map((k) => columns[k]), id);+}++function shift(iso, hours) {+ return new Date(Date.parse(iso) + hours * 3600_000).toISOString();+}++test("a note in a folder with no cloud mapping pushes folder_id undefined, not the local row id", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ // Folder creation fails, so nothing local ever gets a cloud_id.+ cloud.failWith("POST /api/folders/batch-create", { status: 500, error: "folder push down" });+ const work = db.createFolder("Work").folder;+ const saved = db.saveNote("Spec", "body", "personal", null, null, work.id);++ await (await newSyncService(t)).syncAll(true);++ const [batch] = cloud.logFor("/api/notes/batch-create");+ assert.ok(batch, "the note must still be pushed even though the folder push failed");+ const sent = batch.body.notes[0];+ assert.equal(sent.client_note_id, saved.note.client_note_id);+ assert.equal(+ sent.folder_id,+ null,+ "an unmapped folder must null the reference, never leak the local integer id"+ );+ assert.notEqual(sent.folder_id, work.id);+ assert.equal(cloud.notes()[0].folder_id, null);+ assert.equal(db.getNote(saved.note.id).sync_status, "synced");+});++test("pulled notes map cloud folder ids back to local ids and park the row when the folder is unknown", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;+ const warnings = t.mock.method(console, "warn", () => {});++ const work = cloud.seedFolder({+ client_folder_id: "cf-work",+ name: "Work",+ is_default: false,+ sort_order: 7,+ });+ cloud.seedNote({ client_note_id: "cn-mapped", title: "In Work", folder_id: work.id });+ cloud.seedNote({ client_note_id: "cn-orphan", title: "Nowhere", folder_id: "folder-9999" });++ await (await newSyncService(t)).syncAll(true);++ const localWork = folderByName(db, "Work");+ const personal = folderByName(db, "Personal");+ assert.ok(localWork, "the cloud-only folder must materialize locally");+ assert.notEqual(localWork.id, personal.id);++ assert.equal(+ noteByClientId(db, "cn-mapped").folder_id,+ localWork.id,+ "a known cloud folder must resolve through the cloud-to-local map"+ );+ // Filing the orphan anywhere would stick forever (the advanced cursor never+ // re-pulls the row), so the pull parks it until its folder arrives.+ assert.equal(+ noteByClientId(db, "cn-orphan"),+ undefined,+ "an unknown cloud folder must park the row, not file it locally"+ );+ assert.equal(+ browserLocalStorage.getItem("lastSyncedAt.notes"),+ null,+ "a parked pull must leave the note cursor unstamped so the row is re-seen"+ );+ assert.ok(+ warnings.mock.calls.some((c) => String(c.arguments[0]).includes("Parking note")),+ "the parked row must be named in the warnings"+ );+});++test("a fresh device adopts the cloud's default folders by name instead of pushing duplicates", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ const cloudPersonal = cloud.seedFolder({+ client_folder_id: "cf-personal",+ name: "Personal",+ is_default: true,+ sort_order: 0,+ });+ const cloudMeetings = cloud.seedFolder({+ client_folder_id: "cf-meetings",+ name: "Meetings",+ is_default: true,+ sort_order: 1,+ });++ await (await newSyncService(t)).syncAll(true);++ const [batch] = cloud.logFor("/api/folders/batch-create");+ assert.deepEqual(+ batch.body.folders.map((f) => f.name),+ ["Videos"],+ "adopted defaults must be gone from the push, or the cloud forks a second Personal"+ );++ assert.equal(cloud.folders().length, 3);+ assert.equal(cloud.folders().filter((f) => f.name === "Personal").length, 1);+ assert.equal(allFolders(db).length, 3, "adoption must not add a local row either");++ const personal = folderByName(db, "Personal");+ assert.equal(personal.client_folder_id, "cf-personal");+ assert.equal(personal.cloud_id, cloudPersonal.id);+ assert.equal(personal.sync_status, "synced");+ assert.equal(folderByName(db, "Meetings").cloud_id, cloudMeetings.id);+});++test("pushPendingFolders sends name/client_folder_id/is_default/sort_order in row order and links each folder on ack", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ await (await newSyncService(t)).syncAll(true);++ const calls = cloud.logFor("/api/folders/batch-create");+ assert.equal(calls.length, 1);+ assert.equal(calls[0].method, "POST");++ const sent = calls[0].body.folders;+ assert.deepEqual(+ sent.map((f) => f.name),+ ["Meetings", "Personal", "Videos"],+ "input order is what pairs created[i] with fresh[i] during identity adoption"+ );+ for (const folder of sent) {+ assert.deepEqual(Object.keys(folder).sort(), [+ "client_folder_id",+ "is_default",+ "name",+ "sort_order",+ ]);+ assert.equal(folder.is_default, true, "SQLite's 1 must be coerced to a boolean on the wire");+ assert.equal(typeof folder.sort_order, "number");+ assert.equal(typeof folder.client_folder_id, "string");+ }+ assert.deepEqual(+ sent.map((f) => f.sort_order),+ [1, 0, 2]+ );++ for (const local of allFolders(db)) {+ assert.equal(local.sync_status, "synced");+ const remote = cloud.folder(local.client_folder_id);+ assert.ok(remote, `cloud row missing for ${local.name}`);+ assert.equal(local.cloud_id, remote.id, "the ack's cloud id must be stored on the local row");+ }+});++test("a renamed linked folder PATCHes /api/folders/update instead of creating a second cloud folder", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ const work = db.createFolder("Work").folder;+ await (await newSyncService(t)).syncAll(true);+ const cloudId = folderByName(db, "Work").cloud_id;+ assert.ok(cloudId);+ const folderCountAfterFirstPass = cloud.folders().length;++ db.renameFolder(work.id, "Work Renamed");+ // Future-dated so the pull in this same pass cannot stand in for the push ack.+ setFolderRow(db, work.id, { updated_at: "2027-01-01 00:00:00" });+ await (await newSyncService(t)).syncAll(true);++ const patches = cloud.logFor("/api/folders/update");+ assert.equal(patches.length, 1);+ assert.equal(patches[0].method, "PATCH");+ assert.deepEqual(patches[0].body, { id: cloudId, name: "Work Renamed", sort_order: 3 });++ assert.equal(cloud.folder(cloudId).name, "Work Renamed");+ assert.equal(+ cloud.folders().length,+ folderCountAfterFirstPass,+ "a linked folder must never be re-created as a new cloud row"+ );+ assert.equal(folderByName(db, "Work Renamed").sync_status, "synced");+ assert.equal(cloud.logFor("/api/folders/batch-create").length, 1, "no second batch-create");+});++test("a failed folder PATCH leaves that folder pending and does not abort the rest of the loop", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ const alpha = db.createFolder("Alpha").folder;+ const beta = db.createFolder("Beta").folder;+ await (await newSyncService(t)).syncAll(true);+ const alphaCloudId = folderByName(db, "Alpha").cloud_id;+ const betaCloudId = folderByName(db, "Beta").cloud_id;++ db.renameFolder(alpha.id, "Alpha Renamed");+ db.renameFolder(beta.id, "Beta Renamed");+ // Future-dated so the pull in this same pass cannot mask the push outcome.+ setFolderRow(db, alpha.id, { updated_at: "2027-01-01 00:00:00" });+ setFolderRow(db, beta.id, { updated_at: "2027-01-01 00:00:00" });++ cloud.failNext("PATCH /api/folders/update", { status: 500, error: "boom" });+ await (await newSyncService(t)).syncAll(true);++ const patches = cloud.logFor("/api/folders/update");+ assert.equal(+ patches.length,+ 2,+ "the second folder must still be attempted after the first threw"+ );+ assert.deepEqual(patches[0].body, { id: alphaCloudId, name: "Alpha Renamed", sort_order: 3 });+ assert.deepEqual(patches[1].body, { id: betaCloudId, name: "Beta Renamed", sort_order: 4 });++ assert.equal(+ folderByName(db, "Alpha Renamed").sync_status,+ "pending",+ "a folder whose push failed must stay pending so the next pass retries it"+ );+ assert.equal(folderByName(db, "Beta Renamed").sync_status, "synced");+ assert.equal(cloud.folder(alphaCloudId).name, "Alpha");+ assert.equal(cloud.folder(betaCloudId).name, "Beta Renamed");+});++test("pushFolderDeletes sends the cloud id, hard-deletes on ack, and keeps the tombstone on failure", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ const work = db.createFolder("Trash Me").folder;+ await (await newSyncService(t)).syncAll(true);+ const cloudId = folderByName(db, "Trash Me").cloud_id;++ db.deleteFolder(work.id);+ cloud.failNext("DELETE /api/folders/delete", { status: 503, error: "offline" });+ await (await newSyncService(t)).syncAll(true);++ const firstAttempt = cloud.logFor("/api/folders/delete");+ assert.equal(firstAttempt.length, 1);+ assert.equal(firstAttempt[0].method, "DELETE");+ assert.deepEqual(firstAttempt[0].body, { id: cloudId });++ const tombstone = db.db.prepare("SELECT * FROM folders WHERE id = ?").get(work.id);+ assert.ok(tombstone, "a failed delete must keep the local tombstone for the next pass");+ assert.ok(tombstone.deleted_at);+ assert.equal(tombstone.sync_status, "pending");+ assert.equal(cloud.folder(cloudId).deleted_at, null);++ await (await newSyncService(t)).syncAll(true);++ assert.equal(cloud.logFor("/api/folders/delete").length, 2, "the delete must be retried");+ assert.equal(+ db.db.prepare("SELECT * FROM folders WHERE id = ?").get(work.id),+ undefined,+ "an acked delete must drop the local row"+ );+ assert.ok(cloud.folder(cloudId).deleted_at, "the cloud row must be tombstoned");+});++test("pullFolders keeps a local folder whose same-day cloud copy is older and accepts a genuinely newer one", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ // "2026-07-27T11:00:00.000Z" > "2026-07-27 12:00:00" under a raw string+ // compare ('T' > ' '), so an unnormalized gate hands this to the stale cloud.+ const stale = cloud.seedFolder({+ client_folder_id: "cf-stale",+ name: "Stale Cloud Name",+ is_default: false,+ sort_order: 8,+ created_at: "2026-07-27T09:00:00.000Z",+ updated_at: "2026-07-27T11:00:00.000Z",+ });+ const fresher = cloud.seedFolder({+ client_folder_id: "cf-fresher",+ name: "Fresher Cloud Name",+ is_default: false,+ sort_order: 9,+ created_at: "2026-07-27T09:00:00.000Z",+ updated_at: "2026-07-27T13:00:00.000Z",+ });++ const keep = db.createFolder("Local Wins").folder;+ setFolderRow(db, keep.id, {+ client_folder_id: "cf-stale",+ cloud_id: stale.id,+ sync_status: "synced",+ updated_at: "2026-07-27 12:00:00",+ });+ const lose = db.createFolder("Local Loses").folder;+ setFolderRow(db, lose.id, {+ client_folder_id: "cf-fresher",+ cloud_id: fresher.id,+ sync_status: "synced",+ updated_at: "2026-07-27 12:00:00",+ });++ await (await newSyncService(t)).syncAll(true);++ assert.equal(+ folderByClientId(db, "cf-stale").name,+ "Local Wins",+ "an older cloud copy on the same UTC day must not overwrite the local folder"+ );+ assert.equal(+ folderByClientId(db, "cf-fresher").name,+ "Fresher Cloud Name",+ "a strictly newer cloud copy must still win, or the gate is just a no-op"+ );+});++test("a failed folder list leaves lastSyncedAt.folders unstamped so the next pass re-pulls from scratch", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { cloud } = ctx;++ cloud.failWith("/api/folders/list", { status: 500, error: "list down" });+ await (await newSyncService(t)).syncAll(true);++ assert.equal(+ browserLocalStorage.getItem("lastSyncedAt.folders"),+ null,+ "stamping a failed pull would silently skip every folder change in that window"+ );++ cloud.clearFailures();+ await (await newSyncService(t)).syncAll(true);++ const lists = cloud.logFor("/api/folders/list");+ assert.equal(+ lists[lists.length - 1].path,+ "/api/folders/list",+ "with no stamp the recovery pass must ask for the full snapshot"+ );+ assert.ok(browserLocalStorage.getItem("lastSyncedAt.folders"), "a clean pull must stamp");+});++test("the folders cursor advances and the next pass only receives folders changed since the stamp", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ await (await newSyncService(t)).syncAll(true);+ const stamp = browserLocalStorage.getItem("lastSyncedAt.folders");+ assert.match(stamp, /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/);++ cloud.seedFolder({+ client_folder_id: "cf-old",+ name: "Before The Stamp",+ is_default: false,+ sort_order: 20,+ created_at: shift(stamp, -2),+ updated_at: shift(stamp, -1),+ });+ cloud.seedFolder({+ client_folder_id: "cf-new",+ name: "After The Stamp",+ is_default: false,+ sort_order: 21,+ created_at: shift(stamp, -2),+ updated_at: shift(stamp, 1),+ });++ await (await newSyncService(t)).syncAll(true);++ const lists = cloud.logFor("/api/folders/list");+ assert.equal(+ lists[lists.length - 1].path,+ `/api/folders/list?since=${encodeURIComponent(stamp)}`,+ "the stored stamp must be sent as the since cursor"+ );+ assert.ok(folderByName(db, "After The Stamp"), "a folder newer than the cursor must arrive");+ assert.equal(+ folderByName(db, "Before The Stamp"),+ null,+ "re-delivering pre-cursor folders means the cursor was never sent"+ );+ assert.ok(+ Date.parse(browserLocalStorage.getItem("lastSyncedAt.folders")) >= Date.parse(stamp),+ "the cursor must advance"+ );+});++test("a folder tombstone arriving in the delta pull hard-deletes the local folder", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;++ const shared = db.createFolder("Shared").folder;+ await (await newSyncService(t)).syncAll(true);+ const cloudId = folderByName(db, "Shared").cloud_id;+ const stamp = browserLocalStorage.getItem("lastSyncedAt.folders");++ // Another device deletes the folder. The server clock puts the tombstone past+ // the cursor; wall-clock would land in the same millisecond as the stamp.+ cloud.setClock(() => shift(stamp, 1));+ const deleted = await cloud.request({+ method: "DELETE",+ path: "/api/folders/delete",+ body: { id: cloudId },+ });+ assert.equal(deleted.success, true);++ await (await newSyncService(t)).syncAll(true);++ const lists = cloud.logFor("/api/folders/list");+ assert.equal(+ lists[lists.length - 1].path,+ `/api/folders/list?since=${encodeURIComponent(stamp)}`,+ "only the delta list carries tombstones; a snapshot pull would never see this delete"+ );+ assert.equal(+ db.db.prepare("SELECT * FROM folders WHERE id = ?").get(shared.id),+ undefined,+ "a remote delete must remove the local folder"+ );+});diff --git a/test/helpers/syncServiceNotes.test.js b/test/helpers/syncServiceNotes.test.jsnew file mode 100644index 000000000..32c495e16--- /dev/null+++ b/test/helpers/syncServiceNotes.test.js@@ -0,0 +1,651 @@+// Orchestration coverage for the note half of SyncService: the real service+// drives the real cloudApi/NotesService layer over a real DatabaseManager, with+// only the HTTP boundary faked. Assertions land on what was *sent* whenever the+// behaviour under test is about the request, not just on the row that survived.++const test = require("node:test");+const assert = require("node:assert/strict");++const { createDb } = require("./harness/db.js");+const {+ installBrowserGlobals,+ resetBrowserGlobals,+ enableSync,+ establishValidatedAuth,+ stopSyncTimers,+ acquireLock,+ localStorage: localStorageStub,+ window: windowStub,+} = require("./harness/browserGlobals.js");+const { createElectronApi } = require("./harness/electronApiBridge.js");+const { createFakeCloud } = require("./harness/fakeCloud.js");+const { assertNoContentRegression, snapshotNotes } = require("./harness/invariants.js");+const { SyncService } = require("../../src/services/SyncService.ts");+const { NotesService } = require("../../src/services/NotesService.ts");+const { CloudApiError } = require("../../src/services/cloudApi.ts");++// Mirrors SYNC_ALL_LOCK in SyncService.ts.+const SYNC_ALL_LOCK = "openwhispr-sync-all";+const BATCH_SIZE = 50;++// SQLite's datetime() format, the one local rows actually carry.+const LOCAL_EARLY = "2026-07-20 10:00:00";+const LOCAL_LATE = "2026-07-20 11:00:00";++async function setup(t) {+ const db = createDb(t);+ if (!db) return null;+ resetBrowserGlobals();+ installBrowserGlobals();+ enableSync();+ const cloud = createFakeCloud();+ const api = createElectronApi(db, { cloud });+ windowStub.electronAPI = api;+ await establishValidatedAuth();+ const service = new SyncService();+ t.after(() => stopSyncTimers(service));+ return { db, cloud, api, service };+}++// Deterministic timestamps come from SQL, never from a fake clock.+function updateNote(db, id, fields) {+ const keys = Object.keys(fields);+ db.db+ .prepare(`UPDATE notes SET ${keys.map((k) => `${k} = ?`).join(", ")} WHERE id = ?`)+ .run(...keys.map((k) => fields[k]), id);+ return db.getNote(id);+}++function noteCount(db) {+ return db.db.prepare("SELECT COUNT(*) AS c FROM notes").get().c;+}++function queryParam(path, name) {+ const [, query] = path.split("?");+ return new URLSearchParams(query ?? "").get(name);+}++function silenceConsoleErrors(t) {+ return t.mock.method(console, "error", () => {});+}++function errorMessages(mock) {+ return mock.mock.calls.map((call) =>+ call.arguments.map((a) => String(a?.message ?? a)).join(" ")+ );+}++test("migration push sends the full note body, not just the client id", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;++ // The meeting shell exactly as meetingDetectionEngine.js creates it.+ const shell = db.saveNote("Weekly sync", "", "meeting").note;+ updateNote(db, shell.id, { created_at: LOCAL_EARLY, updated_at: LOCAL_EARLY });++ await service.syncAll(true);+ const cloudId = db.getNote(shell.id).cloud_id;+ assert.ok(cloudId, "first pass must give the shell a cloud id");+ assert.equal(cloud.note(cloudId).content, "", "the shell reaches the cloud empty");++ // Transcription lands afterwards and re-queues the row: this is the state the+ // migration branch has to upload.+ const filled = updateNote(db, shell.id, {+ content: "the transcribed meeting body",+ transcript: "raw diarized transcript",+ updated_at: LOCAL_LATE,+ sync_status: "pending",+ });++ const before = snapshotNotes(db);+ const mark = cloud.log.length;+ await service.syncAll(true);++ const patches = cloud.log+ .slice(mark)+ .filter((c) => c.method === "PATCH" && c.path === "/api/notes/update");+ assert.equal(patches.length, 1, "exactly one PATCH for the pending migration note");+ const body = patches[0].body;+ assert.equal(body.id, cloudId);+ assert.equal(body.client_note_id, filled.client_note_id);+ // A payload of { client_note_id } alone changes nothing server-side, so the+ // DB would still look fine; only the sent body exposes the regression.+ assert.equal(body.content, "the transcribed meeting body");+ assert.equal(body.transcript, "raw diarized transcript");+ assert.equal(body.title, "Weekly sync");+ assert.equal(body.updated_at, LOCAL_LATE);++ assert.equal(cloud.note(cloudId).content, "the transcribed meeting body");+ assert.equal(cloud.note(cloudId).transcript, "raw diarized transcript");+ assert.equal(db.getNote(shell.id).sync_status, "synced");+ assertNoContentRegression(before, snapshotNotes(db), cloud.notes());+});++test("migration push marks the note errored when the PATCH fails", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ silenceConsoleErrors(t);++ const shell = db.saveNote("Weekly sync", "", "meeting").note;+ updateNote(db, shell.id, { created_at: LOCAL_EARLY, updated_at: LOCAL_EARLY });+ await service.syncAll(true);+ const cloudId = db.getNote(shell.id).cloud_id;++ updateNote(db, shell.id, {+ content: "the transcribed meeting body",+ updated_at: LOCAL_LATE,+ sync_status: "pending",+ });++ cloud.failNext("PATCH /api/notes/update", { status: 500, error: "boom" });+ const before = snapshotNotes(db);+ await service.syncAll(true);++ const row = db.getNote(shell.id);+ assert.equal(row.sync_status, "error", "a failed push must leave the row retryable, not synced");+ assert.equal(row.cloud_id, cloudId, "the cloud id is not dropped by a failed push");+ assert.equal(row.content, "the transcribed meeting body", "the local body survives the failure");+ assert.equal(cloud.note(cloudId).content, "", "nothing reached the cloud");+ assertNoContentRegression(before, snapshotNotes(db), cloud.notes());+});++test("fresh notes are pushed in BATCH_SIZE chunks with acks matched by client id", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;++ for (let i = 0; i < BATCH_SIZE + 1; i += 1) {+ db.saveNote(`Note ${i}`, `body ${i}`, "personal");+ }++ // The cloud is free to answer in any order; matching acks positionally would+ // hand every note its neighbour's cloud id.+ const inner = windowStub.electronAPI.cloudApiRequest;+ windowStub.electronAPI.cloudApiRequest = async (opts) => {+ const result = await inner(opts);+ if (opts.path === "/api/notes/batch-create" && result.success) {+ return { ...result, data: { created: [...result.data.created].reverse() } };+ }+ return result;+ };++ await service.syncAll(true);++ const batches = cloud.logFor("/api/notes/batch-create");+ assert.equal(batches.length, 2, "51 fresh notes must go out as two chunks");+ assert.equal(batches[0].body.notes.length, BATCH_SIZE);+ assert.equal(batches[1].body.notes.length, 1);++ const rows = db.db.prepare("SELECT * FROM notes ORDER BY id").all();+ assert.equal(rows.length, BATCH_SIZE + 1);+ for (const row of rows) {+ assert.equal(row.sync_status, "synced");+ assert.equal(+ row.cloud_id,+ cloud.note(row.client_note_id).id,+ `note ${row.id} was acked with another note's cloud id`+ );+ }+});++test("a failing chunk marks only its own members as errored", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ silenceConsoleErrors(t);++ for (let i = 0; i < BATCH_SIZE + 1; i += 1) {+ db.saveNote(`Note ${i}`, `body ${i}`, "personal");+ }+ const pending = db.getPendingNotes();+ const firstChunk = pending.slice(0, BATCH_SIZE);+ const secondChunk = pending.slice(BATCH_SIZE);++ // Only the second chunk carries a single note, so this fires on it alone.+ cloud.failNext(+ (call) => call.path === "/api/notes/batch-create" && call.body?.notes?.length === 1,+ { status: 500, error: "chunk rejected" }+ );++ await service.syncAll(true);++ for (const note of firstChunk) {+ const row = db.getNote(note.id);+ assert.equal(row.sync_status, "synced", `note ${note.id} of the healthy chunk must be synced`);+ assert.ok(row.cloud_id);+ }+ for (const note of secondChunk) {+ const row = db.getNote(note.id);+ assert.equal(row.sync_status, "error", `note ${note.id} of the failed chunk must be errored`);+ assert.equal(row.cloud_id, null);+ }+ assert.equal(cloud.notes().length, BATCH_SIZE, "only the healthy chunk reached the cloud");+});++test("pullNotes applies a strictly newer cloud copy and rejects older and tied ones", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;++ // All three cloud copies sit on the same UTC day as the local rows, where a+ // raw string compare wrongly elects the cloud value ('T' > ' ' at index 10).+ const cases = [+ { key: "older", cloudUpdatedAt: "2026-07-20T09:00:00.000Z", applied: false },+ { key: "newer", cloudUpdatedAt: "2026-07-20T11:00:00.000Z", applied: true },+ { key: "tied", cloudUpdatedAt: "2026-07-20T10:00:00.000Z", applied: false },+ ];++ for (const c of cases) {+ const cloudRow = cloud.seedNote({+ client_note_id: `client-${c.key}`,+ title: `Cloud ${c.key}`,+ content: `cloud body ${c.key}`,+ created_at: "2026-07-20T08:00:00.000Z",+ updated_at: c.cloudUpdatedAt,+ });+ const local = db.saveNote(`Local ${c.key}`, `local body ${c.key}`, "personal").note;+ updateNote(db, local.id, {+ client_note_id: cloudRow.client_note_id,+ cloud_id: cloudRow.id,+ sync_status: "synced",+ created_at: LOCAL_EARLY,+ updated_at: LOCAL_EARLY,+ });+ c.localId = local.id;+ }++ const before = snapshotNotes(db);+ await service.syncAll(true);++ for (const c of cases) {+ const row = db.getNote(c.localId);+ if (c.applied) {+ assert.equal(row.content, `cloud body ${c.key}`, "a strictly newer cloud copy must win");+ assert.equal(row.title, `Cloud ${c.key}`);+ } else {+ assert.equal(+ row.content,+ `local body ${c.key}`,+ `the ${c.key} cloud copy must not overwrite the local row`+ );+ assert.equal(row.title, `Local ${c.key}`);+ assert.equal(row.updated_at, LOCAL_EARLY, "a rejected pull must not move updated_at");+ }+ }+ assertNoContentRegression(before, snapshotNotes(db), cloud.notes());+});++test("a cloud tombstone hard-deletes the local note", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;++ const cloudRow = cloud.seedNote({+ client_note_id: "client-tombstoned",+ title: "Deleted elsewhere",+ content: "gone",+ created_at: "2026-07-20T08:00:00.000Z",+ updated_at: "2026-07-20T08:00:00.000Z",+ });+ const local = db.saveNote("Deleted elsewhere", "gone", "personal").note;+ updateNote(db, local.id, {+ client_note_id: cloudRow.client_note_id,+ cloud_id: cloudRow.id,+ sync_status: "synced",+ created_at: LOCAL_EARLY,+ updated_at: LOCAL_EARLY,+ });++ // Tombstones only travel on the delta page, so the cursor must already exist.+ localStorageStub.setItem("lastSyncedAt.notes", "2026-07-01T00:00:00.000Z");+ await cloud.request({ method: "DELETE", path: "/api/notes/delete", body: { id: cloudRow.id } });+ const mark = cloud.log.length;++ await service.syncAll(true);++ const listCalls = cloud.log.slice(mark).filter((c) => c.path.split("?")[0] === "/api/notes/list");+ assert.ok(listCalls.length > 0);+ assert.equal(+ queryParam(listCalls[0].path, "since"),+ "2026-07-01T00:00:00.000Z",+ "the delta page must be requested with the stored cursor"+ );+ assert.equal(db.getNoteByClientId("client-tombstoned"), null, "the local row must be removed");+ assert.equal(noteCount(db), 0);+});++test("a pending local delete issues the DELETE and then hard-deletes locally", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;++ const cloudRow = cloud.seedNote({+ client_note_id: "client-locally-deleted",+ title: "Removed here",+ content: "body",+ created_at: "2026-07-19T09:00:00.000Z",+ updated_at: "2026-07-19T09:00:00.000Z",+ });+ const local = db.saveNote("Removed here", "body", "personal").note;+ updateNote(db, local.id, {+ client_note_id: cloudRow.client_note_id,+ cloud_id: cloudRow.id,+ sync_status: "pending",+ deleted_at: LOCAL_LATE,+ created_at: LOCAL_EARLY,+ updated_at: LOCAL_EARLY,+ });++ await service.syncAll(true);++ const deletes = cloud.logFor("/api/notes/delete");+ assert.equal(deletes.length, 1, "the tombstone must reach the cloud exactly once");+ assert.equal(deletes[0].method, "DELETE");+ assert.deepEqual(deletes[0].body, { id: cloudRow.id });+ assert.equal(db.getNote(local.id), null, "the local row is only removed after the cloud ack");+ assert.ok(cloud.note(cloudRow.id).deleted_at, "the cloud row is tombstoned");+});++test("a 500 on the delete leaves the tombstone pending and the local row intact", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ silenceConsoleErrors(t);++ const cloudRow = cloud.seedNote({+ client_note_id: "client-delete-failed",+ title: "Removed here",+ content: "body",+ created_at: "2026-07-19T09:00:00.000Z",+ updated_at: "2026-07-19T09:00:00.000Z",+ });+ const local = db.saveNote("Removed here", "body", "personal").note;+ updateNote(db, local.id, {+ client_note_id: cloudRow.client_note_id,+ cloud_id: cloudRow.id,+ sync_status: "pending",+ deleted_at: LOCAL_LATE,+ created_at: LOCAL_EARLY,+ updated_at: LOCAL_EARLY,+ });++ cloud.failNext("DELETE /api/notes/delete", { status: 500, error: "delete failed" });+ await service.syncAll(true);++ const row = db.getNote(local.id);+ assert.ok(row, "a failed delete must not remove the row locally");+ assert.equal(row.deleted_at, LOCAL_LATE);+ assert.equal(row.sync_status, "pending", "the tombstone stays queued for the next pass");+ assert.equal(db.getPendingNoteDeletes().length, 1);+ assert.equal(cloud.note(cloudRow.id).deleted_at, null, "the cloud row is untouched");+});++test("the pull pages 120 cloud notes with a correct cursor and stamps the cursor once", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;++ const base = Date.parse("2026-07-20T08:00:00.000Z");+ const seeded = [];+ for (let i = 0; i < 120; i += 1) {+ const at = new Date(base + i * 60000).toISOString();+ seeded.push(+ cloud.seedNote({+ client_note_id: `client-${String(i).padStart(3, "0")}`,+ title: `Cloud note ${i}`,+ content: `cloud body ${i}`,+ created_at: at,+ updated_at: at,+ })+ );+ }++ await service.syncAll(true);++ const listCalls = cloud.logFor("/api/notes/list");+ assert.equal(listCalls.length, 3, "120 notes at a page size of 50 must take three requests");+ assert.equal(queryParam(listCalls[0].path, "limit"), String(BATCH_SIZE));+ assert.equal(queryParam(listCalls[0].path, "before"), null, "the first page carries no cursor");+ // Pages walk created_at backwards, so each cursor is the oldest row of the+ // page before it.+ assert.equal(queryParam(listCalls[1].path, "before"), seeded[70].created_at);+ assert.equal(queryParam(listCalls[2].path, "before"), seeded[20].created_at);++ assert.equal(noteCount(db), 120, "every page must be applied exactly once");+ assert.equal(db.getNoteByClientId("client-000").content, "cloud body 0");+ assert.equal(db.getNoteByClientId("client-119").content, "cloud body 119");+ assert.ok(+ Date.parse(localStorageStub.getItem("lastSyncedAt.notes")) > 0,+ "a clean pull stamps the cursor"+ );+});++test("a mid-pagination failure leaves the pull cursor unstamped", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ silenceConsoleErrors(t);++ const base = Date.parse("2026-07-20T08:00:00.000Z");+ for (let i = 0; i < 120; i += 1) {+ const at = new Date(base + i * 60000).toISOString();+ cloud.seedNote({+ client_note_id: `client-${String(i).padStart(3, "0")}`,+ title: `Cloud note ${i}`,+ content: `cloud body ${i}`,+ created_at: at,+ updated_at: at,+ });+ }++ // Only pages after the first carry a cursor.+ cloud.failNext(+ (call) => call.path.startsWith("/api/notes/list") && call.path.includes("before="),+ { status: 500, error: "page 2 exploded" }+ );++ await service.syncAll(true);++ assert.equal(cloud.logFor("/api/notes/list").length, 2, "paging stops at the failure");+ assert.equal(noteCount(db), 50, "the first page is still applied");+ assert.equal(+ localStorageStub.getItem("lastSyncedAt.notes"),+ null,+ "a partial pull must not advance the cursor, or the missing pages are lost forever"+ );+});++test("canSync gating: sign-in and subscription block every call, backup off blocks personal pushes", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud } = ctx;+ const note = db.saveNote("Pending note", "body", "personal").note;++ for (const key of ["isSignedIn", "isSubscribed"]) {+ enableSync();+ localStorageStub.removeItem(key);+ const service = new SyncService();+ await service.syncAll(true);+ stopSyncTimers(service);+ assert.equal(cloud.log.length, 0, `sync ran with ${key} unset`);+ assert.equal(localStorageStub.getItem("lastSyncedAt"), null);+ }++ // Backup off is not sign-out: team-space membership still syncs, but nothing+ // personal may leave the machine.+ enableSync();+ localStorageStub.removeItem("cloudBackupEnabled");+ const teamOnlyService = new SyncService();+ await teamOnlyService.syncAll(true);+ stopSyncTimers(teamOnlyService);+ assert.equal(+ cloud.logFor("/api/notes/batch-create").length,+ 0,+ "a personal note must not push while backup is off"+ );+ assert.equal(db.getNote(note.id).sync_status, "pending");++ enableSync();+ const mark = cloud.log.length;+ const fullService = new SyncService();+ await fullService.syncAll(true);+ stopSyncTimers(fullService);+ assert.ok(cloud.log.length > mark, "with all three keys set the same pass must do real work");+ assert.equal(+ db.getNote(note.id).sync_status,+ "synced",+ "the personal note pushes once backup is back on"+ );+});++test("an ambient pass bows out when another window holds the lock, a manual pass waits", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ db.saveNote("Pending note", "body", "personal");++ const release = await acquireLock(SYNC_ALL_LOCK);++ // Racing a timer, because an ambient pass that queues instead of bowing out+ // never returns at all while the holder keeps the lock.+ const ambient = service.syncAll(false);+ const outcome = await Promise.race([+ ambient.then(() => "returned"),+ new Promise((resolve) => setTimeout(() => resolve("queued"), 50)),+ ]);+ assert.equal(outcome, "returned", "an ambient pass must bow out, not queue behind the holder");+ assert.equal(cloud.log.length, 0, "an ambient pass must skip while the lock is held");+ assert.equal(localStorageStub.getItem("lastSyncedAt"), null);++ // A manual pass queues instead of skipping.+ const manual = service.syncAll(true);+ await new Promise((resolve) => setImmediate(resolve));+ assert.equal(cloud.log.length, 0, "the manual pass must wait, not run alongside the holder");++ release();+ await manual;+ assert.ok(cloud.log.length > 0, "the manual pass runs once the lock frees");+ assert.ok(localStorageStub.getItem("lastSyncedAt"));+});++test("a request arriving mid-pass re-runs syncAll exactly once", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { cloud, service } = ctx;++ const first = service.syncAll(true);+ // syncing is already true here, so this request must be deferred, not dropped.+ await service.syncAll();+ await first;++ assert.equal(+ cloud.logFor("/api/notes/list").length,+ 2,+ "the deferred request must trigger exactly one extra pass"+ );+});++test("a note pull failure is contained: the pass finishes and later stages still run", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ const errors = silenceConsoleErrors(t);+ const note = db.saveNote("Pending note", "body", "personal").note;++ cloud.failWith("/api/notes/list", { status: 500, error: "list exploded" });+ await service.syncAll(true);++ assert.equal(db.getNote(note.id).sync_status, "synced", "the push before the pull still lands");+ assert.equal(cloud.logFor("/api/transcriptions/list").length, 1, "later stages still run");+ assert.equal(cloud.logFor("/api/snippets/list").length, 1);+ assert.equal(+ localStorageStub.getItem("lastSyncedAt.notes"),+ null,+ "a failed pull must not advance the note cursor"+ );+ assert.ok(localStorageStub.getItem("lastSyncedAt"), "the pass as a whole still completes");+ assert.ok(errorMessages(errors).some((m) => m.includes("Note pull failed")));+});++test("a missing dictionary IPC binding aborts the pass without stamping lastSyncedAt", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ const errors = silenceConsoleErrors(t);+ const note = db.saveNote("Pending note", "body", "personal").note;++ delete windowStub.electronAPI.markDictionarySynced;+ await service.syncAll(true);++ assert.equal(db.getNote(note.id).sync_status, "synced", "stages before the abort still ran");+ assert.equal(cloud.logFor("/api/snippets/list").length, 0, "the pass stops at the bad binding");+ assert.equal(+ localStorageStub.getItem("lastSyncedAt"),+ null,+ "an aborted pass must not claim to have completed"+ );+ assert.ok(+ errorMessages(errors).some((m) => m.includes("markDictionarySynced")),+ "the missing binding must be named in the failure"+ );+});++test("an AUTH_EXPIRED envelope surfaces as a 401 CloudApiError and nothing is marked synced", async (t) => {+ const ctx = await setup(t);+ if (!ctx) return;+ const { db, cloud, service } = ctx;+ silenceConsoleErrors(t);++ const cloudRow = cloud.seedNote({+ client_note_id: "client-queued-delete",+ title: "Removed here",+ content: "body",+ created_at: "2026-07-19T09:00:00.000Z",+ updated_at: "2026-07-19T09:00:00.000Z",+ });+ const fresh = db.saveNote("Fresh note", "fresh body", "personal").note;+ updateNote(db, fresh.id, { created_at: LOCAL_EARLY, updated_at: LOCAL_EARLY });+ const queuedDelete = db.saveNote("Removed here", "body", "personal").note;+ updateNote(db, queuedDelete.id, {+ client_note_id: cloudRow.client_note_id,+ cloud_id: cloudRow.id,+ sync_status: "pending",+ deleted_at: LOCAL_LATE,+ created_at: LOCAL_EARLY,+ updated_at: LOCAL_EARLY,+ });++ cloud.failWith(() => true, {+ status: 401,+ code: "AUTH_EXPIRED",+ error: "Session expired",+ });++ await assert.rejects(+ () => NotesService.list(BATCH_SIZE),+ (err) => {+ assert.ok(err instanceof CloudApiError);+ assert.equal(err.status, 401);+ assert.equal(err.code, "AUTH_EXPIRED");+ assert.equal(err.message, "Session expired");+ return true;+ }+ );++ const before = snapshotNotes(db);+ await service.syncAll(true);++ const freshRow = db.getNote(fresh.id);+ assert.equal(freshRow.cloud_id, null, "an expired session must not hand out a cloud id");+ assert.notEqual(freshRow.sync_status, "synced");+ assert.equal(freshRow.content, "fresh body");++ const deleteRow = db.getNote(queuedDelete.id);+ assert.ok(deleteRow, "the queued delete must survive an expired session");+ assert.equal(deleteRow.sync_status, "pending");+ assert.equal(cloud.notes().length, 1, "nothing new reached the cloud");+ assert.equal(cloud.note(cloudRow.id).deleted_at, null);+ assertNoContentRegression(before, snapshotNotes(db), cloud.notes());+});