e1f8576b95
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
44 lines
1.6 KiB
JavaScript
44 lines
1.6 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { sendPriceChangeEmail, formatPrice, createTransport } = require('../src/mailer');
|
|
|
|
test('formatPrice formats with comma decimal separator', () => {
|
|
assert.equal(formatPrice(19.9), '19,90');
|
|
assert.equal(formatPrice(24.5), '24,50');
|
|
});
|
|
|
|
test('sendPriceChangeEmail sends mail with expected fields', async () => {
|
|
const calls = [];
|
|
const fakeTransport = { sendMail: async (opts) => { calls.push(opts); } };
|
|
await sendPriceChangeEmail(fakeTransport, {
|
|
from: 'korting@sjoerd.app',
|
|
to: 'me@example.com',
|
|
name: 'Voorbeeld product',
|
|
url: 'https://shop.example/product',
|
|
oldPrice: 19.99,
|
|
newPrice: 17.5,
|
|
});
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].from, 'korting@sjoerd.app');
|
|
assert.equal(calls[0].to, 'me@example.com');
|
|
assert.equal(calls[0].subject, 'Prijs gewijzigd: Voorbeeld product');
|
|
assert.match(calls[0].text, /19,99/);
|
|
assert.match(calls[0].text, /17,50/);
|
|
assert.match(calls[0].text, /https:\/\/shop\.example\/product/);
|
|
});
|
|
|
|
test('sendPriceChangeEmail propagates transport errors', async () => {
|
|
const fakeTransport = { sendMail: async () => { throw new Error('smtp down'); } };
|
|
await assert.rejects(
|
|
() => sendPriceChangeEmail(fakeTransport, {
|
|
from: 'a@b.com', to: 'c@d.com', name: 'X', url: 'https://x', oldPrice: 1, newPrice: 2,
|
|
}),
|
|
/smtp down/
|
|
);
|
|
});
|
|
|
|
test('createTransport builds a transport from env vars', () => {
|
|
const transport = createTransport({ SMTP_HOST: 'mail.sjoerd.app', SMTP_PORT: '587', SMTP_USER: 'u', SMTP_PASS: 'p' });
|
|
assert.equal(typeof transport.sendMail, 'function');
|
|
});
|