Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions playground/packages/synq/src/core/create-synq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ function makeDb(config?: Partial<CollectionConfig<Todo>>) {
}

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")
Expand Down Expand Up @@ -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<Todo>,
notes: {
name: "notes",
pull: pullFor("notes"),
} as CollectionConfig<Todo>,
},
})

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(),
Expand Down
20 changes: 4 additions & 16 deletions playground/packages/synq/src/core/sync-engine.todo.test.ts
Original file line number Diff line number Diff line change
@@ -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")
})
34 changes: 33 additions & 1 deletion playground/packages/synq/src/reactive/live-queries.test.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 })
Expand Down
Loading