iterate.com / stdlib

git clone https://git.iterate.com/repos/stdlib

stdlib / tests / mail.test.mjsmain at 41005ca

// tests/mail.test.mjs — which agent a received mail reaches (../mail.ts `whoGetsTheMail`). How
// `mailReceived` reads the email facet and the mail log is not tested here.
// Run from the repo's root: node --test "tests/*.test.mjs"
import assert from "node:assert/strict";
import { test } from "node:test";
import { whoGetsTheMail } from "../mail.ts";

const member = { verified: true, member: true, direct: true };
const outsider = { verified: true, member: false, direct: true };
/** The outside person the agents of these tests wrote to. */
const ADA = "ada@outside.example";
/** The thread's earlier mails that this mind sent to Ada, by the context that sent each. */
const by = (...origins) => origins.map((origin) => ({ origin, to: [ADA] }));

for (const { name, sender, sent, expected } of [
  {
    name: "an outside person's reply to a mail an agent sent goes to that agent, marked as outside",
    sender: outsider,
    sent: by("/agents/web/a1"),
    expected: { agent: "/agents/web/a1", from: "outside" },
  },
  {
    name: "a reply goes to the newest agent that wrote in the thread",
    sender: outsider,
    sent: by("/agents/web/a1", "/integrations/email", "/agents/web/a2"),
    expected: { agent: "/agents/web/a2", from: "outside" },
  },
  {
    name: "an outside person's mail that no agent's mail came before reaches no agent",
    sender: outsider,
    sent: by(),
    expected: null,
  },
  {
    name: "an outside person's mail reaches no agent when only non-agent contexts sent in the thread",
    sender: outsider,
    sent: by("/integrations/email", "/"),
    expected: null,
  },
  {
    name: "a member's reply goes to the agent that wrote",
    sender: member,
    sent: by("/agents/web/a1"),
    expected: { agent: "/agents/web/a1", from: "member" },
  },
  {
    name: "a member's new thread goes to the thread's own agent",
    sender: member,
    sent: by(),
    expected: { agent: null, from: "member" },
  },
  {
    name: "a member's forwarded mail counts as outside",
    sender: { verified: true, member: true, direct: false },
    sent: by("/agents/web/a1"),
    expected: { agent: "/agents/web/a1", from: "outside" },
  },
  {
    name: "a member's forwarded mail that starts a thread reaches no agent",
    sender: { verified: true, member: true, direct: false },
    sent: by(),
    expected: null,
  },
  {
    name: "an unproven sender's reply reaches no agent",
    sender: { verified: false, member: false, direct: false },
    sent: by("/agents/web/a1"),
    expected: null,
  },
])
  test(name, () => {
    // exactness is the point: `null` and the agent's path
    assert.deepEqual(whoGetsTheMail({ sender, automated: false, from: ADA, sent }), expected);
  });

test("a proven outside sender whom no agent wrote to reaches no agent, though the thread holds an agent's mail", () => {
  // anybody who saw one Message-ID of the thread can answer in it
  assert.equal(
    whoGetsTheMail({
      sender: outsider,
      automated: false,
      from: "eve@elsewhere.example",
      sent: by("/agents/web/a1"),
    }),
    null,
  );
});

test("an outside person's reply goes to the agent that wrote to THEM, in to or cc, in any letter case", () => {
  const sent = [
    { origin: "/agents/web/a1", to: ["bob@outside.example", "Ada@Outside.Example"] },
    { origin: "/agents/web/a2", to: ["bob@outside.example"] },
  ];
  assert.deepEqual(whoGetsTheMail({ sender: outsider, automated: false, from: ADA, sent }), {
    agent: "/agents/web/a1",
    from: "outside",
  });
});

test("a person the agent named as 'Name <address>' still gets their reply through", () => {
  const sent = [{ origin: "/agents/web/a1", to: ["Ada Lovelace <Ada@Outside.Example>"] }];
  assert.deepEqual(whoGetsTheMail({ sender: outsider, automated: false, from: ADA, sent }), {
    agent: "/agents/web/a1",
    from: "outside",
  });
  // the received From is the platform's parsed address: one that ends in another's <address> is
  // not read as that address
  assert.equal(
    whoGetsTheMail({
      sender: outsider,
      automated: false,
      from: `eve@elsewhere.example <${ADA}>`,
      sent,
    }),
    null,
  );
});

test("an automated mail reaches no agent, even a member's reply to an agent: another mind's agent's mail is one", () => {
  assert.equal(
    whoGetsTheMail({ sender: member, automated: true, from: ADA, sent: by("/agents/web/a1") }),
    null,
  );
});