-
Notifications
You must be signed in to change notification settings - Fork 36
/
test-helpers.ts
56 lines (50 loc) · 1.86 KB
/
test-helpers.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
import {expect} from '@esm-bundle/chai';
import type {Chain} from '../db/test-helpers';
import type * as dag from '../dag/mod';
import * as db from '../db/mod';
import * as sync from '../sync/mod';
import type {SnapshotMeta} from '../db/commit';
// See db.test_helpers for addLocal, addSnapshot, etc. We can't put addLocalRebase
// there because sync depends on db, and addLocalRebase depends on sync.
// addSyncSnapshot adds a sync snapshot off of the main chain's base snapshot and
// returns it (in chain order). Caller needs to supply which commit to take indexes
// from because it is context dependent (they should come from the parent of the
// first commit to rebase, or from head if no commits will be rebased).
export async function addSyncSnapshot(
chain: Chain,
store: dag.Store,
takeIndexesFrom: number,
): Promise<Chain> {
expect(chain.length >= 2).to.be.true;
let maybeBaseSnapshot: db.Commit<SnapshotMeta> | undefined;
for (let i = chain.length - 1; i > 0; i--) {
const commit = chain[i - 1];
if (commit.isSnapshot()) {
maybeBaseSnapshot = commit;
break;
}
}
if (maybeBaseSnapshot === undefined) {
throw new Error('main chain doesnt have a snapshot or local commit');
}
const baseSnapshot = maybeBaseSnapshot;
const syncChain: Chain = [];
// Add sync snapshot.
const cookie = `sync_cookie_${chain.length}`;
const indexes = db.readIndexesForWrite(chain[takeIndexesFrom]);
await store.withWrite(async dagWrite => {
const w = await db.Write.newSnapshot(
db.whenceHash(baseSnapshot.chunk.hash),
baseSnapshot.mutationID,
cookie,
dagWrite,
indexes,
);
await w.commit(sync.SYNC_HEAD_NAME);
});
const [, commit] = await store.withRead(async dagRead => {
return await db.readCommit(db.whenceHead(sync.SYNC_HEAD_NAME), dagRead);
});
syncChain.push(commit);
return syncChain;
}