About: how pages are checked

tests/sims/chaos.test.ts

The automated tests for the /simulators/chaos-playground simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.

  1. Independent retries: a call with error rate p and r retries fails with probability p^(r+1).
  2. A blackholed dependency costs timeout × (retries + 1).
  3. Latencies are log-normal with the configured median and 99th percentile.
  4. With no fault, the experiment and control groups agree.
  5. The abort stops an experiment that hurts more than the threshold, and the fix makes it pass.
// /simulators/chaos-playground: a fault injected into a dependency, experiment group against control.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { call, experiment, defaults, DEFAULT_DEPS, type Dep } from '../../src/lib/sims/chaos.ts';

function rng(seed: number) {
	let s = seed >>> 0;
	const u = () => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return (s + 0.5) / 4294967296; };
	const g = () => Math.sqrt(-2 * Math.log(u())) * Math.cos(2 * Math.PI * u());
	return { u, g };
}

test('independent retries: a call with error rate p and r retries fails with probability p^(r+1)', () => {
	const R = rng(5);
	const d: Dep = { ...DEFAULT_DEPS.auth, err: 0, timeout: 1e9, retries: 2 };
	const N = 200000;
	let fail = 0;
	for (let i = 0; i < N; i++) if (!call(d, { target: 'auth', kind: 'error', latency: 0, error: 0.3 }, R).ok) fail++;
	const want = 0.3 ** 3, sd = Math.sqrt((want * (1 - want)) / N);
	assert.ok(Math.abs(fail / N - want) < 5 * sd, `${fail / N} vs ${want}`);
});

test('a blackholed dependency costs timeout × (retries + 1)', () => {
	const r = call({ ...DEFAULT_DEPS.auth, timeout: 250, retries: 2 }, { target: 'auth', kind: 'blackhole', latency: 0, error: 0 }, rng(1));
	assert.equal(r.ok, false);
	assert.equal(r.time, 750);
	assert.equal(r.cause, 'timeout');
});

test('latencies are log-normal with the configured median and 99th percentile', () => {
	const R = rng(9);
	const d = { ...DEFAULT_DEPS.catalog, err: 0, timeout: 1e9 };
	const N = 200000;
	const ts = Array.from({ length: N }, () => call(d, null, R).time).sort((a, b) => a - b);
	assert.ok(Math.abs(ts[N / 2] / d.median - 1) < 0.02, `median ${ts[N / 2]} vs ${d.median}`);
	assert.ok(Math.abs(ts[Math.floor(N * 0.99)] / d.p99 - 1) < 0.05, `p99 ${ts[Math.floor(N * 0.99)]} vs ${d.p99}`);
});

test('with no fault, the experiment and control groups agree', () => {
	const p = defaults();
	p.fault = { target: 'recs', kind: 'latency', latency: 0, error: 0 };
	p.abort = false;
	p.blast = 0.5;
	const r = experiment(p);
	assert.ok(Math.abs(r.exp.success - r.ctl.success) < 0.01, `${r.exp.success} vs ${r.ctl.success}`);
	assert.equal(r.aborted, false);
});

test('the abort stops an experiment that hurts more than the threshold, and the fix makes it pass', () => {
	const bad = experiment(defaults());
	assert.equal(bad.aborted, true);
	const p = defaults();
	p.deps.recs.timeout = 250;
	p.deps.recs.fallback = true;
	const fixed = experiment(p);
	assert.equal(fixed.aborted, false);
	assert.equal(fixed.holds, true);
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published