From 8503f43459fd78bf5704df4eeb5d9aa924e877d0 Mon Sep 17 00:00:00 2001 From: weaponsforge Date: Thu, 16 Apr 2026 18:04:49 +0800 Subject: [PATCH] test: add unit tests for buildHtml and EmailTransport Co-Authored-By: Claude Sonnet 4.6 --- app/src/__tests__/build.test.ts | 83 +++++++++++++++++++++++++++++ app/src/__tests__/transport.test.ts | 57 ++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 app/src/__tests__/build.test.ts create mode 100644 app/src/__tests__/transport.test.ts diff --git a/app/src/__tests__/build.test.ts b/app/src/__tests__/build.test.ts new file mode 100644 index 0000000..6815969 --- /dev/null +++ b/app/src/__tests__/build.test.ts @@ -0,0 +1,83 @@ +import path from 'path' +import { beforeAll, describe, expect, it } from 'vitest' + +import { buildHtml } from '@/lib/email/build.js' +import { directory } from '@/utils/helpers.js' +import { EmailSchemaMessages } from '@/types/email.schema.js' + +// buildHtml resolves the EJS template relative to __dirname (set to the build.ts location) +beforeAll(() => { + globalThis.__dirname = path.resolve(directory(import.meta.url), '..', 'lib', 'email') +}) + +const TEST_RECIPIENTS = ['tester@gmail.com'] +const TEST_SENDER = 'sender@gmail.com' + +describe('buildHtml test', () => { + it('should return an HTML string from paragraph content', async () => { + const html = await buildHtml({ + content: ['Hello, World!', 'Second paragraph'], + recipients: TEST_RECIPIENTS, + sender: TEST_SENDER, + }) + + expect(typeof html).toBe('string') + expect(html.length).toBeGreaterThan(0) + }) + + it('should return an HTML string from wysiwyg content', async () => { + const html = await buildHtml({ + content: [], + recipients: TEST_RECIPIENTS, + sender: TEST_SENDER, + wysiwyg: '

Hello, wysiwyg!

', + }) + + expect(typeof html).toBe('string') + expect(html).toContain('Hello, wysiwyg!') + }) + + it('should strip script tags from wysiwyg content', async () => { + const html = await buildHtml({ + content: [], + recipients: TEST_RECIPIENTS, + sender: TEST_SENDER, + wysiwyg: '

Safe content

', + }) + + expect(html).not.toContain('