Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

23.9 KB
Raw Blame
import { createInMemoryLinqWebhookDedupeStore } from '@lobechat/agent-address-linq';
import type { EmailMessageDetail } from '@lobechat/agent-address-mail';
import { computeAgentMailSignature } from '@lobechat/agent-address-mail';
import type { AgentAccountRef } from '@lobechat/types';
import { describe, expect, it } from 'vitest';

import { createAgentMailProvider } from '../providers/agentMail';

/** A claim store of its own, so one test's accepted ids never leak into the next. */
const isolated = () => ({ dedupeStore: createInMemoryLinqWebhookDedupeStore() });

const SECRET = 'whsec_mail_secret';

const json = (body: unknown, status = 200) =>
  new Response(JSON.stringify(body), {
    headers: { 'content-type': 'application/json' },
    status,
  });

const mailDetail = (overrides: Partial<EmailMessageDetail> = {}): EmailMessageDetail => ({
  attachments: [],
  bcc: [],
  cc: [],
  codes: [],
  direction: 'inbound',
  envelope: { from: '[email protected]', to: '[email protected]' },
  error: null,
  from: { address: '[email protected]', name: 'Human' },
  html: null,
  id: 'msg_in_1',
  inboxId: 'inb_1',
  inReplyTo: null,
  links: [],
  messageId: '<[email protected]>',
  read: false,
  receivedAt: '2026-10-02T00:00:00.000Z',
  references: ['<[email protected]>'],
  replyTo: [],
  size: 42,
  snippet: null,
  status: 'received',
  subject: 'Hello',
  text: 'the answer',
  to: [{ address: '[email protected]' }],
  ...overrides,
});

/** Minimal stand-in for the Agent Mail REST surface the provider touches. */
const createMailFetch = (
  options: { detail?: Partial<EmailMessageDetail>; raw?: null | string } = {},
) => {
  const calls: Array<{ body?: unknown; method: string; path: string }> = [];

  const fetchImpl: typeof fetch = async (input, init) => {
    const url = typeof input === 'string' ? input : input.toString();
    const method = init?.method ?? 'GET';
    const { pathname } = new URL(url);
    calls.push({
      body: init?.body ? JSON.parse(String(init.body)) : undefined,
      method,
      path: pathname,
    });

    if (method === 'POST' && pathname === '/v1/inboxes') {
      return json({ address: '[email protected]', clientId: 'cli_1', id: 'inb_1', metadata: {} });
    }
    if (method === 'POST' && pathname === '/v1/webhooks') {
      return json({ id: 'wh_1', secret: SECRET, url: 'https://app.lobehub.com/hook' });
    }
    if (method === 'DELETE' && pathname === '/v1/inboxes/inb_1') {
      return new Response(null, { status: 204 });
    }
    if (method === 'POST' && pathname === '/v1/inboxes/inb_1/messages') {
      return json(mailDetail({ direction: 'outbound', id: 'msg_out_1' }));
    }
    if (method === 'POST' && pathname === '/v1/messages/msg_in_1/reply') {
      return json(mailDetail({ direction: 'outbound', id: 'msg_out_reply_1' }));
    }
    if (method === 'GET' && pathname === '/v1/messages/msg_in_1') {
      return json(mailDetail(options.detail));
    }
    if (method === 'GET' && pathname === '/v1/messages/msg_in_1/raw') {
      if (options.raw === null) return new Response('', { status: 404 });
      return new Response(options.raw ?? 'From: [email protected]\r\nSubject: Hello\r\n\r\nbody');
    }

    throw new Error(`unexpected mail request ${method} ${pathname}`);
  };

  return { calls, fetchImpl };
};

const inboundBody = (address = '[email protected]', messageId = 'msg_in_1') =>
  JSON.stringify({
    createdAt: '2026-10-02T00:00:00.000Z',
    data: {
      inbox: { address, clientId: 'cli_1', id: 'inb_1' },
      message: { id: messageId },
    },
    id: 'evt_1',
    type: 'message.received',
  });

const ref = (overrides: Partial<AgentAccountRef> = {}): AgentAccountRef => ({
  credential: { webhookSecret: SECRET },
  id: 'acc_1',
  identifier: '[email protected]',
  kind: 'mail',
  metadata: { inboxId: 'inb_1' },
  provider: 'agent-mail',
  ...overrides,
});

const webhookProviderOptions = (
  fetchImpl: typeof fetch,
): Parameters<typeof createAgentMailProvider>[0] => ({
  ...isolated(),
  apiKey: 'am_test',
  fetchImpl,
  webhookUrl: 'https://app.lobehub.com/hook',
});

