tests/sims/serial.test.ts
The automated tests for the /simulators/json-vs-protobuf 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.
- Every record in all three datasets encodes to exactly the bytes Google's protobuf runtime writes.
- Bytes written with the v2 schema decode under every other version to the same fields and the same number of unknown fields as the official runtime.
- MessagePack output matches the msgpack Python library byte for byte.
- The encoding guide example: field 1 set to 150 is the three bytes 08 96 01.
// /simulators/json-vs-protobuf: Protocol Buffers and MessagePack bytes, and reading old data with a new schema.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { pbEncode, pbDecode, msgpack, toJsonObj, DATASETS, VERSIONS, EVOLVE_USER } from '../../src/lib/sims/serial.ts';
const REF = fileURLToPath(new URL('./protobuf_ref.py', import.meta.url));
const has = (mod: string) => { try { execFileSync('python3', ['-c', `import ${mod}`], { stdio: 'ignore' }); return true; } catch { return false; } };
const pb = has('google.protobuf'), mp = has('msgpack');
const hex = (b: number[] | Uint8Array) => Buffer.from(b).toString('hex');
const ref = (mode: 'encode' | 'decode', cases: unknown[]) => JSON.parse(execFileSync('python3', [REF], { input: JSON.stringify({ mode, cases }), maxBuffer: 1 << 26 }).toString());
test("every record in all three datasets encodes to exactly the bytes Google's protobuf runtime writes", { skip: !pb && 'python protobuf is not installed' }, () => {
const cases = DATASETS.flatMap((d) => Array.from({ length: 40 }, (_, i) => ({ schema: d.msg, obj: d.make(i) })));
const want = ref('encode', cases);
cases.forEach((c, i) => assert.equal(hex(pbEncode(c.schema, c.obj as Record<string, unknown>)), want[i], `${c.schema.name} record ${i % 40}`));
});
test('bytes written with the v2 schema decode under every other version to the same fields and the same number of unknown fields as the official runtime', { skip: !pb && 'python protobuf is not installed' }, () => {
const v2 = VERSIONS.find((v) => v.id === 'v2')!;
const bytes = hex(pbEncode(v2.msg, EVOLVE_USER));
const want = ref('decode', VERSIONS.map((v) => ({ schema: v.msg, hex: bytes })));
VERSIONS.forEach((v, i) => {
const d = pbDecode(v.msg, Buffer.from(bytes, 'hex'));
const value = Object.fromEntries(Object.entries(d.value).map(([k, x]) => [k, typeof x === 'bigint' ? Number(x) : x]));
assert.deepEqual(value, want[i].value, `${v.id}: decoded fields`);
assert.equal(d.unknown.length, want[i].unknown, `${v.id}: unknown fields`);
});
});
test('MessagePack output matches the msgpack Python library byte for byte', { skip: !mp && 'python msgpack is not installed' }, () => {
const objs = DATASETS.flatMap((d) => Array.from({ length: 20 }, (_, i) => toJsonObj(d.msg, d.make(i) as Record<string, unknown>)));
const want = JSON.parse(execFileSync('python3', ['-c', 'import json, sys, msgpack\nprint(json.dumps([msgpack.packb(o).hex() for o in json.loads(sys.stdin.read())]))'], { input: JSON.stringify(objs) }).toString());
objs.forEach((o, i) => assert.equal(hex(msgpack(o)), want[i], `record ${i}`));
});
test('the encoding guide example: field 1 set to 150 is the three bytes 08 96 01', () => {
assert.equal(hex(pbEncode({ name: 'Test1', fields: [{ no: 1, name: 'a', type: 'int32' }] }, { a: 150 })), '089601');
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.