@unito/integration-sdk
Version:
Integration SDK
35 lines (34 loc) • 1.34 kB
JavaScript
import { strict as assert } from 'node:assert';
// `{type:'break'}` and `{type:'break',children:[]}` are the same canonical
// node; integrations differ on whether they carry the empty array. Compare
// modulo that variant so conformance flags data loss, not representation.
function canonicalize(node) {
const out = { ...node };
if (out.children) {
if (out.children.length === 0) {
delete out.children;
}
else {
out.children = out.children.map(canonicalize);
}
}
return out;
}
function normalize(root) {
return { type: 'root', children: (root.children ?? []).map(canonicalize) };
}
// Asserts one canonical fixture round-trips through a field's codec —
// serialize→parse must not throw and must reproduce the original AST. When
// `divergence` names a documented loss, the fixture is instead asserted to
// STILL diverge, so a later codec fix fails this assertion until the now-stale
// divergence is removed.
export function assertConforms(field, ast, divergence) {
const got = normalize(field.module.parse(field.module.serialize(ast)));
const want = normalize(ast);
if (divergence) {
assert.notDeepEqual(got, want, `no longer diverges — remove divergence (${divergence})`);
}
else {
assert.deepEqual(got, want);
}
}