const replyInput = (): Parameters<ReturnType<typeof createAgentMailProvider>['send']>[1] => ({
  replyToProviderMessageId: 'msg_in_1',
  text: 'hi back',
  to: '[email protected]',
});

describe('agent-mail provider — declaration', () => {
  it('declares the mail identity it issues and what it can do', () => {
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test' });

    expect(provider.provider).toBe('agent-mail');
    expect(provider.kind).toBe('mail');
    expect(provider.capabilities).toEqual({ receive: true, send: true });
  });
});

describe('agent-mail provider — provisioning', () => {
  it('deletes the inbox it just opened when webhook registration fails', async () => {
    const calls: Array<{ method: string; path: string }> = [];
    const fetchImpl: typeof fetch = async (input, init) => {
      const { pathname } = new URL(typeof input === 'string' ? input : input.toString());
      const method = init?.method ?? 'GET';
      calls.push({ method, path: pathname });
      if (method === 'POST' && pathname === '/v1/inboxes')
        return json({ address: '[email protected]', clientId: 'cli_1', id: 'inb_1', metadata: {} });
      if (method === 'POST' && pathname === '/v1/webhooks')
        return json({ error: { message: 'upstream unavailable' } }, 503);
      if (method === 'DELETE' && pathname === '/v1/inboxes/inb_1')
        return new Response(null, { status: 204 });
      throw new Error(`unexpected ${method} ${pathname}`);
    };
    const provider = createAgentMailProvider(webhookProviderOptions(fetchImpl));

    await expect(provider.provision({ agentId: 'agent_7', userId: 'user_1' })).rejects.toThrow(
      /503/,
    );
    expect(calls).toContainEqual({ method: 'DELETE', path: '/v1/inboxes/inb_1' });
  });

  it('keeps an inbox a concurrent provisioning request already bound', async () => {
    // Webhook registration failed here, but the same deterministic `clientId`
    // handed another in-flight request this very inbox, and that request
    // committed its account first. Cleaning up would delete a live mailbox.
    const calls: Array<{ method: string; path: string }> = [];
    const fetchImpl: typeof fetch = async (input, init) => {
      const { pathname } = new URL(typeof input === 'string' ? input : input.toString());
      const method = init?.method ?? 'GET';
      calls.push({ method, path: pathname });
      if (method === 'POST' && pathname === '/v1/inboxes')
        return json({ address: '[email protected]', clientId: 'cli_1', id: 'inb_1', metadata: {} });
      if (method === 'POST' && pathname === '/v1/webhooks')
        return json({ error: { message: 'upstream unavailable' } }, 503);
      throw new Error(`unexpected ${method} ${pathname}`);
    };
    const provider = createAgentMailProvider(webhookProviderOptions(fetchImpl));

    // Not held when the request starts; the other request commits in between,
    // which is exactly the window this guard covers.
    let checks = 0;
    await expect(
      provider.provision({
        agentId: 'agent_7',
        isIdentifierHeld: async () => {
          checks += 1;
          return checks > 1;
        },
        userId: 'user_1',
      }),
    ).rejects.toThrow(/503/);

    expect(calls).not.toContainEqual({ method: 'DELETE', path: '/v1/inboxes/inb_1' });
  });

  it('opens an inbox and stores the returned handle as the identifier', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    const result = await provider.provision({ agentId: 'agent_7', userId: 'user_1' });

    expect(result.identifier).toBe('[email protected]');
    expect(result.metadata).toEqual({ clientId: 'cli_1', inboxId: 'inb_1' });
    expect(calls.some((c) => c.method === 'POST' && c.path === '/v1/inboxes')).toBe(true);
  });

  it('registers the webhook and returns its one-time secret as the credential', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({
      apiKey: 'am_test',
      fetchImpl,
      webhookUrl: 'https://app.lobehub.com/hook',
    });

    const result = await provider.provision({ agentId: 'agent_7', userId: 'user_1' });

    expect(result.credential).toEqual({ webhookSecret: SECRET });
    expect(result.credentialHint?.masked).toBe('••••cret');
    expect(calls.some((c) => c.method === 'POST' && c.path === '/v1/webhooks')).toBe(true);
  });

  it('asks for the inbox by the agent id, so a retry is handed the one already opened', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    await provider.provision({ agentId: 'agent_7', userId: 'user_1' });

    expect(calls.find((c) => c.method === 'POST' && c.path === '/v1/inboxes')?.body).toMatchObject({
      clientId: 'agent_7',
      endUserId: 'agent_7',
    });
  });

  it('opens a further mailbox for a different prefix instead of returning the first', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    await provider.provision({ agentId: 'agent_7', prefix: 'sales', userId: 'user_1' });

    expect(calls.find((c) => c.method === 'POST' && c.path === '/v1/inboxes')?.body).toMatchObject({
      clientId: 'agent_7-sales',
      username: 'sales',
    });
  });

  it('does not register a second webhook for an inbox a live account already holds', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider(webhookProviderOptions(fetchImpl));

    // The first attempt committed its row and only the response was lost: the
    // row keeps the webhook secret, so a fresh webhook would sign deliveries
    // with a secret nothing stores.
    const result = await provider.provision({
      agentId: 'agent_7',
      isIdentifierHeld: async (identifier) => identifier === '[email protected]',
      userId: 'user_1',
    });

    expect(result).toEqual({
      identifier: '[email protected]',
      metadata: { clientId: 'cli_1', inboxId: 'inb_1' },
    });
    expect(calls.some((c) => c.path === '/v1/webhooks')).toBe(false);
  });

  it('releases the inbox on release', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    await provider.release(ref());

    expect(calls).toContainEqual(
      expect.objectContaining({ method: 'DELETE', path: '/v1/inboxes/inb_1' }),
    );
  });

  it('treats an inbox that is already gone as released', async () => {
    const fetchImpl: typeof fetch = async () =>
      json({ error: { code: 'not_found', message: 'Inbox not found' } }, 404);
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    await expect(provider.release(ref())).resolves.toBeUndefined();
  });

  it('still surfaces a release the provider actually refused', async () => {
    const fetchImpl: typeof fetch = async () => json({ error: { message: 'boom' } }, 500);
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    await expect(provider.release(ref())).rejects.toThrow(/500/);
  });
});

