import { describe, it, expect, afterEach } from 'vitest'; import { CapabilityProviderRegistry } from './capability.registry'; // The registry reads env in its constructor, so each case sets env → constructs a fresh registry → // asserts → restores env. const SMTP_KEYS = ['IIOS_SMTP_HOST', 'IIOS_SMTP_USER', 'IIOS_SMTP_PASS', 'IIOS_PROVIDER_URL_EMAIL']; const saved: Record = {}; function set(env: Record) { for (const k of SMTP_KEYS) { saved[k] = process.env[k]; delete process.env[k]; } for (const [k, v] of Object.entries(env)) if (v != null) process.env[k] = v; } afterEach(() => { for (const k of SMTP_KEYS) { if (saved[k] == null) delete process.env[k]; else process.env[k] = saved[k]; } }); describe('CapabilityProviderRegistry — EMAIL precedence (SMTP > HTTP > sandbox)', () => { it('binds SMTP for EMAIL when the SMTP env trio is set', () => { set({ IIOS_SMTP_HOST: 'smtp.test', IIOS_SMTP_USER: 'accounts@x', IIOS_SMTP_PASS: 'p' }); expect(new CapabilityProviderRegistry().forChannel('EMAIL').name).toBe('smtp'); }); it('binds the HTTP EmailProvider when only IIOS_PROVIDER_URL_EMAIL is set', () => { set({ IIOS_PROVIDER_URL_EMAIL: 'https://relay.test/send' }); expect(new CapabilityProviderRegistry().forChannel('EMAIL').name).toBe('email-http'); }); it('SMTP wins over the HTTP relay when both are set', () => { set({ IIOS_SMTP_HOST: 'smtp.test', IIOS_SMTP_USER: 'accounts@x', IIOS_SMTP_PASS: 'p', IIOS_PROVIDER_URL_EMAIL: 'https://relay.test/send' }); expect(new CapabilityProviderRegistry().forChannel('EMAIL').name).toBe('smtp'); }); it('falls back to the sandbox when neither is configured', () => { set({}); expect(new CapabilityProviderRegistry().forChannel('EMAIL').name).toBe('sandbox'); }); });