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

collect-idb-databases.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
235
236
237
import {expect} from '@esm-bundle/chai';import {fakeHash} from '../hash.js';import {TestMemStore} from '../kv/test-mem-store';import {makeClientMap, setClients} from './clients-test-helpers';import { IDBDatabasesStore, IndexedDBDatabase, IndexedDBName,} from './idb-databases-store';import {collectIDBDatabases} from './collect-idb-databases';import * as dag from '../dag/mod';import type {ClientMap} from './clients.js';import {assertNotUndefined} from '../asserts.js';import {SinonFakeTimers, useFakeTimers} from 'sinon';import {REPLICACHE_FORMAT_VERSION} from '../replicache.js';
suite('collectIDBDatabases', async () => { let clock: SinonFakeTimers;
setup(() => { clock = useFakeTimers(0); });
teardown(() => { clock.restore(); });
type Entries = [IndexedDBDatabase, ClientMap][];
const makeIndexedDBDatabase = ( name: string, lastOpenedTimestampMS = Date.now(), replicacheFormatVersion = REPLICACHE_FORMAT_VERSION, schemaVersion = 'schemaVersion-' + name, replicacheName = 'replicacheName-' + name, ): IndexedDBDatabase => ({ name, replicacheFormatVersion, schemaVersion, replicacheName, lastOpenedTimestampMS, });
const t = ( name: string, entries: Entries, now: number, expectedDatabases: string[], ) => { for (const legacy of [false, true]) { test(name + ' > time ' + now + (legacy ? ' > legacy' : ''), async () => { const store = new IDBDatabasesStore(_ => new TestMemStore()); const clientDagStores = new Map<IndexedDBName, dag.Store>(); for (const [db, clients] of entries) { const dagStore = new dag.TestStore(); clientDagStores.set(db.name, dagStore); if (legacy) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const {lastOpenedTimestampMS: _, ...rest} = db; await store.putDatabaseForTesting(rest); } else { await store.putDatabaseForTesting(db); }
await setClients(clients, dagStore); }
const newDagStore = (name: string) => { const dagStore = clientDagStores.get(name); assertNotUndefined(dagStore); return dagStore; };
const maxAge = 1000;
const controller = new AbortController(); await collectIDBDatabases( store, controller.signal, now, maxAge, newDagStore, );
expect(Object.keys(await store.getDatabases())).to.deep.equal( expectedDatabases, ); }); } };
t('empty', [], 0, []);
{ const entries: Entries = [ [ makeIndexedDBDatabase('a', 0), makeClientMap({ clientA1: { headHash: fakeHash('a1'), heartbeatTimestampMs: 0, }, }), ], ]; t('one idb, one client', entries, 0, ['a']); t('one idb, one client', entries, 1000, []); t('one idb, one client', entries, 2000, []); }
{ const entries: Entries = [ [ makeIndexedDBDatabase('a', 0), makeClientMap({ clientA1: { headHash: fakeHash('a1'), heartbeatTimestampMs: 0, }, }), ], [ makeIndexedDBDatabase('b', 1000), makeClientMap({ clientB1: { headHash: fakeHash('b1'), heartbeatTimestampMs: 1000, }, }), ], ]; t('x', entries, 0, ['a', 'b']); t('x', entries, 1000, ['b']); t('x', entries, 2000, []); }
{ const entries: Entries = [ [ makeIndexedDBDatabase('a', 2000), makeClientMap({ clientA1: { headHash: fakeHash('a1'), heartbeatTimestampMs: 0, }, clientA2: { headHash: fakeHash('a2'), heartbeatTimestampMs: 2000, }, }), ], [ makeIndexedDBDatabase('b', 1000), makeClientMap({ clientB1: { headHash: fakeHash('b1'), heartbeatTimestampMs: 1000, }, }), ], ]; t('two idb, three clients', entries, 0, ['a', 'b']); t('two idb, three clients', entries, 1000, ['a', 'b']); t('two idb, three clients', entries, 2000, ['a']); t('two idb, three clients', entries, 3000, []); }
{ const entries: Entries = [ [ makeIndexedDBDatabase('a', 3000), makeClientMap({ clientA1: { headHash: fakeHash('a1'), heartbeatTimestampMs: 1000, }, clientA2: { headHash: fakeHash('a2'), heartbeatTimestampMs: 3000, }, }), ], [ makeIndexedDBDatabase('b', 4000), makeClientMap({ clientB1: { headHash: fakeHash('b1'), heartbeatTimestampMs: 2000, }, clientB2: { headHash: fakeHash('b2'), heartbeatTimestampMs: 4000, }, }), ], ]; t('two idb, four clients', entries, 1000, ['a', 'b']); t('two idb, four clients', entries, 2000, ['a', 'b']); t('two idb, four clients', entries, 3000, ['a', 'b']); t('two idb, four clients', entries, 4000, ['b']); t('two idb, four clients', entries, 5000, []); }
{ const entries: Entries = [ [ makeIndexedDBDatabase('a', 0, REPLICACHE_FORMAT_VERSION + 1), makeClientMap({ clientA1: { headHash: fakeHash('a1'), heartbeatTimestampMs: 0, }, }), ], ]; t('one idb, one client, format version too new', entries, 0, ['a']); t('one idb, one client, format version too new', entries, 1000, ['a']); t('one idb, one client, format version too new', entries, 2000, ['a']); }
{ const entries: Entries = [ [ makeIndexedDBDatabase('a', 0, REPLICACHE_FORMAT_VERSION - 1), makeClientMap({ clientA1: { headHash: fakeHash('a1'), heartbeatTimestampMs: 0, }, }), ], ]; t('one idb, one client, old format version', entries, 0, ['a']); t('one idb, one client, old format version', entries, 1000, []); }});
replicache

Version Info

Tagged at
2 years ago