describe('agent-mail provider — inbound', () => {
  it('routes a delivery by the inbox address in its payload', () => {
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test' });

    expect(provider.resolveInboundIdentifier({ body: inboundBody(), headers: {} })).toBe(
      '[email protected]',
    );
    expect(provider.resolveInboundIdentifier({ body: 'not json', headers: {} })).toBeUndefined();
  });

  it('accepts a correctly signed delivery and rejects a forged one', async () => {
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test' });
    const body = inboundBody();
    const timestamp = Math.floor(Date.now() / 1000);

    const accepted = await provider.verifyInbound(
      {
        body,
        headers: { 'x-agentmail-signature': computeAgentMailSignature(SECRET, body, timestamp) },
      },
      ref(),
    );
    expect(accepted).toEqual({
      eventId: 'evt_1',
      occurredAt: new Date('2026-10-02T00:00:00.000Z'),
      payload: expect.objectContaining({ id: 'evt_1' }),
      type: 'message.received',
    });

    const forged = await provider.verifyInbound(
      {
        body,
        headers: {
          'x-agentmail-signature': computeAgentMailSignature('wrong_secret', body, timestamp),
        },
      },
      ref(),
    );
    expect(forged).toBeNull();
  });

  it('flags a replay of an event it already accepted as a duplicate', async () => {
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test' });
    const body = inboundBody();
    const headers = {
      'x-agentmail-signature': computeAgentMailSignature(
        SECRET,
        body,
        Math.floor(Date.now() / 1000),
      ),
    };

    await expect(provider.verifyInbound({ body, headers }, ref())).resolves.toEqual(
      expect.objectContaining({ eventId: 'evt_1' }),
    );
    await expect(provider.verifyInbound({ body, headers }, ref())).resolves.toEqual(
      expect.objectContaining({ duplicate: true }),
    );
  });

  it('remembers an accepted event across provider instances, as each webhook request builds one', async () => {
    const body = inboundBody('[email protected]', 'msg_in_1').replace('evt_1', 'evt_cross_instance');
    const headers = {
      'x-agentmail-signature': computeAgentMailSignature(
        SECRET,
        body,
        Math.floor(Date.now() / 1000),
      ),
    };

    const firstRequest = createAgentMailProvider({ apiKey: 'am_test' });
    const secondRequest = createAgentMailProvider({ apiKey: 'am_test' });

    await expect(firstRequest.verifyInbound({ body, headers }, ref())).resolves.toEqual(
      expect.objectContaining({ eventId: 'evt_cross_instance' }),
    );
    await expect(secondRequest.verifyInbound({ body, headers }, ref())).resolves.toEqual(
      expect.objectContaining({ duplicate: true }),
    );
  });

  it('falls back to the shared secret when the account carries none', async () => {
    const provider = createAgentMailProvider({
      ...isolated(),
      apiKey: 'am_test',
      webhookSecret: SECRET,
    });
    const body = inboundBody();
    const timestamp = Math.floor(Date.now() / 1000);

    await expect(
      provider.verifyInbound(
        {
          body,
          headers: { 'x-agentmail-signature': computeAgentMailSignature(SECRET, body, timestamp) },
        },
        ref({ credential: null }),
      ),
    ).resolves.toEqual(expect.objectContaining({ eventId: 'evt_1' }));
  });

  it('processes a retry after the message fetch failed instead of dropping it', async () => {
    let detailAttempts = 0;
    const { fetchImpl: base } = createMailFetch();
    const fetchImpl: typeof fetch = async (input, init) => {
      const { pathname } = new URL(typeof input === 'string' ? input : input.toString());
      if (pathname === '/v1/messages/msg_in_1' && ++detailAttempts === 1)
        return json({ error: { message: 'temporarily unavailable' } }, 503);
      return base(input, init);
    };
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });
    const body = inboundBody();
    const request = {
      body,
      headers: {
        'x-agentmail-signature': computeAgentMailSignature(
          SECRET,
          body,
          Math.floor(Date.now() / 1000),
        ),
      },
    };

    const first = await provider.verifyInbound(request, ref());
    await expect(provider.normalizeInbound(first!, ref())).rejects.toThrow(/503/);

    // The provider retries the same event: it must be processed, not acked as a duplicate.
    const retry = await provider.verifyInbound(request, ref());
    expect(retry).not.toHaveProperty('duplicate', true);
    await expect(provider.normalizeInbound(retry!, ref())).resolves.toMatchObject({
      providerMessageId: 'msg_in_1',
    });
  });

  it('normalizes a message, stripping the quoted history', async () => {
    const { fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    const message = await provider.normalizeInbound(
      { eventId: 'evt_1', payload: JSON.parse(inboundBody()) },
      ref(),
    );

    expect(message).toMatchObject({
      from: '[email protected]',
      providerMessageId: 'msg_in_1',
      subject: 'Hello',
      threadKey: '<[email protected]>',
      to: '[email protected]',
    });
    expect(message!.text).toContain('the answer');
  });

  it('drops a message the deployment key can read but this inbox does not own', async () => {
    // The fetch back is not scoped to the account: it runs with the deployment
    // key. A caller who installs their own webhook secret can sign a delivery
    // whose payload names their inbox but whose message id lives elsewhere.
    const { fetchImpl } = createMailFetch({ detail: { inboxId: 'inb_someone_else' } });
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    const message = await provider.normalizeInbound(
      { eventId: 'evt_1', payload: JSON.parse(inboundBody()) },
      ref(),
    );

    expect(message).toBeNull();
  });

  it('carries the attachments, so an attachment-only email still reaches the agent with its files', async () => {
    const { fetchImpl } = createMailFetch({
      detail: {
        attachments: [
          { contentType: 'application/pdf', filename: 'invoice.pdf', index: 0, size: 1024 },
          { index: 1 },
        ],
        text: null,
      },
    });
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    const message = await provider.normalizeInbound(
      { eventId: 'evt_1', payload: JSON.parse(inboundBody()) },
      ref(),
    );

    expect(message?.text).toBe('');
    expect(message?.attachments).toEqual([
      {
        mimeType: 'application/pdf',
        name: 'invoice.pdf',
        size: 1024,
        url: 'https://api.lobe.id/v1/messages/msg_in_1/attachments/0',
      },
      {
        mimeType: 'application/octet-stream',
        name: undefined,
        size: undefined,
        url: 'https://api.lobe.id/v1/messages/msg_in_1/attachments/1',
      },
    ]);
  });

  it('hands over the mail detail a mail record needs: addressing, RFC threading, attachments', async () => {
    const { fetchImpl } = createMailFetch({
      detail: {
        attachments: [
          { contentType: 'application/pdf', filename: 'invoice.pdf', index: 0, size: 1024 },
        ],
        cc: [{ address: '[email protected]' }],
        from: { address: '[email protected]', name: 'Human' },
        inReplyTo: '<[email protected]>',
        references: ['<[email protected]>', '<[email protected]>'],
        replyTo: [{ address: '[email protected]', name: 'Desk' }],
      },
    });
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    const message = await provider.normalizeInbound(
      { eventId: 'evt_1', payload: JSON.parse(inboundBody()) },
      ref(),
    );

    expect(message?.mail).toEqual({
      attachments: [
        { contentType: 'application/pdf', filename: 'invoice.pdf', index: 0, size: 1024 },
      ],
      cc: [{ address: '[email protected]' }],
      fromName: 'Human',
      inReplyTo: '<[email protected]>',
      messageId: '<[email protected]>',
      references: ['<[email protected]>', '<[email protected]>'],
      replyTo: [{ address: '[email protected]', name: 'Desk' }],
      to: [{ address: '[email protected]' }],
    });
  });

  it('drops robot mail instead of handing it to the agent', async () => {
    const { fetchImpl } = createMailFetch({
      raw: 'From: [email protected]\r\nSubject: Hello\r\nAuto-Submitted: auto-replied\r\n\r\nx',
    });
    const provider = createAgentMailProvider({ ...isolated(), apiKey: 'am_test', fetchImpl });

    const message = await provider.normalizeInbound(
      { eventId: 'evt_1', payload: JSON.parse(inboundBody()) },
      ref(),
    );

    expect(message).toBeNull();
  });
});

