Open History to see the last commit
1import { mkdtempSync, rmSync } from 'node:fs'
2import { tmpdir } from 'node:os'
3import { join } from 'node:path'
4import { afterEach, expect, it } from 'vitest'
5import { createCollection } from '@tanstack/db'
6import {
7 createTauriSQLitePersistence as createIndexPersistence,
8 persistedCollectionOptions,
9} from '../src'
10import { createTauriSQLitePersistence as createTauriPersistence } from '../src/tauri'
11import { createTauriSQLiteTestDatabase } from './helpers/tauri-sql-test-db'
12
13type Todo = {
14 id: string
15 title: string
16 score: number
17}
18
19const activeCleanupFns: Array<() => void | Promise<void>> = []
20
21afterEach(async () => {
22 while (activeCleanupFns.length > 0) {
23 const cleanupFn = activeCleanupFns.pop()
24 await Promise.resolve(cleanupFn?.())
25 }
26})
27
28function createTempSqlitePath(): string {
29 const tempDirectory = mkdtempSync(
30 join(tmpdir(), `db-tauri-persistence-test-`),
31 )
32 const dbPath = join(tempDirectory, `state.sqlite`)
33 activeCleanupFns.push(() => {
34 rmSync(tempDirectory, { recursive: true, force: true })
35 })
36 return dbPath
37}
38
39it(`persists data across app restart (close and reopen)`, async () => {
40 const dbPath = createTempSqlitePath()
41 const collectionId = `todos-restart`
42
43 const firstDatabase = createTauriSQLiteTestDatabase({ filename: dbPath })
44 const firstPersistence = createTauriPersistence({
45 database: firstDatabase,
46 })
47 const firstAdapter = firstPersistence.adapter
48
49 await firstAdapter.applyCommittedTx(collectionId, {
50 txId: `tx-restart-1`,
51 term: 1,
52 seq: 1,
53 rowVersion: 1,
54 mutations: [
55 {
56 type: `insert`,
57 key: `1`,
58 value: {
59 id: `1`,
60 title: `Survives restart`,
61 score: 10,
62 },
63 },
64 ],
65 })
66 await Promise.resolve(firstDatabase.close())
67
68 const secondDatabase = createTauriSQLiteTestDatabase({ filename: dbPath })
69 activeCleanupFns.push(async () => {
70 await Promise.resolve(secondDatabase.close())
71 })
72 const secondPersistence = createTauriPersistence({
73 database: secondDatabase,
74 })
75 const secondAdapter = secondPersistence.adapter
76
77 const rows = await secondAdapter.loadSubset(collectionId, {})
78 expect(rows).toEqual([
79 {
80 key: `1`,
81 value: {
82 id: `1`,
83 title: `Survives restart`,
84 score: 10,
85 },
86 },
87 ])
88})
89
90it(`shares the same runtime behavior through index and tauri entrypoints`, async () => {
91 const dbPath = createTempSqlitePath()
92 const collectionId = `todos-entrypoints`
93 const database = createTauriSQLiteTestDatabase({ filename: dbPath })
94 activeCleanupFns.push(async () => {
95 await Promise.resolve(database.close())
96 })
97
98 const indexPersistence = createIndexPersistence({ database })
99 const tauriPersistence = createTauriPersistence({ database })
100
101 await indexPersistence.adapter.applyCommittedTx(collectionId, {
102 txId: `tx-entrypoint-1`,
103 term: 1,
104 seq: 1,
105 rowVersion: 1,
106 mutations: [
107 {
108 type: `insert`,
109 key: `1`,
110 value: {
111 id: `1`,
112 title: `Entry point parity`,
113 score: 1,
114 },
115 },
116 ],
117 })
118
119 const rows = await tauriPersistence.adapter.loadSubset(collectionId, {})
120 expect(rows[0]?.value.title).toBe(`Entry point parity`)
121})
122
123it(`resumes persisted sync after cleanup and restart`, async () => {
124 const dbPath = createTempSqlitePath()
125 const collectionId = `todos-lifecycle`
126 const database = createTauriSQLiteTestDatabase({ filename: dbPath })
127 activeCleanupFns.push(async () => {
128 await Promise.resolve(database.close())
129 })
130
131 const persistence = createTauriPersistence({
132 database,
133 })
134 const collection = createCollection(
135 persistedCollectionOptions<Todo, string>({
136 id: collectionId,
137 getKey: (todo) => todo.id,
138 persistence,
139 syncMode: `eager`,
140 }),
141 )
142 activeCleanupFns.push(() => collection.cleanup())
143
144 await collection.stateWhenReady()
145 const initialInsert = collection.insert({
146 id: `1`,
147 title: `Before cleanup`,
148 score: 1,
149 })
150 await initialInsert.isPersisted.promise
151 expect(collection.get(`1`)?.title).toBe(`Before cleanup`)
152
153 await collection.cleanup()
154 collection.startSyncImmediate()
155 await collection.stateWhenReady()
156
157 const resumedInsert = collection.insert({
158 id: `2`,
159 title: `After restart`,
160 score: 2,
161 })
162 await resumedInsert.isPersisted.promise
163 expect(collection.get(`2`)?.title).toBe(`After restart`)
164
165 const persistedRows = await persistence.adapter.loadSubset(collectionId, {})
166 expect(persistedRows).toEqual(
167 expect.arrayContaining([
168 expect.objectContaining({
169 key: `1`,
170 }),
171 expect.objectContaining({
172 key: `2`,
173 }),
174 ]),
175 )
176})