tcef-notifications/test/helpers.ts
vjrj a5ba2cdcf1 Fase 1a: driver mongodb v3.7 (produccion es MongoDB 3.2)
El smoke-test contra prod revelo MongoDB 3.2.11 (EOL): el driver v6 exige >=4.2
y no conecta. Cambios:

- mongodb ^3.7.4 + @types/mongodb; db.ts con useUnifiedTopology, sin retryWrites
- sends.ts: deteccion de duplicate-key (11000) sin depender de MongoServerError
- poller.ts: FilterQuery en vez de Filter
- tests de integracion migrados de mongodb-memory-server a un fake en memoria
  (test/fake-repo.ts) porque no hay mongod 3.x ejecutable en hosts modernos;
  emula operadores ($ne/$in/$gte/$exists), $set/$unset e indice unico (11000)
- docs: legacy-behavior.md §0 (entorno MongoDB 3.2, sin change streams -> fase
  1b usara polling; recomendacion de upgrade de Mongo como fase propia)

47 tests en verde, typecheck y build OK. Datos reales del import: 413 pendientes
FCM, 0 email, 8176 users con fireBaseToken.
2026-07-13 00:58:27 +02:00

88 lines
2.7 KiB
TypeScript

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<Repos> {
const repos = makeRepos();
await repos.sends.createIndex({ notificationId: 1, channel: 1 }, { unique: true, name: 'uniq' });
return repos;
}
export function testCfg(over: Partial<Config> = {}): 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 <t@x>' },
rootUrl: 'https://fuegos.comunes.org/',
gmaps: {},
logLevel: 'silent',
serviceName: 'tcef-notifications',
...over,
};
}
export function aNotif(over: Partial<NotificationDoc> = {}): 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> = {}): 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 });
},
};
}