describe('agent-mail provider — outbound', () => {
  it('sends through the inbox and returns the provider message id', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ apiKey: 'am_test', fetchImpl });

    const result = await provider.send(ref(), {
      subject: 'Re: Hello',
      text: 'hi back',
      to: '[email protected]',
    });

    expect(result).toEqual({ providerMessageId: 'msg_out_1' });
    const send = calls.find((c) => c.method === 'POST' && c.path === '/v1/inboxes/inb_1/messages');
    expect(send?.body).toMatchObject({
      html: '<p>hi back</p>',
      subject: 'Re: Hello',
      text: 'hi back',
      to: ['[email protected]'],
    });
  });

  it('answers through the reply call so the reply lands in the original thread', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ apiKey: 'am_test', fetchImpl });

    const result = await provider.send(ref(), {
      replyToProviderMessageId: 'msg_in_1',
      text: '**thanks**',
      threadKey: '<[email protected]>',
      to: '[email protected]',
    });

    expect(result).toEqual({ providerMessageId: 'msg_out_reply_1' });
    expect(calls.some((c) => c.path === '/v1/inboxes/inb_1/messages')).toBe(false);
    expect(calls).toContainEqual({
      body: { html: '<p><strong>thanks</strong></p>', text: 'thanks' },
      method: 'POST',
      path: '/v1/messages/msg_in_1/reply',
    });
  });

  it('renders the agent Markdown into an HTML part plus a plain-text alternative', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ apiKey: 'am_test', fetchImpl });

    await provider.send(ref(), {
      subject: 'Brief',
      text: '**Done.** See [the docs](https://lobehub.com/docs)\n\n1. one\n2. two',
      to: '[email protected]',
    });

    const send = calls.find((c) => c.method === 'POST' && c.path === '/v1/inboxes/inb_1/messages');
    const body = send?.body as undefined | { html?: string; text?: string };
    expect(body?.html).toContain('<strong>Done.</strong>');
    expect(body?.html).toContain('<a href="https://lobehub.com/docs"');
    expect(body?.html).toContain('<ol><li>one</li><li>two</li></ol>');
    expect(body?.html).not.toContain('**');
    expect(body?.text).toBe('Done. See the docs (https://lobehub.com/docs)\n\n- one\n- two');
    expect(body?.text).not.toContain('**');
  });
});

