From 9c802c9df829e1743a2cc69fbd2eb8b300318fba Mon Sep 17 00:00:00 2001 From: gebibd00-jpg Date: Sat, 23 May 2026 09:07:36 +0800 Subject: [PATCH] test: cover resend plugin --- plugins/resend/index.test.ts | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 plugins/resend/index.test.ts diff --git a/plugins/resend/index.test.ts b/plugins/resend/index.test.ts new file mode 100644 index 0000000..960e9f9 --- /dev/null +++ b/plugins/resend/index.test.ts @@ -0,0 +1,97 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { ResendPlugin } from './index' + +const fetchMock = vi.fn() + +describe('ResendPlugin', () => { + beforeEach(() => { + vi.clearAllMocks() + vi.stubGlobal('fetch', fetchMock) + }) + + it('initializes as an authless plugin and stores the API key', () => { + const plugin = new ResendPlugin({ apiKey: 're_test_key' }) + + expect(plugin.name).toBe('starbasedb:resend') + expect(plugin.opts.requiresAuth).toBe(false) + expect(plugin.apiKey).toBe('re_test_key') + }) + + it('posts email payloads to Resend with bearer auth', async () => { + fetchMock.mockResolvedValueOnce({ + ok: true, + json: vi.fn().mockResolvedValue({ + id: 'email_123', + from: 'hello@example.com', + }), + }) + + const plugin = new ResendPlugin({ apiKey: 're_live_key' }) + const result = await plugin.sendEmail( + 'hello@example.com', + ['ada@example.com', 'grace@example.com'], + 'Welcome', + '

Hello

' + ) + + expect(fetchMock).toHaveBeenCalledTimes(1) + expect(fetchMock).toHaveBeenCalledWith( + 'https://api.resend.com/emails', + { + method: 'POST', + headers: { + Authorization: 'Bearer re_live_key', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + from: 'hello@example.com', + to: ['ada@example.com', 'grace@example.com'], + subject: 'Welcome', + html: '

Hello

', + }), + } + ) + expect(result).toEqual({ + id: 'email_123', + from: 'hello@example.com', + }) + }) + + it('throws the Resend error message when the API rejects the request', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + json: vi.fn().mockResolvedValue({ + message: 'Invalid API key', + }), + }) + + const plugin = new ResendPlugin({ apiKey: 'bad_key' }) + + await expect( + plugin.sendEmail( + 'hello@example.com', + ['ada@example.com'], + 'Welcome', + '

Hello

' + ) + ).rejects.toThrow('Invalid API key') + }) + + it('uses a fallback error when Resend returns no message', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + json: vi.fn().mockResolvedValue({}), + }) + + const plugin = new ResendPlugin({ apiKey: 'bad_key' }) + + await expect( + plugin.sendEmail( + 'hello@example.com', + ['ada@example.com'], + 'Welcome', + '

Hello

' + ) + ).rejects.toThrow('Failed to send email') + }) +})