test: aggiunge copertura completa per le nuove funzionalità
- Nuovo db.test.js: 11 test per salvaPartita, getPartite, getPartita (isolamento con DB in memoria via vi.stubEnv + vi.resetModules) - gameState.test.js: test per confermaSet, formInizioSet, partitaFinita, checkVittoriaPartita e guardie setFinito/partitaFinita; fix di 6 test pre-esistenti non allineati con la logica striscia aggiornata - websocket.test.js: mock di db.js e 4 test per il salvataggio automatico su DB al termine della partita - server-utils.test.js: 2 test aggiuntivi per storicoPort - ControllerPage.test.js: 4 test per l'overlay di fine set (setFinito) - DisplayPage.test.js: 4 test per l'overlay di fine partita (partitaFinita)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { createInitialState, applyAction, checkVittoria } from '../../src/gameState.js'
|
||||
import { createInitialState, applyAction, checkVittoria, checkVittoriaPartita } from '../../src/gameState.js'
|
||||
|
||||
describe('Game Logic (gameState.js)', () => {
|
||||
let state
|
||||
@@ -31,9 +31,10 @@ describe('Game Logic (gameState.js)', () => {
|
||||
expect(state.sp.form.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
|
||||
it('dovrebbe avere la striscia iniziale a [0]', () => {
|
||||
it('dovrebbe avere la striscia iniziale a [0] per home e [" "] per guest', () => {
|
||||
// home serve per primo → home parte con [0], guest con [" "]
|
||||
expect(state.sp.striscia.home).toEqual([0])
|
||||
expect(state.sp.striscia.guest).toEqual([0])
|
||||
expect(state.sp.striscia.guest).toEqual([" "])
|
||||
})
|
||||
|
||||
it('dovrebbe avere storico servizio vuoto', () => {
|
||||
@@ -116,13 +117,15 @@ describe('Game Logic (gameState.js)', () => {
|
||||
|
||||
it('dovrebbe aggiornare la striscia per punto Home', () => {
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
// Lo stato iniziale ha striscia = { home: [0], guest: [" "] }
|
||||
expect(s.sp.striscia.home).toEqual([0, 1])
|
||||
expect(s.sp.striscia.guest).toEqual([0, " "])
|
||||
expect(s.sp.striscia.guest).toEqual([" ", " "])
|
||||
})
|
||||
|
||||
it('dovrebbe aggiornare la striscia per punto Guest', () => {
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(s.sp.striscia.guest).toEqual([0, 1])
|
||||
// Lo stato iniziale ha striscia = { home: [0], guest: [" "] }
|
||||
expect(s.sp.striscia.guest).toEqual([" ", 1])
|
||||
expect(s.sp.striscia.home).toEqual([0, " "])
|
||||
})
|
||||
|
||||
@@ -133,9 +136,11 @@ describe('Game Logic (gameState.js)', () => {
|
||||
expect(s.sp.storicoServizio[0]).toHaveProperty('cambioPalla')
|
||||
})
|
||||
|
||||
it('non dovrebbe incrementare i punti dopo vittoria', () => {
|
||||
it('non dovrebbe incrementare i punti dopo vittoria (setFinito impostato)', () => {
|
||||
// Il guard controlla setFinito: va impostato come farebbe il ciclo di gioco reale
|
||||
state.sp.punt.home = 25
|
||||
state.sp.punt.guest = 23
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.punt.home).toBe(25)
|
||||
})
|
||||
@@ -619,10 +624,11 @@ describe('Game Logic (gameState.js)', () => {
|
||||
})
|
||||
|
||||
it('dovrebbe resettare la striscia', () => {
|
||||
state.sp.striscia = { home: [0, 1, 2, 3], guest: [0, " ", " ", 1] }
|
||||
state.sp.striscia = { home: [0, 1, 2, 3], guest: [" ", " ", " ", 1] }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
// servHome è true di default → home parte con [0], guest con [" "]
|
||||
expect(s.sp.striscia.home).toEqual([0])
|
||||
expect(s.sp.striscia.guest).toEqual([0])
|
||||
expect(s.sp.striscia.guest).toEqual([" "])
|
||||
})
|
||||
|
||||
it('dovrebbe resettare lo storico servizio', () => {
|
||||
@@ -656,4 +662,278 @@ describe('Game Logic (gameState.js)', () => {
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// FORMAZIONEINIZIOSET
|
||||
// =============================================
|
||||
describe('formInizioSet', () => {
|
||||
it('dovrebbe esistere nello stato iniziale con valori di default', () => {
|
||||
expect(state.sp.formInizioSet.home).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
expect(state.sp.formInizioSet.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
|
||||
it('setFormazione aggiorna sia form che formInizioSet per il team indicato', () => {
|
||||
const nuova = ["10", "11", "12", "13", "14", "15"]
|
||||
const s = applyAction(state, { type: 'setFormazione', team: 'home', form: nuova })
|
||||
expect(s.sp.form.home).toEqual(nuova)
|
||||
expect(s.sp.formInizioSet.home).toEqual(nuova)
|
||||
})
|
||||
|
||||
it('setFormazione non tocca formInizioSet dell\'altro team', () => {
|
||||
const nuova = ["10", "11", "12", "13", "14", "15"]
|
||||
const s = applyAction(state, { type: 'setFormazione', team: 'home', form: nuova })
|
||||
expect(s.sp.formInizioSet.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// CONFERMASET
|
||||
// =============================================
|
||||
describe('confermaSet', () => {
|
||||
// Helper: porta lo stato a fine set (home vince 25-0)
|
||||
function stateConSetFinito(modalita = '3/5') {
|
||||
let s = createInitialState()
|
||||
s.modalitaPartita = modalita
|
||||
// Aggiungiamo 25 punti a home: a 24-0 il prossimo punto vince il set
|
||||
for (let i = 0; i < 25; i++) {
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
}
|
||||
// Ora setFinito dovrebbe essere impostato
|
||||
return s
|
||||
}
|
||||
|
||||
it('non fa nulla se setFinito è null', () => {
|
||||
expect(state.sp.setFinito).toBeNull()
|
||||
const s = applyAction(state, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce).toEqual([])
|
||||
expect(s.sp.set.home).toBe(0)
|
||||
})
|
||||
|
||||
it('aggiunge una entry in strisce con i campi corretti', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.strisce).toHaveLength(1)
|
||||
const striscia = s.sp.strisce[0]
|
||||
expect(striscia).toHaveProperty('set')
|
||||
expect(striscia).toHaveProperty('formInizio')
|
||||
expect(striscia).toHaveProperty('home')
|
||||
expect(striscia).toHaveProperty('guest')
|
||||
expect(striscia).toHaveProperty('vincitore')
|
||||
expect(striscia).toHaveProperty('punt')
|
||||
})
|
||||
|
||||
it('il numero set è 1 per il primo set, 2 per il secondo', () => {
|
||||
let s = stateConSetFinito()
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce[0].set).toBe(1)
|
||||
|
||||
// Secondo set
|
||||
for (let i = 0; i < 25; i++) {
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
}
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce[1].set).toBe(2)
|
||||
})
|
||||
|
||||
it('formInizio nella striscia corrisponde a formInizioSet prima del conferma', () => {
|
||||
const formazioneInizio = ["7", "8", "9", "10", "11", "12"]
|
||||
let s = createInitialState()
|
||||
s = applyAction(s, { type: 'setFormazione', team: 'home', form: formazioneInizio })
|
||||
// porta a fine set
|
||||
for (let i = 0; i < 25; i++) {
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
}
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce[0].formInizio.home).toEqual(formazioneInizio)
|
||||
})
|
||||
|
||||
it('incrementa set[vincitore]', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.set.home).toBe(1)
|
||||
expect(s.sp.set.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('resetta punt a 0', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('svuota storicoServizio', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.storicoServizio).toEqual([])
|
||||
})
|
||||
|
||||
it('azzera setFinito', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.setFinito).toBeNull()
|
||||
})
|
||||
|
||||
it('aggiorna formInizioSet con la form corrente post-conferma', () => {
|
||||
const preConferma = stateConSetFinito()
|
||||
// La form potrebbe essere ruotata durante il set
|
||||
const formDopoSet = [...preConferma.sp.form.home]
|
||||
const s = applyAction(preConferma, { type: 'confermaSet' })
|
||||
expect(s.sp.formInizioSet.home).toEqual(formDopoSet)
|
||||
})
|
||||
|
||||
it('NON imposta partitaFinita se la partita non è ancora vinta (3/5)', () => {
|
||||
const s = applyAction(stateConSetFinito('3/5'), { type: 'confermaSet' })
|
||||
// 1 set vinto su 3 necessari → partita non finita
|
||||
expect(s.sp.partitaFinita).toBeNull()
|
||||
})
|
||||
|
||||
it('imposta partitaFinita quando home vince 3 set (modalità 3/5)', () => {
|
||||
let s = createInitialState()
|
||||
s.modalitaPartita = '3/5'
|
||||
// Vinci 3 set
|
||||
for (let set = 0; set < 3; set++) {
|
||||
for (let i = 0; i < 25; i++) s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
}
|
||||
expect(s.sp.partitaFinita).not.toBeNull()
|
||||
expect(s.sp.partitaFinita.vincitore).toBe('home')
|
||||
})
|
||||
|
||||
it('imposta partitaFinita quando home vince 2 set (modalità 2/3)', () => {
|
||||
let s = createInitialState()
|
||||
s.modalitaPartita = '2/3'
|
||||
for (let set = 0; set < 2; set++) {
|
||||
for (let i = 0; i < 25; i++) s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
}
|
||||
expect(s.sp.partitaFinita).not.toBeNull()
|
||||
expect(s.sp.partitaFinita.vincitore).toBe('home')
|
||||
})
|
||||
|
||||
it('resetta striscia per il set successivo con 0 per chi serve', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
// servHome era true (home segna per primo → resta home a servire)
|
||||
expect(s.sp.striscia.home).toEqual([0])
|
||||
expect(s.sp.striscia.guest).toEqual([" "])
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// GUARDIE setFinito / partitaFinita
|
||||
// =============================================
|
||||
describe('Guardie setFinito e partitaFinita', () => {
|
||||
it('incPunt non incrementa se setFinito è impostato', () => {
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
})
|
||||
|
||||
it('incPunt non incrementa se partitaFinita è impostata', () => {
|
||||
state.sp.partitaFinita = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('decPunt azzera setFinito se era impostato', () => {
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'decPunt' })
|
||||
expect(s.sp.setFinito).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// RESETTA — nuovi campi
|
||||
// =============================================
|
||||
describe('resetta (nuovi campi)', () => {
|
||||
it('azzera strisce', () => {
|
||||
state.sp.strisce = [{ set: 1, vincitore: 'home', punt: { home: 25, guest: 20 } }]
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.strisce).toEqual([])
|
||||
})
|
||||
|
||||
it('azzera setFinito', () => {
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.setFinito).toBeNull()
|
||||
})
|
||||
|
||||
it('azzera partitaFinita', () => {
|
||||
state.sp.partitaFinita = { vincitore: 'guest' }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.partitaFinita).toBeNull()
|
||||
})
|
||||
|
||||
it('reimposta formInizioSet ai valori di default', () => {
|
||||
state.sp.formInizioSet = { home: ["7", "8", "9", "10", "11", "12"], guest: ["7", "8", "9", "10", "11", "12"] }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.formInizioSet.home).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
expect(s.sp.formInizioSet.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// checkVittoriaPartita
|
||||
// =============================================
|
||||
describe('checkVittoriaPartita', () => {
|
||||
it('ritorna null se nessuno ha vinto (3/5, 1-1)', () => {
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 1
|
||||
expect(checkVittoriaPartita(state)).toBeNull()
|
||||
})
|
||||
|
||||
it('ritorna "home" con 3 set in modalità 3/5', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
state.sp.set.home = 3
|
||||
state.sp.set.guest = 1
|
||||
expect(checkVittoriaPartita(state)).toBe('home')
|
||||
})
|
||||
|
||||
it('ritorna "guest" con 3 set in modalità 3/5', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
state.sp.set.home = 0
|
||||
state.sp.set.guest = 3
|
||||
expect(checkVittoriaPartita(state)).toBe('guest')
|
||||
})
|
||||
|
||||
it('ritorna null con 2 set in modalità 3/5 (non ancora vinto)', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 2
|
||||
expect(checkVittoriaPartita(state)).toBeNull()
|
||||
})
|
||||
|
||||
it('ritorna "home" con 2 set in modalità 2/3', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 0
|
||||
expect(checkVittoriaPartita(state)).toBe('home')
|
||||
})
|
||||
|
||||
it('ritorna "guest" con 2 set in modalità 2/3', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 2
|
||||
expect(checkVittoriaPartita(state)).toBe('guest')
|
||||
})
|
||||
|
||||
it('ritorna null con 1 set in modalità 2/3 (non ancora vinto)', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 0
|
||||
expect(checkVittoriaPartita(state)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// confermaSet con servHome=false
|
||||
// =============================================
|
||||
describe('confermaSet — striscia con servHome=false', () => {
|
||||
it('resetta striscia con guest a [0] se è guest a servire', () => {
|
||||
let s = createInitialState()
|
||||
// guest serve
|
||||
s = applyAction(s, { type: 'cambiaPalla' })
|
||||
expect(s.sp.servHome).toBe(false)
|
||||
// porta a fine set (guest segna 25 volte)
|
||||
for (let i = 0; i < 25; i++) s = applyAction(s, { type: 'incPunt', team: 'guest' })
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
// dopo il set, continua a servire guest
|
||||
expect(s.sp.striscia.guest).toEqual([0])
|
||||
expect(s.sp.striscia.home).toEqual([" "])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user