-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathbrowser-single-tab-persisted-collection.e2e.test.ts
More file actions
271 lines (248 loc) · 7.32 KB
/
browser-single-tab-persisted-collection.e2e.test.ts
File metadata and controls
271 lines (248 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterAll, afterEach, beforeAll } from 'vitest'
import { BTreeIndex, createCollection } from '@tanstack/db'
import {
createBrowserWASQLitePersistence,
persistedCollectionOptions,
} from '../src'
import { generateSeedData } from '../../db-collection-e2e/src/fixtures/seed-data'
import { runPersistedCollectionConformanceSuite } from '../../db-sqlite-persistence-core/tests/contracts/persisted-collection-conformance-contract'
import { createWASQLiteTestDatabase } from '../tests/helpers/wa-sqlite-test-db'
import type { Collection } from '@tanstack/db'
import type { BrowserWASQLiteDatabase } from '../src'
import type {
Comment,
E2ETestConfig,
Post,
User,
} from '../../db-collection-e2e/src/types'
type PersistableRow = {
id: string
}
type BrowserPersistedCollectionTestConfig = E2ETestConfig
type PersistedCollectionHarness<T extends PersistableRow> = {
collection: Collection<T, string | number>
seedPersisted: (rows: Array<T>) => Promise<void>
}
let config: BrowserPersistedCollectionTestConfig
function createPersistedCollection<T extends PersistableRow>(
database: BrowserWASQLiteDatabase,
id: string,
syncMode: `eager` | `on-demand`,
): PersistedCollectionHarness<T> {
const persistence = createBrowserWASQLitePersistence({
database,
})
let seedTxSequence = 0
const seedPersisted = async (rows: Array<T>): Promise<void> => {
if (rows.length === 0) {
return
}
seedTxSequence++
await persistence.adapter.applyCommittedTx(id, {
txId: `seed-${id}-${seedTxSequence}`,
term: 1,
seq: seedTxSequence,
rowVersion: seedTxSequence,
mutations: rows.map((row) => ({
type: `insert` as const,
key: row.id,
value: row,
})),
})
}
const collection = createCollection(
persistedCollectionOptions<T, string | number>({
id,
syncMode,
getKey: (item) => item.id,
persistence,
autoIndex: `eager`,
defaultIndexType: BTreeIndex,
}),
)
return {
collection,
seedPersisted,
}
}
type PersistedTransactionHandle = {
isPersisted: {
promise: Promise<unknown>
}
}
async function waitForPersisted(
transaction: PersistedTransactionHandle,
): Promise<void> {
await transaction.isPersisted.promise
}
async function seedCollection<T extends PersistableRow>(
collection: Collection<T, string | number>,
rows: Array<T>,
): Promise<void> {
const tx = collection.insert(rows)
await waitForPersisted(tx)
}
async function insertRowIntoCollections<T extends PersistableRow>(
collections: ReadonlyArray<Collection<T, string | number>>,
row: T,
): Promise<void> {
for (const collection of collections) {
const tx = collection.insert(row)
await waitForPersisted(tx)
}
}
async function updateRowAcrossCollections<T extends PersistableRow>(
collections: ReadonlyArray<Collection<T, string | number>>,
id: string,
updates: Partial<T>,
): Promise<void> {
for (const collection of collections) {
if (!collection.has(id)) {
continue
}
const tx = collection.update(id, (draft) => {
Object.assign(draft, updates)
})
await waitForPersisted(tx)
}
}
async function deleteRowAcrossCollections<T extends PersistableRow>(
collections: ReadonlyArray<Collection<T, string | number>>,
id: string,
): Promise<void> {
for (const collection of collections) {
if (!collection.has(id)) {
continue
}
const tx = collection.delete(id)
await waitForPersisted(tx)
}
}
beforeAll(async () => {
const tempDirectory = mkdtempSync(join(tmpdir(), `db-browser-persisted-e2e-`))
const dbPath = join(tempDirectory, `state.sqlite`)
const suiteId = Date.now().toString(36)
const database = createWASQLiteTestDatabase({
filename: dbPath,
})
const seedData = generateSeedData()
const eagerUsers = createPersistedCollection<User>(
database,
`browser-persisted-users-eager-${suiteId}`,
`eager`,
)
const eagerPosts = createPersistedCollection<Post>(
database,
`browser-persisted-posts-eager-${suiteId}`,
`eager`,
)
const eagerComments = createPersistedCollection<Comment>(
database,
`browser-persisted-comments-eager-${suiteId}`,
`eager`,
)
const onDemandUsers = createPersistedCollection<User>(
database,
`browser-persisted-users-ondemand-${suiteId}`,
`on-demand`,
)
const onDemandPosts = createPersistedCollection<Post>(
database,
`browser-persisted-posts-ondemand-${suiteId}`,
`on-demand`,
)
const onDemandComments = createPersistedCollection<Comment>(
database,
`browser-persisted-comments-ondemand-${suiteId}`,
`on-demand`,
)
await Promise.all([
eagerUsers.collection.preload(),
eagerPosts.collection.preload(),
eagerComments.collection.preload(),
])
await seedCollection(eagerUsers.collection, seedData.users)
await seedCollection(eagerPosts.collection, seedData.posts)
await seedCollection(eagerComments.collection, seedData.comments)
await onDemandUsers.seedPersisted(seedData.users)
await onDemandPosts.seedPersisted(seedData.posts)
await onDemandComments.seedPersisted(seedData.comments)
config = {
collections: {
eager: {
users: eagerUsers.collection,
posts: eagerPosts.collection,
comments: eagerComments.collection,
},
onDemand: {
users: onDemandUsers.collection,
posts: onDemandPosts.collection,
comments: onDemandComments.collection,
},
},
mutations: {
insertUser: async (user) =>
insertRowIntoCollections(
[eagerUsers.collection, onDemandUsers.collection],
user,
),
updateUser: async (id, updates) =>
updateRowAcrossCollections(
[eagerUsers.collection, onDemandUsers.collection],
id,
updates,
),
deleteUser: async (id) =>
deleteRowAcrossCollections(
[eagerUsers.collection, onDemandUsers.collection],
id,
),
insertPost: async (post) =>
insertRowIntoCollections(
[eagerPosts.collection, onDemandPosts.collection],
post,
),
},
setup: async () => {},
afterEach: async () => {
await Promise.all([
onDemandUsers.collection.cleanup(),
onDemandPosts.collection.cleanup(),
onDemandComments.collection.cleanup(),
])
onDemandUsers.collection.startSyncImmediate()
onDemandPosts.collection.startSyncImmediate()
onDemandComments.collection.startSyncImmediate()
},
teardown: async () => {
await Promise.all([
eagerUsers.collection.cleanup(),
eagerPosts.collection.cleanup(),
eagerComments.collection.cleanup(),
onDemandUsers.collection.cleanup(),
onDemandPosts.collection.cleanup(),
onDemandComments.collection.cleanup(),
])
await Promise.resolve(database.close?.())
rmSync(tempDirectory, { recursive: true, force: true })
},
}
})
afterEach(async () => {
if (config.afterEach) {
await config.afterEach()
}
})
afterAll(async () => {
await config.teardown()
})
function getConfig(): Promise<BrowserPersistedCollectionTestConfig> {
return Promise.resolve(config)
}
runPersistedCollectionConformanceSuite(
`browser single-tab persisted collection conformance`,
getConfig,
)