|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The auth mails reach `sendTemplate` through `withMailTenantCurrency`, which |
| 5 | + * adds the organization's default currency to a call that names none, so a |
| 6 | + * template's no-argument `{{ amount | currency }}` hole formats in it rather |
| 7 | + * than being refused for a default the organization has. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, expect, it, vi } from 'vitest'; |
| 11 | +import type { IEmailService, SendTemplateInput } from '@objectstack/spec/contracts'; |
| 12 | +import { withMailTenantCurrency } from './mail-tenant-currency.js'; |
| 13 | + |
| 14 | +function emailDouble() { |
| 15 | + const sendTemplate = vi.fn(async (_input: SendTemplateInput) => ({ id: 'mail_1', status: 'sent' as const })); |
| 16 | + const send = vi.fn(async () => ({ id: 'mail_2', status: 'sent' as const })); |
| 17 | + return { sendTemplate, send, service: { send, sendTemplate } as unknown as IEmailService }; |
| 18 | +} |
| 19 | + |
| 20 | +describe('withMailTenantCurrency', () => { |
| 21 | + it('adds the named organization\'s default to a call that names none', async () => { |
| 22 | + const email = emailDouble(); |
| 23 | + const reads: Array<string | undefined> = []; |
| 24 | + const wired = withMailTenantCurrency(email.service, async (org) => { reads.push(org); return org === 'org_cn' ? 'CNY' : undefined; }); |
| 25 | + await wired.sendTemplate({ template: 'auth.invitation', to: 'a@x.com', organizationId: 'org_cn' }); |
| 26 | + expect(reads).toEqual(['org_cn']); |
| 27 | + expect(email.sendTemplate).toHaveBeenCalledWith({ template: 'auth.invitation', to: 'a@x.com', organizationId: 'org_cn', currency: 'CNY' }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('identity mail, which names no organization, reads the deployment-wide default', async () => { |
| 31 | + const email = emailDouble(); |
| 32 | + const reads: Array<string | undefined> = []; |
| 33 | + const wired = withMailTenantCurrency(email.service, async (org) => { reads.push(org); return 'EUR'; }); |
| 34 | + await wired.sendTemplate({ template: 'auth.verify_email', to: 'a@x.com', identityMail: true }); |
| 35 | + expect(reads).toEqual([undefined]); |
| 36 | + expect(email.sendTemplate.mock.calls[0]![0]).toMatchObject({ currency: 'EUR' }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('a call that names a currency keeps it, and the default is not read', async () => { |
| 40 | + const email = emailDouble(); |
| 41 | + const resolve = vi.fn(async () => 'CNY'); |
| 42 | + await withMailTenantCurrency(email.service, resolve).sendTemplate({ template: 't', to: 'a@x.com', currency: 'JPY' }); |
| 43 | + expect(resolve).not.toHaveBeenCalled(); |
| 44 | + expect(email.sendTemplate.mock.calls[0]![0]).toMatchObject({ currency: 'JPY' }); |
| 45 | + }); |
| 46 | + |
| 47 | + it.each([ |
| 48 | + ['no default', async () => undefined], |
| 49 | + ['a failed read', async () => { throw new Error('settings unavailable'); }], |
| 50 | + ])('with %s it adds no `currency` key, and the render then refuses such a hole', async (_label, resolve) => { |
| 51 | + const email = emailDouble(); |
| 52 | + await withMailTenantCurrency(email.service, resolve).sendTemplate({ template: 't', to: 'a@x.com' }); |
| 53 | + expect(email.sendTemplate.mock.calls[0]![0]).not.toHaveProperty('currency'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('every other member is the service\'s own', async () => { |
| 57 | + const email = emailDouble(); |
| 58 | + await withMailTenantCurrency(email.service, async () => 'CNY').send({ to: 'a@x.com' } as never); |
| 59 | + expect(email.send).toHaveBeenCalledWith({ to: 'a@x.com' }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments