Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

12.4 KB
Raw Blame
const test = require("node:test");
const assert = require("node:assert/strict");

const load = () => import("../../../src/helpers/connectors/emailCompose.js");

const TRICKY = {
  to: ["[email protected]", "[email protected]"],
  cc: ["o'[email protected]"],
  subject: "Lunch 🍜 & plans #1 — 50% off?",
  body: "Hi Gabe,\n\nمرحبا — a+b=c & 100% sure? #yes\nSee you!",
};

test("web targets round-trip every field exactly", async () => {
  const { buildComposeRequest } = await load();
  const expected = {
    gmail: { host: "mail.google.com", subjectKey: "su" },
    outlookWork: { host: "outlook.office.com", subjectKey: "subject" },
    outlookPersonal: { host: "outlook.live.com", subjectKey: "subject" },
  };
  for (const [target, { host, subjectKey }] of Object.entries(expected)) {
    const { url, clipboardText } = buildComposeRequest({ target, ...TRICKY });
    const parsed = new URL(url);
    assert.equal(parsed.host, host, target);
    assert.equal(parsed.searchParams.get("to"), TRICKY.to.join(","), target);
    assert.equal(parsed.searchParams.get("cc"), TRICKY.cc.join(","), target);
    assert.equal(parsed.searchParams.get(subjectKey), TRICKY.subject, target);
    assert.equal(parsed.searchParams.get("body"), TRICKY.body, target);
    assert.equal(clipboardText, null, target);
  }
});

test("mailto keeps addresses readable and uses CRLF line breaks", async () => {
  const { buildComposeRequest } = await load();
  const { url } = buildComposeRequest({ target: "mailto", ...TRICKY });
  const parsed = new URL(url);
  assert.equal(parsed.protocol, "mailto:");
  assert.equal(decodeURIComponent(parsed.pathname), TRICKY.to.join(","));
  assert.equal(parsed.searchParams.get("subject"), TRICKY.subject);
  assert.equal(parsed.searchParams.get("body"), TRICKY.body.replace(/\n/g, "\r\n"));
  assert.match(url, /^mailto:gabe%2Blunch@example\.com,dana@example\.org\?/);
});

test("mailto turns every kind of line break into CRLF and keeps the subject on one line", async () => {
  const { buildComposeRequest } = await load();
  const { url } = buildComposeRequest({
    target: "mailto",
    to: ["[email protected]"],
    subject: "Hi\r\nBcc: [email protected]",
    body: "one\rtwo\nthree\r\nfour",
  });
  const parsed = new URL(url);
  assert.equal(parsed.searchParams.get("subject"), "Hi Bcc: [email protected]");
  assert.equal(parsed.searchParams.get("body"), "one\r\ntwo\r\nthree\r\nfour");
});

test("empty fields are left out of the URL", async () => {
  const { buildComposeRequest } = await load();
  const { url } = buildComposeRequest({ target: "outlookWork", to: ["[email protected]"] });
  assert.equal(url, "https://outlook.office.com/mail/deeplink/[email protected]");
});

test("a body that would push the URL past the limit moves to the clipboard", async () => {
  const { buildComposeRequest } = await load();
  const body = "é".repeat(400);
  const { url, clipboardText, bodyCopied } = buildComposeRequest({
    target: "gmail",
    to: ["[email protected]"],
    subject: "Notes",
    body,
  });
  assert.equal(clipboardText, body);
  assert.equal(bodyCopied, true);
  assert.equal(new URL(url).searchParams.get("body"), null);
  assert.equal(new URL(url).searchParams.get("su"), "Notes");
  assert.ok(url.length <= 2000);
});

test("a body that fits stays in the URL", async () => {
  const { buildComposeRequest } = await load();
  const body = "x".repeat(1500);
  const { url, clipboardText } = buildComposeRequest({
    target: "mailto",
    to: ["[email protected]"],
    body,
  });
  assert.equal(clipboardText, null);
  assert.equal(new URL(url).searchParams.get("body"), body);
});

test("a subject too long for any link moves to the clipboard, even with no body", async () => {
  const { buildComposeRequest } = await load();
  const subject = "Quarterly planning ".repeat(120).trim();
  for (const body of ["", "See the agenda below."]) {
    const result = buildComposeRequest({
      target: "outlookWork",
      to: ["[email protected]"],
      subject,
      body,
    });
    assert.equal(result.ok, true);
    assert.equal(result.subjectCopied, true);
    assert.equal(result.clipboardText, body ? `${subject}\n\n${body}` : subject);
    assert.equal(result.bodyCopied, Boolean(body));
    assert.equal(new URL(result.url).searchParams.get("subject"), null);
    assert.ok(result.url.length <= 2000);
  }
});

