// Run: node --test transition_dates.test.js (Node 18+) const test = require("node:test"); const assert = require("node:assert/strict"); const { LABEL, FLAG, gracePeriodEnd, gracePeriodReport, capGapEnd, capGapEligibility } = require("./transition_dates.js"); test("grace period: 60 days counted from the day after", () => { assert.equal(gracePeriodEnd("2026-03-15", "2028-09-30"), "2026-05-14"); }); test("grace period: validity end comes first", () => { assert.equal(gracePeriodEnd("2026-03-15", "2026-04-30"), "2026-04-30"); }); test("grace period: crosses the year boundary", () => { assert.equal(gracePeriodEnd("2026-12-20", "2029-01-01"), "2027-02-18"); }); test("grace period: bad input rejected", () => { assert.throws(() => gracePeriodEnd("2026-03-15", "2026-03-01")); assert.throws(() => gracePeriodEnd("2026-02-30", "2028-09-30")); assert.throws(() => gracePeriodEnd("03/15/2026", "2028-09-30")); }); test("grace report: paid date differs -> both dates and flag", () => { const r = gracePeriodReport("2026-03-15", "2028-09-30", "2026-03-31"); assert.equal(r.byLastDayWorked, "2026-05-14"); assert.equal(r.byLastDayPaid, "2026-05-30"); assert.equal(r.flag, FLAG); assert.equal(r.label, LABEL); }); test("grace report: paid date same -> no flag", () => { const r = gracePeriodReport("2026-03-15", "2028-09-30", "2026-03-15"); assert.equal(r.flag, undefined); assert.equal(r.byLastDayPaid, undefined); assert.equal(r.label, LABEL); }); test("grace report: pay past validity end stops at validity end", () => { assert.equal(gracePeriodReport("2026-03-15", "2026-04-30", "2026-05-15").byLastDayPaid, "2026-04-30"); }); test("cap-gap end", () => { assert.equal(capGapEnd(2027, "2026-10-01"), "2026-10-01"); assert.equal(capGapEnd(2027, null), "2027-04-01"); assert.equal(capGapEnd(2027), "2027-04-01"); assert.equal(capGapEnd(2027, "2026-11-15"), "2026-11-15"); }); const base = { programEnd: "2025-05-15", changeOfStatus: true, capSubject: true }; test("eligibility: filed during OPT -> status and work", () => { const r = capGapEligibility({ ...base, filedOn: "2026-04-03", optEadEnd: "2026-06-30" }); assert.equal(r.statusExtended, true); assert.equal(r.workExtended, true); assert.equal(r.label, LABEL); }); test("eligibility: filed in F-1 grace period -> status only", () => { const r = capGapEligibility({ ...base, filedOn: "2026-04-03", optEadEnd: "2026-03-20" }); assert.equal(r.statusExtended, true); assert.equal(r.workExtended, false); assert.match(r.reason, /2026-05-19/); }); test("eligibility: last day of grace period -> status only", () => { const r = capGapEligibility({ ...base, filedOn: "2026-05-19", optEadEnd: "2026-03-20" }); assert.equal(r.statusExtended, true); assert.equal(r.workExtended, false); }); test("eligibility: filed after grace period -> neither", () => { const r = capGapEligibility({ ...base, filedOn: "2026-05-20", optEadEnd: "2026-03-20" }); assert.equal(r.statusExtended, false); assert.equal(r.workExtended, false); }); test("eligibility: filed on EAD end date -> work", () => { const r = capGapEligibility({ ...base, filedOn: "2026-06-30", optEadEnd: "2026-06-30" }); assert.equal(r.workExtended, true); }); test("eligibility: consular processing -> neither", () => { const r = capGapEligibility({ ...base, filedOn: "2026-04-03", optEadEnd: "2026-06-30", changeOfStatus: false }); assert.equal(r.statusExtended, false); assert.equal(r.workExtended, false); assert.equal(r.reason, "consular processing is not cap-gap eligible"); }); test("eligibility: cap-exempt -> neither", () => { const r = capGapEligibility({ ...base, filedOn: "2026-04-03", optEadEnd: "2026-06-30", capSubject: false }); assert.equal(r.statusExtended, false); assert.equal(r.reason, "cap-exempt petitions are not cap-gap eligible"); }); test("eligibility: no OPT, filed during study -> status only", () => { const r = capGapEligibility({ ...base, filedOn: "2026-04-03", optEadEnd: null, programEnd: "2026-05-15" }); assert.equal(r.statusExtended, true); assert.equal(r.workExtended, false); }); test("flag quotes only the official page", () => { assert.ok(FLAG.includes("last day for which a salary or wage is paid")); assert.ok(!FLAG.includes("attorneys read")); }); test("no OPT: filed during F-1 grace gives status only", () => { const r = capGapEligibility({ filedOn: "2026-06-01", optEadEnd: null, programEnd: "2026-05-15", changeOfStatus: true, capSubject: true }); assert.equal(r.statusExtended, true); assert.equal(r.workExtended, false); });