-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.ts
234 lines (207 loc) · 9.99 KB
/
index.test.ts
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
import * as expect from 'expect'
import { setupStorexTest } from '@worldbrain/storex-pattern-modules/lib/index.tests'
import { SharedSyncLogStorage } from './shared-sync-log/storex';
import { ClientSyncLogStorage } from './client-sync-log';
import { CustomAutoPkMiddleware } from './custom-auto-pk';
import { SyncLoggingMiddleware } from './logging-middleware';
import { shareLogEntries, receiveLogEntries, doSync } from '.';
import { reconcileSyncLog } from './reconciliation';
type PromiseContentType<T> = T extends Promise<infer U> ? U : T
describe('Storex sync integration tests', () => {
async function setupBackend(options) {
return setupStorexTest<{sharedSyncLog : SharedSyncLogStorage}>({
dbName: 'backend',
collections: {},
modules: {
sharedSyncLog: ({storageManager}) => new SharedSyncLogStorage({storageManager})
}
})
}
function createGetNow(options : {start : number}) {
let now = options.start
return () => now++
}
async function setupClient(options : {backend, clientName : string, getNow : () => number, pkGenerator : () => string}) {
const { storageManager, modules } = await setupStorexTest<{clientSyncLog : ClientSyncLogStorage}>({
dbName: `client-${options.clientName}`,
collections: {
user: {
version: new Date('2019-01-01'),
fields: {
displayName: { type: 'string' }
}
},
email: {
version: new Date('2019-01-01'),
fields: {
address: { type: 'string' },
},
relationships: [
{ childOf: 'user' }
]
}
},
modules: {
clientSyncLog: ({storageManager}) => new ClientSyncLogStorage({storageManager})
}
})
const includeCollections = ['user', 'email']
const pkMiddleware = new CustomAutoPkMiddleware({ pkGenerator: options.pkGenerator })
pkMiddleware.setup({ storageRegistry: storageManager.registry, collections: includeCollections })
const syncLoggingMiddleware = new SyncLoggingMiddleware({ storageManager, clientSyncLog: modules.clientSyncLog, includeCollections })
syncLoggingMiddleware._getNow = options.getNow
storageManager.setMiddleware([
pkMiddleware,
syncLoggingMiddleware
])
return { storageManager, modules, deviceId: null, objects: {} }
}
async function setupTest(options : {clients? : {name : string}[], getNow : () => number}) {
let idsGenerated = 0
const pkGenerator = () => `id-${++idsGenerated}`
const userId = 1
const backend = await setupBackend({})
const clients : {[name : string] : PromiseContentType<ReturnType<typeof setupClient>>} = {}
for (const { name } of options.clients || []) {
clients[name] = await setupClient({backend, clientName: name, getNow: options.getNow, pkGenerator})
clients[name].deviceId = await backend.modules.sharedSyncLog.createDeviceId({ userId, sharedUntil: 10 })
}
return { backend, clients, userId }
}
describe('shareLogEntries()', () => {
async function setupShareTest() {
const { backend, clients, userId } = await setupTest({clients: [{ name: 'one' }, { name: 'two' } ], getNow: createGetNow({start: 2})})
await clients.one.storageManager.collection('user').createObject({displayName: 'Joe', emails: [{address: '[email protected]'}]})
const share = (options : {now: number}) => shareLogEntries({
sharedSyncLog: backend.modules.sharedSyncLog, clientSyncLog: clients.one.modules.clientSyncLog,
userId, deviceId: clients.one.deviceId, now: options.now
})
return { backend, clients, userId, share }
}
it('should correctly share log entries', async () => {
const { backend, clients, userId, share } = await setupShareTest()
await share({now: 55})
expect(await backend.modules.sharedSyncLog.getUnsyncedEntries({deviceId: clients.two.deviceId})).toEqual([
{
id: expect.anything(),
userId,
deviceId: clients.one.deviceId,
createdOn: 2,
sharedOn: 55,
data: '{"operation":"create","collection":"user","pk":"id-1","field":null,"value":{"displayName":"Joe"}}',
},
{
id: expect.anything(),
userId,
deviceId: clients.one.deviceId,
createdOn: 3,
sharedOn: 55,
data: '{"operation":"create","collection":"email","pk":"id-2","field":null,"value":{"address":"[email protected]"}}',
},
])
})
it('should not reshare entries that are already shared', async () => {
const { backend, clients, share } = await setupShareTest()
await share({now: 55})
const entries = await backend.modules.sharedSyncLog.getUnsyncedEntries({deviceId: clients.two.deviceId})
await share({now: 60})
expect(await backend.modules.sharedSyncLog.getUnsyncedEntries({deviceId: clients.two.deviceId})).toEqual(entries)
})
})
describe('receiveLogEntries()', () => {
async function setupReceiveTest() {
const { backend, clients, userId } = await setupTest({clients: [{ name: 'one' }, { name: 'two' } ], getNow: createGetNow({start: 2})})
const receive = async (options : {now : number}) => {
await receiveLogEntries({
clientSyncLog: clients.one.modules.clientSyncLog,
sharedSyncLog: backend.modules.sharedSyncLog,
deviceId: clients.one.deviceId,
now: options.now,
})
}
return { backend, clients, userId, receive }
}
it('should correctly receive unsynced entries and write them to the local log marked as needing integration', async () => {
const { backend, clients, userId, receive } = await setupReceiveTest()
await clients.one.storageManager.collection('user').createObject({displayName: 'Bob'})
await backend.modules.sharedSyncLog.writeEntries([
{
userId,
deviceId: clients.one.deviceId,
createdOn: 5,
sharedOn: 55,
data: '{"operation":"create","collection":"user","pk":"id-2","field":null,"value":{"displayName":"Joe"}}',
},
{
userId,
deviceId: clients.one.deviceId,
createdOn: 7,
sharedOn: 55,
data: '{"operation":"create","collection":"email","pk":"id-3","field":null,"value":{"address":"[email protected]"}}',
},
], { userId, deviceId: clients.one.deviceId })
await receive({now: 60})
expect(await clients.one.modules.clientSyncLog.getEntriesCreatedAfter(1)).toEqual([
{
id: expect.anything(),
createdOn: 2,
sharedOn: null,
needsIntegration: false,
collection: 'user',
pk: 'id-1',
operation: 'create',
value: { displayName: 'Bob' },
},
{
id: expect.anything(),
createdOn: 5,
sharedOn: 60,
needsIntegration: true,
collection: 'user',
pk: 'id-2',
field: null,
operation: 'create',
value: { displayName: 'Joe' },
},
{
id: expect.anything(),
createdOn: 7,
sharedOn: 60,
needsIntegration: true,
collection: 'email',
pk: 'id-3',
field: null,
operation: 'create',
value: { address: '[email protected]' },
}
])
})
})
describe('doSync()', () => {
async function setupSyncTest() {
const { backend, clients, userId } = await setupTest({ clients: [{ name: 'one' }, { name: 'two' }], getNow: createGetNow({ start: 50 }) })
const sync = async (options : {clientName : string, now : number}) => {
const client = clients[options.clientName]
await doSync({
clientSyncLog: client.modules.clientSyncLog,
sharedSyncLog: backend.modules.sharedSyncLog,
storageManager: client.storageManager,
reconciler: reconcileSyncLog,
now: options.now,
userId,
deviceId: client.deviceId
})
}
return { clients, sync }
}
it('should work when pulling changes after being offline', async () => {
const { clients, sync } = await setupSyncTest()
const orig = (await clients.one.storageManager.collection('user').createObject({displayName: 'Joe', emails: [{address: '[email protected]'}]})).object
const { emails, ...user } = orig
await sync({clientName: 'one', now: 55})
await sync({clientName: 'two', now: 60})
expect(await clients.two.storageManager.collection('user').findObject({id: user.id})).toEqual(user)
expect(await clients.two.storageManager.collection('email').findObject({id: emails[0].id})).toEqual(emails[0])
})
})
})