|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + assertEngineDeleteDispatch, |
| 6 | + assertEngineFindOnePredicate, |
| 7 | + assertEngineUpdateDispatch, |
| 8 | + type EngineFindOneQueryInput, |
| 9 | +} from '@objectstack/metadata-core'; |
| 10 | +import { |
| 11 | + NotificationTemplateStore, |
| 12 | + TEMPLATE_DECLARED_FALLBACK_LOCALE, |
| 13 | + type NotificationTemplateStoreWiring, |
| 14 | +} from './template-renderer.js'; |
| 15 | +import { MessagingServicePlugin } from './messaging-service-plugin.js'; |
| 16 | +import type { MessagingService } from './messaging-service.js'; |
| 17 | +import type { Delivery } from './channel.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * The store's locale ladder reads the stack's DECLARED fallback |
| 21 | + * (`i18n.fallbackLocale`, else `defaultLocale`, through the i18n service's |
| 22 | + * `getFallbackLocale`), as the email template ladder does since #28880: the |
| 23 | + * named tag, its primary subtag, the declared fallback and ITS primary subtag, |
| 24 | + * then `DEFAULT_LOCALE`. |
| 25 | + */ |
| 26 | + |
| 27 | +/** A `sys_notification_template` row in `org_1`. */ |
| 28 | +function row(locale: string, subject = locale) { |
| 29 | + return { organization_id: 'org_1', topic: 't', channel: 'email', locale, is_active: true, subject, body: 'B' }; |
| 30 | +} |
| 31 | + |
| 32 | +function fakeData(rows: any[]) { |
| 33 | + const locales: string[] = []; |
| 34 | + return { |
| 35 | + locales, |
| 36 | + engine: { |
| 37 | + async findOne(object: string, query: EngineFindOneQueryInput & { where?: any }) { |
| 38 | + assertEngineFindOnePredicate(object, query); |
| 39 | + const w = query?.where ?? {}; |
| 40 | + locales.push(w.locale); |
| 41 | + return ( |
| 42 | + rows.find( |
| 43 | + (r) => |
| 44 | + r.organization_id === w.organization_id && r.topic === w.topic && |
| 45 | + r.channel === w.channel && r.locale === w.locale && r.is_active, |
| 46 | + ) ?? null |
| 47 | + ); |
| 48 | + }, |
| 49 | + async find() { return []; }, |
| 50 | + async insert(_o: string, r: any) { return { id: 'x', ...r }; }, |
| 51 | + async update(_o: string, data: any, options?: any) { assertEngineUpdateDispatch(data, options); return {}; }, |
| 52 | + async delete(_o: string, options?: any) { assertEngineDeleteDispatch(options); return {}; }, |
| 53 | + async count() { return 0; }, |
| 54 | + async aggregate() { return []; }, |
| 55 | + } as any, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +function store(engine: any, fallback?: () => string | undefined) { |
| 60 | + const wiring: NotificationTemplateStoreWiring = { getData: () => engine }; |
| 61 | + if (fallback) wiring[TEMPLATE_DECLARED_FALLBACK_LOCALE] = fallback; |
| 62 | + return new NotificationTemplateStore(wiring); |
| 63 | +} |
| 64 | + |
| 65 | +describe('NotificationTemplateStore — the declared fallback locale (#28919)', () => { |
| 66 | + it('picks the declared fallback before en when the recipient locale has no row', async () => { |
| 67 | + const data = fakeData([row('zh-CN'), row('en')]); |
| 68 | + const got = await store(data.engine, () => 'zh-CN').load('t', 'email', 'fr-FR', 'org_1'); |
| 69 | + expect(got).toMatchObject({ subject: 'zh-CN' }); |
| 70 | + expect(data.locales).toEqual(['fr-FR', 'fr', 'zh-CN']); |
| 71 | + }); |
| 72 | + |
| 73 | + it('folds the declared fallback to its primary subtag before en', async () => { |
| 74 | + const data = fakeData([row('zh'), row('en')]); |
| 75 | + const got = await store(data.engine, () => 'zh-CN').load('t', 'email', 'fr-FR', 'org_1'); |
| 76 | + expect(got).toMatchObject({ subject: 'zh' }); |
| 77 | + expect(data.locales).toEqual(['fr-FR', 'fr', 'zh-CN', 'zh']); |
| 78 | + }); |
| 79 | + |
| 80 | + it("the recipient's primary subtag still wins over the declared fallback", async () => { |
| 81 | + const data = fakeData([row('fr'), row('zh-CN'), row('en')]); |
| 82 | + const got = await store(data.engine, () => 'zh-CN').load('t', 'email', 'fr-CA', 'org_1'); |
| 83 | + expect(got).toMatchObject({ subject: 'fr' }); |
| 84 | + expect(data.locales).toEqual(['fr-CA', 'fr']); |
| 85 | + }); |
| 86 | + |
| 87 | + it('lands en when neither the recipient nor the declared fallback has a row', async () => { |
| 88 | + const data = fakeData([row('en')]); |
| 89 | + const got = await store(data.engine, () => 'zh-CN').load('t', 'email', 'fr-FR', 'org_1'); |
| 90 | + expect(got).toMatchObject({ subject: 'en' }); |
| 91 | + expect(data.locales).toEqual(['fr-FR', 'fr', 'zh-CN', 'zh', 'en']); |
| 92 | + }); |
| 93 | + |
| 94 | + it('reads the declared fallback at each resolve, so a later answer is seen', async () => { |
| 95 | + const data = fakeData([row('ja'), row('zh-CN'), row('en')]); |
| 96 | + let declared: string | undefined; |
| 97 | + const s = store(data.engine, () => declared); |
| 98 | + expect(await s.load('t', 'email', 'fr-FR', 'org_1')).toMatchObject({ subject: 'en' }); |
| 99 | + declared = 'zh-CN'; |
| 100 | + expect(await s.load('t', 'email', 'fr-FR', 'org_1')).toMatchObject({ subject: 'zh-CN' }); |
| 101 | + declared = 'ja-JP'; |
| 102 | + expect(await s.load('t', 'email', 'fr-FR', 'org_1')).toMatchObject({ subject: 'ja' }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('a store with no declared fallback answers exactly as before', () => { |
| 106 | + for (const [label, fallback] of [ |
| 107 | + ['no fallback reader wired', undefined], |
| 108 | + ['a reader that answers nothing', () => undefined], |
| 109 | + ['a reader that answers an empty tag', () => ' '], |
| 110 | + ['a reader that throws', () => { throw new Error('i18n down'); }], |
| 111 | + ] as Array<[string, (() => string | undefined) | undefined]>) { |
| 112 | + it(label, async () => { |
| 113 | + const data = fakeData([row('zh-CN'), row('en')]); |
| 114 | + const got = await store(data.engine, fallback).load('t', 'email', 'fr-FR', 'org_1'); |
| 115 | + expect(got).toMatchObject({ subject: 'en' }); |
| 116 | + expect(data.locales).toEqual(['fr-FR', 'fr', 'en']); |
| 117 | + }); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it('a call that names no locale keeps its single en rung', async () => { |
| 122 | + const data = fakeData([row('zh-CN')]); |
| 123 | + expect(await store(data.engine, () => 'zh-CN').load('t', 'email', undefined, 'org_1')).toBeNull(); |
| 124 | + expect(data.locales).toEqual(['en']); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +/** |
| 129 | + * The composition: the plugin hands its store the i18n service's |
| 130 | + * `getFallbackLocale`, resolved at each send (the service may register after |
| 131 | + * `kernel:ready`), and a host with no i18n service renders as before. |
| 132 | + */ |
| 133 | +describe('MessagingServicePlugin — the email channel reads the declared fallback (#28919)', () => { |
| 134 | + function composed() { |
| 135 | + const data = fakeData([row('zh-CN', 'zh-CN subject'), row('en', 'en subject')]); |
| 136 | + const engine = { |
| 137 | + ...data.engine, |
| 138 | + async syncObjectSchema() {}, |
| 139 | + async findOne(object: string, query: any) { |
| 140 | + if (object === 'sys_user') return { email: 'ada@example.com', locale: 'fr-FR' }; |
| 141 | + if (object === 'sys_notification_template') return data.engine.findOne(object, query); |
| 142 | + assertEngineFindOnePredicate(object, query); |
| 143 | + return null; |
| 144 | + }, |
| 145 | + }; |
| 146 | + const services = new Map<string, unknown>([['data', engine], ['manifest', { register() {} }]]); |
| 147 | + const readyHooks: Array<() => Promise<void> | void> = []; |
| 148 | + const logger = { info() {}, warn() {}, error() {}, debug() {}, child() { return logger; } }; |
| 149 | + const ctx = { |
| 150 | + logger, |
| 151 | + getService(name: string) { |
| 152 | + if (!services.has(name)) throw new Error(`service '${name}' not found`); |
| 153 | + return services.get(name); |
| 154 | + }, |
| 155 | + registerService(name: string, svc: unknown) { services.set(name, svc); }, |
| 156 | + hook(event: string, fn: () => Promise<void> | void) { if (event === 'kernel:ready') readyHooks.push(fn); }, |
| 157 | + } as any; |
| 158 | + const sent: any[] = []; |
| 159 | + services.set('email', { async send(input: any) { sent.push(input); return { id: 'mail_1' }; } }); |
| 160 | + return { ctx, data, sent, fireReady: async () => { for (const fn of readyHooks) await fn(); } }; |
| 161 | + } |
| 162 | + |
| 163 | + const delivery: Delivery = { |
| 164 | + channel: 'email', |
| 165 | + recipient: 'user_1', |
| 166 | + notification: { |
| 167 | + notificationId: 'evt_1', |
| 168 | + topic: 't', |
| 169 | + title: 'T', |
| 170 | + body: 'B', |
| 171 | + severity: 'info', |
| 172 | + recipients: ['user_1'], |
| 173 | + organizationId: 'org_1', |
| 174 | + payload: {}, |
| 175 | + } as any, |
| 176 | + }; |
| 177 | + const sendCtx = { logger: { info() {}, warn() {}, error() {} } } as any; |
| 178 | + |
| 179 | + it('renders the declared fallback row for a recipient locale with no row, with i18n registered after kernel:ready', async () => { |
| 180 | + const { ctx, data, sent, fireReady } = composed(); |
| 181 | + await new MessagingServicePlugin({ reliableDelivery: false }).init(ctx); |
| 182 | + await fireReady(); |
| 183 | + ctx.registerService('i18n', { getFallbackLocale: () => 'zh-CN' }); |
| 184 | + |
| 185 | + const messaging = ctx.getService('messaging') as MessagingService; |
| 186 | + const result = await messaging.getChannel('email')!.send(sendCtx, delivery); |
| 187 | + expect(result.ok).toBe(true); |
| 188 | + expect(sent[0]?.subject).toBe('zh-CN subject'); |
| 189 | + expect(data.locales).toEqual(['fr-FR', 'fr', 'zh-CN']); |
| 190 | + }); |
| 191 | + |
| 192 | + it('renders the en row on a host with no i18n service', async () => { |
| 193 | + const { ctx, data, sent, fireReady } = composed(); |
| 194 | + await new MessagingServicePlugin({ reliableDelivery: false }).init(ctx); |
| 195 | + await fireReady(); |
| 196 | + |
| 197 | + const messaging = ctx.getService('messaging') as MessagingService; |
| 198 | + const result = await messaging.getChannel('email')!.send(sendCtx, delivery); |
| 199 | + expect(result.ok).toBe(true); |
| 200 | + expect(sent[0]?.subject).toBe('en subject'); |
| 201 | + expect(data.locales).toEqual(['fr-FR', 'fr', 'en']); |
| 202 | + }); |
| 203 | +}); |
0 commit comments