readable-demo / kit /testing /parity.js
akagabi's picture
Upload folder using huggingface_hub
f5d2194 verified
Raw
History Blame Contribute Delete
1.21 kB
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
export function loadCorpus(path) {
return JSON.parse(readFileSync(path, "utf8"));
}
export function defineParityTests({ name = "applier", applier, corpusPath, records, expectedCount }) {
const applyTags = typeof applier === "function" ? applier : applier && applier.applyTags;
if (typeof applyTags !== "function") {
throw new Error("applier must be a function or a module exporting applyTags");
}
const list = records ?? loadCorpus(corpusPath);
if (expectedCount != null) {
test(`${name}: parity corpus has the full record set`, () => {
assert.equal(list.length, expectedCount);
});
}
test(`${name}: reconstructs every parity record exactly`, () => {
assert.ok(list.length > 0, "parity corpus is empty");
for (const [index, record] of list.entries()) {
const { text, spans } = applyTags(record.tokens, record.tags);
assert.equal(text, record.expected_text, `record ${index}: text mismatch`);
if (record.expected_spans) {
assert.deepEqual(spans, record.expected_spans, `record ${index}: span mismatch`);
}
}
});
}