test("recipients too long for any link are refused instead of opening a broken link", async () => {
  const { buildComposeRequest } = await load();
  const to = Array.from({ length: 120 }, (_, i) => `teammate.number.${i}@example.com`);
  assert.deepEqual(buildComposeRequest({ target: "mailto", to, subject: "Hi", body: "Hello" }), {
    ok: false,
    reason: "too_long",
  });
});

test("no returned link ever exceeds the limit", async () => {
  const { buildComposeRequest, COMPOSE_TARGETS, maxComposeUrlLength } = await load();
  for (const platform of ["darwin", "linux", "win32"]) {
    for (const target of COMPOSE_TARGETS) {
      const limit = maxComposeUrlLength(target, platform);
      for (const recipients of [1, 10, 40, 80]) {
        for (const subjectLength of [0, 50, 900, 2500]) {
          for (const bodyLength of [0, 300, 1900, 5000]) {
            const result = buildComposeRequest({
              target,
              to: Array.from({ length: recipients }, (_, i) => `person${i}@example.com`),
              subject: "ü".repeat(subjectLength),
              body: "é ".repeat(bodyLength),
              platform,
            });
            if (result.ok) {
              assert.ok(
                result.url.length <= limit,
                `${platform} ${target} ${recipients}/${subjectLength}/${bodyLength}: ${result.url.length}`
              );
            }
          }
        }
      }
    }
  }
});

test("Gmail on macOS and Linux keeps a longer non-ASCII body in the link", async () => {
  const { buildComposeRequest, maxComposeUrlLength } = await load();
  // ~500 Cyrillic characters encode to ~5,000 URL characters.
  const body = "Привет, это письмо. ".repeat(25);
  const request = (target, platform) =>
    buildComposeRequest({ target, to: ["[email protected]"], subject: "Notes", body, platform });

  for (const platform of ["darwin", "linux"]) {
    const { url, clipboardText } = request("gmail", platform);
    assert.equal(clipboardText, null, platform);
    assert.equal(new URL(url).searchParams.get("body"), body, platform);
    assert.ok(url.length <= maxComposeUrlLength("gmail", platform));
  }
  // Windows caps every opened URL near 2,081; the other targets are unmeasured.
  assert.equal(request("gmail", "win32").clipboardText, body);
  assert.equal(request("outlookWork", "darwin").clipboardText, body);
  assert.equal(request("mailto", "linux").clipboardText, body);
  assert.equal(maxComposeUrlLength("gmail", undefined), 2000);
});

test("email address validation", async () => {
  const { isValidEmailAddress } = await load();
  for (const good of [
    "[email protected]",
    "[email protected]",
    "o'[email protected]",
    "josé@münchen.de",
    "[email protected]",
    "info@пример.рф",
    "a@παράδειγμα.gr",
    "[email protected]",
    // Scripts that spell words with combining marks after a label's first letter.
    "a@उदाहरण.भारत",
    "उपयोगकर्ता@उदाहरण.कॉम",
    "a@ตัวอย่าง.ไทย",
    "a@مِثال.com",
    "a@উদাহরণ.বাংলা",
    "a@இந்தியா.இந்தியா",
    "jo\u0308rg@mu\u0308ller.de",
    "山田@例え.jp",
    "用户@例子.广告",
    "사용자@예시.한국",
    "משתמש@דוגמה.ישראל",
    "a@ምሳሌ.et",
    "a@straße.de",
    "a@2гис.рф",
    "[email protected]",
  ]) {
    assert.equal(isValidEmailAddress(good), true, good);
  }
  for (const bad of [
    "Gabe",
    "gabe@",
    "@example.com",
    "a@b",
    "a [email protected]",
    "[email protected],[email protected]",
    // Not a dot-atom local part.
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "a\\@b.example.com",
    // RFC 5321 caps a local part at 64 octets.
    `${"a".repeat(65)}@example.com`,
    `${"山".repeat(22)}@example.com`,
    // Would decode as an encoded word in the To header.
    "[email protected]",
    "",
    null,
  ]) {
    assert.equal(isValidEmailAddress(bad), false, String(bad));
  }
  assert.equal(isValidEmailAddress(`${"a".repeat(64)}@example.com`), true);
  assert.equal(isValidEmailAddress(`${"山".repeat(21)}@example.com`), true, "63 UTF-8 bytes");
});

