Skip to content

Email for professionals and teams

6.0 KB
Raw Blame
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import test, { after } from "node:test";
import { fileURLToPath, pathToFileURL } from "node:url";
import { build } from "esbuild";
import Database from "better-sqlite3";

const root = join(dirname(fileURLToPath(import.meta.url)), "..");
const outDir = mkdtempSync(join(tmpdir(), "mailflare-recipient-display-"));
after(() => rmSync(outDir, { recursive: true, force: true }));

async function bundle(entry, outfile) {
	await build({
		entryPoints: [join(root, entry)],
		outfile: join(outDir, outfile),
		bundle: true,
		platform: "node",
		format: "esm",
		target: "node22",
		logLevel: "silent",
		// The DOM-only HTML pipeline is not needed here and its parser deps do not bundle for node.
		alias: { "@/lib/email/html": join(root, "tests/stubs/email-html.mjs"), "@": join(root, "src") },
		external: ["react", "react-dom", "next", "next/*"],
	});
	return import(pathToFileURL(join(outDir, outfile)).href);
}

const display = await bundle("src/lib/email/recipient-display.ts", "display.mjs");
const thread = await bundle("src/components/messages/conversation-thread-utils.ts", "thread.mjs");
const contacts = await bundle("src/lib/contacts/utils.ts", "contacts.mjs");
const parties = await bundle("src/app/(dashboard)/inbox/[messageId]/utils.ts", "parties.mjs");
const validators = await bundle("src/lib/validators.ts", "validators.mjs");

test("address style drops the display name", () => {
	assert.equal(display.formatRecipientAddress("Catch All <[email protected]>", "address"), "[email protected]");
	assert.equal(display.formatRecipientAddress('"Chen, Maya" <[email protected]>', "address"), "[email protected]");
	assert.equal(display.formatRecipientAddress("[email protected]", "address"), "[email protected]");
});

test("full style keeps Name <mailbox> and leaves a bare address alone", () => {
	assert.equal(display.formatRecipientAddress("Redacción <[email protected]>", "full"), "Redacción <[email protected]>");
	assert.equal(display.formatRecipientAddress('"Chen, Maya" <[email protected]>', "full"), "Chen, Maya <[email protected]>");
	assert.equal(display.formatRecipientAddress("[email protected]", "full"), "[email protected]");
	assert.equal(
		display.formatRecipientAddressList("Ada <[email protected]>, [email protected]", "full"),
		"Ada <[email protected]>, [email protected]",
	);
});

test("contact name is never the formatted label", () => {
	assert.equal(display.recipientContactName('"Chen, Maya" <[email protected]>'), "Chen, Maya");
	assert.equal(display.recipientContactName("[email protected]"), "[email protected]");
});

test("thread cards follow the same style and still collapse a long list", () => {
	const message = {
		toAddr: "Ada <[email protected]>, Bob <[email protected]>, Cid <[email protected]>, Dee <[email protected]>",
		ccAddr: "",
	};
	assert.equal(thread.getConversationRecipients(message, "address"), "[email protected], [email protected], [email protected], +1");
	assert.equal(
		thread.getConversationRecipients(message, "full"),
		"Ada <[email protected]>, Bob <[email protected]>, Cid <[email protected]>, +1",
	);
});

test("inbound To is the address, not me", () => {
	const header = parties.getMessageHeaderParties({
		direction: "inbound",
		fromAddr: "Ada Lovelace <[email protected]>",
		toAddr: "Catch All <[email protected]>",
	});
	assert.equal(header.toName, "[email protected]");
	assert.equal(header.toName.includes("me"), false);

	const several = parties.getMessageHeaderParties({
		direction: "inbound",
		fromAddr: "[email protected]",
		toAddr: "[email protected], Two <[email protected]>",
	});
	assert.equal(several.toName, "[email protected], [email protected]");
});

test("outbound To still prefers the contact name", () => {
	const header = parties.getMessageHeaderParties({
		direction: "outbound",
		fromAddr: "[email protected]",
		toAddr: "Ada <[email protected]>",
		toContactName: "Ada Lovelace",
	});
	assert.equal(header.toName, "Ada Lovelace");
});

test("a message's own From name beats a shared contact's learned name", () => {
	const shared = { displayName: "LiveKit Hiring Team", source: "inbound" };
	assert.equal(contacts.getSenderDisplayName('"PostHog Team" <[email protected]>', shared), "PostHog Team");
	assert.equal(
		contacts.getSenderDisplayName('"LiveKit Hiring Team" <[email protected]>', { displayName: "PostHog Team", source: "outbound" }),
		"LiveKit Hiring Team",
	);
});

test("a manually named contact overrides the message's From name", () => {
	assert.equal(
		contacts.getSenderDisplayName('"PostHog Team" <[email protected]>', { displayName: "Ashby Recruiting", source: "manual" }),
		"Ashby Recruiting",
	);
});

test("the learned contact name fills in when the message has no From name", () => {
	assert.equal(
		contacts.getSenderDisplayName("[email protected]", { displayName: "PostHog Team", source: "inbound" }),
		"PostHog Team",
	);
	assert.equal(contacts.getSenderDisplayName("[email protected]", null), null);
});

test("the setting accepts only a boolean", () => {
	assert.equal(validators.updateRecipientAddressSettingsSchema.parse({ enabled: true }).enabled, true);
	assert.equal(validators.updateRecipientAddressSettingsSchema.parse({ enabled: false }).enabled, false);
	assert.throws(() => validators.updateRecipientAddressSettingsSchema.parse({ enabled: "yes" }));
});

test("migration adds the user column off by default and the journal lists it", () => {
	const db = new Database(":memory:");
	db.exec("CREATE TABLE users (id text primary key)");
	db.exec(readFileSync(join(root, "drizzle/migrations/0050_add_show_full_recipient_addresses.sql"), "utf8"));
	db.exec("INSERT INTO users (id) VALUES ('u')");
	assert.equal(db.prepare("SELECT show_full_recipient_addresses AS value FROM users").get().value, 0);

	const journal = JSON.parse(readFileSync(join(root, "drizzle/migrations/meta/_journal.json"), "utf8"));
	const entry = journal.entries.find((item) => item.tag === "0050_add_show_full_recipient_addresses");
	assert.equal(entry?.idx, 42);
});