import { describe, expect, it } from 'vitest' import { reduce } from './reducer.js' import { STUB_WAYPOINTS, computeOutcome, computeVoteResult, pendingResolutions, projectFor, stationInteraction, workersMetTargets, } from './selectors.js' import { balance, timerFor } from './config.js' import { DIDS, doActivity, openVote, run, setup } from './testutil.js' import type { GameConfig, GameState } from './types.js' const [SAB, A, B, C] = DIDS // A minimal game (1 resource, target 1) for outcome tests. function smallGame(): GameState { const config: GameConfig = { resources: ['wood'], targets: { wood: 1 }, roundDurationMs: 1_000, timer1Ms: 2_000, timer2Ms: 5_000, timer3Ms: 8_000, slotCooldownMs: 3_000, debrisTtlMs: 15_000, voteDurationMs: 45_000, moveSpeedPxPerSec: 140, saboteurGraceMs: 45_000, } let s = reduce(undefined, { type: 'gameCreated', serverTime: 0, code: 'X', config, stations: [ { id: 'res-wood', pos: { x: 0, y: 0 }, area: { x: -8, y: -8, w: 16, h: 16 }, slotCount: 2, collect: ['wood'], deposit: [], scoreboard: [], meeting: false }, { id: 'deposit', pos: { x: 0, y: 1 }, area: { x: -8, y: -8, w: 16, h: 16 }, slotCount: 2, collect: [], deposit: ['wood'], scoreboard: [], meeting: false }, { id: 'meeting', pos: { x: 0, y: 2 }, area: { x: -8, y: -8, w: 16, h: 16 }, slotCount: 1, collect: [], deposit: [], scoreboard: [], meeting: true }, ], }) for (const did of [SAB, A]) { s = reduce(s, { type: 'playerJoined', serverTime: 0, did, handle: did, spriteRef: null, pos: { x: 0, y: 0 } }) } return reduce(s, { type: 'roundStarted', serverTime: 1, roles: { [SAB]: 'saboteur', [A]: 'worker' }, spawns: { [SAB]: { x: 0, y: 0 }, [A]: { x: 0, y: 0 } }, endsAt: 1_001, }) } // A game with a multi-use station ('general': collects ore, accepts wood deposits, AND a scoreboard) // plus a wood collector and a pure deposit point — for the carry/discard menu rules. stationInteraction // ignores position, so every station can share an origin area. function combinedGame(): GameState { const { config } = balance({ playerCount: 2, roundDurationMs: 600_000, resourceCount: 4 }) const area = { x: -8, y: -8, w: 16, h: 16 } let s = reduce(undefined, { type: 'gameCreated', serverTime: 0, code: 'Z', config, stations: [ { id: 'res-wood', pos: { x: 0, y: 0 }, area, slotCount: 3, collect: ['wood'], deposit: [], scoreboard: [], meeting: false }, { id: 'general', pos: { x: 0, y: 0 }, area, slotCount: 3, collect: ['ore'], deposit: ['wood'], scoreboard: ['wood'], meeting: false }, { id: 'drop', pos: { x: 0, y: 0 }, area, slotCount: 3, collect: [], deposit: ['wood'], scoreboard: [], meeting: false }, ], }) for (const did of [SAB, A]) { s = reduce(s, { type: 'playerJoined', serverTime: 0, did, handle: did, spriteRef: null, pos: { x: 0, y: 0 } }) } return reduce(s, { type: 'roundStarted', serverTime: 1, roles: { [SAB]: 'saboteur', [A]: 'worker' }, spawns: { [SAB]: { x: 0, y: 0 }, [A]: { x: 0, y: 0 } }, endsAt: 600_001, }) } describe('projection: movement path', () => { it('gives self the full route + destination but others only a short forward stub and no destination', () => { const s = setup({ startRound: true }) const dest = { x: 200, y: 200 } const path = [ { x: 10, y: 10 }, { x: 20, y: 20 }, { x: 30, y: 30 }, { x: 40, y: 40 }, ] const moving: GameState = { ...s, players: { ...s.players, [A]: { ...s.players[A]!, dest, path } }, } // the moving player sees their whole path and where they're headed const self = projectFor(moving, A).players[A]! expect(self.path).toEqual(path) expect(self.dest).toEqual(dest) // everyone else sees at most STUB_WAYPOINTS waypoints (near-term heading) and never the destination const other = projectFor(moving, SAB).players[A]! expect(other.path).toEqual(path.slice(0, STUB_WAYPOINTS)) expect(other.path.length).toBeLessThanOrEqual(STUB_WAYPOINTS) expect(other.dest).toBeNull() // at round end the full route is revealed to all const ended = reduce(moving, { type: 'roundEnded', serverTime: 9, outcome: 'saboteur' }) expect(projectFor(ended, SAB).players[A]?.path).toEqual(path) }) }) describe('projection: roles', () => { it('hides other players roles, shows your own, reveals all at game end', () => { const s = setup({ startRound: true }) const forA = projectFor(s, A) expect(forA.players[A]?.role).toBe('worker') expect(forA.players[SAB]?.role).toBeNull() const ended = reduce(s, { type: 'roundEnded', serverTime: 9, outcome: 'saboteur' }) expect(projectFor(ended, A).players[SAB]?.role).toBe('saboteur') }) }) describe('projection: slot secrecy', () => { it('never exposes per-slot occupancy or cooldown — only capacity', () => { let s = setup({ startRound: true }) s = doActivity(s, A, 'res-wood', 0, 'collect', 100) // slot 0 now cooling const st = projectFor(s, B).stations['res-wood']! expect(st.slotCount).toBe(3) expect('slots' in st).toBe(false) expect('collectCooldownUntil' in st).toBe(false) }) it('does not reveal which slot another player is occupying', () => { let s = setup({ startRound: true }) s = reduce(s, { type: 'actionStarted', serverTime: 100, did: A, stationId: 'res-wood', slot: 2, kind: 'collect', endsAt: 2_100, }) const active = projectFor(s, B).players[A]!.active! expect(active.stationId).toBe('res-wood') // which station is public (they're standing there) expect('slot' in active).toBe(false) // which slot is secret expect(active.kind).toBeNull() // and the activity kind too }) }) describe('projection: sabotaged station', () => { it('appears as working until the viewer has inspected it', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'res-wood', 0, 'sabotage', 100) expect(projectFor(s, B).stations['res-wood']?.state).toBe('working') s = doActivity(s, A, 'res-wood', 1, 'inspect', 9_000) expect(projectFor(s, A).stations['res-wood']?.state).toBe('sabotaged') expect(projectFor(s, B).stations['res-wood']?.state).toBe('working') // B still in the dark }) it('never leaks the trigger timing, even to someone who has inspected', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'res-wood', 0, 'sabotage', 100) s = doActivity(s, A, 'res-wood', 1, 'inspect', 9_000) // A now knows it's sabotaged const st = projectFor(s, A).stations['res-wood']! expect(st.state).toBe('sabotaged') // the WHAT is allowed expect('collectionsSinceSabotage' in st).toBe(false) // the WHEN never leaves the server expect('sabotagedAt' in st).toBe(false) }) it('repairing a sabotaged station is indistinguishable from repairing a working one', () => { // case 1: B repairs an untouched (working) station let working = setup({ startRound: true }) working = doActivity(working, B, 'res-wood', 0, 'repair', 9_000) // case 2: B repairs a secretly-sabotaged station, never having inspected it let sabotaged = setup({ startRound: true }) sabotaged = doActivity(sabotaged, SAB, 'res-wood', 1, 'sabotage', 100) sabotaged = doActivity(sabotaged, B, 'res-wood', 0, 'repair', 9_000) const a = stationInteraction(working, B, 'res-wood')! const b = stationInteraction(sabotaged, B, 'res-wood')! // both read as working with the same options — B cannot tell which one had been sabotaged, // and in both cases knows the next collection will succeed expect(a.state).toBe('working') expect(b.state).toBe('working') expect(b.broken).toBe(a.broken) expect(b.options).toEqual(a.options) }) }) describe('projection: carry + active authenticity', () => { it('hides fake-vs-real carry and the activity kind from others', () => { let s = setup({ startRound: true }) // SAB mid-sabotage (started, not resolved) s = reduce(s, { type: 'actionStarted', serverTime: 100, did: SAB, stationId: 'res-wood', slot: 0, kind: 'sabotage', endsAt: 100 + timerFor('sabotage', s.config), }) const otherView = projectFor(s, A).players[SAB] expect(otherView?.active?.kind).toBeNull() // dwell visible, choice hidden expect(otherView?.active?.endsAt).toBeGreaterThan(100) expect(projectFor(s, SAB).players[SAB]?.active?.kind).toBe('sabotage') // self sees it }) it('hides carry authenticity from others but not from self', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'res-wood', 0, 'pretendCollect', 100) expect(projectFor(s, A).players[SAB]?.carrying).toEqual({ resourceId: 'wood', authentic: null }) expect(projectFor(s, SAB).players[SAB]?.carrying).toEqual({ resourceId: 'wood', authentic: false }) }) }) describe('projection: debris', () => { it('hides authenticity until the viewer inspects it', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'res-wood', 0, 'pretendCollect', 100) s = reduce(s, { type: 'itemDropped', serverTime: 5_000, did: SAB, debrisId: 'd1', pos: { x: 1, y: 1 } }) expect(projectFor(s, A).debris['d1']?.authentic).toBeNull() s = reduce(s, { type: 'debrisInspected', serverTime: 6_000, did: A, debrisId: 'd1' }) expect(projectFor(s, A).debris['d1']?.authentic).toBe(false) }) }) describe('stationInteraction', () => { // options are now {kind, resourceId?} objects; compare by kind const kinds = (m: NonNullable>) => m.options.map((o) => o.kind) it('offers workers collect/inspect/repair on a working station, no sabotage tools', () => { const s = setup({ startRound: true }) const menu = stationInteraction(s, A, 'res-wood')! expect(menu.broken).toBe(false) expect(menu.canCollect).toBe(true) expect(kinds(menu)).toEqual(expect.arrayContaining(['doNothing', 'collect', 'inspect', 'repair'])) expect(kinds(menu)).not.toContain('sabotage') // collect is offered per in-play resource, carrying its resourceId for the client to label expect(menu.options).toContainEqual({ kind: 'collect', resourceId: 'wood' }) }) it('when every slot is busy/cooling you cannot collect (no collect option offered)', () => { let s = setup({ startRound: true }) // res-wood has 3 slots // all three slots get collected from, depleting the station s = doActivity(s, A, 'res-wood', 0, 'collect', 100) s = doActivity(s, B, 'res-wood', 1, 'collect', 100) s = doActivity(s, C, 'res-wood', 2, 'collect', 100) // every slot is now regenerating, so the saboteur cannot collect either const menu = stationInteraction(s, SAB, 'res-wood')! expect(menu.canCollect).toBe(false) expect(kinds(menu)).not.toContain('collect') // and the disguise is hidden too — fake collect can't be offered when a real one couldn't expect(kinds(menu)).not.toContain('pretendCollect') expect(kinds(menu)).toContain('inspect') // non-collect actions still available (free slot) }) it('offers the saboteur their extra tools', () => { const s = setup({ startRound: true }) const menu = stationInteraction(s, SAB, 'res-wood')! expect(kinds(menu)).toEqual(expect.arrayContaining(['pretendCollect', 'breakStation', 'sabotage'])) expect(menu.options).toContainEqual({ kind: 'pretendCollect', resourceId: 'wood' }) }) it('reports broken and limits options to repair (+ fakeRepair for the saboteur)', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'res-wood', 0, 'breakStation', 100) const worker = stationInteraction(s, A, 'res-wood')! expect(worker.broken).toBe(true) expect(kinds(worker)).toEqual(['doNothing', 'repair']) const sab = stationInteraction(s, SAB, 'res-wood')! expect(kinds(sab)).toEqual(['doNothing', 'repair', 'fakeRepair']) }) it('the saboteur knows their own sabotage for free; workers must inspect to learn', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'res-wood', 0, 'sabotage', 100) expect(stationInteraction(s, SAB, 'res-wood')!.state).toBe('sabotaged') // free knowledge expect(stationInteraction(s, A, 'res-wood')!.state).toBe('working') // worker unaware // a worker inspecting learns it; the saboteur inspecting just confirms what they know (cover) s = doActivity(s, A, 'res-wood', 1, 'inspect', 9_000) expect(stationInteraction(s, A, 'res-wood')!.state).toBe('sabotaged') }) it('fakeRepair is offered on broken and sabotaged (cover), but omitted on working (no-op)', () => { let s = setup({ startRound: true }) expect(kinds(stationInteraction(s, SAB, 'res-wood')!)).not.toContain('fakeRepair') // working s = doActivity(s, SAB, 'res-wood', 0, 'sabotage', 100) // the saboteur (who knows it's sabotaged) is offered fakeRepair — cover that keeps the trap expect(kinds(stationInteraction(s, SAB, 'res-wood')!)).toContain('fakeRepair') // sabotaged let s2 = setup({ startRound: true }) s2 = doActivity(s2, SAB, 'res-wood', 0, 'breakStation', 100) expect(kinds(stationInteraction(s2, SAB, 'res-wood')!)).toContain('fakeRepair') // broken }) it('carrying real: a pure deposit point demands a deposit; elsewhere actions become "discard and X"', () => { let s = setup({ startRound: true }) s = doActivity(s, A, 'res-wood', 0, 'collect', 100) // now carrying real wood // res-wood is collect-only: you can't deposit here, so every action becomes discard-and-X const wood = stationInteraction(s, A, 'res-wood')! expect(kinds(wood)).toEqual(expect.arrayContaining(['doNothing', 'collect', 'inspect', 'repair'])) expect(kinds(wood)).not.toContain('deposit') // ...and each is flagged discardFirst (it drops the carried wood, leaving debris, first) expect(wood.options.find((o) => o.kind === 'collect')?.discardFirst).toBe(true) expect(wood.options.find((o) => o.kind === 'inspect')?.discardFirst).toBe(true) // the deposit point is pure deposit and accepts wood: you must deposit, no discard escape hatch const dep = stationInteraction(s, A, 'deposit')! expect(dep.options).toEqual([{ kind: 'doNothing' }, { kind: 'deposit', resourceId: 'wood' }]) }) it('special stations are stateful: scoreboard/meeting offer their action + inspect/repair', () => { const s = setup({ startRound: true }) const sb = stationInteraction(s, A, 'scoreboard')! expect(kinds(sb)).toEqual(expect.arrayContaining(['scoreboard', 'inspect', 'repair'])) const mt = stationInteraction(s, A, 'meeting')! expect(kinds(mt)).toEqual(expect.arrayContaining(['callVote', 'inspect', 'repair'])) }) it('the saboteur gets a pretend-check + break at the scoreboard, and break at the voting station', () => { const s = setup({ startRound: true }) const sb = stationInteraction(s, SAB, 'scoreboard')! // "check resources" + a fake "pretend to check" (never rolls) + the standard sabotage toolkit expect(kinds(sb)).toEqual(expect.arrayContaining(['scoreboard', 'pretendScoreboard', 'breakStation', 'sabotage'])) const mt = stationInteraction(s, SAB, 'meeting')! // a voting station has no fake variant — just the break/sabotage tools alongside callVote expect(kinds(mt)).toEqual(expect.arrayContaining(['callVote', 'breakStation', 'sabotage'])) expect(kinds(mt)).not.toContain('pretendScoreboard') }) it('callVote is suppressed while a vote is already running', () => { let s = setup({ startRound: true }) expect(kinds(stationInteraction(s, A, 'meeting')!)).toContain('callVote') s = openVote(s, B, 100) // B opens a vote expect(s.vote).not.toBeNull() expect(kinds(stationInteraction(s, A, 'meeting')!)).not.toContain('callVote') }) it('a broken special station offers only repair (+ fakeRepair for the saboteur), no action', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'scoreboard', 0, 'breakStation', 100) expect(kinds(stationInteraction(s, A, 'scoreboard')!)).toEqual(['doNothing', 'repair']) expect(kinds(stationInteraction(s, SAB, 'scoreboard')!)).toEqual(['doNothing', 'repair', 'fakeRepair']) }) it('a pure deposit point gates bare-handed break/sabotage; fake-carry cover unlocks a cost-1 break', () => { let s = setup({ startRound: true }) // the standard deposit point is deposit-only (no collect): empty-handed the saboteur may // inspect/repair it, but NOT freely break or sabotage it — that needs fake-carry cover. const bare = stationInteraction(s, SAB, 'deposit')! expect(kinds(bare)).toEqual(expect.arrayContaining(['inspect', 'repair'])) expect(kinds(bare)).not.toContain('breakStation') expect(kinds(bare)).not.toContain('sabotage') // a worker, as before, only inspects/repairs const worker = stationInteraction(s, A, 'deposit')! expect(kinds(worker)).toEqual(expect.arrayContaining(['inspect', 'repair'])) expect(kinds(worker)).not.toContain('sabotage') // fake-carry cover: the saboteur fake-collects, then gets a cost-1 break (keeps the fake in hand) // plus a clean fakeDeposit at the deposit point. s = doActivity(s, SAB, 'res-wood', 0, 'pretendCollect', 100) const covered = stationInteraction(s, SAB, 'deposit')! const brk = covered.options.find((o) => o.kind === 'breakStation') expect(brk).toBeDefined() expect(brk?.discardFirst).toBeFalsy() // the carve-out spends the fake cleanly, no incriminating drop expect(covered.options).toContainEqual({ kind: 'fakeDeposit', resourceId: 'wood' }) }) }) describe('stationInteraction: multi-use stations + carry', () => { const kinds = (m: NonNullable>) => m.options.map((o) => o.kind) it('a real carrier may deposit at a multi-use station OR discard-and-X for anything else', () => { let s = combinedGame() s = doActivity(s, A, 'res-wood', 0, 'collect', 100) // A carries real wood const m = stationInteraction(s, A, 'general')! // deposit the wood (no discard)... expect(m.options).toContainEqual({ kind: 'deposit', resourceId: 'wood' }) // ...or drop it to do something else — those options are flagged discardFirst expect(m.options.find((o) => o.kind === 'inspect')?.discardFirst).toBe(true) expect(m.options.find((o) => o.kind === 'collect')?.discardFirst).toBe(true) // discard-and-collect ore }) it('a saboteur really carrying gets "discard and break" at a multi-use station', () => { let s = combinedGame() s = doActivity(s, SAB, 'res-wood', 0, 'collect', 100) // saboteurs can carry real resources too const m = stationInteraction(s, SAB, 'general')! expect(m.options).toContainEqual({ kind: 'deposit', resourceId: 'wood' }) expect(m.options.find((o) => o.kind === 'breakStation')?.discardFirst).toBe(true) // drop first }) it('a fake carrier gets a cost-1 break + clean fakeDeposit; other tools are discard-only', () => { let s = combinedGame() s = doActivity(s, SAB, 'res-wood', 0, 'pretendCollect', 100) // fake wood const m = stationInteraction(s, SAB, 'general')! expect(m.options.find((o) => o.kind === 'breakStation')?.discardFirst).toBeFalsy() // spends the fake expect(m.options).toContainEqual({ kind: 'fakeDeposit', resourceId: 'wood' }) // sabotage is not a carve-out, so it's only reachable as discard-and-sabotage (drops the fake) expect(m.options.find((o) => o.kind === 'sabotage')?.discardFirst).toBe(true) }) it('a pure deposit point demands a deposit from a real carrier — no discard escape hatch', () => { let s = combinedGame() s = doActivity(s, A, 'res-wood', 0, 'collect', 100) expect(kinds(stationInteraction(s, A, 'drop')!)).toEqual(['doNothing', 'deposit']) }) }) describe('action cover (saboteur Timer-1 break looks like a failed collect)', () => { it('breakStation is indistinguishable from a collect that hits the sabotage dice', () => { const START = 10_000 // A worker collects on a (secretly) sabotaged station and the dice break it let failColl = setup({ startRound: true }) failColl = doActivity(failColl, SAB, 'res-wood', 0, 'sabotage', 50) const endsA = START + timerFor('collect', failColl.config) failColl = reduce(failColl, { type: 'actionStarted', serverTime: START, did: A, stationId: 'res-wood', slot: 1, kind: 'collect', resourceId: 'wood', endsAt: endsA, }) const aDuring = projectFor(failColl, C).players[A]!.active! const aStationDuring = projectFor(failColl, C).stations['res-wood']!.state failColl = reduce(failColl, { type: 'actionResolved', serverTime: endsA, did: A, rng: { sabotageTriggered: true } }) // The saboteur just breaks a working station let brk = setup({ startRound: true }) const endsB = START + timerFor('breakStation', brk.config) brk = reduce(brk, { type: 'actionStarted', serverTime: START, did: SAB, stationId: 'res-wood', slot: 1, kind: 'breakStation', endsAt: endsB, }) const bDuring = projectFor(brk, C).players[SAB]!.active! const bStationDuring = projectFor(brk, C).stations['res-wood']!.state brk = reduce(brk, { type: 'actionResolved', serverTime: endsB, did: SAB }) // same Timer-1 duration, hidden action kind, station looks fine during then breaks, no resource expect(endsA).toBe(endsB) expect(aDuring.kind).toBeNull() expect(bDuring.kind).toBeNull() expect(aDuring.endsAt).toBe(bDuring.endsAt) expect(aStationDuring).toBe('working') expect(bStationDuring).toBe('working') expect(projectFor(failColl, C).stations['res-wood']!.state).toBe('broken') expect(projectFor(brk, C).stations['res-wood']!.state).toBe('broken') expect(projectFor(failColl, C).players[A]!.carrying).toBeNull() expect(projectFor(brk, C).players[SAB]!.carrying).toBeNull() }) }) describe('social deduction: costly, private, self-defeating proof', () => { it('a watcher proves a sabotage to THEMSELVES via before/after inspect — not to anyone else', () => { let s = setup({ startRound: true }) // SAB saboteur; A watcher; C bystander // BEFORE: A inspects and confirms the station is genuinely working (rules out prior sabotage) s = doActivity(s, A, 'res-wood', 0, 'inspect', 100) expect(projectFor(s, A).stations['res-wood']!.state).toBe('working') // the suspect sabotages it s = doActivity(s, SAB, 'res-wood', 1, 'sabotage', 20_000) // A's knowledge is now STALE — a freshly sabotaged station still reads working until re-checked expect(projectFor(s, A).stations['res-wood']!.state).toBe('working') // AFTER: A pays for a second inspect and catches it s = doActivity(s, A, 'res-wood', 0, 'inspect', 40_000) expect(projectFor(s, A).stations['res-wood']!.state).toBe('sabotaged') // proven to A // ...but a bystander who didn't pay the cost still sees working — the proof can't be shared expect(projectFor(s, C).stations['res-wood']!.state).toBe('working') }) it('a fake repair is exposed by a follow-up inspect; a real repair survives it', () => { // fake repair: still sabotaged underneath let fake = setup({ startRound: true }) fake = doActivity(fake, SAB, 'res-wood', 0, 'breakStation', 100) // broken (public) fake = doActivity(fake, SAB, 'res-wood', 0, 'fakeRepair', 20_000) // looks repaired expect(projectFor(fake, A).stations['res-wood']!.state).toBe('working') fake = doActivity(fake, A, 'res-wood', 1, 'inspect', 40_000) expect(projectFor(fake, A).stations['res-wood']!.state).toBe('sabotaged') // busted // real repair: an inspector finds nothing — honest play defeats the proof let real = setup({ startRound: true }) real = doActivity(real, SAB, 'res-wood', 0, 'breakStation', 100) real = doActivity(real, B, 'res-wood', 0, 'repair', 20_000) real = doActivity(real, A, 'res-wood', 1, 'inspect', 40_000) expect(projectFor(real, A).stations['res-wood']!.state).toBe('working') }) it('single-slot cooldown tell: a real collect shows "No woods" after; a fake collect does not', () => { // a 1-slot wood station, so depletion is visible at the station level const { config } = balance({ playerCount: 2, roundDurationMs: 600_000, resourceCount: 1 }) let g = reduce(undefined, { type: 'gameCreated', serverTime: 0, code: 'Y', config, stations: [{ id: 'res-wood', pos: { x: 0, y: 0 }, area: { x: -8, y: -8, w: 16, h: 16 }, slotCount: 1, collect: ['wood'], deposit: [], scoreboard: [], meeting: false }], }) for (const did of [SAB, A]) { g = reduce(g, { type: 'playerJoined', serverTime: 0, did, handle: did, spriteRef: null, pos: { x: 0, y: 0 } }) } g = reduce(g, { type: 'roundStarted', serverTime: 1, roles: { [SAB]: 'saboteur', [A]: 'worker' }, spawns: { [SAB]: { x: 0, y: 0 }, [A]: { x: 0, y: 0 } }, endsAt: 600_001 }) // a REAL collect cools the only slot — an onlooker now cannot collect ("No woods") const real = doActivity(g, A, 'res-wood', 0, 'collect', 100) expect(stationInteraction(real, SAB, 'res-wood')!.canCollect).toBe(false) // a FAKE collect leaves the slot collectable — the cooldown never triggered (the tell) const fake = doActivity(g, SAB, 'res-wood', 0, 'pretendCollect', 100) expect(stationInteraction(fake, A, 'res-wood')!.canCollect).toBe(true) }) it('a fake resource is incriminating evidence once dropped (a real one is innocuous)', () => { let s = setup({ startRound: true }) s = doActivity(s, SAB, 'res-wood', 0, 'pretendCollect', 100) // saboteur now holds a fake s = reduce(s, { type: 'itemDropped', serverTime: 5_000, did: SAB, debrisId: 'd', pos: { x: 1, y: 1 } }) // before inspecting, an onlooker can't tell the dropped item's authenticity expect(projectFor(s, A).debris['d']!.authentic).toBeNull() // paying to inspect the debris reveals it was fake — evidence a worker's drop would not leave s = reduce(s, { type: 'debrisInspected', serverTime: 6_000, did: A, debrisId: 'd' }) expect(projectFor(s, A).debris['d']!.authentic).toBe(false) }) }) describe('outcome', () => { it('workers win when every target is met', () => { let s = smallGame() s = doActivity(s, A, 'res-wood', 0, 'collect', 10) s = doActivity(s, A, 'deposit', 0, 'deposit', 5_000) expect(workersMetTargets(s)).toBe(true) expect(computeOutcome(s)).toBe('workers') }) it('saboteur wins when targets unmet and not caught', () => { const s = smallGame() expect(workersMetTargets(s)).toBe(false) expect(computeOutcome(s)).toBe('saboteur') }) it('no-one wins when targets unmet but the saboteur was caught', () => { let s = smallGame() s = openVote(s, A, 10) // a vote opens (the only way to kick), then resolves to a kick s = reduce(s, { type: 'voteEnded', serverTime: 5_000, kickedDid: SAB }) expect(computeOutcome(s)).toBe('no-one') }) }) describe('vote result', () => { it('requires a strict majority of the living', () => { let s = setup({ startRound: true }) // 4 alive, threshold = 3 s = openVote(s, A, 10) s = run( s, { type: 'voteCast', serverTime: 3_000, voterDid: A, targetDid: SAB }, { type: 'voteCast', serverTime: 3_001, voterDid: B, targetDid: SAB }, ) expect(computeVoteResult(s)).toBeNull() // only 2 of 4 s = reduce(s, { type: 'voteCast', serverTime: 3_002, voterDid: C, targetDid: SAB }) expect(computeVoteResult(s)).toBe(SAB) // 3 of 4 }) it('voters can change their vote, and a majority makes the vote resolvable on the next tick', () => { let s = setup({ startRound: true }) // 4 alive, threshold = 3 s = openVote(s, A, 10) s = run( s, { type: 'voteCast', serverTime: 3_000, voterDid: A, targetDid: B }, { type: 'voteCast', serverTime: 3_001, voterDid: B, targetDid: SAB }, { type: 'voteCast', serverTime: 3_002, voterDid: C, targetDid: SAB }, ) expect(pendingResolutions(s)).not.toContainEqual({ kind: 'vote' }) // 2/4, timer not up // A changes their mind -> SAB now has a majority, so the vote is due even before the timer s = reduce(s, { type: 'voteCast', serverTime: 3_003, voterDid: A, targetDid: SAB }) expect(computeVoteResult(s)).toBe(SAB) expect(pendingResolutions(s)).toContainEqual({ kind: 'vote' }) }) }) describe('pendingResolutions', () => { it('reports actions, debris, votes and round end that have come due', () => { let s = setup({ startRound: true }) const endsAt = 100 + timerFor('collect', s.config) s = reduce(s, { type: 'actionStarted', serverTime: 100, did: A, stationId: 'res-wood', slot: 0, kind: 'collect', endsAt, }) expect(pendingResolutions(s)).toHaveLength(0) // not due yet s = reduce(s, { type: 'tick', serverTime: endsAt }) expect(pendingResolutions(s)).toContainEqual({ kind: 'action', did: A }) // round end comes due once now passes endsAt const late = reduce(s, { type: 'tick', serverTime: s.endsAt! + 1 }) expect(pendingResolutions(late)).toContainEqual({ kind: 'round' }) }) }) describe('saboteur grace / abort (§3.6)', () => { it('arms the grace timer when the saboteur disconnects mid-round, and reports it once lapsed', () => { let s = setup({ startRound: true }) // SAB (did:s) is the saboteur s = reduce(s, { type: 'playerDisconnected', serverTime: 1_000, did: SAB }) expect(s.saboteurGraceEndsAt).toBe(1_000 + s.config.saboteurGraceMs) expect(pendingResolutions(s)).not.toContainEqual({ kind: 'grace' }) // not lapsed yet s = reduce(s, { type: 'tick', serverTime: s.saboteurGraceEndsAt! }) expect(pendingResolutions(s)).toContainEqual({ kind: 'grace' }) }) it('disarms the abort when the saboteur reconnects in time', () => { let s = setup({ startRound: true }) s = reduce(s, { type: 'playerDisconnected', serverTime: 1_000, did: SAB }) s = reduce(s, { type: 'playerReconnected', serverTime: 2_000, did: SAB }) expect(s.saboteurGraceEndsAt).toBeNull() const late = reduce(s, { type: 'tick', serverTime: 1_000 + s.config.saboteurGraceMs + 1 }) expect(pendingResolutions(late)).not.toContainEqual({ kind: 'grace' }) }) it('does not arm for a worker disconnect', () => { let s = setup({ startRound: true }) s = reduce(s, { type: 'playerDisconnected', serverTime: 1_000, did: A }) // a worker expect(s.saboteurGraceEndsAt).toBeNull() }) it('never leaks the grace deadline through projection (it would identify the saboteur)', () => { let s = setup({ startRound: true }) s = reduce(s, { type: 'playerDisconnected', serverTime: 1_000, did: SAB }) expect('saboteurGraceEndsAt' in projectFor(s, A)).toBe(false) expect('saboteurGraceEndsAt' in projectFor(s, null)).toBe(false) }) })