61 lines
3.3 KiB
TypeScript
61 lines
3.3 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { test } from 'node:test';
|
|
import sharp from 'sharp';
|
|
import { buildWeightReminderEmail, loadWeightReminderPortrait } from './weightReminderEmail.js';
|
|
import type { WeightReminder } from '../reminders/weightReminders.js';
|
|
|
|
const bird: WeightReminder = {
|
|
bird_id: 'bird-1', workspace_id: 1, bird_name: 'Kiwi & <Peep>', species: 'Cockatiel',
|
|
workspace_name: 'Our <Flock>', activity_at: '2026-09-01T12:00:00Z', recipient: 'owner@example.test',
|
|
photo_data_url: null, photo_object_key: null,
|
|
};
|
|
|
|
test('branded grouped email embeds distinct portraits and escapes dynamic content', async () => {
|
|
const mail = await buildWeightReminderEmail([bird, { ...bird, bird_id: 'bird-2', bird_name: 'Yoda' }], 'https://flockpal.test/?a=1&b=2');
|
|
assert.equal(mail.attachments?.length, 3);
|
|
assert.deepEqual(mail.attachments?.map(a => a.cid), ['flockpal-logo', 'weight-bird-0', 'weight-bird-1']);
|
|
for (const attachment of mail.attachments ?? []) {
|
|
assert.equal(attachment.contentDisposition, 'inline');
|
|
assert.ok(Buffer.isBuffer(attachment.content));
|
|
assert.ok(String(mail.html).includes(`cid:${attachment.cid}`));
|
|
}
|
|
assert.match(String(mail.html), /Kiwi & <Peep>/);
|
|
assert.match(String(mail.html), /Our <Flock>/);
|
|
assert.match(String(mail.html), /a=1&b=2/);
|
|
assert.match(String(mail.text), /Kiwi & <Peep>/);
|
|
});
|
|
|
|
test('database portrait is resized and invalid photos fall back to the default', async () => {
|
|
const source = await readFile(new URL('../../assets/yoda.png', import.meta.url));
|
|
const photo = await loadWeightReminderPortrait({ ...bird, photo_data_url: `data:image/png;base64,${source.toString('base64')}` });
|
|
const metadata = await sharp(photo).metadata();
|
|
assert.equal(metadata.width, 192);
|
|
assert.equal(metadata.height, 192);
|
|
assert.equal(metadata.format, 'jpeg');
|
|
assert.deepEqual(await loadWeightReminderPortrait({ ...bird, photo_data_url: 'data:image/png;base64,broken' }), await loadWeightReminderPortrait(bird));
|
|
});
|
|
|
|
test('private S3 portrait is downloaded and embedded rather than linked', async (t) => {
|
|
const env = { IMAGE_STORAGE_PROVIDER: 's3', S3_ENDPOINT: 'https://storage.example.test', S3_REGION: 'us-east-1', S3_BUCKET: 'portraits', S3_ACCESS_KEY_ID: 'test', S3_SECRET_ACCESS_KEY: 'test' };
|
|
const previous = Object.fromEntries(Object.keys(env).map(key => [key, process.env[key]]));
|
|
Object.assign(process.env, env);
|
|
try {
|
|
const source = await readFile(new URL('../../assets/yoda.png', import.meta.url));
|
|
t.mock.method(globalThis, 'fetch', async (url: string) => {
|
|
assert.match(String(url), /private-bird.png/);
|
|
return new Response(source);
|
|
});
|
|
const photo = await loadWeightReminderPortrait({ ...bird, photo_object_key: 'private-bird.png' });
|
|
assert.equal((await sharp(photo).metadata()).width, 192);
|
|
t.mock.restoreAll();
|
|
t.mock.method(globalThis, 'fetch', async () => new Response('', { status: 404 }));
|
|
assert.deepEqual(await loadWeightReminderPortrait({ ...bird, photo_object_key: 'missing.png' }), await loadWeightReminderPortrait(bird));
|
|
} finally {
|
|
for (const [key, value] of Object.entries(previous)) {
|
|
if (value === undefined) delete process.env[key];
|
|
else process.env[key] = value;
|
|
}
|
|
}
|
|
});
|