SPB Git

Repository Public

sync.test.ts 7.0 KB 108 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/sync.test.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Change feed for native clients (migration 0002): every write bumps the revision, opening a note does not, permanent deletions leave a45 * tombstone, a client-chosen id is created once. Runs against a *_dev database; everything it creates is purged at the end.46 */47import "../helpers/testdb";48import { test, before, after } from "node:test";49import assert from "node:assert/strict";50import { eq, inArray, and } from "drizzle-orm";51import { randomUUID } from "node:crypto";52import { db, schema, sql } from "../../src/db/index";53import { createPage, updatePage, softDeletePage, restorePage, purgePage, touchOpened, findPageAnyState } from "../../src/lib/pages/service";54import { setPageTags } from "../../src/lib/inbox/service";55import { deviceLabel } from "../../src/lib/auth/tokens";5657let ws: string; const made: string[] = [];58const doc = (t: string) => ({ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: t }] }] });59const rev = async (id: string) => Number((await db.select({ rev: schema.pages.rev }).from(schema.pages).where(eq(schema.pages.id, id)))[0]?.rev ?? -1);60before(async () => { const [w] = await db.select().from(schema.workspaces).limit(1); assert.ok(w, "workspace missing"); ws = w.id; });61after(async () => {62  if (made.length) await db.delete(schema.pages).where(inArray(schema.pages.id, made));63  await db.delete(schema.tags).where(and(eq(schema.tags.workspaceId, ws), inArray(schema.tags.name, ["sync-a", "sync-b"])));64  if (made.length) await db.delete(schema.syncTombstones).where(inArray(schema.syncTombstones.id, made));65  await sql.end();66});6768test("every kind of write moves the revision forward; opening a note does not", async () => {69  const p = await createPage(ws, { title: "sync · rev", content: doc("a") }); made.push(p.id);70  const r0 = await rev(p.id); assert.ok(r0 > 0);71  await updatePage(ws, p.id, { content: doc("b") }, { baseVersion: p.version }); const r1 = await rev(p.id); assert.ok(r1 > r0, "content");72  await updatePage(ws, p.id, { pinned: true }); const r2 = await rev(p.id); assert.ok(r2 > r1, "metadata");73  await setPageTags(ws, p.id, ["sync-a", "sync-b"]); const r3 = await rev(p.id); assert.ok(r3 > r2, "tags added");74  await setPageTags(ws, p.id, ["sync-a"]); const r4 = await rev(p.id); assert.ok(r4 > r3, "tag removed");75  await softDeletePage(ws, p.id); const r5 = await rev(p.id); assert.ok(r5 > r4, "trash");76  await restorePage(ws, p.id); const r6 = await rev(p.id); assert.ok(r6 > r5, "restore");77  await touchOpened(p.id); assert.equal(await rev(p.id), r6, "opening is not a change");78  await db.update(schema.pages).set({ title: "sync · rev" }).where(eq(schema.pages.id, p.id)); assert.equal(await rev(p.id), r6, "a write that changes nothing is not a change");79});80test("a permanent deletion leaves a tombstone with a newer revision; children go with it", async () => {81  const parent = await createPage(ws, { title: "sync · parent" }); const child = await createPage(ws, { title: "sync · child", parentId: parent.id }); made.push(parent.id, child.id);82  const before = Math.max(await rev(parent.id), await rev(child.id));83  await softDeletePage(ws, parent.id); await purgePage(ws, parent.id);84  const t = await db.select().from(schema.syncTombstones).where(inArray(schema.syncTombstones.id, [parent.id, child.id]));85  assert.equal(t.filter((x) => x.kind === "page" && x.workspaceId === ws).length, 2, "3.0: the sub-page trashed by the same action is purged with its parent (2.x left it orphaned in the trash); see next-defects.test.ts for the children that are kept");86  assert.ok(t.every((x) => Number(x.rev) > before));87});88test("a client-chosen id creates the page once and never a second time", async () => {89  const id = randomUUID(); made.push(id);90  assert.equal(await findPageAnyState(ws, id), null);91  const p = await createPage(ws, { id, title: "sync · offline note", content: doc("written offline") }); assert.equal(p.id, id);92  const again = await findPageAnyState(ws, id); assert.equal(again?.id, id); assert.equal(again?.title, "sync · offline note");93  await softDeletePage(ws, id); assert.ok((await findPageAnyState(ws, id))?.deletedAt, "found even in the trash");94  await purgePage(ws, id); assert.equal((await db.select().from(schema.syncTombstones).where(eq(schema.syncTombstones.id, id))).length, 1);95  const back = await createPage(ws, { id, title: "sync · offline note (again)" }); assert.equal(back.id, id);96  assert.equal((await db.select().from(schema.syncTombstones).where(eq(schema.syncTombstones.id, id))).length, 0, "re-creating an id clears its tombstone");97});98test("revisions are strictly increasing across concurrent writers", async () => {99  const ps = await Promise.all(Array.from({ length: 12 }, (_, i) => createPage(ws, { title: `sync · burst ${i}` }))); made.push(...ps.map((p) => p.id));100  await Promise.all(ps.map((p, i) => updatePage(ws, p.id, { content: doc(`burst ${i}`) })));101  const revs = await Promise.all(ps.map((p) => rev(p.id))); assert.equal(new Set(revs).size, revs.length);102});103test("native clients get a readable device label", () => {104  assert.equal(deviceLabel("SPNotes/1.0.0 (Macintosh; Mac OS X 27.0) native"), "SP Notes · Mac");105  assert.equal(deviceLabel("SPNotes/1.0 (iPhone; iOS 27.0) native"), "SP Notes · iPhone");106  assert.equal(deviceLabel("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/27.0 Safari/605.1.15"), "Safari · Mac");107});108
Files main
Menu

Theme