describe('agent-mail provider — reply ownership', () => {
  it('refuses to reply to a message that lives in another account’s inbox', async () => {
    // One deployment API key spans every inbox, and the reply route sends as the
    // inbox that owns the message. Answering a foreign id would speak as another
    // agent, skipping every lifecycle and capability check done for this one.
    const { calls, fetchImpl } = createMailFetch({ detail: { inboxId: 'inb_other' } });
    const provider = createAgentMailProvider({ apiKey: 'am_test', fetchImpl });

    await expect(provider.send(ref(), replyInput())).rejects.toMatchObject({
      code: 'reply_target_not_owned',
      name: 'AgentAccountError',
    });

    // The refusal happens before the send: nothing was written to the provider.
    expect(calls.filter((call) => call.method === 'POST')).toEqual([]);
  });

  it('replies when the target belongs to the account’s own inbox', async () => {
    const { calls, fetchImpl } = createMailFetch();
    const provider = createAgentMailProvider({ apiKey: 'am_test', fetchImpl });

    const result = await provider.send(ref(), replyInput());

    expect(calls).toEqual([
      { body: undefined, method: 'GET', path: '/v1/messages/msg_in_1' },
      {
        body: { html: '<p>hi back</p>', text: 'hi back' },
        method: 'POST',
        path: '/v1/messages/msg_in_1/reply',
      },
    ]);
    expect(result).toEqual({ providerMessageId: 'msg_out_reply_1' });
  });
});