import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { clearEmailSenderCache, resolveEmailSenderAsync } from '../../email/resolve.js';
import { setEmailProviderFactory, setEmailSender } from '../../email/sender.js';
import { EMAIL_CONFIG_COLLECTION } from '../../email/settings-store.js';
import { initDB } from '../../db.js';
function adapter(tag) {
    return { tag, send: async () => { } };
}
function dbWithConfig(data) {
    return {
        document: {
            findFirst: async ({ where }) => where?.collection === EMAIL_CONFIG_COLLECTION && data ? { data } : null,
        },
    };
}
describe('resolveEmailSenderAsync', () => {
    beforeEach(() => {
        delete globalThis.__actuateConfig;
        setEmailSender(null);
        setEmailProviderFactory(null);
        clearEmailSenderCache();
        initDB(dbWithConfig(null));
    });
    afterEach(() => {
        setEmailSender(null);
        setEmailProviderFactory(null);
        clearEmailSenderCache();
        delete globalThis.__actuateConfig;
    });
    it('returns null when nothing is configured', async () => {
        expect(await resolveEmailSenderAsync(dbWithConfig(null))).toBeNull();
    });
    it('prefers the platform adapter over everything', async () => {
        const platform = adapter('platform');
        globalThis.__actuateConfig = { platform: { email: platform } };
        setEmailSender(adapter('env'));
        setEmailProviderFactory(() => adapter('db'));
        const resolved = await resolveEmailSenderAsync(dbWithConfig({ provider: 'resend', from: 'a@b.co', apiKeyEncrypted: 'plain-key' }));
        expect(resolved.tag).toBe('platform');
    });
    it('prefers DB-configured settings over the env-registered sender', async () => {
        setEmailSender(adapter('env'));
        const seen = [];
        setEmailProviderFactory((settings) => {
            seen.push(settings);
            return adapter('db');
        });
        const resolved = await resolveEmailSenderAsync(dbWithConfig({
            provider: 'resend',
            from: 'a@b.co',
            apiKeyEncrypted: 'plain-key',
            updatedAt: '2026-01-01T00:00:00.000Z',
        }));
        expect(resolved.tag).toBe('db');
        expect(seen).toEqual([{ provider: 'resend', from: 'a@b.co', apiKey: 'plain-key' }]);
    });
    it('falls back to the env sender when no DB config or factory exists', async () => {
        setEmailSender(adapter('env'));
        // Factory registered but no DB config:
        setEmailProviderFactory(() => adapter('db'));
        expect((await resolveEmailSenderAsync(dbWithConfig(null))).tag).toBe('env');
        // DB config present but no factory (plugin not installed):
        setEmailProviderFactory(null);
        clearEmailSenderCache();
        expect((await resolveEmailSenderAsync(dbWithConfig({ provider: 'resend', from: 'a@b.co', apiKeyEncrypted: 'k' }))).tag).toBe('env');
    });
    it('falls back to the env sender when the DB config is incomplete', async () => {
        setEmailSender(adapter('env'));
        setEmailProviderFactory(() => adapter('db'));
        const resolved = await resolveEmailSenderAsync(dbWithConfig({ provider: 'resend', from: 'a@b.co' }));
        expect(resolved.tag).toBe('env');
    });
    it('memoizes the built adapter per config stamp', async () => {
        let builds = 0;
        setEmailProviderFactory(() => {
            builds++;
            return adapter('db');
        });
        const db = dbWithConfig({
            provider: 'resend',
            from: 'a@b.co',
            apiKeyEncrypted: 'k',
            updatedAt: '2026-01-01T00:00:00.000Z',
        });
        await resolveEmailSenderAsync(db);
        await resolveEmailSenderAsync(db);
        expect(builds).toBe(1);
    });
});
//# sourceMappingURL=resolve.test.js.map