import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { validateSignup } = await load();const TODAY = "2024-06-15";const run = (form, today = TODAY) => { const r = h.res(() => validateSignup(form, today)); return r.ok ? { ok: true, value: h.shape(r.value, ["referralCode"]) } : r;};const invalid = (...pairs) => ({ ok: false, error: { kind: "invalid", errors: pairs.map(([field, code]) => ({ field, code })) } });const base = () => ({ email: "bob@example.com", password: "hunter2hunter", confirmPassword: "hunter2hunter", birthDate: "2000-01-31" });const with_ = (patch) => ({ ...base(), ...patch });test("valid signup", () => { assert.deepEqual(run(with_({ email: " Bob@Example.COM ", referralCode: " ABC-1234 ", extra: 1 })), { ok: true, value: { email: "bob@example.com", password: "hunter2hunter", birthDate: "2000-01-31", referralCode: { some: true, value: "ABC-1234" } }, });});test("referral code absent, null or blank", () => { for (const referralCode of [undefined, null, "", " "]) { const form = referralCode === undefined ? base() : with_({ referralCode }); assert.deepEqual(h.okOf(run(form)).referralCode, { some: false }, String(referralCode)); }});test("empty form reports every required field", () => { assert.deepEqual(run({}), invalid(["email", "required"], ["password", "required"], ["birthDate", "required"]));});test("null and empty values count as missing", () => { assert.deepEqual( run({ email: null, password: null, confirmPassword: null, birthDate: null, referralCode: null }), invalid(["email", "required"], ["password", "required"], ["birthDate", "required"]), ); assert.deepEqual(run(with_({ email: " ", password: "", birthDate: "" })), invalid(["email", "required"], ["password", "required"], ["birthDate", "required"]));});test("non-string values are format errors", () => { assert.deepEqual( run({ email: 0, password: 12345678901, confirmPassword: 12345678901, birthDate: 20000101, referralCode: 0 }), invalid(["email", "format"], ["password", "format"], ["birthDate", "format"], ["referralCode", "format"]), ); assert.deepEqual(run(with_({ email: false, referralCode: false })), invalid(["email", "format"], ["referralCode", "format"]));});test("email format", () => { for (const email of ["bob", "bob@example", "@example.com", "bob@@example.com", "b ob@example.com", "bob@exa mple.com"]) { assert.deepEqual(run(with_({ email })), invalid(["email", "format"]), email); }});test("password rules, one error per field", () => { assert.deepEqual(run(with_({ password: "short1", confirmPassword: "short1" })), invalid(["password", "too_short"])); assert.deepEqual(run(with_({ password: "abc", confirmPassword: "abc" })), invalid(["password", "too_short"])); assert.deepEqual(run(with_({ password: "abcdefghij", confirmPassword: "abcdefghij" })), invalid(["password", "no_digit"])); assert.deepEqual(run(with_({ password: "abcdefgh1", confirmPassword: "abcdefgh1" })), invalid(["password", "too_short"])); assert.equal(h.okOf(run(with_({ password: "abcdefghi1", confirmPassword: "abcdefghi1" }))).password, "abcdefghi1");});test("password is not trimmed", () => { assert.equal(h.okOf(run(with_({ password: " abcdefgh1 ", confirmPassword: " abcdefgh1 " }))).password, " abcdefgh1 "); assert.deepEqual(run(with_({ password: " abcdefg1", confirmPassword: "abcdefg1" })), invalid(["confirmPassword", "mismatch"]));});test("confirmPassword", () => { assert.deepEqual(run(with_({ confirmPassword: "hunter2hunteR" })), invalid(["confirmPassword", "mismatch"])); const { confirmPassword: _c, ...noConfirm } = base(); assert.deepEqual(run(noConfirm), invalid(["confirmPassword", "mismatch"])); assert.deepEqual(run(with_({ password: "short1", confirmPassword: "short2" })), invalid(["password", "too_short"], ["confirmPassword", "mismatch"])); assert.deepEqual(run(with_({ password: "", confirmPassword: "x" })), invalid(["password", "required"]));});test("birthDate format", () => { for (const birthDate of ["2000-02-30", "2001-02-29", "2000-1-31", "31/01/2000", "2000-01-31T00:00:00Z", " 2000-01-31", "2000-13-01", "2000-00-10"]) { assert.deepEqual(run(with_({ birthDate })), invalid(["birthDate", "format"]), birthDate); } assert.equal(h.okOf(run(with_({ birthDate: "2000-02-29" }))).birthDate, "2000-02-29");});test("turns 13 exactly on the birthday", () => { assert.equal(h.okOf(run(with_({ birthDate: "2011-06-15" }))).birthDate, "2011-06-15"); assert.deepEqual(run(with_({ birthDate: "2011-06-16" })), invalid(["birthDate", "too_young"])); assert.deepEqual(run(with_({ birthDate: "2011-12-01" })), invalid(["birthDate", "too_young"])); assert.deepEqual(run(with_({ birthDate: "2030-01-01" })), invalid(["birthDate", "too_young"])); assert.equal(h.okOf(run(with_({ birthDate: "2010-12-31" }))).birthDate, "2010-12-31");});test("February 29 birthdays", () => { assert.deepEqual(run(with_({ birthDate: "2008-02-29" }), "2021-02-28"), invalid(["birthDate", "too_young"])); assert.equal(h.okOf(run(with_({ birthDate: "2008-02-29" }), "2021-03-01")).birthDate, "2008-02-29"); assert.deepEqual(run(with_({ birthDate: "2008-03-01" }), "2021-02-28"), invalid(["birthDate", "too_young"]));});test("referral code format", () => { for (const referralCode of ["abc-1234", "ABC-123", "ABCD-1234", "ABC1234", "AB-12345"]) { assert.deepEqual(run(with_({ referralCode })), invalid(["referralCode", "format"]), referralCode); }});test("all errors together, in field order, and form not modified", () => { const form = { email: "x", password: "abcdefghijk", confirmPassword: "nope", birthDate: "2020-01-01", referralCode: "zzz" }; const snapshot = { ...form }; assert.deepEqual( run(form), invalid(["email", "format"], ["password", "no_digit"], ["confirmPassword", "mismatch"], ["birthDate", "too_young"], ["referralCode", "format"]), ); assert.deepEqual(form, snapshot);});