AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { afterEach, describe, expect, test, vi } from 'vitest';
import { sendEmailAction } from '../src/lib/actions/send-email';
import { sendEmailAiAction } from '../src/lib/actions/send-email-ai';
import { failure, replies, run, SEND_REPLY, stubFetch } from './helpers';
afterEach(() => vi.unstubAllGlobals());
const base = { from_email: '[email protected]', subject: 'Hi', content_type: 'html', content: '<p>Hi</p>' };
describe('send_email (existing action)', () => {
test('keeps the old request for the old props and returns {status, headers, body}', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY, headers: { 'ratelimit-remaining': '9' } }]));
const result = await run({ action: sendEmailAction, props: { ...base, from_name: 'Acme', to: ['[email protected]'], headers: { 'X-Campaign-ID': 'welcome' } } });
expect(seen[0].method).toBe('POST');
expect(seen[0].url.toString()).toBe('https://api.emailit.com/v2/emails');
expect(seen[0].body).toEqual({ from: '"Acme" <[email protected]>', to: ['[email protected]'], subject: 'Hi', html: '<p>Hi</p>', headers: { 'X-Campaign-ID': 'welcome' } });
expect(seen[0].headers.get('idempotency-key')).toBeNull();
expect(result).toMatchObject({ status: 200, body: { id: 'em_1' }, headers: { 'ratelimit-remaining': '9' } });
});
test('plain-text content goes in text; the plain-text version goes with HTML', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
await run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], content_type: 'text', content: 'hello' } });
expect(seen[0].body).toMatchObject({ text: 'hello' });
expect(seen[0].body).not.toHaveProperty('html');
await run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], plain_text: 'Hi (text)' } });
expect(seen[1].body).toMatchObject({ html: '<p>Hi</p>', text: 'Hi (text)' });
});
test('recipients are trimmed, split on commas, and empty entries dropped', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
await run({ action: sendEmailAction, props: { ...base, to: [' [email protected] ', '', '[email protected], [email protected]'], cc: [''], reply_to: '[email protected], [email protected]' } });
expect(seen[0].body).toMatchObject({ to: ['[email protected]', '[email protected]', '[email protected]'], reply_to: ['[email protected]', '[email protected]'] });
expect(seen[0].body).not.toHaveProperty('cc');
});
test('a comma inside a quoted display name is not split; Emailit rejects it, so the step explains why', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
await expect(run({ action: sendEmailAction, props: { ...base, to: ['"Doe, Jane" <[email protected]>'] } })).rejects.toThrow('comma inside a display name');
await run({ action: sendEmailAction, props: { ...base, to: ['"Jane Doe" <[email protected]>, [email protected]'], reply_to: '"Support" <[email protected]>, [email protected]' } });
expect(seen).toHaveLength(1);
expect(seen[0].body).toMatchObject({ to: ['"Jane Doe" <[email protected]>', '[email protected]'], reply_to: ['"Support" <[email protected]>', '[email protected]'] });
});
test('a sender name Emailit would reject fails clearly instead of being changed', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], from_name: 'Acme "Support"' } })).rejects.toThrow('Sender Name cannot contain quotes');
await run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], from_name: 'Acme Support, EU' } });
expect(seen).toHaveLength(1);
expect(seen[0].body).toMatchObject({ from: '"Acme Support, EU" <[email protected]>' });
});
test('the 50-address limit applies per field, not combined', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
const fifty = Array.from({ length: 50 }, (_, index) => `u${index}@x.io`);
await run({ action: sendEmailAction, props: { ...base, to: fifty, cc: fifty, bcc: fifty } });
expect(seen).toHaveLength(1);
await expect(run({ action: sendEmailAction, props: { ...base, to: [...fifty, '[email protected]'] } })).rejects.toThrow('at most 50');
expect(seen).toHaveLength(1);
});
test('header injection is refused before any request', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], headers: { 'X-A': 'ok\r\nBcc: [email protected]' } } })).rejects.toThrow('line breaks');
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], headers: { 'X A\n': 'v' } } })).rejects.toThrow('not a valid header name');
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], from_name: 'Evil\r\nBcc: x' } })).rejects.toThrow('line breaks');
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]\nBcc: [email protected]'] } })).rejects.toThrow('line breaks');
expect(seen).toHaveLength(0);
});
test('idempotency key, schedule, template, variables, meta, tracking and attachments are sent', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
await run({
action: sendEmailAction,
props: {
from_email: '[email protected]',
content_type: 'html',
to: ['[email protected]'],
template: 'welcome',
variables: { first_name: 'Ada' },
meta: { order_id: 1042 },
scheduled_at: '2026-10-20T09:00:00+02:00',
tracking: 'off',
attachments: [{ filename: 'invoice.pdf', url: 'https://files.acme.com/i.pdf' }],
attachment_file: { filename: 'note.txt', extension: 'txt', data: Buffer.from('hi'), base64: Buffer.from('hi').toString('base64') },
idempotency_key: 'order-1042',
},
});
expect(seen[0].headers.get('idempotency-key')).toBe('order-1042');
expect(seen[0].body).toEqual({
from: '[email protected]',
to: ['[email protected]'],
template: 'welcome',
variables: { first_name: 'Ada' },
meta: { order_id: '1042' },
scheduled_at: '2026-10-20T07:00:00.000Z',
tracking: false,
attachments: [
{ filename: 'invoice.pdf', url: 'https://files.acme.com/i.pdf' },
{ filename: 'note.txt', content: 'aGk=', content_type: 'text/plain' },
],
});
});
test('bad input fails before any request', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], scheduled_at: 'next tuesday-ish' } })).rejects.toThrow('not a valid date-time');
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], idempotency_key: 'has space' } })).rejects.toThrow('Idempotency Key');
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], subject: undefined } })).rejects.toThrow('Subject is required');
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], content: ' ' } })).rejects.toThrow('email body');
await expect(run({ action: sendEmailAction, props: { ...base, to: [] } })).rejects.toThrow('at least one');
await expect(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'], attachments: [{ filename: 'a.pdf', url: 'file:///etc/passwd' }] } })).rejects.toThrow('http');
expect(seen).toHaveLength(0);
});
test('a sandbox rejection explains what to do', async () => {
stubFetch(replies([{ status: 403, body: { error: 'Workspace not verified', code: 'unverified_workspace_recipient' } }]));
expect(String(await failure(run({ action: sendEmailAction, props: { ...base, to: ['[email protected]'] } })))).toContain('sandbox mode');
});
});
describe('send_email_ai', () => {
test('returns only the email body and sends html + text together', async () => {
const seen = stubFetch(replies([{ body: SEND_REPLY }]));
const result = await run({ action: sendEmailAiAction, props: { from_email: '[email protected]', to: ['[email protected]'], subject: 'Hi', html: '<b>x</b>', text: 'x', idempotency_key: 'k-1' } });
expect(result).toEqual(SEND_REPLY);
expect(seen[0].body).toMatchObject({ html: '<b>x</b>', text: 'x' });
expect(seen[0].headers.get('idempotency-key')).toBe('k-1');
});
});