test("addresses that could disguise the recipient or carry URL junk are refused", async () => {
  const { isValidEmailAddress } = await load();
  for (const bad of [
    "‮[email protected]", // right-to-left override: displays as [email protected]
    "[email protected]​.evil.io", // zero-width space
    "alice‍@corp.com", // zero-width joiner
    "alice\u0000@corp.com",
    "alice\u0007@corp.com",
    "[email protected]?subject=x",
    "[email protected]#frag",
    "[email protected]&",
    "[email protected]",
    "[email protected]/../x",
    "[email protected]",
    "alice@corp\u3164.com", // Hangul filler: a letter that renders as nothing
    "[email protected]\u3164",
    "alice\u034F@corp.com", // combining grapheme joiner
    "alice@corp\u2060.com", // word joiner
    "alice@c\u043Erp.com", // Cyrillic о inside a Latin label
    "alice@\u03B1pple.com", // Greek α inside a Latin label
    "alice@\u0585rp.com", // Armenian օ inside a Latin label
    "a@я\u043D\u0434екс-mail.ru",
    "alice\uD800@corp.com", // lone surrogate
    "alice@corp\u200C.com", // zero-width non-joiner
    "a\uFE0F[email protected]", // variation selector
    "a\u00AD[email protected]", // soft hyphen
    "[email protected]", // no domain has an all-numeric top level
    "[email protected]", // the URL parser reads it as 127.0.0.1
    "[email protected]",
    "[email protected]",
  ]) {
    assert.equal(isValidEmailAddress(bad), false, JSON.stringify(bad));
  }
});

test("a display-name form keeps only the address inside the brackets", async () => {
  const { bareEmailAddress } = await load();
  assert.equal(bareEmailAddress("Josh Lee <[email protected]>"), "[email protected]");
  assert.equal(bareEmailAddress(' "Lee, Josh" < [email protected] > '), "[email protected]");
  assert.equal(bareEmailAddress("  [email protected] "), "[email protected]");
  // Anything else is left for the validator to refuse.
  assert.equal(
    bareEmailAddress("Josh <[email protected]> <[email protected]>"),
    "Josh <[email protected]> <[email protected]>"
  );
  assert.equal(bareEmailAddress("Josh"), "Josh");
  // A decomposed umlaut becomes the same address as the composed one.
  assert.equal(bareEmailAddress("jo\u0308rg@mu\u0308ller.de"), "jörg@müller.de");
});

test("a recipient with a non-ASCII domain is labelled with the ASCII form it routes to", async () => {
  const { recipientLabel } = await load();
  assert.equal(recipientLabel("[email protected]"), "[email protected]");
  assert.equal(recipientLabel("jö[email protected]"), "jö[email protected]");
  // A whole-script look-alike of apple.com.
  assert.equal(recipientLabel("a@аррӏе.com"), "a@аррӏе.com (xn--80ak6aa92e.com)");
  assert.equal(recipientLabel("a@müller.de"), "a@müller.de (xn--mller-kva.de)");
  assert.equal(recipientLabel("a@example.com"), "a@example.com (example.com)");
  assert.equal(recipientLabel("a@उदाहरण.भारत"), "a@उदाहरण.भारत (xn--p1b6ci4b4b3a.xn--h2brj9c)");
});

test("half an emoji in the subject or body can't break the link", async () => {
  const { buildComposeRequest } = await load();
  for (const target of ["gmail", "mailto"]) {
    const { url } = buildComposeRequest({
      target,
      to: ["[email protected]"],
      subject: "Hi \uD83D",
      body: "\uDC00 done",
      platform: "darwin",
    });
    const params = new URL(url).searchParams;
    assert.equal(params.get(target === "gmail" ? "su" : "subject"), "Hi \uFFFD");
    assert.equal(params.get("body").replace(/\r/g, ""), "\uFFFD done");
  }
  // An overflowing body keeps the replacement on the clipboard too.
  const { clipboardText } = buildComposeRequest({
    target: "mailto",
    to: ["[email protected]"],
    body: `${"word ".repeat(600)}\uD83D`,
    platform: "win32",
  });
  assert.ok(clipboardText.endsWith("\uFFFD"));
});

test("an unknown target is rejected", async () => {
  const { buildComposeRequest } = await load();
  assert.throws(
    () => buildComposeRequest({ target: "yahoo", to: ["[email protected]"] }),
    /Unknown compose target/
  );
});