From d8563ffa8fa553326238ad6b30e3eb4fa0f78423 Mon Sep 17 00:00:00 2001 From: "arrz.dev" <55147716+arrzdev@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:34:18 +0000 Subject: [PATCH] test(synq): done roadmap todos become real tests, only isSyncing stays todo The roadmap todo file listed the db factory, the live-query engine behind useCollection and the IndexedDB adapter as unbuilt, but all three exist. Covered behaviour loses its todo; the two gaps get tests: db.sync() versus db[name].sync() scope, and finite-gcTime ref-counted teardown. isSyncing does not exist yet and stays a todo. --- .../synq/src/core/create-synq.test.ts | 36 +++++++++++++++++-- .../synq/src/core/sync-engine.todo.test.ts | 20 +++-------- .../synq/src/reactive/live-queries.test.ts | 34 +++++++++++++++++- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/playground/packages/synq/src/core/create-synq.test.ts b/playground/packages/synq/src/core/create-synq.test.ts index 049ed05e..dd2d2cca 100644 --- a/playground/packages/synq/src/core/create-synq.test.ts +++ b/playground/packages/synq/src/core/create-synq.test.ts @@ -28,14 +28,17 @@ function makeDb(config?: Partial>) { } describe("createSynqStorage — local reads/writes", () => { - it("insert shows up optimistically as pending", async () => { - const { db } = makeDb() + it("insert appends an op and shows up optimistically as pending", async () => { + const { db, storage } = makeDb() const created = await db.todos.insert({ title: "buy milk", checked: false, position: 0, }) expect(created.$sync.$synced).toBe(false) + const ops = await storage.getOps("todos") + expect(ops).toHaveLength(1) + expect(ops[0]).toMatchObject({ type: "INSERT", rowId: created.$id }) const got = await db.todos.get(created.$id) expect(got).toMatchObject({ title: "buy milk" }) expect(got?.$sync.$syncStatus).toBe("pending") @@ -193,6 +196,35 @@ describe("createSynqStorage — sync", () => { expect(got?.$sync.$syncStatus).toBe("synced") }) + it("db.sync() runs every collection; db[name].sync() runs only that one", async () => { + const pulled: string[] = [] + const pullFor = (name: string) => async () => { + pulled.push(name) + return { changes: [], nextCursor: 1 } + } + const db = createSynqStorage({ + storageAdapter: createMemoryStorage(), + collections: { + todos: { + name: "todos", + pull: pullFor("todos"), + } as CollectionConfig, + notes: { + name: "notes", + pull: pullFor("notes"), + } as CollectionConfig, + }, + }) + + const outcomes = await db.sync() + expect(outcomes).toHaveLength(2) + expect(pulled).toEqual(["todos", "notes"]) + + pulled.length = 0 + await db.notes.sync() + expect(pulled).toEqual(["notes"]) + }) + it("pulls shared remote rows into the local view", async () => { const db = createSynqStorage({ storageAdapter: createMemoryStorage(), diff --git a/playground/packages/synq/src/core/sync-engine.todo.test.ts b/playground/packages/synq/src/core/sync-engine.todo.test.ts index 40e0ae39..5098c57e 100644 --- a/playground/packages/synq/src/core/sync-engine.todo.test.ts +++ b/playground/packages/synq/src/core/sync-engine.todo.test.ts @@ -1,22 +1,10 @@ import { describe, it } from "vitest" //---- Roadmap: remaining layers ------------------------------------ -//the pure core + snapshot sync engine + in-memory storage are done. what -//is left is the ergonomic surface and the browser persistence. +//the db factory, the live-query engine behind useCollection and the +//IndexedDB adapter are done and tested in their own files. what is left +//is a sync-progress signal on the React surface. -describe("db factory (createSynq)", () => { - it.todo("exposes db[name] handles with insert/update/delete/get/query") - it.todo("db.sync() reconciles every collection; db[name].sync() one") - it.todo("insert appends an op and the read view shows it optimistically") -}) - -describe("react useQuery adapter", () => { - it.todo("serves warm memory cache instantly, then live-updates") - it.todo("query cache is ref-counted and GC'd after the last unmount") +describe("react useCollection adapter", () => { it.todo("exposes isSyncing during a sync run") }) - -describe("indexeddb storage adapter", () => { - it.todo("persists rows/ops/cursor across reopen (fake-indexeddb)") - it.todo("notifies subscribers once per atomic transaction") -}) diff --git a/playground/packages/synq/src/reactive/live-queries.test.ts b/playground/packages/synq/src/reactive/live-queries.test.ts index 51d4022a..c9ffa91a 100644 --- a/playground/packages/synq/src/reactive/live-queries.test.ts +++ b/playground/packages/synq/src/reactive/live-queries.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest" +import { afterEach, describe, expect, it, vi } from "vitest" import { createMemoryStorage } from "#synq/adapters/memory.adapter" import { createSynqStorage } from "#synq/core/create-synq" import { createLiveQueries } from "#synq/reactive/live-queries" @@ -70,6 +70,38 @@ describe("createLiveQueries — dedup + reactivity", () => { }) describe("createLiveQueries — gcTime semantics", () => { + afterEach(() => { + vi.useRealTimers() + }) + + it("is ref-counted: the observer stops only after the last unmount + gcTime", async () => { + const { db, handle } = makeHandle() + const engine = createLiveQueries(handle) + const a = engine.observe({ gcTime: 1000 }, () => {}) + const b = engine.observe({ gcTime: 1000 }, () => {}) + await flush() + + vi.useFakeTimers() + //one of two observers leaves → still live + a.unsubscribe() + await db.todos.insert({ title: "a", checked: false, position: 0 }) + await vi.advanceTimersByTimeAsync(0) + expect(b.get().data).toHaveLength(1) + + //last observer leaves → keeps processing inside the gcTime window… + b.unsubscribe() + await vi.advanceTimersByTimeAsync(999) + await db.todos.insert({ title: "b", checked: false, position: 1 }) + await vi.advanceTimersByTimeAsync(0) + expect(b.get().data).toHaveLength(2) + + //…and is torn down once it elapses: later writes are not picked up + await vi.advanceTimersByTimeAsync(1) + await db.todos.insert({ title: "c", checked: false, position: 2 }) + await vi.advanceTimersByTimeAsync(0) + expect(b.get().data).toHaveLength(2) + }) + it("gcTime:0 stops processing on unmount but keeps last data for SWR", async () => { const { db, handle } = makeHandle() await db.todos.insert({ title: "a", checked: false, position: 0 })