Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/lens/js/test/host.mjs

3.1 KiB, 1 run

created by r1870400018:65622, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// A simulated tab for the writer core: a Map-backed `localStorage` that can be made to run out
2// of quota, a send function that records every post and answers as told, a console per host
3// (so a wrap on one cannot leak to the next), and timers that can be fired at once while still
4// recording the delay that was asked for.
5//
6// This is the part of Daimond's `debugshare.test.mjs` harness the core needs. What it dropped is
7// the fake DOM and the app's globals (`DaimondSync`, `DaimondIdentity`), which belong to Daimond.
8import { createWriter } from '../writer.js';
9
10export const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
11
12export async function until(cond, ms) {
13 const end = Date.now() + (ms || 4000);
14 while (Date.now() < end) { if (cond()) return true; await sleep(2); }
15 return cond();
16}
17
18export function makeHost(cfg) {
19 cfg = cfg || {};
20 const store = cfg.store || new Map();
21 let writes = 0;
22 const storage = {
23 getItem: (k) => (store.has(k) ? store.get(k) : null),
24 setItem: (k, v) => {
25 writes += 1;
26 if (cfg.quota && cfg.quota(k, writes)) {
27 const e = new Error('QuotaExceededError');
28 e.name = 'QuotaExceededError';
29 throw e;
30 }
31 store.set(k, String(v));
32 },
33 removeItem: (k) => store.delete(k),
34 };
35 const logged = [];
36 const console_ = {};
37 ['log', 'info', 'debug', 'warn', 'error'].forEach((lvl) => {
38 console_[lvl] = function () { logged.push(lvl + ':' + [].slice.call(arguments).join(' ')); };
39 });
40 const posts = [];
41 let halted = false;
42 const timerDelays = [];
43 const setT = cfg.fastTimers
44 ? (fn, ms) => { timerDelays.push(ms); return setTimeout(fn, 1); }
45 : (fn, ms) => { timerDelays.push(ms); return setTimeout(fn, ms); };
46 // What the app's send function would do: one POST, answered with a status.
47 const send = (rows) => {
48 if (halted) return new Promise(() => {});
49 const rec = { rows: rows.map((r) => Object.assign({}, r)), status: 200, at: Date.now() };
50 rec.status = cfg.respond ? cfg.respond(posts.length, rec) : 200;
51 posts.push(rec);
52 if (cfg.reply === 'bare') return Promise.resolve(undefined);
53 if (cfg.reply === 'throw') return Promise.reject(new Error('network down'));
54 return Promise.resolve({ ok: rec.status >= 200 && rec.status < 300, status: rec.status });
55 };
56 const host = { storage, setTimeout: setT, clearTimeout, console: console_, now: () => Date.now(), document: null };
57 return {
58 store, host, posts, logged, timerDelays, send, console: console_,
59 halt: () => { halted = true; },
60 landed: () => [].concat.apply([], posts.filter((p) => p.status >= 200 && p.status < 300).map((p) => p.rows)),
61 };
62}
63
64export function makeWriter(h, over) {
65 let on = true;
66 const state = { on: () => on, set: (v) => { on = !!v; } };
67 const w = createWriter(Object.assign({
68 host: h.host,
69 keys: { outbox: 'lens-outbox', seq: 'lens-seq', postAt: 'lens-lastpost' },
70 device: () => 'devAAAA1111222233334444',
71 build: () => 'build0123456789abcdef0123456789',
72 enabled: () => on,
73 send: h.send,
74 ownPath: '/lens/post',
75 }, over || {}));
76 w.state = state;
77 return w;
78}
79
80export const parse = (rows) => rows.map((r) => JSON.parse(r.data));