Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 14x 5x 7x 14x 18x 17x 14x 1639x 1639x 1635x 1635x 1635x 5x 17x 17x 17x 17x 17x 22x 5x 22x 22x 22x 22x 22x 22x 22x 5x 22x 22x 22x 22x 22x 22x 1638x 22x 22x 22x 22x 22x | import {LevelLocalRepo, type LevelLocalRepoOpts} from '../local/level/LevelLocalRepo';
import {Locks} from 'thingies/lib/Locks';
import {BehaviorSubject} from 'rxjs';
import {setup as remoteSetup} from '../remote/__tests__/setup';
import {MemoryLevel} from 'memory-level';
import {pubsub as createPubsub} from '../pubsub';
import type {BinStrLevel, LevelLocalRepoPubSub} from '../local/level/types';
import {EditSessionFactory} from '../session/EditSessionFactory';
import {Model, Patch} from 'json-joy/lib/json-crdt';
/* tslint:disable:no-console */
export class Testbed {
public static readonly create = () => {
return new Testbed();
};
public static readonly createRepo = (opts?: Partial<LevelLocalRepoOpts>) => {
return Testbed.create().createBrowser().createTab().createRepo(opts);
};
constructor(
public readonly remote: ReturnType<typeof remoteSetup> = remoteSetup(),
public genId: () => string = () => Date.now().toString(36) + Math.random().toString(36).slice(2),
) {}
public createBrowser(): BrowserTestbed {
return new BrowserTestbed(this);
}
public readonly getModelFromRemote = async (id: string | string[]): Promise<Model> => {
Eif (Array.isArray(id)) id = id.join('/');
const res = await this.remote.client.call('block.get', {id});
const model = Model.fromBinary(res.block.snapshot.blob);
for (const batch of res.block.tip)
for (const patch of batch.patches) model.applyPatch(Patch.fromBinary(patch.blob));
return model;
};
}
export class BrowserTestbed {
public readonly id: string;
public readonly locks: Locks;
public readonly kv: BinStrLevel;
constructor(
public readonly global: Testbed,
public readonly sid: number = 12345678,
) {
this.id = this.global.genId();
// TODO: Namespace locks to a specific repo.
this.locks = new Locks();
this.kv = new MemoryLevel<string, Uint8Array>({
keyEncoding: 'utf8',
valueEncoding: 'view',
}) as unknown as BinStrLevel;
}
public createTab(): BrowserTabTestbed {
return new BrowserTabTestbed(this);
}
}
export class BrowserTabTestbed {
public readonly pubsubBusName: string;
public readonly pubsub: LevelLocalRepoPubSub;
constructor(public readonly browser: BrowserTestbed) {
this.pubsubBusName = 'pubsub-bus-' + this.browser.id;
this.pubsub = createPubsub(this.pubsubBusName) as LevelLocalRepoPubSub;
}
public createRepo(opts?: Partial<LevelLocalRepoOpts>): LocalRepoTestbed {
const repo = new LevelLocalRepo({
kv: this.browser.kv,
locks: this.browser.locks,
sid: this.browser.sid,
rpc: this.browser.global.remote.remote,
pubsub: this.pubsub,
connected$: new BehaviorSubject(true),
onSyncError: (error) => console.error(error),
...opts,
});
return new LocalRepoTestbed(this, repo);
}
public readonly stop = async () => {
this.pubsub.end();
};
}
export class LocalRepoTestbed {
public readonly sessions: EditSessionFactory;
public readonly col: string[] = ['collection', 'sub-collection'];
public readonly blockId: string[] = [...this.col, this.tab.browser.id];
constructor(
public readonly tab: BrowserTabTestbed,
public readonly repo: LevelLocalRepo,
) {
this.sessions = new EditSessionFactory({
sid: tab.browser.sid,
repo,
});
}
public readonly getModelFromRemote = async (id: string | string[]): Promise<Model> => {
return await this.tab.browser.global.getModelFromRemote(id);
};
public readonly stop = async () => {
await this.repo.stop();
};
public readonly stopTab = async () => {
await this.stop();
await this.tab.stop();
};
}
|