AkurAI Build
Menu

Public repository / 0216cf0f6008aae88a648a82e9b3a462a6688302

AkurAI-Mail / src/lib/webmail/recipients.test.ts

Latest change 22202d1bde69be4ace8a299dffd91f8ff24378df - Recover deployed webmail frontend (frontend parity with EC2 a6afffc) by Ólafur Búi Ólafsson

import { describe, expect, test } from "bun:test";
import { completeRecipient, currentTerm, extractEmail, matchSuggestions, mergeRecent, recipientSuggestions, splitRecipients } from "./recipients";

describe("recipients", () => {
  test("splits comma lists", () => {
    expect(splitRecipients(" a@x.is, ,b@x.is ")).toEqual(["a@x.is", "b@x.is"]);
  });

  test("puts new recent recipients first without case-insensitive duplicates, capped at 20", () => {
    expect(mergeRecent(["B@x.is"], "a@x.is, b@x.is")).toEqual(["a@x.is", "B@x.is"]);
    const many = Array.from({ length: 25 }, (_, i) => `u${i}@x.is`);
    expect(mergeRecent([], many.join(","))).toHaveLength(20);
    expect(mergeRecent(["a@x.is"], " ")).toEqual(["a@x.is"]);
  });

  test("suggests contacts then recent addresses that are not contacts", () => {
    expect(recipientSuggestions([{ name: "A", email: "a@x.is" }], ["A@x.is", "b@x.is"])).toEqual([{ name: "A", email: "a@x.is" }, { email: "b@x.is" }]);
  });

  test("matches the term being typed and skips already chosen addresses", () => {
    const pool = [{ email: "anna@x.is" }, { email: "ann@y.is" }, { email: "bob@x.is" }];
    expect(currentTerm("bob@x.is, an")).toBe("an");
    expect(matchSuggestions("ann@y.is, an", pool).map((s) => s.email)).toEqual(["anna@x.is"]);
    expect(matchSuggestions("", pool)).toEqual([]);
  });

  test("completes the current term and leaves room for the next", () => {
    expect(completeRecipient("bob@x.is, an", "anna@x.is")).toBe("bob@x.is, anna@x.is, ");
  });

  test("extracts the bare address", () => {
    expect(extractEmail(`"Anna" <Anna@X.is>`)).toBe("anna@x.is");
    expect(extractEmail("nobody")).toBe("");
  });
});