// Random choices, from one seed. Every draw of a run comes from here, in the // order the steps make them, so a seed gives the same requests every time. import { SEED } from './args.mjs' let state = SEED >>> 0 export const rand = () => { // mulberry32 state = (state + 0x6d2b79f5) >>> 0 let t = state t = Math.imul(t ^ (t >>> 15), t | 1) t ^= t + Math.imul(t ^ (t >>> 7), t | 61) return ((t ^ (t >>> 14)) >>> 0) / 4294967296 } export const below = (n) => Math.floor(rand() * n) export const chance = (p) => rand() < p export const pick = (xs) => xs[below(xs.length)] export const subset = (xs, p) => xs.filter(() => chance(p)) export const COLLECTIONS = ['com.example.note', 'com.example.todo', 'com.example.test.item'] export const RKEYS = ['a', 'b', 'c', 'ab', 'abc', 'b1', 'b2', 'x-y', 'z~z', 'self', 'a.b', 'k_9', 'Ab'] const BAD_RKEYS = ['.', '..', 'a/b', 'a b', '#x', 'x'.repeat(600)] export const word = () => pick(['hi', 'héllo', 'note', '', 'a longer string with spaces', '日本語', '🙂']) export function value(depth = 0) { const r = below(depth > 1 ? 4 : 6) if (r === 0) return word() if (r === 1) return below(2000) - 1000 if (r === 2) return chance(0.5) if (r === 3) return null if (r === 4) return Array.from({ length: below(4) }, () => value(depth + 1)) return Object.fromEntries(Array.from({ length: below(4) }, () => [pick(['k', 'title', 'n', 'zz', 'a1']), value(depth + 1)])) } /// A record for `collection`, usually valid, sometimes not. export function record(collection, known) { const rec = { $type: collection, text: word(), n: below(100), tags: subset(['x', 'y', 'z'], 0.5) } if (chance(0.3)) rec.extra = value() if (chance(0.15) && known.length) rec.ref = { $link: pick(known) } if (chance(0.1)) rec.blob = { $bytes: pick(['aGVsbG8', '', 'AAEC/w']) } const r = rand() if (r < 0.03) delete rec.$type else if (r < 0.06) rec.$type = pick(COLLECTIONS.filter((c) => c !== collection)) else if (r < 0.09) rec.ratio = 1.5 return rec } /// A record valid for `collection`. export const goodRecord = (collection) => ({ $type: collection, text: word(), n: below(100), ...(chance(0.3) ? { extra: value() } : {}) }) export const rkey = () => (chance(0.05) ? pick(BAD_RKEYS) : pick(RKEYS))