g1t/apps/web/app/lib/emails.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { AccountEmail } from "@g1t/contracts"; |
| 5 | |
| 6 | import { addressActions, backupChoices, pendingFields } from "./emails.ts"; |
| 7 | |
| 8 | const email = (address: string, verified: boolean, primary = false): AccountEmail => ({ |
| 9 | email: address, |
| 10 | verified, |
| 11 | primary, |
| 12 | backup: false, |
| 13 | createdAt: "2026-10-05T00:00:00.000Z", |
| 14 | verifiedAt: verified ? "2026-10-05T00:00:00.000Z" : null, |
| 15 | }); |
| 16 | |
| 17 | test("the primary cannot be removed, and only a confirmed address can become it", () => { |
| 18 | const all = [email("a@x.io", true, true), email("b@x.io", true), email("c@x.io", false)]; |
| 19 | assert.deepEqual(addressActions(all[0]!, all), { makePrimary: false, remove: false, resend: false }); |
| 20 | assert.deepEqual(addressActions(all[1]!, all), { makePrimary: true, remove: true, resend: false }); |
| 21 | assert.deepEqual(addressActions(all[2]!, all), { makePrimary: false, remove: true, resend: true }); |
| 22 | }); |
| 23 | |
| 24 | test("the last confirmed address stays, even when it is not the primary", () => { |
| 25 | const all = [email("a@x.io", false, true), email("b@x.io", true)]; |
| 26 | assert.equal(addressActions(all[1]!, all).remove, false); |
| 27 | }); |
| 28 | |
| 29 | test("the backup is a confirmed address other than the primary", () => { |
| 30 | const all = [email("a@x.io", true, true), email("b@x.io", true), email("c@x.io", false)]; |
| 31 | assert.deepEqual(backupChoices(all).map((choice) => choice.email), ["b@x.io"]); |
| 32 | }); |
| 33 | |
| 34 | test("a change waiting on the password keeps its fields but never the password", () => { |
| 35 | const form = new FormData(); |
| 36 | form.set("intent", "remove-email"); |
| 37 | form.set("email", "b@x.io"); |
| 38 | form.set("password", "hunter22hunter22"); |
| 39 | assert.deepEqual(pendingFields(form), { email: "b@x.io" }); |
| 40 | }); |