tests/sims/isolation.test.ts
The automated tests for the /simulators/isolation-levels 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.
- Each anomaly happens, waits, errors or is prevented at each level exactly as PostgreSQL's manual describes.
// /simulators/isolation-levels: anomalies under READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { SCENARIOS, run, outcome, type Level } from '../../src/lib/sims/isolation.ts';
// What PostgreSQL's manual (chapter 13, "Concurrency Control") says happens; READ UNCOMMITTED is the
// SQL Server and MySQL behaviour, since PostgreSQL runs it as READ COMMITTED.
const EXPECTED: Record<string, Record<Level, string>> = {
dirty: { RU: 'Happens', RC: 'Prevented', RR: 'Prevented', SER: 'Prevented' },
nonrepeatable: { RU: 'Happens', RC: 'Happens', RR: 'Prevented', SER: 'Prevented' },
phantom: { RU: 'Happens', RC: 'Happens', RR: 'Prevented', SER: 'Prevented' }, // PostgreSQL's REPEATABLE READ also stops phantoms
'lost-update': { RU: 'Happens', RC: 'Happens', RR: 'Error, retry', SER: 'Error, retry' }, // "could not serialize access due to concurrent update"
'atomic-update': { RU: 'Waits, then correct', RC: 'Waits, then correct', RR: 'Error, retry', SER: 'Error, retry' },
'write-skew': { RU: 'Happens', RC: 'Happens', RR: 'Happens', SER: 'Error, retry' } // only serializable snapshot isolation catches it
};
test("each anomaly happens, waits, errors or is prevented at each level exactly as PostgreSQL's manual describes", () => {
assert.deepEqual(SCENARIOS.map((s) => s.id).sort(), Object.keys(EXPECTED).sort(), 'scenario list changed: update this table');
for (const sc of SCENARIOS) for (const level of ['RU', 'RC', 'RR', 'SER'] as Level[]) {
assert.equal(outcome(sc, run(sc, level)).how, EXPECTED[sc.id][level], `${sc.id} at ${level}`);
}
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.