import { describe, expect, it } from 'vitest'; import { nlStats, activeSources, languageSignal, type NlStats } from './nl-stats'; // The identified total is a deduplicated DID union, never a sum. These invariants keep a bad // aggregate (or an accidental sum) from shipping a wrong headline. The loader also enforces // them at build time; this pins the behaviour and documents the contract. describe('nl-stats aggregate', () => { const counts = Object.values(nlStats.identified.bySource); it('is a real dedupe: total is at least the largest single source', () => { expect(nlStats.identified.total).toBeGreaterThanOrEqual(Math.max(0, ...counts)); }); it('is a union, not a sum: total never exceeds the summed sources', () => { const sum = counts.reduce((a, b) => a + b, 0); expect(nlStats.identified.total).toBeLessThanOrEqual(sum); }); it('exposes only the sources that have identified accounts', () => { for (const s of activeSources) { expect(nlStats.identified.bySource[s]).toBeGreaterThan(0); } const inactive = (['nlHandle', 'starterpack', 'sifaNl', 'sifaCompany'] as const).filter( (s) => !activeSources.includes(s), ); for (const s of inactive) { expect(nlStats.identified.bySource[s]).toBe(0); } }); // Language is a signal, never confirmation: Dutch is spoken in Belgium too, so "posted in // Dutch" cannot place an account in the Netherlands. It must stay out of the floor. it('never counts the language signal into the identified floor', () => { expect(Object.keys(nlStats.identified.bySource)).not.toContain('dutchPoster'); if (languageSignal) { expect(languageSignal.count).toBeGreaterThan(0); // The floor may not silently absorb it: signalOnly accounts are outside the union. expect(languageSignal.alsoConfirmed).toBeLessThanOrEqual(nlStats.identified.total); expect(languageSignal.alsoConfirmed + languageSignal.signalOnly).toBe(languageSignal.count); } }); it('carries an estimate tier with a positive modelled figure', () => { expect(nlStats.estimate.modelledNl).toBeGreaterThan(0); expect(nlStats.estimate.blueskyActive).toBeGreaterThan(0); }); it('rejects a summed (non-deduped) total', () => { const bad = structuredClone(nlStats) as NlStats; bad.identified.bySource = { nlHandle: 100, starterpack: 100, sifaNl: 0, sifaCompany: 0 }; bad.identified.total = 200; // sum, not a union // total (200) == sum (200) is allowed (no overlap); 201 is not. bad.identified.total = 201; const sum = Object.values(bad.identified.bySource).reduce((a, b) => a + b, 0); expect(bad.identified.total).toBeGreaterThan(sum); }); });