Something went wrong. Try again.
A Social Deduction / Deception Game
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import { describe, expect, it } from 'vitest'import { attributesFromGenerator, type GeneratorRecord } from './attributes.js'import { allClues, escalateFragment, generateCase, verifyCase, type MapInfo } from './generate.js'import type { SuspectAttributes } from './types.js'
function mulberry32(seed: number): () => number { let a = seed >>> 0 return () => { a = (a + 0x6d2b79f5) | 0 let t = Math.imul(a ^ (a >>> 15), 1 | a) t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t return ((t ^ (t >>> 14)) >>> 0) / 4294967296 }}
function suspect(did: string, layers: Array<[string, string, string?]>): SuspectAttributes { const rec: GeneratorRecord = { bodyType: 'male', layers: layers.map(([id, title, main]) => ({ id, title, colors: main ? { main } : undefined })), } return attributesFromGenerator(did, did, rec)!}
// Core overlap-by-2 lattice (from construct.test) + a spread of varied noise suspects.const POOL: SuspectAttributes[] = [ suspect('C', [['tops', 'Shirt', '#c0392b'], ['bottoms', 'Pants', '#2ecc71'], ['headwear', 'Hard Hat', '#888888']]), suspect('w12', [['tops', 'Shirt', '#c0392b'], ['bottoms', 'Pants', '#2ecc71'], ['headwear', 'Beret', '#111111']]), suspect('w23', [['bottoms', 'Pants', '#2ecc71'], ['headwear', 'Hard Hat', '#888888'], ['tops', 'Tee', '#2244cc']]), suspect('w1', [['tops', 'Shirt', '#c0392b'], ['bottoms', 'Pants', '#34495e'], ['headwear', 'Cap', '#111111']]), suspect('w3', [['headwear', 'Hard Hat', '#888888'], ['tops', 'Tee', '#f1c40f'], ['bottoms', 'Pants', '#34495e']]), suspect('n1', [['tops', 'Hoodie', '#8e44ad'], ['feet', 'Boots', '#000000']]), suspect('n2', [['headwear', 'Beanie', '#16a085'], ['tops', 'Tank', '#e67e22']]), suspect('n3', [['glasses', 'Round', '#222222'], ['tops', 'Blazer', '#2c3e50']]), suspect('n4', [['bottoms', 'Skirt', '#c0392b'], ['feet', 'Sandals', '#ffffff']]), suspect('n5', [['headwear', 'Fedora', '#634f30'], ['tops', 'Vest', '#95a5a6']]), suspect('n6', [['tops', 'Sweater', '#27ae60'], ['bottoms', 'Jeans', '#2980b9']]), suspect('n7', [['headwear', 'Cap', '#c0392b'], ['tops', 'Jersey', '#f1c40f']]), suspect('n8', [['hair', 'Long', '#000000'], ['tops', 'Cardigan', '#d35400']]),]
const MAP: MapInfo = { stations: [ { id: 'st-ore', resource: 'ore', verb: 'collect' }, { id: 'st-crates', resource: 'crates', verb: 'deposit' }, { id: 'st-water', resource: 'water', verb: 'collect' }, { id: 'st-trash', resource: 'trash', verb: 'deposit' }, { id: 'st-tally', resource: 'eggs', verb: 'scoreboard' }, ],}
describe('generateCase — §5 invariants over many seeds', () => { it('every produced case passes verifyCase (over-match invariant + the new stacking rules)', () => { let built = 0 for (let seed = 1; seed <= 40; seed++) { const c = generateCase(POOL, MAP, mulberry32(seed)) if (!c) continue built++ expect(verifyCase(c)).toEqual([]) expect(c.suspects.some((s) => s.did === c.culpritDid)).toBe(true) expect(c.incriminatingClueIds).toHaveLength(3) } expect(built).toBeGreaterThan(30) // the pool reliably supports a build })
it('the culprit is denied in all 3 fakes; every innocent in ≤2 (recomputed over the whole cast)', () => { const c = generateCase(POOL, MAP, mulberry32(3))! const fakes = allClues(c).filter((e) => e.incriminating) const counts = new Map<string, number>() for (const f of fakes) for (const d of f.matchSet) counts.set(d, (counts.get(d) ?? 0) + 1) expect(counts.get(c.culpritDid)).toBe(3) for (const [d, n] of counts) if (d !== c.culpritDid) expect(n).toBeLessThanOrEqual(2) })
it('non-incriminating sources match their own fragment and own it when questioned', () => { const c = generateCase(POOL, MAP, mulberry32(5))! const noise = allClues(c).filter((e) => !e.incriminating) expect(noise.length).toBeGreaterThan(0) for (const piece of noise) expect(piece.matchSet).toContain(piece.trueSource) })
it('every incriminating clue is the deepest slot of a non-working station, gated behind ≥1 ordinary clue', () => { for (let seed = 1; seed <= 20; seed++) { const c = generateCase(POOL, MAP, mulberry32(seed)) if (!c) continue for (const st of c.stations) { const inc = st.clues.filter((e) => e.incriminating) expect(inc.length).toBeLessThanOrEqual(1) if (inc.length === 1) { expect(st.clues[st.clues.length - 1]!.incriminating).toBe(true) // deepest expect(st.clues.length).toBeGreaterThanOrEqual(2) // gated expect(st.condition).not.toBe('working') } } } })
it('respects the limits: no stack deeper than 3, working ⟺ ≤1 clue, clues run more-suspects-first', () => { for (let seed = 1; seed <= 20; seed++) { const c = generateCase(POOL, MAP, mulberry32(seed)) if (!c) continue for (const st of c.stations) { expect(st.clues.length).toBeLessThanOrEqual(3) expect(st.condition === 'working').toBe(st.clues.length <= 1) const ordinary = st.clues.filter((e) => !e.incriminating) for (let i = 1; i < ordinary.length; i++) { expect(ordinary[i]!.matchSet.length).toBeLessThanOrEqual(ordinary[i - 1]!.matchSet.length) } } } })
it('returns null with fewer than 3 inspectable stations (one host per fake)', () => { expect(generateCase(POOL, { stations: MAP.stations.slice(0, 2) }, mulberry32(1))).toBeNull() })
it('returns null for a pool too uniform to support the rule-of-3 lattice', () => { const uniform = Array.from({ length: 6 }, (_, i) => suspect(`u${i}`, [['tops', `Shirt${i}`, '#000000']])) expect(generateCase(uniform, MAP, mulberry32(1))).toBeNull() })})
describe('escalateFragment — the over-match fix (§3.4 / §5)', () => { const culprit = suspect('C', [['tops', 'Basic T-Shirt', '#c0392b']]) // red const witness = suspect('W', [['tops', 'Basic T-Shirt', '#c0392b']]) // red — must stay matched const overBlue = suspect('O', [['tops', 'Basic T-Shirt', '#2244cc']]) // blue — over-matches the title const overRed = suspect('R', [['tops', 'Basic T-Shirt', '#c0392b']]) // red — inseparable from culprit
it('escalates a common title fragment to the combined token, excluding the over-matcher', () => { const byDid = new Map([culprit, witness, overBlue].map((s) => [s.did, s])) const tighter = escalateFragment('tops#title#Basic T-Shirt', culprit, byDid, ['C', 'W'], 'O') expect(tighter).toBe('tops#both#Basic T-Shirt#red') })
it('returns null when no tightening separates the over-matcher (same item AND colour)', () => { const byDid = new Map([culprit, witness, overRed].map((s) => [s.did, s])) expect(escalateFragment('tops#title#Basic T-Shirt', culprit, byDid, ['C', 'W'], 'R')).toBeNull() })
it('a pool seeded with a common-item over-matcher still yields a clean case (escalate or swap)', () => { const withOverMatcher = [ ...POOL, suspect('n9', [['tops', 'Shirt', '#2244cc'], ['bottoms', 'Pants', '#2ecc71'], ['headwear', 'Hard Hat', '#888888']]), ] for (let seed = 1; seed <= 20; seed++) { const c = generateCase(withOverMatcher, MAP, mulberry32(seed)) if (c) expect(verifyCase(c)).toEqual([]) } })})