deno.land / x / replicache@v10.0.0-beta.0 / replicache-persist.test.ts

replicache-persist.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
import { addData, expectLogContext, initReplicacheTesting, replicacheForTesting, tickAFewTimes,} from './test-util';import {expect} from '@esm-bundle/chai';import * as sinon from 'sinon';
// fetch-mock has invalid d.ts file so we removed that on npm install.// eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-expect-errorimport fetchMock from 'fetch-mock/esm/client';import * as kv from './kv/mod';import * as dag from './dag/mod';import * as persist from './persist/mod';import {assertNotTempHash} from './hash';import {assertNotUndefined} from './asserts';import {deleteClientForTesting} from './persist/clients-test-helpers.js';
initReplicacheTesting();
let perdag: dag.Store | undefined;teardown(async () => { await perdag?.close();});
test('basic persist & load', async () => { const pullURL = 'https://diff.com/pull'; const rep = await replicacheForTesting('persist-test', { pullURL, }); const clientID = await rep.clientID;
perdag = new dag.StoreImpl( new kv.IDBStore(rep.idbName), dag.throwChunkHasher, assertNotTempHash, );
const clientBeforePull = await perdag.withRead(read => persist.getClient(clientID, read), ); assertNotUndefined(clientBeforePull);
fetchMock.postOnce(pullURL, { cookie: '', lastMutationID: 2, patch: [ { op: 'put', key: 'a', value: 1, }, { op: 'put', key: 'b', value: 2, }, ], });
rep.pull();
// maxWaitAttempts * waitMs should be at least PERSIST_TIMEOUT // plus some buffer for the persist process to complete const maxWaitAttempts = 20; const waitMs = 100; let waitAttempt = 0; const run = true; while (run) { if (waitAttempt++ > maxWaitAttempts) { throw new Error( `Persist did not complete in ${maxWaitAttempts * waitMs} ms`, ); } await tickAFewTimes(waitMs); const client: persist.Client | undefined = await perdag.withRead(read => persist.getClient(clientID, read), ); assertNotUndefined(client); if (clientBeforePull.headHash !== client.headHash) { // persist has completed break; } }
await rep.query(async tx => { expect(await tx.get('a')).to.equal(1); expect(await tx.get('b')).to.equal(2); });
// If we create another instance it will lazy load the data from IDB const rep2 = await replicacheForTesting('persist-test', { pullURL, }); await rep2.query(async tx => { expect(await tx.get('a')).to.equal(1); expect(await tx.get('b')).to.equal(2); });
expect(await rep.clientID).to.not.equal(await rep2.clientID);});
suite('onClientStateNotFound', () => { test('Called in persist if collected', async () => { const consoleErrorStub = sinon.stub(console, 'error');
const rep = await replicacheForTesting('called-in-persist', { mutators: {addData}, });
await rep.mutate.addData({foo: 'bar'}); await rep.persist();
const clientID = await rep.clientID; await deleteClientForTesting(clientID, rep.perdag);
const onClientStateNotFound = sinon.fake(); rep.onClientStateNotFound = onClientStateNotFound; await rep.persist();
expect(onClientStateNotFound.callCount).to.equal(1); expect(onClientStateNotFound.lastCall.args).to.deep.equal([ {type: 'NotFoundOnClient'}, ]); expectLogContext( consoleErrorStub, 0, rep, `Client state not found, clientID: ${clientID}`, ); });
test('Called in query if collected', async () => { const consoleErrorStub = sinon.stub(console, 'error');
const rep = await replicacheForTesting('called-in-query', { mutators: {addData}, });
await rep.mutate.addData({foo: 'bar'}); await rep.persist(); const clientID = await rep.clientID; await deleteClientForTesting(clientID, rep.perdag); await rep.close();
const rep2 = await replicacheForTesting('called-in-query', { mutators: {addData}, });
const clientID2 = await rep2.clientID; await deleteClientForTesting(clientID2, rep2.perdag);
const onClientStateNotFound = sinon.fake(); rep2.onClientStateNotFound = onClientStateNotFound;
let e: unknown; try { await rep2.query(async tx => { await tx.get('foo'); }); } catch (err) { e = err; } expect(e).to.be.instanceOf(persist.ClientStateNotFoundError); expectLogContext( consoleErrorStub, 0, rep2, `Client state not found, clientID: ${clientID2}`, ); expect(onClientStateNotFound.lastCall.args).to.deep.equal([ {type: 'NotFoundOnClient'}, ]); });
test('Called in mutate if collected', async () => { const consoleErrorStub = sinon.stub(console, 'error');
const rep = await replicacheForTesting('called-in-mutate', { mutators: { addData, async check(tx, key) { await tx.has(key); }, }, });
await rep.mutate.addData({foo: 'bar'}); await rep.persist(); const clientID = await rep.clientID; await deleteClientForTesting(clientID, rep.perdag); await rep.close();
const rep2 = await replicacheForTesting('called-in-query', { mutators: { async check(tx, key) { await tx.has(key); }, }, });
const clientID2 = await rep2.clientID; await deleteClientForTesting(clientID2, rep2.perdag);
const onClientStateNotFound = sinon.fake(); rep2.onClientStateNotFound = onClientStateNotFound;
let e: unknown; try { // Another mutate will trigger await rep2.mutate.check('x'); } catch (err) { e = err; }
expect(e).to.be.instanceOf(persist.ClientStateNotFoundError); expectLogContext( consoleErrorStub, 0, rep2, `Client state not found, clientID: ${clientID2}`, ); expect(onClientStateNotFound.lastCall.args).to.deep.equal([ {type: 'NotFoundOnClient'}, ]); });});
replicache

Version Info

Tagged at
2 years ago