import { pino } from 'pino'; import { ObjectId } from 'mongodb'; import type { Repos } from '../src/db.js'; import type { Config } from '../src/config.js'; import type { FcmClient, FcmSendResult } from '../src/fcm.js'; import type { Mailer } from '../src/mailer.js'; import type { EmailContent, PushPayload } from '../src/messages.js'; import type { NotificationDoc, UserDoc } from '../src/types.js'; import { makeRepos } from './fake-repo.js'; export const silentLog = pino({ level: 'silent' }); /** Fresh in-memory repos with the idempotency unique index applied. */ export async function freshRepos(): Promise { const repos = makeRepos(); await repos.sends.createIndex({ notificationId: 1, channel: 1 }, { unique: true, name: 'uniq' }); return repos; } export function testCfg(over: Partial = {}): Config { return { mode: 'full', killSwitch: false, ownedChannels: ['fcm', 'email'], mongoUrl: 'mongodb://x', dbName: 'fuegos', redis: { host: 'x', port: 6379, db: 0 }, poll: { intervalMs: 1000, batchSize: 500 }, limits: { perUserPerDay: 20, maxPerBatch: 2000 }, canary: { userIds: [], subsIds: [] }, retries: { attempts: 3, backoffMs: 100 }, fcm: {}, email: { from: 'test ' }, rootUrl: 'https://fuegos.comunes.org/', gmaps: {}, logLevel: 'silent', serviceName: 'tcef-notifications', ...over, }; } export function aNotif(over: Partial = {}): NotificationDoc { return { _id: new ObjectId(), userId: 'u1', subsId: new ObjectId(), content: '🔥 Fuego cerca:', geo: { type: 'Point', coordinates: [-8.5, 42.3] }, type: 'mobile', when: new Date('2026-07-12T18:30:00Z'), sealed: 'fire-1', ...over, }; } export function aUser(over: Partial = {}): UserDoc { return { _id: 'u1', lang: 'es', fireBaseToken: 'tok-abc', profile: { name: { first: 'Ana' } }, emails: [{ address: 'ana@example.com', verified: true }], ...over, }; } /** Recording FCM fake with programmable result. */ export function fakeFcm(result: FcmSendResult | (() => FcmSendResult)): FcmClient & { calls: Array<{ token: string; payload: PushPayload }> } { const calls: Array<{ token: string; payload: PushPayload }> = []; return { calls, async send(token, payload) { calls.push({ token, payload }); return typeof result === 'function' ? result() : result; }, }; } export function fakeMailer(opts: { fail?: boolean } = {}): Mailer & { calls: Array<{ to: string; content: EmailContent }> } { const calls: Array<{ to: string; content: EmailContent }> = []; return { calls, async send(to, content) { if (opts.fail) throw new Error('smtp down'); calls.push({ to, content }); }, }; }