Fase 1b: cableado del matcher (ingesta polling, runner shadow/full, comparador)

- config: bloque matcher (MATCHER_MODE off/shadow/full, IRON_PASSWORD, audiencia,
  poll, batch) + validacion
- db: colecciones subscriptions/activefires/notifications_shadow/matcher_state +
  ensureMatcherIndexes (2dsphere en notifications_shadow para el dedupe 500m)
- matcher/context: MatchContext sobre Mongo real (candidatas geo $near 1000km +
  audiencia; dedupe geo $near 500m; lang del owner)
- matcher/ingest: polling de activefires por createdAt>checkpoint (Mongo 3.2 sin
  change streams), checkpoint persistido en matcher_state
- matcher/runner: loop shadow(->notifications_shadow)/full(->notifications),
  idempotente por dedupe+checkpoint
- matcher/compare: comparador de shadow (shadowOnly=spam, realOnly=faltantes,
  contentMismatches) — el gate antes del cutover
- index: arranca el matcher; fake-repo ampliado (upsert, sort, $gt/$lt)
- docs/env/README actualizados

73 tests en verde, typecheck y build OK.
This commit is contained in:
vjrj 2026-07-13 18:07:06 +02:00
parent b3aeb6108f
commit a1ed206985
13 changed files with 531 additions and 19 deletions

52
test/compare.test.ts Normal file
View file

@ -0,0 +1,52 @@
import { describe, expect, it } from 'vitest';
import { ObjectId } from 'mongodb';
import { compareShadow, notifKey } from '../src/matcher/compare.js';
import type { NotificationDoc } from '../src/types.js';
function n(userId: string, lon: number, lat: number, content = 'c'): NotificationDoc {
return {
_id: new ObjectId(),
userId,
content,
geo: { type: 'Point', coordinates: [lon, lat] },
type: 'mobile',
when: new Date('2026-07-13T12:00:00Z'),
sealed: 's',
};
}
describe('compareShadow', () => {
it('matches identical sets', () => {
const a = [n('u1', -3.7, 40.4), n('u2', -4, 41)];
const b = [n('u1', -3.7, 40.4), n('u2', -4, 41)];
const diff = compareShadow(a, b);
expect(diff.matched).toBe(2);
expect(diff.shadowOnly).toHaveLength(0);
expect(diff.realOnly).toHaveLength(0);
});
it('flags shadowOnly (spam risk) when we generate an extra notification', () => {
const shadow = [n('u1', -3.7, 40.4), n('u3', -5, 42)];
const real = [n('u1', -3.7, 40.4)];
const diff = compareShadow(shadow, real);
expect(diff.shadowOnly).toEqual([notifKey(n('u3', -5, 42))]);
expect(diff.realOnly).toHaveLength(0);
});
it('flags realOnly (missing) when node-red had one we do not', () => {
const shadow = [n('u1', -3.7, 40.4)];
const real = [n('u1', -3.7, 40.4), n('u2', -4, 41)];
const diff = compareShadow(shadow, real);
expect(diff.realOnly).toEqual([notifKey(n('u2', -4, 41))]);
expect(diff.shadowOnly).toHaveLength(0);
});
it('reports content mismatches on matched keys', () => {
const shadow = [n('u1', -3.7, 40.4, 'nuevo texto')];
const real = [n('u1', -3.7, 40.4, 'viejo texto')];
const diff = compareShadow(shadow, real);
expect(diff.matched).toBe(1);
expect(diff.contentMismatches).toHaveLength(1);
expect(diff.contentMismatches[0]).toMatchObject({ shadow: 'nuevo texto', real: 'viejo texto' });
});
});