All files / json-crdt-repo/src/local/level/__tests__ setup.ts

72.54% Statements 37/51
62.5% Branches 5/8
55.55% Functions 5/9
72.91% Lines 35/48

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 9810x 10x 10x 10x 10x 10x 10x   10x       10x     90x 90x 90x 90x 90x 90x       90x 90x 113x 113x 113x                   113x 113x 113x   113x   90x 90x                                                 90x 90x 37x 24x 24x   24x   90x 90x 90x                                  
import {LevelLocalRepo, type LevelLocalRepoOpts} from '../LevelLocalRepo';
import {Locks} from 'thingies/lib/Locks';
import {Model, Patch} from 'json-joy/lib/json-crdt';
import {Log} from 'json-joy/lib/json-crdt/log/Log';
import {BehaviorSubject} from 'rxjs';
import {setup as remoteSetup} from '../../../remote/__tests__/setup';
import {MemoryLevel} from 'memory-level';
import type {BinStrLevel, LevelLocalRepoPubSub} from '../types';
import {pubsub as createPubsub} from '../../../pubsub';
 
/* tslint:disable:no-console */
 
export const setup = async (
  opts: {remote?: ReturnType<typeof remoteSetup>; local?: Partial<LevelLocalRepoOpts>} = {},
) => {
  const remote = opts.remote ?? remoteSetup();
  const locks = new Locks();
  const genId = () => Date.now().toString(36) + Math.random().toString(36).slice(2);
  const id = genId();
  const col = ['collection', 'sub-collection'];
  const kv = new MemoryLevel<string, Uint8Array>({
    keyEncoding: 'utf8',
    valueEncoding: 'view',
  }) as unknown as BinStrLevel;
  const blockId = [...col, id];
  const createLocal = (sid = 12345678) => {
    const busName = 'test-' + id;
    const pubsub = createPubsub(busName) as LevelLocalRepoPubSub;
    const local = new LevelLocalRepo({
      kv,
      locks,
      sid,
      rpc: remote.remote,
      connected$: new BehaviorSubject(true),
      pubsub,
      onSyncError: (error) => console.error(error),
      ...opts.local,
    });
    const stop = async () => {
      await local.stop();
      pubsub.end();
    };
    return {kv, sid, local, pubsub, stop};
  };
  const {sid, local, pubsub, stop} = createLocal();
  const createRemote = (localOpts: Partial<LevelLocalRepoOpts> = {}) => {
    const sid = localOpts.sid ?? 123456789;
    const busName = 'test-' + id;
    const pubsub = createPubsub(busName) as LevelLocalRepoPubSub;
    const kv = new MemoryLevel<string, Uint8Array>({
      keyEncoding: 'utf8',
      valueEncoding: 'view',
    }) as unknown as BinStrLevel;
    const locks = new Locks();
    const local = new LevelLocalRepo({
      kv,
      locks,
      sid,
      rpc: remote.remote,
      connected$: new BehaviorSubject(true),
      pubsub,
      onSyncError: (error) => console.error(error),
      ...opts.local,
    });
    const stop = async () => {
      await local.stop();
      pubsub.end();
    };
    return {kv, sid, local, pubsub, stop};
  };
  const log = Log.fromNewModel(Model.create(undefined, sid));
  const getModelFromRemote = async (id: string = blockId.join('/')): Promise<Model> => {
    const res = await 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;
  };
  log.end.api.root({foo: 'bar'});
  log.end.api.flush();
  return {
    remote,
    locks,
    createLocal,
    createRemote,
    pubsub,
    local,
    sid,
    log,
    genId,
    id,
    col,
    blockId,
    getModelFromRemote,
    stop,
  };
};