All files / json-crdt-server/src/__tests__ setup.ts

100% Statements 48/48
100% Branches 10/10
100% Functions 8/8
100% Lines 41/41

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 624x 4x 4x 4x 4x 4x 4x 4x     4x 238007x 199160x 160004x 79948x 159281x 79948x   80056x     4x 93x 93x 93x 93x 1528x 93x 93x 93x   93x     4x 27x 27x     4x 27x       27x 27x     4x 27x 27x 27x 27x 27x 27x 27x            
import * as fs from 'node:fs';
import {MemoryLevel} from 'memory-level';
import {CalleeCaller} from '@jsonjoy.com/rpc-calls/lib/caller/CalleeCaller';
import {createCaller} from '../routes';
import {LevelStore} from '../services/blocks/store/level/LevelStore';
import {Services} from '../services/Services';
import {ClassicLevel} from 'classic-level';
import {MemoryStore} from '../services/blocks/store/MemoryStore';
import type {Store} from '../services/blocks/store/types';
 
const deepClone = (val: unknown): unknown => {
  if (val instanceof Uint8Array) return new Uint8Array(val);
  if (Array.isArray(val)) return val.map(deepClone);
  if (val !== null && typeof val === 'object') {
    const out: Record<string, unknown> = {};
    for (const k of Object.keys(val)) out[k] = deepClone((val as Record<string, unknown>)[k]);
    return out;
  }
  return val;
};
 
export const setup = async (store: Store = new MemoryStore(), close?: () => Promise<void>) => {
  const services = new Services({store});
  const {caller} = createCaller(services);
  const client = new CalleeCaller(caller.rpc as any, {} as any);
  const call = async (method: string, req?: unknown): Promise<any> =>
    deepClone(await client.call(method as any, req as any));
  const call$ = client.call$.bind(client);
  const stop = async (): Promise<void> => {
    await close?.();
  };
  return {call, call$, stop};
};
 
export const setupMemory = async () => {
  const store = new MemoryStore();
  return setup(store);
};
 
export const setupLevelMemory = async () => {
  const kv = new MemoryLevel<string, Uint8Array>({
    keyEncoding: 'utf8',
    valueEncoding: 'view',
  });
  const store = new LevelStore(<any>kv);
  return setup(store);
};
 
export const setupLevelClassic = async (name: string = 'main') => {
  const dir = `${__dirname}/test-tmp/leveldb/${name}`;
  const kv = new ClassicLevel<string, Uint8Array>(dir, {valueEncoding: 'view'});
  await kv.open();
  const store = new LevelStore(<any>kv);
  return setup(store, async () => {
    await kv.close();
    fs.rmSync(dir, {recursive: true, force: true});
  });
};
 
export type JsonCrdtTestSetup = typeof setup;
export type ApiTestSetup = typeof setupMemory;