Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
// Fixture-based tests for api/_lib/email.js — exercise every template and
// the no-API-key short-circuit without mocking the Resend SDK.
//
// Module-level constants (FROM, REPLY, APP_URL) snapshot env at import time,
// so each suite uses vi.resetModules() + dynamic import to load a fresh
// instance with the env it wants to assert against.
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import { JSDOM } from 'jsdom';
const TEST_ENV = {
EMAIL_FROM: 'three.ws <[email protected]>',
APP_ORIGIN: 'https://three.ws.test',
};
const RESEND_KEY = 'rk_test_fixture_value';
async function loadEmail(envOverrides = {}) {
const merged = { ...TEST_ENV, ...envOverrides };
const saved = {};
for (const k of Object.keys(merged)) {
saved[k] = process.env[k];
if (merged[k] === null) delete process.env[k];
else process.env[k] = merged[k];
}
vi.resetModules();
const mod = await import('../api/_lib/email.js');
const restore = () => {
for (const k of Object.keys(merged)) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
};
return { mod, restore };
}
function parseHtml(html) {
const dom = new JSDOM(html, { contentType: 'text/html' });
return dom.window.document;
}
// Helper — sanity-check that the rendered HTML is a real, parseable document
// with the basic transactional-email scaffolding (lang attr, charset meta,
// viewport meta, <title>, <body>, single .card root). Used by every template.
function assertCommonScaffolding(doc, expectedTitle) {
expect(doc.documentElement.tagName.toLowerCase()).toBe('html');
expect(doc.documentElement.getAttribute('lang')).toBe('en');
expect(doc.querySelector('meta[charset="utf-8"]')).not.toBeNull();
expect(doc.querySelector('meta[name="viewport"]')).not.toBeNull();
expect(doc.querySelector('title')).not.toBeNull();
expect(doc.querySelector('title').textContent).toBe(expectedTitle);
expect(doc.querySelector('body')).not.toBeNull();
expect(doc.querySelectorAll('div.card')).toHaveLength(1);
}
// Helper — every action link in the email must be an absolute https URL.
// Catches the class of bug where APP_URL is unset and href becomes "/dashboard/".
function assertAllLinksAbsoluteHttps(doc) {
const anchors = doc.querySelectorAll('a[href]');
expect(anchors.length).toBeGreaterThan(0);
for (const a of anchors) {
const href = a.getAttribute('href');
expect(href).toMatch(/^https:\/\//);
}
}
// ─── Welcome template ────────────────────────────────────────────────────────
describe('email — welcome template', () => {
let mod, restore;
beforeAll(async () => {
({ mod, restore } = await loadEmail());
});
afterAll(() => restore());
it('renderWelcome includes the recipient display name and dashboard link', () => {
const { subject, html, text } = mod.renderWelcome({ displayName: 'Alice' });
expect(subject).toBe('Welcome to three.ws');
expect(html).toContain('Welcome, Alice');
expect(text).toContain('Welcome to three.ws, Alice');
expect(html).toContain('https://three.ws.test/dashboard/');
expect(text).toContain('https://three.ws.test/dashboard/');
});
it('falls back to "there" when displayName is missing', () => {
const { html, text } = mod.renderWelcome({});
expect(html).toContain('Welcome, there');
expect(text).toContain('Welcome to three.ws, there');
});
it('html-escapes the display name to prevent injection', () => {
const { html } = mod.renderWelcome({ displayName: '<script>x</script>' });
expect(html).not.toContain('<script>x</script>');
expect(html).toContain('<script>x</script>');
});
it('produces parseable HTML with the expected scaffolding', () => {
const { html } = mod.renderWelcome({ displayName: 'Bob' });
const doc = parseHtml(html);
assertCommonScaffolding(doc, 'Welcome to three.ws');
assertAllLinksAbsoluteHttps(doc);
expect(doc.querySelector('h1').textContent).toContain('Welcome, Bob');
});
});
// ─── Verify template ─────────────────────────────────────────────────────────
describe('email — verification template', () => {
let mod, restore;
beforeAll(async () => {
({ mod, restore } = await loadEmail());
});
afterAll(() => restore());
it('renderVerify embeds the code and expiry in subject/html/text', () => {
const { subject, html, text } = mod.renderVerify({ code: '482913', expiresInMinutes: 30 });
expect(subject).toBe('482913 — verify your email');
expect(html).toContain('482913');
expect(html).toContain('30 minutes');
expect(text).toContain('482913');
expect(text).toContain('30 minutes');
});
it('defaults expiresInMinutes to 30', () => {
const { html } = mod.renderVerify({ code: '000000' });
expect(html).toContain('30 minutes');
});
it('html-escapes the code', () => {
const { html } = mod.renderVerify({ code: '<x>' });
expect(html).toContain('<x>');
expect(html).not.toContain('<div class="code"><x>');
});
it('produces parseable HTML with the verify scaffolding', () => {
const { html } = mod.renderVerify({ code: '482913', expiresInMinutes: 15 });
const doc = parseHtml(html);
assertCommonScaffolding(doc, 'Verify your email');
expect(doc.querySelector('div.code').textContent).toBe('482913');
expect(doc.querySelector('h1').textContent).toBe('Verify your email');
});
});
// ─── Password reset template ─────────────────────────────────────────────────
describe('email — password reset template', () => {
let mod, restore;
beforeAll(async () => {
({ mod, restore } = await loadEmail());
});
afterAll(() => restore());
const url = 'https://three.ws.test/reset-password?token=abcdef1234567890';
it('renderPasswordReset embeds the reset URL and expiry', () => {
const { subject, html, text } = mod.renderPasswordReset({ resetUrl: url, expiresInMinutes: 60 });
expect(subject).toBe('Reset your three.ws password');
expect(html).toContain(url);
expect(html).toContain('60 minutes');
expect(text).toContain(url);
expect(text).toContain('60 minutes');
});
it('defaults expiresInMinutes to 60', () => {
const { html } = mod.renderPasswordReset({ resetUrl: url });
expect(html).toContain('60 minutes');
});
it('escapes the reset URL in the href attribute (defence in depth)', () => {
const { html } = mod.renderPasswordReset({ resetUrl: 'https://example.test/r?token=a"b&c<' });
expect(html).toContain('"');
expect(html).toContain('&');
expect(html).toContain('<');
});
it('renders the action button as an absolute https link', () => {
const { html } = mod.renderPasswordReset({ resetUrl: url });
const doc = parseHtml(html);
assertCommonScaffolding(doc, 'Reset your three.ws password');
const btn = doc.querySelector('a.btn');
expect(btn).not.toBeNull();
expect(btn.getAttribute('href')).toBe(url);
expect(btn.textContent).toBe('Reset password');
});
});
// ─── Subscription confirm template ───────────────────────────────────────────
describe('email — subscription confirmation template', () => {
let mod, restore;
beforeAll(async () => {
({ mod, restore } = await loadEmail());
});
afterAll(() => restore());
it('renderSubscriptionConfirm includes plan, chain, and transaction id', () => {
const { subject, html, text } = mod.renderSubscriptionConfirm({
plan: 'pro',
chain: 'solana',
txId: '5a3xJv8K9LmNbqRtUvWxYz1234567890abcdefghijklmno',
});
expect(subject).toBe('three.ws Pro plan activated');
expect(html).toContain('Pro plan activated');
expect(html).toContain('Solana');
expect(html).toContain('5a3xJv8K9LmNbqRtUvWxYz1234567890abcdefghijklmno');
expect(text).toContain('pro plan');
expect(text).toContain('solana');
expect(text).toContain('5a3xJv8K9LmNbqRtUvWxYz1234567890abcdefghijklmno');
});
it('omits the transaction row when txId is empty', () => {
const { html } = mod.renderSubscriptionConfirm({ plan: 'pro', chain: 'evm', txId: '' });
expect(html).not.toContain('Transaction:');
});
it('html-escapes the transaction id', () => {
const { html } = mod.renderSubscriptionConfirm({ plan: 'pro', chain: 'evm', txId: '<tx>' });
expect(html).toContain('<tx>');
});
it('produces parseable HTML with the subscription scaffolding', () => {
const { html } = mod.renderSubscriptionConfirm({ plan: 'creator', chain: 'evm', txId: '0xabc' });
const doc = parseHtml(html);
assertCommonScaffolding(doc, 'Creator plan activated');
assertAllLinksAbsoluteHttps(doc);
const btn = doc.querySelector('a.btn');
expect(btn.textContent).toBe('Go to Dashboard');
expect(btn.getAttribute('href')).toBe('https://three.ws.test/dashboard/');
});
});
// ─── buildPayload ────────────────────────────────────────────────────────────
describe('email — buildPayload', () => {
it('constructs the Resend payload with from/to/subject/html/text', async () => {
const { mod, restore } = await loadEmail();
try {
const payload = mod.buildPayload({
to: '[email protected]',
subject: 'hi',
html: '<p>hi</p>',
text: 'hi',
});
expect(payload).toEqual({
from: 'three.ws <[email protected]>',
replyTo: '[email protected]',
to: '[email protected]',
subject: 'hi',
html: '<p>hi</p>',
text: 'hi',
});
} finally {
restore();
}
});
it('defaults replyTo to [email protected], overridable via EMAIL_REPLY_TO', async () => {
const { mod, restore } = await loadEmail({ EMAIL_REPLY_TO: '[email protected]' });
try {
const payload = mod.buildPayload({
to: '[email protected]',
subject: 'hi',
html: '<p>hi</p>',
text: 'hi',
});
expect(payload.replyTo).toBe('[email protected]');
expect(payload.from).toBe('three.ws <[email protected]>');
} finally {
restore();
}
});
it('falls back to the default sender when EMAIL_FROM is unset', async () => {
const { mod, restore } = await loadEmail({ EMAIL_FROM: null });
try {
const payload = mod.buildPayload({
to: '[email protected]',
subject: 'hi',
html: '<p>hi</p>',
text: 'hi',
});
expect(payload.from).toBe('three.ws <[email protected]>');
} finally {
restore();
}
});
});
// ─── sendEmail no-API-key short-circuit ──────────────────────────────────────
describe('email — sendEmail short-circuits when RESEND_API_KEY is unset', () => {
let mod, restore;
beforeAll(async () => {
({ mod, restore } = await loadEmail({ RESEND_API_KEY: null }));
});
afterAll(() => restore());
it('sendEmail returns the documented skipped shape', async () => {
const result = await mod.sendEmail({
to: '[email protected]',
subject: 'hi',
html: '<p>hi</p>',
text: 'hi',
});
expect(result).toEqual({ skipped: true, reason: 'missing_api_key' });
});
it('sendWelcomeEmail skips without throwing', async () => {
await expect(
mod.sendWelcomeEmail({ to: '[email protected]', displayName: 'A' }),
).resolves.toEqual({ skipped: true, reason: 'missing_api_key' });
});
it('sendVerificationEmail skips without throwing', async () => {
await expect(
mod.sendVerificationEmail({ to: '[email protected]', code: '123456' }),
).resolves.toEqual({ skipped: true, reason: 'missing_api_key' });
});
it('sendPasswordResetEmail skips without throwing', async () => {
await expect(
mod.sendPasswordResetEmail({
to: '[email protected]',
resetUrl: 'https://three.ws.test/reset-password?token=x',
}),
).resolves.toEqual({ skipped: true, reason: 'missing_api_key' });
});
it('sendSubscriptionConfirmEmail skips without throwing', async () => {
await expect(
mod.sendSubscriptionConfirmEmail({
to: '[email protected]',
plan: 'pro',
chain: 'solana',
txId: 'tx-id-1',
}),
).resolves.toEqual({ skipped: true, reason: 'missing_api_key' });
});
});
// ─── End-to-end payload assembly through send* wrappers ──────────────────────
// With RESEND_API_KEY set, every template wrapper should compose the same
// payload buildPayload produces. We assert by reconstructing the payload from
// the renderer output — proves the wrapper and the renderer agree, without
// any network call or Resend mock.
describe('email — template wrappers compose payloads via buildPayload', () => {
let mod, restore;
beforeAll(async () => {
({ mod, restore } = await loadEmail({ RESEND_API_KEY: RESEND_KEY }));
});
afterAll(() => restore());
it('welcome payload', () => {
const rendered = mod.renderWelcome({ displayName: 'Alice' });
const payload = mod.buildPayload({ to: '[email protected]', ...rendered });
expect(payload.subject).toBe('Welcome to three.ws');
expect(payload.html).toContain('Welcome, Alice');
expect(payload.text).toContain('Welcome to three.ws, Alice');
expect(payload.to).toBe('[email protected]');
expect(payload.from).toBe('three.ws <[email protected]>');
});
it('verify payload', () => {
const rendered = mod.renderVerify({ code: '482913', expiresInMinutes: 30 });
const payload = mod.buildPayload({ to: '[email protected]', ...rendered });
expect(payload.subject).toBe('482913 — verify your email');
expect(payload.html).toContain('482913');
expect(payload.text).toContain('482913');
});
it('password reset payload', () => {
const url = 'https://three.ws.test/reset-password?token=abc';
const rendered = mod.renderPasswordReset({ resetUrl: url, expiresInMinutes: 60 });
const payload = mod.buildPayload({ to: '[email protected]', ...rendered });
expect(payload.subject).toBe('Reset your three.ws password');
expect(payload.html).toContain(url);
expect(payload.text).toContain(url);
});
it('subscription payload', () => {
const rendered = mod.renderSubscriptionConfirm({ plan: 'pro', chain: 'solana', txId: 'tx-1' });
const payload = mod.buildPayload({ to: '[email protected]', ...rendered });
expect(payload.subject).toBe('three.ws Pro plan activated');
expect(payload.html).toContain('Pro plan activated');
expect(payload.html).toContain('Solana');
expect(payload.html).toContain('tx-1');
});
it('forge completion payload carries the prompt, share link, and preview', () => {
const rendered = mod.renderForgeComplete({
prompt: 'a brass desk lamp, articulated arm',
creationPath: '/forge?share=abc-123',
previewImageUrl: 'https://cdn.example.test/preview.webp',
});
const payload = mod.buildPayload({ to: '[email protected]', ...rendered });
expect(payload.subject).toContain('a brass desk lamp');
expect(payload.html).toContain('/forge?share=abc-123');
expect(payload.html).toContain('https://cdn.example.test/preview.webp');
expect(payload.text).toContain('/forge?share=abc-123');
});
it('forge completion payload escapes a hostile prompt and stands without a preview', () => {
const rendered = mod.renderForgeComplete({
prompt: '<script>alert(1)</script>',
creationPath: '/forge?share=abc-123',
previewImageUrl: null,
});
expect(rendered.html).not.toContain('<script>alert(1)</script>');
expect(rendered.html).not.toContain('<img');
});
});