Spaces:
Running
Running
Download tests/engine.test.mjs from sumankrsh/what-changed: direct link, hf CLI and curl.
- Browser
- Download file 7.27 kB
-
https://huggingface.co/spaces/sumankrsh/what-changed/resolve/main/tests/engine.test.mjs
- Command line
-
hf download hf://spaces/sumankrsh/what-changed/tests/engine.test.mjs
-
curl -L -o engine.test.mjs https://huggingface.co/spaces/sumankrsh/what-changed/resolve/main/tests/engine.test.mjs
7.27 kB
| // Unit tests for the comparison engine. Run: node --test tests/ | |
| // Built by Suman Mukherjee. Apache-2.0. | |
| import test from "node:test"; | |
| import assert from "node:assert/strict"; | |
| import { compare, extractFacts, segment, parseNumWords, exportText, docContext } from "../engine.js"; | |
| import { SAMPLES } from "../samples.js"; | |
| const facts = (t, type) => extractFacts(t, docContext(t, t)).filter((f) => !type || f.type === type); | |
| const titles = (b, a) => compare(b, a).items.map((i) => `[${i.cat}] ${i.title}`); | |
| test("Indian and international money formats", () => { | |
| const m = facts("Rent ₹25,000, deposit Rs. 1,00,000/-, CTC INR 18,00,000, bonus ₹ 1.5 lakh, cover Rs 2 crore, fee $12,000, cap USD 1.2 million, rate €950 and 45,000 rupees.", "Money"); | |
| assert.deepEqual(m.map((f) => f.value), [25000, 100000, 1800000, 150000, 20000000, 12000, 1200000, 950, 45000]); | |
| assert.deepEqual(m.map((f) => f.currency), ["INR", "INR", "INR", "INR", "INR", "USD", "USD", "EUR", "INR"]); | |
| assert.equal(m[1].text, "Rs. 1,00,000"); | |
| }); | |
| test("percent change is computed for money in the same currency", () => { | |
| const t = titles("The Tenant shall pay a monthly rent of ₹25,000 each month.", "The Tenant shall pay a monthly rent of ₹27,500 each month."); | |
| assert.deepEqual(t, ["[Money] Monthly rent: ₹25,000 → ₹27,500 (+10%)"]); | |
| }); | |
| test("the same amount written differently is not a change that matters", () => { | |
| assert.deepEqual(titles("The deposit is Rs. 50,000 in total.", "The deposit is ₹50,000 in total."), []); | |
| assert.deepEqual(titles("Refund within thirty (30) days.", "Refund within 30 days."), []); | |
| }); | |
| test("durations in words, digits and both", () => { | |
| const d = facts("for thirty (30) days, then 2 months, one year, a 12-month lock-in, 11 (eleven) months, ninety days' notice and 4 business hours", "Duration"); | |
| assert.deepEqual(d.map((f) => f.norm), ["30 day", "2 month", "1 year", "12 month", "11 month", "90 day", "4 business hour"]); | |
| assert.equal(parseNumWords("one hundred and eighty"), 180); | |
| assert.equal(parseNumWords("forty-five"), 45); | |
| }); | |
| test("notice period change reads in plain words", () => { | |
| const t = titles("Either party may end this agreement by giving one (1) month's notice.", "Either party may end this agreement by giving two (2) months' notice."); | |
| assert.deepEqual(t, ["[Duration] Notice period: one (1) month → two (2) months (longer)"]); | |
| }); | |
| test("ambiguous numeric dates are flagged, not guessed", () => { | |
| const [a] = facts("Starts on 05/01/2027.", "Date"); | |
| assert.equal(a.ambiguous, true); | |
| assert.match(a.note, /5 January or 1 May/); | |
| const [b] = facts("Starts on 25/01/2027.", "Date"); | |
| assert.equal(b.ambiguous, false); | |
| assert.equal(b.norm, "2027-1-25"); | |
| const t = titles("The term starts on 05/01/2027.", "The term starts on 06/01/2027."); | |
| assert.match(t[0], /05\/01\/2027 → 06\/01\/2027 \(ambiguous date format/); | |
| }); | |
| test("written dates and recurring due days", () => { | |
| const d = facts("Join on 1 December 2026, sign by January 15, 2027, review 2027-03-01, pay on or before the 5th day of each month.", "Date"); | |
| assert.deepEqual(d.map((f) => f.norm), ["2026-12-1", "2027-1-15", "2027-3-1", "dom:5"]); | |
| assert.deepEqual(titles("Rent is due on the 5th day of each month.", "Rent is due on the 1st day of each month."), ["[Date] Date: 5th day of each month → 1st day of each month (earlier)"]); | |
| }); | |
| test("obligation strength: may to shall not is reported, will to shall is not", () => { | |
| const t = titles("6.1 The Tenant may sublet a part of the Premises.", "6.1 The Tenant shall not sublet any part of the Premises."); | |
| assert.equal(t.length, 1); | |
| assert.match(t[0], /^\[Obligation\] Obligation wording: “The Tenant may sublet/); | |
| assert.deepEqual(titles("The rent will be paid by bank transfer.", "The rent shall be paid by bank transfer."), []); | |
| assert.match(titles("The deposit is refundable on exit.", "The deposit is non-refundable on exit.")[0], /^\[Obligation\]/); | |
| }); | |
| test("names and parties", () => { | |
| assert.deepEqual(titles("Reporting\nYou will report to Kavya Menon, Head of Analytics.\n\nLeave\nYou get leave.", "Reporting\nYou will report to Arjun Thakkar, Head of Analytics.\n\nLeave\nYou get leave."), ["[Party] Reporting: Kavya Menon → Arjun Thakkar"]); | |
| // a capitalised ordinary word at the start of a sentence is not a name | |
| assert.deepEqual(titles("Subject: Offer of employment", "Subject: Revised offer of employment"), []); | |
| }); | |
| test("clauses added, removed and moved", () => { | |
| const b = "1. RENT\nThe rent is ₹10,000.\n\n2. LOCK-IN\nNeither party may end this agreement in the first six (6) months.\n\n3. PETS\nPets are allowed with consent.\n\n4. LAW\nIndian law applies to this agreement."; | |
| const a = "1. RENT\nThe rent is ₹10,000.\n\n2. LAW\nIndian law applies to this agreement.\n\n3. PETS\nPets are allowed with consent.\n\n4. PAINTING\nThe Tenant shall pay for painting when leaving."; | |
| const r = compare(b, a); | |
| const cats = r.items.map((i) => i.cat).sort(); | |
| assert.deepEqual(cats, ["Clause added", "Clause removed"]); | |
| assert.equal(r.stats.moved, 2); // the heading and its paragraph | |
| }); | |
| test("identical and empty inputs", () => { | |
| const s = SAMPLES.rent.before; | |
| assert.equal(compare(s, s).items.length, 0); | |
| assert.equal(compare("", "").items.length, 0); | |
| assert.equal(segment("").length, 0); | |
| }); | |
| test("built-in samples give the planted changes", () => { | |
| const rent = compare(SAMPLES.rent.before, SAMPLES.rent.after).items.map((i) => i.title); | |
| assert.equal(rent.length, 15); | |
| assert.ok(rent.includes("Monthly rent: ₹25,000 → ₹27,500 (+10%)")); | |
| assert.ok(rent.includes("Security deposit: ₹75,000 → ₹1,00,000 (+33.3%)")); | |
| const offer = compare(SAMPLES.offer.before, SAMPLES.offer.after).items.map((i) => i.title); | |
| assert.equal(offer.length, 15); | |
| assert.ok(offer.includes("CTC: INR 18,00,000 → INR 16,50,000 (−8.3%)")); | |
| }); | |
| test("hard-wrapped text gives the same result as unwrapped text", () => { | |
| const wrap = (t, w) => t.split("\n").map((line) => { const out = []; let cur = ""; for (const x of line.split(" ")) { if ((cur + " " + x).trim().length > w) { out.push(cur.trim()); cur = x; } else cur += " " + x; } out.push(cur.trim()); return out.join("\n"); }).join("\n"); | |
| const s = SAMPLES.rent; | |
| const base = compare(s.before, s.after).items.map((i) => i.title); | |
| assert.deepEqual(compare(wrap(s.before, 58), s.after).items.map((i) => i.title), base); | |
| }); | |
| test("export as text and Markdown includes the caution", () => { | |
| const items = compare(SAMPLES.rent.before, SAMPLES.rent.after).items; | |
| const txt = exportText(items, { before: "a.txt", after: "b.txt", date: "14 Nov 2026" }); | |
| assert.match(txt, /15 changes that matter/); | |
| assert.match(txt, /does not tell you whether a change is fair or legal/); | |
| const md = exportText(items, {}, true); | |
| assert.match(md, /^## What changed/); | |
| assert.match(md, /\*\*Money\*\*/); | |
| }); | |
| test("long documents stay fast", () => { | |
| const s = SAMPLES.rent; | |
| const many = (t) => Array.from({ length: 150 }, (_, i) => t.replace(/Flat 302/g, `Flat ${1000 + i}`)).join("\n\n"); | |
| const t0 = Date.now(); | |
| const r = compare(many(s.before), many(s.after)); | |
| assert.ok(r.stats.beforeWords > 45000); | |
| assert.ok(Date.now() - t0 < 5000); | |
| }); | |