import assert from "node:assert/strict"; import fs from "node:fs"; import crypto from "node:crypto"; import { parseSetup, parseTaxonomy, createSession, exportCheckpoint, readCheckpoint, sessionFromCheckpoint, } from "./queue.mjs"; const utf8 = (x) => new TextEncoder().encode(JSON.stringify(x)); const hash = (x) => crypto.createHash("sha256").update(JSON.stringify(x)).digest("hex"); const sample = fs.readFileSync( new URL("./sample.jsonl", import.meta.url), "utf8", ); const taxonomy = "bug\nquestion\nexample"; let groups = 0; const parsed = parseSetup(sample, taxonomy), s = await createSession(parsed.inventory); assert.equal(s.summary().total, 12); assert.equal(s.summary().unlabelled, 12); assert.equal(s.summary().unknown_group_records, 3); assert.equal(s.revision, 0); assert.equal(s.dirty, true); assert.equal(s.inventoryHash, hash(parsed.inventory)); assert.throws(() => { s.inventory.items[0].text = "changed"; }); groups++; s.decide("labelled", "bug"); assert.equal(s.activeId, "item-02"); s.decide("abstained"); assert.equal(s.activeId, "item-03"); assert.equal(s.summary().labelled, 1); assert.equal(s.summary().abstained, 1); assert.equal(s.summary().unlabelled, 10); assert.equal(s.revision, 2); s.select("item-01"); assert.equal(s.decide("labelled", "bug"), false); assert.equal(s.revision, 2); assert.equal(s.activeId, "item-01"); assert.equal(s.undoCount, 2); groups++; s.setFilter("abstained"); assert.equal(s.activeId, "item-02"); s.decide("labelled", "question"); assert.equal(s.filter, "all"); assert.equal(s.activeId, "item-03"); s.undo(); assert.equal(s.activeId, "item-02"); assert.equal(s.annotation("item-02").status, "abstained"); s.decide("unlabelled"); assert.equal(s.annotation("item-02").label, null); assert.equal(s.summary().reviewed, 1); groups++; const checkpoint = await exportCheckpoint(s); const { content_sha256, ...payload } = checkpoint; assert.equal(content_sha256, hash(payload)); const verified = await readCheckpoint(utf8(checkpoint)); const restored = sessionFromCheckpoint(verified); assert.deepEqual(restored.payload(), s.payload()); assert.equal(restored.undoCount, 0); assert.equal(restored.dirty, false); assert.throws(() => sessionFromCheckpoint(JSON.parse(JSON.stringify(checkpoint))), ); assert.equal(restored.markDownloaded(restored.revision + 1), false); assert.equal(restored.dirty, false); restored.decide("labelled", "example"); assert.equal(restored.dirty, true); const old = restored.revision; restored.decide("abstained"); assert.equal(restored.markDownloaded(old), false); groups++; for (const mutation of [ (x) => { x.inventory.items[0].text += " altered"; }, (x) => { x.annotations[0].label = "foreign"; }, (x) => { x.annotations.push(x.annotations[0]); }, (x) => { x.annotations.pop(); }, (x) => { x.annotations[0].id = "alien"; }, (x) => { x.annotations[0].status = "abstained"; x.annotations[0].label = "bug"; }, (x) => { x.revision = true; }, (x) => { x.extra = true; }, (x) => { x.content_sha256 = ["a".repeat(64)]; }, ]) { const bad = JSON.parse(JSON.stringify(checkpoint)); mutation(bad); await assert.rejects(() => readCheckpoint(utf8(bad))); } await assert.rejects(() => readCheckpoint(new Uint8Array([255]))); await assert.rejects(() => readCheckpoint(new Uint8Array(4 * 1048576 + 1))); groups++; const all = await createSession(parsed.inventory); for (let i = 0; i < 12; i++) all.decide("abstained"); assert.equal(all.summary().labelled, 0); assert.equal(all.summary().abstained, 12); assert.equal(all.summary().unlabelled, 0); assert.equal(all.summary().reviewed_fraction, 1); all.setFilter("unlabelled"); assert.equal(all.activeId, null); assert.deepEqual(all.visibleIds(), []); assert.equal(all.annotationJSONL().trim().split("\n").length, 12); all.setFilter("all"); all.navigate(1); assert.equal(all.activeId, "item-02"); groups++; const history = await createSession(parsed.inventory); for (let i = 0; i < 25; i++) history.decide("labelled", i % 2 ? "bug" : "question", "item-01"); assert.equal(history.undoCount, 20); for (let i = 0; i < 20; i++) assert.equal(history.undo(), true); assert.equal(history.undo(), false); assert.equal(history.revision, 45); groups++; assert.deepEqual(parseTaxonomy("\u0085bug\u001f\nquestion\n"), [ "bug", "question", ]); assert.throws(() => parseTaxonomy("bug\nbug")); assert.throws(() => parseTaxonomy("one")); assert.throws(() => parseSetup(sample + "{}\n", taxonomy)); assert.throws(() => parseSetup(" ".repeat(1048577), taxonomy)); assert.throws(() => parseSetup( JSON.stringify({ id: "bad", group: null, text: "\ud800" }), taxonomy, ), ); const proto = await createSession({ taxonomy: ["a", "b"], items: [{ id: "__proto__", group: null, text: "A😀\u2028B\ufeff" }], }); const pv = await readCheckpoint(utf8(await exportCheckpoint(proto))); assert.equal( sessionFromCheckpoint(pv).inventory.items[0].text, "A😀\u2028B\ufeff", ); groups++; const ceiling = JSON.parse(JSON.stringify(checkpoint)); ceiling.revision = 1000000000; delete ceiling.content_sha256; ceiling.content_sha256 = hash(ceiling); const capped = sessionFromCheckpoint(await readCheckpoint(utf8(ceiling))); assert.throws(() => capped.decide("labelled", "example"), /Revision limit/); assert.equal(capped.revision, 1000000000); groups++; // Real asynchronous digest snapshots must not mark newer work as downloaded. const race = await createSession(parsed.inventory), pending = exportCheckpoint(race); race.decide("labelled", "bug"); const earlier = await pending; assert.equal(earlier.revision, 0); assert.equal(earlier.annotations[0].status, "unlabelled"); assert.equal(race.markDownloaded(earlier.revision), false); assert.equal(race.dirty, true); for (const change of [ (x) => (x.annotations[0].label = "not-in-taxonomy"), (x) => (x.annotations[0].id = "alien"), (x) => x.annotations.pop(), (x) => x.annotations.push(x.annotations[0]), ]) { const bad = JSON.parse(JSON.stringify(checkpoint)); change(bad); delete bad.content_sha256; bad.content_sha256 = hash(bad); await assert.rejects(() => readCheckpoint(utf8(bad)), /annotation/i); } const rehashed = JSON.parse(JSON.stringify(checkpoint)); rehashed.inventory.items[0].text += " intentionally changed"; rehashed.inventory_sha256 = hash(rehashed.inventory); delete rehashed.content_sha256; rehashed.content_sha256 = hash(rehashed); assert.equal( sessionFromCheckpoint(await readCheckpoint(utf8(rehashed))).inventory.items[0] .text, rehashed.inventory.items[0].text, ); groups++; fs.writeFileSync( new URL("./example-checkpoint.json", import.meta.url), JSON.stringify(checkpoint, null, 2) + "\n", ); fs.writeFileSync( new URL("./example-annotations.jsonl", import.meta.url), s.annotationJSONL(), ); console.log( JSON.stringify({ status: "PASS", groups, example_summary: s.summary(), checkpoint_sha: checkpoint.content_sha256, }), );