|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// framework#5447 — `config.email.persist` reaches `EmailServicePlugin`. |
| 4 | +// |
| 5 | +// `EmailServiceConfigSchema` has declared `persist` ("Persist to sys_email |
| 6 | +// (default true)") for as long as the plugin has honoured it, and the plugin |
| 7 | +// half was never in doubt: it builds no `EmailPersistence` when |
| 8 | +// `persist === false`, and says so in its own queue-delivery diagnostics. What |
| 9 | +// did not exist was the segment between them. `config.email` has exactly one |
| 10 | +// reader in the repo — `resolveEmailCapabilityArg`, whose return value IS the |
| 11 | +// plugin's constructor argument (see the `cap === 'email'` arm of the |
| 12 | +// capability loop) — and it read every declared key except this one. |
| 13 | +// |
| 14 | +// So the key was declared, typed, parsed, and documented, and a PII-sensitive |
| 15 | +// deployment that wrote `persist: false` to keep message bodies out of the |
| 16 | +// database kept writing every body to `sys_email`. That is Prime Directive |
| 17 | +// #10's declared != enforced with a privacy blast radius, and ADR-0049's |
| 18 | +// enforce-or-remove was answered "enforce". |
| 19 | +// |
| 20 | +// The end-to-end block below is the point of this file. Asserting only that |
| 21 | +// the resolver emits `persist: false` would re-pin the same declaration twice |
| 22 | +// (a resolver key name against a test's expectation of that key name) and |
| 23 | +// could not tell whether the plugin reads `persist` at all — which is exactly |
| 24 | +// the class of gap being closed. So it boots the REAL `EmailServicePlugin` |
| 25 | +// over the resolver's REAL output and asks the plugin what it built. |
| 26 | + |
| 27 | +import { describe, it, expect, vi } from 'vitest'; |
| 28 | +import { EmailServicePlugin } from '@objectstack/plugin-email'; |
| 29 | +import { resolveEmailCapabilityArg } from './serve.js'; |
| 30 | + |
| 31 | +// ── resolver ─────────────────────────────────────────────────────────────── |
| 32 | + |
| 33 | +describe('resolveEmailCapabilityArg — sys_email persistence (#5447)', () => { |
| 34 | + it('leaves persist unset when neither config nor env declares it', () => { |
| 35 | + // The no-regression case: absent, not `false`. An option nobody wrote must |
| 36 | + // not appear in what the plugin is constructed with, because the plugin's |
| 37 | + // default (persist ON) is what every existing deployment is running and |
| 38 | + // emitting `persist: true` here would only look equivalent. |
| 39 | + expect(resolveEmailCapabilityArg({}, {})).not.toHaveProperty('options.persist'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('carries config.email.persist:false through to the constructor options', () => { |
| 43 | + const { options } = resolveEmailCapabilityArg({ persist: false }, {}); |
| 44 | + expect(options.persist).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('carries an explicit config.email.persist:true too', () => { |
| 48 | + const { options } = resolveEmailCapabilityArg({ persist: true }, {}); |
| 49 | + expect(options.persist).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('reads OS_EMAIL_PERSIST_ENABLED on the same truth table as OS_EMAIL_QUEUE_ENABLED', () => { |
| 53 | + // One truth table for both flags, which is why `envBooleanFlag` was |
| 54 | + // extracted instead of the list being written a second time: an operator |
| 55 | + // who learned `on` works for the queue flag must not find it silently |
| 56 | + // means "off" here. |
| 57 | + for (const on of ['1', 'true', 'TRUE', 'yes', 'on', ' On ']) { |
| 58 | + expect(resolveEmailCapabilityArg({}, { OS_EMAIL_PERSIST_ENABLED: on }).options.persist, on) |
| 59 | + .toBe(true); |
| 60 | + } |
| 61 | + for (const off of ['0', 'false', 'no', 'off', '']) { |
| 62 | + expect(resolveEmailCapabilityArg({}, { OS_EMAIL_PERSIST_ENABLED: off }).options.persist, off) |
| 63 | + .toBe(false); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + it('lets env override config in BOTH directions', () => { |
| 68 | + // Both directions, because a one-way test passes just as well against a |
| 69 | + // resolver that ignores config entirely. |
| 70 | + expect( |
| 71 | + resolveEmailCapabilityArg({ persist: true }, { OS_EMAIL_PERSIST_ENABLED: 'false' }) |
| 72 | + .options.persist, |
| 73 | + ).toBe(false); |
| 74 | + expect( |
| 75 | + resolveEmailCapabilityArg({ persist: false }, { OS_EMAIL_PERSIST_ENABLED: 'true' }) |
| 76 | + .options.persist, |
| 77 | + ).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not let an UNSET env var read as false over a config that said true', () => { |
| 81 | + // The tri-state `envBooleanFlag` exists for this case: `undefined` must |
| 82 | + // fall through to config, not resolve to `false`. |
| 83 | + expect( |
| 84 | + resolveEmailCapabilityArg({ persist: true }, { OS_EMAIL_QUEUE_ENABLED: 'true' }) |
| 85 | + .options.persist, |
| 86 | + ).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('keeps the queue flag resolving exactly as it did before the extraction', () => { |
| 90 | + // `envBooleanFlag` replaced OS_EMAIL_QUEUE_ENABLED's inline list; these |
| 91 | + // pin that the refactor was behaviour-preserving, empty string included. |
| 92 | + expect(resolveEmailCapabilityArg({}, { OS_EMAIL_QUEUE_ENABLED: 'on' }).options.queueDelivery) |
| 93 | + .toBe(true); |
| 94 | + expect(resolveEmailCapabilityArg({ queueDelivery: true }, { OS_EMAIL_QUEUE_ENABLED: '' }) |
| 95 | + .options.queueDelivery).toBe(false); |
| 96 | + expect(resolveEmailCapabilityArg({ queueDelivery: true }, {}).options.queueDelivery).toBe(true); |
| 97 | + expect(resolveEmailCapabilityArg({}, {})).not.toHaveProperty('options.queueDelivery'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('does not disturb the rest of the resolved options', () => { |
| 101 | + const { options } = resolveEmailCapabilityArg( |
| 102 | + { provider: 'smtp', persist: false, options: { host: 'smtp.acme.test' } }, |
| 103 | + {}, |
| 104 | + ); |
| 105 | + expect(options).toMatchObject({ |
| 106 | + provider: 'smtp', |
| 107 | + persist: false, |
| 108 | + providerOptions: { host: 'smtp.acme.test' }, |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +// ── end to end: resolver output -> real plugin ───────────────────────────── |
| 114 | + |
| 115 | +/** |
| 116 | + * Minimal ObjectQL stand-in. `start()`'s `kernel:ready` handler needs an |
| 117 | + * engine to exist before it decides anything about persistence, and the |
| 118 | + * stranded-outbox sweep reads `sys_email` on the persisting path. |
| 119 | + */ |
| 120 | +function fakeEngine() { |
| 121 | + return { |
| 122 | + async find() { return []; }, |
| 123 | + async insert(_table: string, data: any) { return { id: data?.id }; }, |
| 124 | + async update() { return {}; }, |
| 125 | + async delete() { return { deleted: 0 }; }, |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Boot the real plugin the way `os serve` does — through the resolver — and |
| 131 | + * report whether an `EmailPersistence` was built. |
| 132 | + * |
| 133 | + * `EmailService.setPersistence` is called only on the persisting branch, and |
| 134 | + * it is what puts `persistence` into the live service's options, so reading |
| 135 | + * that back is a direct observation of the branch the plugin took rather than |
| 136 | + * a restatement of the flag it was handed. |
| 137 | + */ |
| 138 | +async function bootThroughResolver( |
| 139 | + cfgEmail: Record<string, any>, |
| 140 | + env: NodeJS.ProcessEnv = {}, |
| 141 | +): Promise<{ persistenceBuilt: boolean; constructedWith: Record<string, unknown> }> { |
| 142 | + const constructedWith = resolveEmailCapabilityArg(cfgEmail, env).options; |
| 143 | + |
| 144 | + const services: Record<string, unknown> = { manifest: { register: () => {} }, objectql: fakeEngine() }; |
| 145 | + const hooks: Record<string, Array<() => Promise<void> | void>> = {}; |
| 146 | + const ctx: any = { |
| 147 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 148 | + getService: (name: string) => { |
| 149 | + if (!(name in services)) throw new Error(`service '${name}' not registered`); |
| 150 | + return services[name]; |
| 151 | + }, |
| 152 | + registerService: (name: string, svc: unknown) => { services[name] = svc; }, |
| 153 | + hook: (name: string, fn: () => Promise<void> | void) => { (hooks[name] ??= []).push(fn); }, |
| 154 | + }; |
| 155 | + |
| 156 | + const plugin = new EmailServicePlugin(constructedWith as any); |
| 157 | + await plugin.init(ctx); |
| 158 | + await plugin.start(ctx); |
| 159 | + for (const fn of hooks['kernel:ready'] ?? []) await fn(); |
| 160 | + |
| 161 | + const service = services.email as { options: { persistence?: unknown } }; |
| 162 | + return { persistenceBuilt: service.options.persistence != null, constructedWith }; |
| 163 | +} |
| 164 | + |
| 165 | +describe('config.email.persist reaches EmailServicePlugin (#5447)', () => { |
| 166 | + it('builds NO EmailPersistence when the config says persist:false', async () => { |
| 167 | + const { persistenceBuilt, constructedWith } = await bootThroughResolver({ persist: false }); |
| 168 | + expect(constructedWith.persist).toBe(false); |
| 169 | + expect(persistenceBuilt).toBe(false); |
| 170 | + }, 60_000); |
| 171 | + |
| 172 | + it('still persists when the config says nothing — behaviour before #5447, unchanged', async () => { |
| 173 | + // The compatibility anchor. Every deployment that never wrote the key is |
| 174 | + // this case, and it must be indistinguishable from the pre-#5447 build. |
| 175 | + const { persistenceBuilt, constructedWith } = await bootThroughResolver({}); |
| 176 | + expect(constructedWith).not.toHaveProperty('persist'); |
| 177 | + expect(persistenceBuilt).toBe(true); |
| 178 | + }, 60_000); |
| 179 | + |
| 180 | + // The two cases below are completeness pins, NOT discriminating ones, and |
| 181 | + // saying so is the point of this comment. Their expected outcome — |
| 182 | + // persistence built — is also what an unwired resolver produces, because the |
| 183 | + // plugin default is ON. Deleting the wiring leaves both GREEN (measured: 8 |
| 184 | + // of these 13 go red, these two and the three default-behaviour pins do |
| 185 | + // not). They are worth keeping as the positive half of the matrix; they are |
| 186 | + // not evidence the carrier exists. The assertions that prove that are the |
| 187 | + // `persist:false` one above and the `OS_EMAIL_PERSIST_ENABLED=false` one |
| 188 | + // below — the two whose expectation DIFFERS from the default. |
| 189 | + it('persists on an explicit persist:true', async () => { |
| 190 | + const { persistenceBuilt } = await bootThroughResolver({ persist: true }); |
| 191 | + expect(persistenceBuilt).toBe(true); |
| 192 | + }, 60_000); |
| 193 | + |
| 194 | + it('lets OS_EMAIL_PERSIST_ENABLED=false switch persistence off over a config that said true', |
| 195 | + async () => { |
| 196 | + const { persistenceBuilt } = await bootThroughResolver( |
| 197 | + { persist: true }, |
| 198 | + { OS_EMAIL_PERSIST_ENABLED: 'false' }, |
| 199 | + ); |
| 200 | + expect(persistenceBuilt).toBe(false); |
| 201 | + }, 60_000); |
| 202 | + |
| 203 | + it('lets OS_EMAIL_PERSIST_ENABLED=true switch it back on over a config that said false', |
| 204 | + async () => { |
| 205 | + const { persistenceBuilt } = await bootThroughResolver( |
| 206 | + { persist: false }, |
| 207 | + { OS_EMAIL_PERSIST_ENABLED: 'true' }, |
| 208 | + ); |
| 209 | + expect(persistenceBuilt).toBe(true); |
| 210 | + }, 60_000); |
| 211 | +}); |
0 commit comments