import { beforeEach, describe, expect, it } from 'vitest' import { getCoolState, getCoolVoteTally, isValidCoolSessionId, recordCoolVote, resetCoolDemoStore, resetCoolRound, updateCoolState, } from '../server/utils/cool-votes' describe('Cool City demo state and vote tally', () => { beforeEach(() => resetCoolDemoStore()) it('starts with the Ruth card and 10:00, shortest, closed', () => { const state = getCoolState('preselection') expect(state.scene).toBe('ruth') expect(state.time).toBe('10:00') expect(state.selectedRoute).toBe('shortest') expect(state.voteOpen).toBe(false) expect(getCoolVoteTally('preselection').total).toBe(0) }) it('updates scene, time, route and vote phase', () => { updateCoolState('preselection', { scene: 'routes', time: '16:00', selectedRoute: 'accessible' }) updateCoolState('preselection', { scene: 'vote', voteOpen: true }) const state = getCoolState('preselection') expect(state.scene).toBe('vote') expect(state.time).toBe('16:00') expect(state.selectedRoute).toBe('accessible') expect(state.voteOpen).toBe(true) }) it('does not count duplicate sessions', () => { updateCoolState('preselection', { scene: 'vote', voteOpen: true }) const first = recordCoolVote('preselection', 'accessible', 'session_12345678') expect(first.duplicate).toBe(false) expect(first.tally.counts.accessible).toBe(1) const duplicate = recordCoolVote('preselection', 'shortest', 'session_12345678') expect(duplicate.duplicate).toBe(true) expect(duplicate.route).toBe('accessible') expect(duplicate.tally.counts.shortest).toBe(0) expect(duplicate.tally.total).toBe(1) }) it('keeps preselection and final rounds separate', () => { updateCoolState('preselection', { scene: 'vote', voteOpen: true }) updateCoolState('final', { scene: 'vote', voteOpen: true }) recordCoolVote('preselection', 'shortest', 'session_aaaaaaaa') recordCoolVote('final', 'accessible', 'session_bbbbbbbb') expect(getCoolVoteTally('preselection').counts.shortest).toBe(1) expect(getCoolVoteTally('final').counts.accessible).toBe(1) expect(getCoolVoteTally('final').counts.shortest).toBe(0) }) it('refuses votes before the round is open', () => { expect(() => recordCoolVote('preselection', 'accessible', 'session_12345678')).toThrow( 'Voting is not open', ) }) it('resets votes and state for one round only', () => { updateCoolState('preselection', { scene: 'vote', voteOpen: true }) updateCoolState('final', { scene: 'vote', voteOpen: true }) recordCoolVote('preselection', 'accessible', 'session_aaaaaaaa') recordCoolVote('final', 'shortest', 'session_bbbbbbbb') resetCoolRound('preselection') expect(getCoolState('preselection').scene).toBe('ruth') expect(getCoolVoteTally('preselection').total).toBe(0) expect(getCoolVoteTally('final').total).toBe(1) }) it('accepts opaque random session IDs and rejects unsafe values', () => { expect(isValidCoolSessionId('8a1f9a2e-9e2c-4f6b-9c2b-3a4b5c6d7e8f')).toBe(true) expect(isValidCoolSessionId('session_12345678')).toBe(true) expect(isValidCoolSessionId('short')).toBe(false) expect(isValidCoolSessionId('has spaces in it')).toBe(false) expect(isValidCoolSessionId('')).toBe(false) expect(isValidCoolSessionId(undefined)).toBe(false) }) })