tcef-notifications/test/poller.test.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

81 lines
3 KiB
TypeScript

import { beforeEach, describe, expect, it } from 'vitest';
import type { Queue } from 'bullmq';
import type { Repos } from '../src/db.js';
import type { Config } from '../src/config.js';
import { createPoller } from '../src/poller.js';
import { pendingFilter } from '../src/poller.js';
import type { Channel, SendJob } from '../src/types.js';
import { aNotif, freshRepos, silentLog, testCfg } from './helpers.js';
let repos: Repos;
const baseCfg: Config = testCfg();
beforeEach(async () => {
repos = await freshRepos();
});
/** Fake queue recording add() calls. */
function fakeQueues() {
const added: Record<Channel, SendJob[]> = { fcm: [], email: [] };
const make = (ch: Channel) =>
({
async add(_name: string, job: SendJob) {
added[ch].push(job);
},
}) as unknown as Queue<SendJob>;
return { queues: { fcm: make('fcm'), email: make('email') } as Record<Channel, Queue<SendJob>>, added };
}
describe('pendingFilter', () => {
it('uses the CORRECT field names (old cron had a typo)', () => {
expect(pendingFilter('fcm')).toEqual({ type: 'mobile', notified: { $ne: true } });
expect(pendingFilter('email')).toEqual({ type: 'web', emailNotified: { $ne: true } });
});
});
describe('poller.runOnce', () => {
it('enqueues only pending docs of owned channels and claims them', async () => {
await repos.notifications.insertMany([
aNotif({ type: 'mobile' }), // pending fcm
aNotif({ type: 'mobile', notified: true }), // already done
aNotif({ type: 'web' }), // pending email
aNotif({ type: 'web', emailNotified: true }), // already done
]);
const { queues, added } = fakeQueues();
const poller = createPoller(baseCfg, repos, queues, silentLog);
const res = await poller.runOnce();
expect(res).toEqual({ fcm: 1, email: 1 });
expect(added.fcm).toHaveLength(1);
expect(added.email).toHaveLength(1);
// claimedBy provenance set
expect(await repos.notifications.countDocuments({ claimedBy: baseCfg.serviceName })).toBe(2);
});
it('only polls owned channels', async () => {
await repos.notifications.insertMany([aNotif({ type: 'mobile' }), aNotif({ type: 'web' })]);
const { queues, added } = fakeQueues();
const cfg = { ...baseCfg, ownedChannels: ['fcm'] as Channel[] };
const poller = createPoller(cfg, repos, queues, silentLog);
const res = await poller.runOnce();
expect(res.fcm).toBe(1);
expect(added.email).toHaveLength(0);
});
it('circuit breaker halts on oversized batch', async () => {
const many = Array.from({ length: 5 }, () => aNotif({ type: 'mobile' }));
await repos.notifications.insertMany(many);
const { queues, added } = fakeQueues();
const cfg = { ...baseCfg, ownedChannels: ['fcm'] as Channel[], limits: { ...baseCfg.limits, maxPerBatch: 3 } };
const poller = createPoller(cfg, repos, queues, silentLog);
const res = await poller.runOnce();
// Batch of 5 > max 3 -> nothing enqueued, poller halted.
expect(res.fcm).toBe(0);
expect(added.fcm).toHaveLength(0);
});
});