About: how pages are checked

tests/sims/sysboot.test.ts

The automated tests for the /simulators/linux-boot 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. Start times, readiness and failures match a second implementation on random unit graphs.
  2. An ordering cycle is reported instead of a schedule.
  3. Every example on the page parses without errors.
// /simulators/linux-boot: the systemd boot transaction.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { boot, parseUnits, type Unit, type UType } from '../../src/lib/sims/sysboot.ts';
import { BOOT_PRESETS } from '../../src/lib/sims/sysbootPresets.ts';
import { checker, lcg } from './check.ts';

/**
 * A second implementation of the same rules, done a different way (repeated relaxation instead of a
 * topological sort): systemd.unit(5) After=/Before= ordering, Wants=/Requires= pulling units into the
 * transaction, systemd.target(5)'s implicit After= on a target's dependencies, and a unit that Requires=
 * a failed unit it is ordered after failing too. Type=simple/exec units count as started at once; the
 * others when their start-up finishes.
 */
function reference(units: Unit[], goal: string, failing: string[]) {
	const by = new Map(units.map((u) => [u.name, u]));
	const tx = new Set<string>();
	const todo = [goal];
	while (todo.length) { const n = todo.pop()!; if (tx.has(n) || !by.has(n)) continue; tx.add(n); todo.push(...by.get(n)!.wants, ...by.get(n)!.requires); }
	const preds = new Map([...tx].map((n) => [n, new Set<string>()]));
	for (const n of tx) {
		const u = by.get(n)!;
		u.after.filter((a) => tx.has(a)).forEach((a) => preds.get(n)!.add(a));
		u.before.filter((b) => tx.has(b)).forEach((b) => preds.get(b)!.add(n));
	}
	for (const n of tx) {
		const u = by.get(n)!;
		if (u.type !== 'target') continue;
		for (const d of [...u.wants, ...u.requires]) {
			if (!tx.has(d)) continue;
			const o = by.get(d)!;
			if (!(u.after.includes(d) || u.before.includes(d) || o.after.includes(n) || o.before.includes(n))) preds.get(n)!.add(d);
		}
	}
	const start = new Map<string, number>(), ready = new Map<string, number>(), state = new Map<string, string>();
	for (let round = 0; round <= tx.size + 1; round++) {
		let changed = false;
		for (const n of tx) {
			const ps = [...preds.get(n)!];
			if (!ps.every((p) => ready.has(p))) continue;
			const u = by.get(n)!;
			const s = Math.max(0, ...ps.map((p) => ready.get(p)!));
			const dep = u.requires.some((r) => !by.has(r) || (preds.get(n)!.has(r) && ['failed', 'dependency'].includes(state.get(r)!)));
			const st = dep ? 'dependency' : failing.includes(n) ? 'failed' : 'active';
			const rd = dep ? s : u.type === 'simple' || u.type === 'exec' ? s : s + u.ms;
			if (start.get(n) !== s || ready.get(n) !== rd || state.get(n) !== st) { start.set(n, s); ready.set(n, rd); state.set(n, st); changed = true; }
		}
		if (!changed) break;
	}
	return { start, ready, state, cyclic: start.size < tx.size };
}

function randomUnits(r: () => number, n: number, cyclic: boolean): Unit[] {
	const types: UType[] = ['simple', 'exec', 'notify', 'oneshot', 'forking', 'target'];
	const names = Array.from({ length: n }, (_, i) => `u${i}.${i % 5 === 0 ? 'target' : 'service'}`);
	const units: Unit[] = names.map((name, i) => ({ name, ms: Math.floor(r() * 2000), type: name.endsWith('.target') ? 'target' : types[Math.floor(r() * 5)], after: [], before: [], wants: [], requires: [] }));
	if (units[0].type === 'target') units[0].ms = 0;
	for (const u of units) if (u.type === 'target') u.ms = 0;
	// ordering edges only point from higher index to lower, so the graph is acyclic unless asked otherwise
	for (let i = 1; i < n; i++) for (let j = 0; j < i; j++) {
		const x = r();
		if (x < 0.15) units[i].after.push(names[j]);
		else if (x < 0.2) units[j].before.push(names[i]);
		const y = r();
		if (y < 0.15) units[j].wants.push(names[i]);
		else if (y < 0.22) units[j].requires.push(names[i]);
	}
	if (cyclic) { units[0].after.push(names[n - 1]); units[n - 1].after.push(names[0]); units[0].wants.push(names[n - 1]); }
	return units;
}

test('start times, readiness and failures match a second implementation on random unit graphs', () => {
	const { ok, done } = checker();
	const r = lcg(11);
	let cycles = 0;
	for (let it = 0; it < 500; it++) {
		const units = randomUnits(r, 4 + Math.floor(r() * 14), false);
		const failing = units.filter(() => r() < 0.1).map((u) => u.name);
		const got = boot(units, units[0].name, failing);
		const ref = reference(units, units[0].name, failing);
		// a target's implicit After= on what it pulls in can close a real ordering cycle; both must see it
		ok((got.cycle !== null) === ref.cyclic, `cycle ${got.cycle} vs reference ${ref.cyclic}`);
		if (ref.cyclic) { cycles++; continue; }
		for (const j of got.jobs) {
			ok(j.start === ref.start.get(j.name), `${j.name} start ${j.start} vs ${ref.start.get(j.name)}`);
			ok(j.ready === ref.ready.get(j.name), `${j.name} ready ${j.ready} vs ${ref.ready.get(j.name)}`);
			ok(j.state === ref.state.get(j.name), `${j.name} state ${j.state} vs ${ref.state.get(j.name)}`);
		}
		ok(got.jobs.length === ref.start.size, `transaction size ${got.jobs.length} vs ${ref.start.size}`);
	}
	ok(cycles < 250, `${cycles} of 500 random graphs were cyclic, too few left to compare`);
	done();
});

test('an ordering cycle is reported instead of a schedule', () => {
	const r = lcg(2);
	for (let it = 0; it < 50; it++) {
		const units = randomUnits(r, 6, true);
		const got = boot(units, units[0].name);
		assert.ok(got.cycle && got.cycle.length >= 2, 'cycle not found');
		assert.equal(got.cycle![0], got.cycle![got.cycle!.length - 1]);
	}
});

test('every example on the page parses without errors', () => {
	for (const p of BOOT_PRESETS) {
		const { units, bad } = parseUnits(p.units);
		assert.deepEqual(bad, [], p.id);
		assert.ok(units.length > 0, p.id);
	}
});

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

Published