import { createHash } from 'node:crypto' import { readFile } from 'node:fs/promises' import { describe, expect, it } from 'vitest' import { FACTOR_BOUNDS, SCORE_TREE, SCORE_VERSION, scoreEntity, scoreTreeDocument, treeScore, } from './scoring.mjs' import { scoreEligible } from './metrics.mjs' const here = (name) => new URL(name, import.meta.url) const FORBIDDEN_COMPOSITES = new Set(['experimental', 'wildcard', 'misc', 'other', 'test']) function treeHash(tree = scoreTreeDocument()) { return createHash('sha256').update(JSON.stringify(tree.categories)).digest('hex') } function allHundred() { return Object.fromEntries( SCORE_TREE.categories.flatMap((category) => category.factors.map((factor) => [factor.id, 100]), ), ) } describe('composite invariants', () => { it('has 4-8 meaningful composites with unique ids and positive weights', () => { expect(SCORE_TREE.categories.length).toBeGreaterThanOrEqual(4) expect(SCORE_TREE.categories.length).toBeLessThanOrEqual(8) const ids = new Set() for (const category of SCORE_TREE.categories) { expect(ids.has(category.id)).toBe(false) ids.add(category.id) expect(category.name).toBeTruthy() expect(category.icon).toBeTruthy() expect(category.vibe).toBeTruthy() expect(category.detail).toBeTruthy() expect(category.weight).toBeGreaterThan(0) expect(FORBIDDEN_COMPOSITES.has(category.name)).toBe(false) } const total = SCORE_TREE.categories.reduce((sum, category) => sum + category.weight, 0) expect(total).toBeCloseTo(1) }) it('assigns every scorer to exactly one composite', () => { const seen = new Set() for (const category of SCORE_TREE.categories) { for (const factor of category.factors) { expect(seen.has(factor.id)).toBe(false) seen.add(factor.id) expect(factor.name).toBeTruthy() expect(factor.scale).toBeTruthy() expect(factor.detail).toBeTruthy() expect(factor.weight).toBeGreaterThan(0) expect(['clamp', 'divide', 'none']).toContain(FACTOR_BOUNDS[factor.id]?.bound) } } }) it('keeps every raw scorer in the tree and no raw scorer outside it', () => { const result = scoreEntity( { publishedAt: '2026-10-26T00:00:00.000Z', textContent: 'A short post.' }, { now: Date.parse('2026-10-26T00:00:00.000Z') }, ) const treeIds = SCORE_TREE.categories .flatMap((category) => category.factors.map((factor) => factor.id)) .sort() expect(Object.keys(result.raw).sort()).toEqual(treeIds) expect(Object.keys(result.bounded).sort()).toEqual(treeIds) }) it('builds the final score only from composites, not orphan scorers', () => { const factors = allHundred() expect(treeScore(factors)).toBeCloseTo(100) expect(treeScore({ ...factors, notInTheTree: 0 })).toBeCloseTo(100) const withoutOneComposite = { ...factors } for (const factor of SCORE_TREE.categories[0].factors) withoutOneComposite[factor.id] = 0 expect(treeScore(withoutOneComposite)).toBeCloseTo( 100 - SCORE_TREE.categories[0].weight * 100, ) }) }) describe('canonical version', () => { it('keeps the emitted tree on the current version', async () => { const checked = JSON.parse(await readFile(here('./score-tree.json'), 'utf8')) expect(checked).toEqual(scoreTreeDocument()) expect(checked.version).toBe(SCORE_VERSION) expect(checked.schema).toBe(1) }) it('records every canonical tree in the audit history', async () => { const history = JSON.parse(await readFile(here('./score-tree-history.json'), 'utf8')) const versions = history.versions.map((entry) => entry.version) expect(new Set(versions).size).toBe(versions.length) expect(versions).toEqual([...versions].sort((a, b) => a - b)) const latest = history.versions.at(-1) expect(latest.version).toBe(SCORE_VERSION) expect(latest.summary).toBeTruthy() expect(latest.treeHash).toBe(treeHash()) }) it('publishes the canonical tree in the site snapshot', async () => { const data = JSON.parse( await readFile(here('../../web/public/panoptiblog/data.json'), 'utf8'), ) expect(data.method.version).toBe(SCORE_VERSION) expect(data.method.tree).toEqual(SCORE_TREE) expect(data.accounts).toEqual([]) expect(data.method.thresholds.minimumWords).toBe(1_000) expect(data.publications.every((publication) => publication.wordCount >= 1_000)).toBe(true) expect(data.publications.every((publication) => publication.language === 'eng')).toBe(true) expect(data.publications.every((publication) => publication.author?.did)).toBe(true) expect(data.cohort).toHaveLength(data.totals.rankedPublications) expect(data.cohort[0].f).toHaveLength( SCORE_TREE.categories.reduce((count, category) => count + category.factors.length, 0), ) expect( data.publications.every( (publication) => publication.publisherPercentile === undefined && publication.bestRealBloggerPercentile === undefined, ), ).toBe(true) }) }) describe('opt-out invariant', () => { const base = { spam: { excluded: false }, wordCount: 1_000, discoverable: true, language: 'eng', } it('keeps undiscoverable publications out of the scored cohort', () => { expect(scoreEligible({ ...base, discoverable: false }, 1_000)).toBe(false) expect(scoreEligible({ ...base, discoverable: false }, 1_000, { includeUndiscoverable: true })).toBe( true, ) }) it('keeps non-English publications out of the scored cohort', () => { expect(scoreEligible({ ...base, language: 'spa' }, 1_000)).toBe(false) expect(scoreEligible({ ...base, language: 'spa' }, 1_000, { includeNonEnglish: true })).toBe(true) }) it('also keeps spam and under-minimum publications out', () => { expect(scoreEligible({ ...base, spam: { excluded: true } }, 1_000)).toBe(false) expect(scoreEligible({ ...base, wordCount: 999 }, 1_000)).toBe(false) expect(scoreEligible(base, 1_000)).toBe(true) }) })