Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.
TypeScript
/**
* Pure-function tests for the email helpers. Everything here is
* dependency-free — no DB, no HTTP — so we can run `npm test` without
* spinning up Postgres / Redis / the server. The functions exercised
* here are the safety-critical bits (sanitization + address splitting +
* threading id normalization); they're where a regression would silently
* break either delivery (subject corruption, lost CC) or security (XSS
* via HTML email).
*
* Run: npm test
*/
import { test } from 'node:test'
import assert from 'node:assert/strict'
import {
sanitizeSubject,
splitReplyAddresses,
sanitizeEmailHtml,
parseAddress,
formatAddress,
normalizeMessageId,
computeAgentAddress,
sendViaProvider,
} from '../email.js'
/* ============================== sanitizeSubject ============================ */
test('sanitizeSubject strips ASCII C0 controls and DEL', () => {
assert.equal(sanitizeSubject('hello\x00world\x07'), 'hello world')
assert.equal(sanitizeSubject('a\x7Fb'), 'a b')
})
test('sanitizeSubject collapses runs of whitespace', () => {
assert.equal(sanitizeSubject(' a b\t\nc '), 'a b c')
})
test('sanitizeSubject caps at 200 chars', () => {
const s = sanitizeSubject('x'.repeat(500))
assert.equal(s.length, 200)
})
test('sanitizeSubject returns empty on whitespace-only input', () => {
assert.equal(sanitizeSubject(' \t\n '), '')
})
test('sanitizeSubject strips zero-width / BOM characters', () => {
// = zero-width space, = BOM
assert.equal(sanitizeSubject('hithere'), 'hithere')
})
/* ============================ parseAddress ================================ */
test('parseAddress handles bare address', () => {
assert.deepEqual(parseAddress('[email protected]'), { addr: '[email protected]', name: null })
})
test('parseAddress handles Name <addr> form', () => {
assert.deepEqual(parseAddress('Alice <[email protected]>'), { addr: '[email protected]', name: 'Alice' })
})
test('parseAddress handles quoted name', () => {
assert.deepEqual(parseAddress('"Alice, A." <[email protected]>'), { addr: '[email protected]', name: 'Alice, A.' })
})
test('parseAddress lowercases the address', () => {
assert.deepEqual(parseAddress('Alice <[email protected]>'), { addr: '[email protected]', name: 'Alice' })
})
test('parseAddress rejects garbage', () => {
assert.equal(parseAddress(''), null)
assert.equal(parseAddress('no-at-sign'), null)
assert.equal(parseAddress(' '), null)
})
test('formatAddress quotes names with delimiters', () => {
assert.equal(formatAddress('[email protected]', 'Plain Name'), 'Plain Name <[email protected]>')
assert.equal(formatAddress('[email protected]', 'Has, Comma'), '"Has, Comma" <[email protected]>')
assert.equal(formatAddress('[email protected]', null), '[email protected]')
})
/* ============================ splitReplyAddresses ========================== */
test('splitReplyAddresses puts original From in TO, original To+Cc in CC', () => {
const r = splitReplyAddresses({
originalFrom: 'Alice <[email protected]>',
originalTo: ['[email protected]', '[email protected]'],
originalCc: ['[email protected]'],
selfAddresses: ['[email protected]'],
})
assert.deepEqual(r.to, ['Alice <[email protected]>'])
assert.deepEqual(r.cc, ['[email protected]', '[email protected]', '[email protected]'])
})
test('splitReplyAddresses removes self from both lists', () => {
const r = splitReplyAddresses({
originalFrom: 'Alice <[email protected]>',
originalTo: ['[email protected]', '[email protected]'],
originalCc: ['[email protected]'],
selfAddresses: ['[email protected]'],
})
assert.deepEqual(r.to, ['Alice <[email protected]>'])
assert.deepEqual(r.cc, ['[email protected]'])
})
test('splitReplyAddresses dedupes when From also appears in TO/CC', () => {
const r = splitReplyAddresses({
originalFrom: 'Alice <[email protected]>',
originalTo: ['[email protected]'],
originalCc: [],
selfAddresses: ['[email protected]'],
})
assert.deepEqual(r.to, ['Alice <[email protected]>'])
assert.deepEqual(r.cc, [])
})
test('splitReplyAddresses returns empty TO when From is self', () => {
const r = splitReplyAddresses({
originalFrom: '[email protected]',
originalTo: ['[email protected]'],
originalCc: [],
selfAddresses: ['[email protected]'],
})
assert.deepEqual(r.to, [])
assert.deepEqual(r.cc, ['[email protected]'])
})
/* ============================= sanitizeEmailHtml =========================== */
test('sanitizeEmailHtml strips <script> tag bodies', () => {
const html = '<p>hello</p><script>alert(1)</script><p>world</p>'
const out = sanitizeEmailHtml(html)
assert.ok(!/<script/i.test(out), `expected no <script>, got: ${out}`)
assert.ok(!out.includes('alert(1)'), `expected no script body, got: ${out}`)
})
test('sanitizeEmailHtml strips <iframe>', () => {
const out = sanitizeEmailHtml('<iframe src="https://evil/"></iframe>')
assert.ok(!/<iframe/i.test(out))
})
test('sanitizeEmailHtml strips <style> blocks', () => {
const out = sanitizeEmailHtml('<style>body{display:none}</style><p>x</p>')
assert.ok(!/<style/i.test(out))
assert.ok(out.includes('<p>x</p>'))
})
test('sanitizeEmailHtml strips on* event handlers', () => {
const out = sanitizeEmailHtml('<a href="x" onclick="alert(1)" onmouseover=evil>x</a>')
assert.ok(!/onclick/i.test(out))
assert.ok(!/onmouseover/i.test(out))
assert.ok(out.includes('href'))
})
test('sanitizeEmailHtml neutralizes javascript: URLs', () => {
const out = sanitizeEmailHtml('<a href="javascript:alert(1)">click</a>')
assert.ok(!/javascript:/i.test(out))
})
test('sanitizeEmailHtml neutralizes vbscript: URLs', () => {
const out = sanitizeEmailHtml('<a href="vbscript:msgbox">x</a>')
assert.ok(!/vbscript:/i.test(out))
})
test('sanitizeEmailHtml strips <meta refresh>', () => {
const out = sanitizeEmailHtml('<meta http-equiv="refresh" content="0;url=evil"><p>hi</p>')
assert.ok(!/<meta/i.test(out))
})
test('sanitizeEmailHtml preserves normal mail markup', () => {
const html = '<p>Dear customer,</p><table><tr><td>cell</td></tr></table>'
const out = sanitizeEmailHtml(html)
assert.ok(out.includes('<p>'))
assert.ok(out.includes('<table'))
assert.ok(out.includes('cell'))
})
test('sanitizeEmailHtml preserves data:image URLs', () => {
const out = sanitizeEmailHtml('<img src="data:image/png;base64,abc">')
assert.ok(/data:image\/png/i.test(out), `expected data:image preserved, got: ${out}`)
})
/* ============================ normalizeMessageId =========================== */
test('normalizeMessageId strips angle brackets + lowercases', () => {
assert.equal(normalizeMessageId('<ABC@HOST>'), 'abc@host')
assert.equal(normalizeMessageId('abc@host'), 'abc@host')
})
test('normalizeMessageId returns null on null/empty', () => {
assert.equal(normalizeMessageId(null), null)
assert.equal(normalizeMessageId(''), null)
assert.equal(normalizeMessageId(' '), null)
})
/* ============================ computeAgentAddress ========================== */
test('computeAgentAddress emits <id>.<slug>@<EMAIL_DOMAIN>', () => {
// Only valid when EMAIL_DOMAIN is set — guard so the test doesn't fail
// in environments that intentionally leave it blank.
const addr = computeAgentAddress('aurora', 'acme')
if (!addr) return // EMAIL_DOMAIN unset — skip
assert.match(addr, /^aurora\.acme@/)
})
test('computeAgentAddress strips dots from id portion', () => {
const addr = computeAgentAddress('a.b.c', 'acme')
if (!addr) return
// The id portion ("a.b.c") gets sanitized — dots removed (replaced with -).
// The slug separator remains the LAST dot only.
assert.equal(addr.split('@')[0].split('.').length, 2, `expected exactly one dot separator, got: ${addr}`)
})
test('sendViaProvider derives a stable Resend idempotency key from Message-ID', async () => {
const savedFetch = globalThis.fetch
const savedKey = process.env.RESEND_API_KEY
const requestHeaders: Headers[] = []
process.env.RESEND_API_KEY = 're_test_only'
globalThis.fetch = (async (_input: string | URL | Request, init?: RequestInit) => {
requestHeaders.push(new Headers(init?.headers))
return new Response(JSON.stringify({ id: 'provider-test-id' }), {
status: 200,
headers: { 'content-type': 'application/json' },
})
}) as typeof fetch
try {
const result = await sendViaProvider({
from: '[email protected]',
to: ['[email protected]'],
subject: 'idempotent',
text: 'body',
messageId: '[email protected]',
})
assert.equal(result.ok, true)
assert.equal(
requestHeaders[0]?.get('idempotency-key'),
'cumora-email/[email protected]',
)
} finally {
globalThis.fetch = savedFetch
if (savedKey === undefined) delete process.env.RESEND_API_KEY
else process.env.RESEND_API_KEY = savedKey
}
})