test: ripara la suite Vitest e migra gli e2e all'architettura attuale

La suite era allineata a una vecchia forma dello stato (sp.punt/sp.set/
sp.servHome) e a una vecchia architettura e2e (controller su :3001).
Baseline iniziale: 77/170 test Vitest falliti.

Vitest (ora 212/212 verdi):
- gameState.test.js: riscritto con helper che derivano punteggio/set/
  servizio dalla striscia; aggiunto blocco formInizio
- server-utils.js: getNetworkIPs accetta interfacce iniettabili e
  printServerInfo accetta gli IP iniettabili (deterministico anche su WSL);
  filtro LAN unificato (esclude 127./169.254./172.)
- websocket + stress: punteggio letto via punteggio(striscia)
- ControllerPage/DisplayPage: forzato layout mobile (viewport portrait),
  punteggi impostati via striscia; aggiunto test bottone REFERTO
- nuovi: referto.test.js, persist.test.js (mock fs), wsMixin.test.js,
  integration/server.test.js (routing)

Refactor di supporto:
- referto.js: estratta buildRefertoHtml(state, now) pura; generaReferto
  resta wrapper con window.open/print
- server.js: estratti createApp()/startServer(); avvio solo se entrypoint

e2e (migrazione parziale, NON ancora verificata verde):
- tutti i riferimenti controller :3001 -> :3000/controller
- forzato viewport portrait sul controller prima del goto
- reset helper: chiude il dialog di configurazione che doReset apre
- game-simulation: gestione del dialog SET VINTO automatico a 25
This commit is contained in:
2026-06-21 00:36:02 +02:00
parent ddf68010a4
commit eb37f8319f
19 changed files with 931 additions and 302 deletions
+208 -145
View File
@@ -1,5 +1,39 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { createInitialState, applyAction, checkVittoria, checkVittoriaPartita } from '../../src/gameState.js'
import {
createInitialState, applyAction, checkVittoria, checkVittoriaPartita,
punteggio, setVinti, servizio,
} from '../../src/gameState.js'
// =============================================
// HELPER
// Lo stato non memorizza più punteggio/set/servizio direttamente:
// si ricavano dalla striscia tramite le funzioni pure esportate.
// =============================================
// Punteggio del set in corso
const puntDi = (s) => punteggio(s.sp.striscia)
// Set vinti nel match
const setDi = (s) => setVinti(s.sp.striscia)
// true se serve Home
const servDi = (s) => servizio(s.sp.striscia)
// Imposta chi serve a inizio set (set in corso a 0-0)
function setServizioIniziale(state, team) {
state.sp.striscia.at(-1).serv = team === 'home' ? 'h' : 'g'
}
// Imposta il punteggio del set in corso costruendo una ris coerente
function setPunteggio(state, home, guest) {
state.sp.striscia.at(-1).ris = 'h'.repeat(home) + 'g'.repeat(guest)
}
// Aggiunge set già conclusi (vinti) PRIMA del set in corso
function setSetVinti(state, home, guest) {
const conclusi = []
for (let i = 0; i < home; i++) conclusi.push({ serv: 'h', ris: '', vinc: 'h' })
for (let i = 0; i < guest; i++) conclusi.push({ serv: 'g', ris: '', vinc: 'g' })
state.sp.striscia = [...conclusi, state.sp.striscia.at(-1)]
}
describe('Game Logic (gameState.js)', () => {
let state
@@ -13,17 +47,17 @@ describe('Game Logic (gameState.js)', () => {
// =============================================
describe('Stato iniziale', () => {
it('dovrebbe iniziare con 0-0', () => {
expect(state.sp.punt.home).toBe(0)
expect(state.sp.punt.guest).toBe(0)
expect(puntDi(state).home).toBe(0)
expect(puntDi(state).guest).toBe(0)
})
it('dovrebbe avere i set a 0', () => {
expect(state.sp.set.home).toBe(0)
expect(state.sp.set.guest).toBe(0)
expect(setDi(state).home).toBe(0)
expect(setDi(state).guest).toBe(0)
})
it('dovrebbe avere servizio Home', () => {
expect(state.sp.servHome).toBe(true)
expect(servDi(state)).toBe(true)
})
it('dovrebbe avere formazione di default [1-6]', () => {
@@ -69,43 +103,43 @@ describe('Game Logic (gameState.js)', () => {
describe('incPunt', () => {
it('dovrebbe incrementare i punti Home', () => {
const newState = applyAction(state, { type: 'incPunt', team: 'home' })
expect(newState.sp.punt.home).toBe(1)
expect(newState.sp.punt.guest).toBe(0)
expect(puntDi(newState).home).toBe(1)
expect(puntDi(newState).guest).toBe(0)
})
it('dovrebbe incrementare i punti Guest', () => {
const newState = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(newState.sp.punt.guest).toBe(1)
expect(newState.sp.punt.home).toBe(0)
expect(puntDi(newState).guest).toBe(1)
expect(puntDi(newState).home).toBe(0)
})
it('dovrebbe gestire il cambio palla (Guest segna, batteva Home)', () => {
state.sp.servHome = true
setServizioIniziale(state, 'home')
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(s1.sp.servHome).toBe(false)
expect(servDi(s1)).toBe(false)
})
it('dovrebbe gestire il cambio palla (Home segna, batteva Guest)', () => {
state.sp.servHome = false
setServizioIniziale(state, 'guest')
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s1.sp.servHome).toBe(true)
expect(servDi(s1)).toBe(true)
})
it('non dovrebbe cambiare palla se segna chi batte', () => {
state.sp.servHome = true
setServizioIniziale(state, 'home')
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s1.sp.servHome).toBe(true)
expect(servDi(s1)).toBe(true)
})
it('dovrebbe ruotare la formazione al cambio palla', () => {
state.sp.servHome = true
setServizioIniziale(state, 'home')
state.sp.form.guest = ["1", "2", "3", "4", "5", "6"]
const newState = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(newState.sp.form.guest).toEqual(["2", "3", "4", "5", "6", "1"])
})
it('non dovrebbe ruotare la formazione se non c\'è cambio palla', () => {
state.sp.servHome = true
setServizioIniziale(state, 'home')
state.sp.form.home = ["1", "2", "3", "4", "5", "6"]
const newState = applyAction(state, { type: 'incPunt', team: 'home' })
expect(newState.sp.form.home).toEqual(["1", "2", "3", "4", "5", "6"])
@@ -129,10 +163,9 @@ describe('Game Logic (gameState.js)', () => {
})
it('non dovrebbe incrementare i punti dopo vittoria', () => {
state.sp.punt.home = 25
state.sp.punt.guest = 23
setPunteggio(state, 25, 23)
const s = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s.sp.punt.home).toBe(25)
expect(puntDi(s).home).toBe(25)
})
})
@@ -143,33 +176,33 @@ describe('Game Logic (gameState.js)', () => {
it('dovrebbe annullare l\'ultimo punto Home', () => {
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
const s2 = applyAction(s1, { type: 'decPunt' })
expect(s2.sp.punt.home).toBe(0)
expect(s2.sp.punt.guest).toBe(0)
expect(puntDi(s2).home).toBe(0)
expect(puntDi(s2).guest).toBe(0)
})
it('dovrebbe annullare l\'ultimo punto Guest', () => {
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
const s2 = applyAction(s1, { type: 'decPunt' })
expect(s2.sp.punt.home).toBe(0)
expect(s2.sp.punt.guest).toBe(0)
expect(puntDi(s2).home).toBe(0)
expect(puntDi(s2).guest).toBe(0)
})
it('non dovrebbe fare nulla sullo stato iniziale', () => {
const s = applyAction(state, { type: 'decPunt' })
expect(s.sp.punt.home).toBe(0)
expect(s.sp.punt.guest).toBe(0)
expect(puntDi(s).home).toBe(0)
expect(puntDi(s).guest).toBe(0)
})
it('dovrebbe ripristinare il servizio dopo undo con cambio palla', () => {
state.sp.servHome = true
setServizioIniziale(state, 'home')
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(s1.sp.servHome).toBe(false)
expect(servDi(s1)).toBe(false)
const s2 = applyAction(s1, { type: 'decPunt' })
expect(s2.sp.servHome).toBe(true)
expect(servDi(s2)).toBe(true)
})
it('dovrebbe invertire la rotazione dopo undo con cambio palla', () => {
state.sp.servHome = true
setServizioIniziale(state, 'home')
state.sp.form.guest = ["1", "2", "3", "4", "5", "6"]
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(s1.sp.form.guest).toEqual(["2", "3", "4", "5", "6", "1"])
@@ -188,14 +221,66 @@ describe('Game Logic (gameState.js)', () => {
s = applyAction(s, { type: 'incPunt', team: 'home' })
s = applyAction(s, { type: 'incPunt', team: 'guest' })
s = applyAction(s, { type: 'incPunt', team: 'home' })
expect(s.sp.punt.home).toBe(2)
expect(s.sp.punt.guest).toBe(1)
expect(puntDi(s).home).toBe(2)
expect(puntDi(s).guest).toBe(1)
s = applyAction(s, { type: 'decPunt' })
expect(s.sp.punt.home).toBe(1)
expect(puntDi(s).home).toBe(1)
s = applyAction(s, { type: 'decPunt' })
expect(s.sp.punt.guest).toBe(0)
expect(puntDi(s).guest).toBe(0)
s = applyAction(s, { type: 'decPunt' })
expect(s.sp.punt.home).toBe(0)
expect(puntDi(s).home).toBe(0)
})
})
// =============================================
// FORMAZIONE DI PARTENZA (formInizio)
// =============================================
describe('formInizio', () => {
it('dovrebbe salvare la formazione corrente al primo punto del set', () => {
const s = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s.sp.striscia.at(-1).formInizio).toEqual({
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
})
})
it('formInizio è uno snapshot: la rotazione successiva non lo modifica', () => {
setServizioIniziale(state, 'home')
// 1° punto Home: nessun cambio palla, salva formInizio
let s = applyAction(state, { type: 'incPunt', team: 'home' })
// 2° punto Guest: cambio palla → ruota la formazione guest
s = applyAction(s, { type: 'incPunt', team: 'guest' })
expect(s.sp.form.guest).toEqual(["2", "3", "4", "5", "6", "1"])
// lo snapshot resta quello iniziale
expect(s.sp.striscia.at(-1).formInizio.guest).toEqual(["1", "2", "3", "4", "5", "6"])
})
it('decPunt che riporta il set a 0-0 cancella formInizio', () => {
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s1.sp.striscia.at(-1).formInizio).toBeDefined()
const s2 = applyAction(s1, { type: 'decPunt' })
expect(s2.sp.striscia.at(-1).formInizio).toBeUndefined()
})
it('decPunt con punti ancora presenti NON cancella formInizio', () => {
let s = applyAction(state, { type: 'incPunt', team: 'home' })
s = applyAction(s, { type: 'incPunt', team: 'home' })
s = applyAction(s, { type: 'decPunt' })
expect(s.sp.striscia.at(-1).ris).toBe('h')
expect(s.sp.striscia.at(-1).formInizio).toBeDefined()
})
it('ogni set mantiene la propria formInizio', () => {
const custom = ['7', '8', '9', '10', '11', '12']
let s = applyAction(state, { type: 'setFormazione', team: 'home', form: custom })
// primo punto del set 1: salva formInizio custom
s = applyAction(s, { type: 'incPunt', team: 'home' })
// chiude il set 1 e ne apre uno nuovo (formazioni resettate a default)
s = applyAction(s, { type: 'nuovoSet', team: 'home' })
// primo punto del set 2: salva formInizio default
s = applyAction(s, { type: 'incPunt', team: 'home' })
expect(s.sp.striscia[0].formInizio.home).toEqual(custom)
expect(s.sp.striscia.at(-1).formInizio.home).toEqual(['1', '2', '3', '4', '5', '6'])
})
})
@@ -205,24 +290,27 @@ describe('Game Logic (gameState.js)', () => {
describe('incSet', () => {
it('dovrebbe incrementare il set Home', () => {
const s = applyAction(state, { type: 'incSet', team: 'home' })
expect(s.sp.set.home).toBe(1)
expect(setDi(s).home).toBe(1)
})
it('dovrebbe incrementare il set Guest', () => {
const s = applyAction(state, { type: 'incSet', team: 'guest' })
expect(s.sp.set.guest).toBe(1)
expect(setDi(s).guest).toBe(1)
})
it('dovrebbe fare wrap da 2 a 0', () => {
state.sp.set.home = 2
const s = applyAction(state, { type: 'incSet', team: 'home' })
expect(s.sp.set.home).toBe(0)
let s = applyAction(state, { type: 'incSet', team: 'home' })
s = applyAction(s, { type: 'incSet', team: 'home' })
expect(setDi(s).home).toBe(2)
s = applyAction(s, { type: 'incSet', team: 'home' })
expect(setDi(s).home).toBe(0)
})
it('dovrebbe incrementare da 1 a 2', () => {
state.sp.set.home = 1
const s = applyAction(state, { type: 'incSet', team: 'home' })
expect(s.sp.set.home).toBe(2)
let s = applyAction(state, { type: 'incSet', team: 'home' })
expect(setDi(s).home).toBe(1)
s = applyAction(s, { type: 'incSet', team: 'home' })
expect(setDi(s).home).toBe(2)
})
})
@@ -231,18 +319,16 @@ describe('Game Logic (gameState.js)', () => {
// =============================================
describe('nuovoSet', () => {
it('dovrebbe incrementare il set della squadra vincente', () => {
state.sp.punt.home = 25
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
expect(s.sp.set.home).toBe(1)
expect(s.sp.set.guest).toBe(0)
expect(setDi(s).home).toBe(1)
expect(setDi(s).guest).toBe(0)
})
it('dovrebbe azzerare i punti', () => {
state.sp.punt.home = 25
state.sp.punt.guest = 10
it('dovrebbe azzerare i punti nel nuovo set', () => {
setPunteggio(state, 25, 10)
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
expect(s.sp.punt.home).toBe(0)
expect(s.sp.punt.guest).toBe(0)
expect(puntDi(s).home).toBe(0)
expect(puntDi(s).guest).toBe(0)
})
it('dovrebbe aggiungere un nuovo set vuoto alla striscia', () => {
@@ -268,37 +354,35 @@ describe('Game Logic (gameState.js)', () => {
it('dovrebbe ignorare team non valido', () => {
const s = applyAction(state, { type: 'nuovoSet', team: 'invalid' })
expect(s.sp.set.home).toBe(0)
expect(s.sp.set.guest).toBe(0)
expect(setDi(s).home).toBe(0)
expect(setDi(s).guest).toBe(0)
})
it('in 2/3 dovrebbe registrare il set vincente senza resettare il punteggio', () => {
it('in 2/3 alla palla match registra il set vincente senza aprirne uno nuovo', () => {
state.modalitaPartita = '2/3'
state.sp.set.home = 1
state.sp.punt.home = 25
state.sp.punt.guest = 18
setSetVinti(state, 1, 0)
setPunteggio(state, 25, 18)
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
expect(s.sp.set.home).toBe(2)
expect(s.sp.punt.home).toBe(25)
expect(s.sp.punt.guest).toBe(18)
expect(setDi(s).home).toBe(2)
expect(puntDi(s).home).toBe(25)
expect(puntDi(s).guest).toBe(18)
})
it('in 3/5 dovrebbe registrare il set vincente senza resettare il punteggio', () => {
it('in 3/5 alla palla match registra il set vincente senza aprirne uno nuovo', () => {
state.modalitaPartita = '3/5'
state.sp.set.home = 2
state.sp.punt.home = 25
state.sp.punt.guest = 20
setSetVinti(state, 2, 0)
setPunteggio(state, 25, 20)
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
expect(s.sp.set.home).toBe(3)
expect(s.sp.punt.home).toBe(25)
expect(s.sp.punt.guest).toBe(20)
expect(setDi(s).home).toBe(3)
expect(puntDi(s).home).toBe(25)
expect(puntDi(s).guest).toBe(20)
})
it('dovrebbe ignorare nuovoSet se la partita è già finita', () => {
state.modalitaPartita = '2/3'
state.sp.set.home = 2
setSetVinti(state, 2, 0)
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
expect(s.sp.set.home).toBe(2)
expect(setDi(s).home).toBe(2)
})
})
@@ -308,29 +392,33 @@ describe('Game Logic (gameState.js)', () => {
describe('checkVittoriaPartita', () => {
it('in 2/3 restituisce false se nessuno ha 2 set', () => {
state.modalitaPartita = '2/3'
state.sp.set.home = 1
state.sp.set.guest = 0
setSetVinti(state, 1, 0)
expect(checkVittoriaPartita(state)).toBe(false)
})
it('in 2/3 restituisce true se home ha 2 set', () => {
state.modalitaPartita = '2/3'
state.sp.set.home = 2
setSetVinti(state, 2, 0)
expect(checkVittoriaPartita(state)).toBe(true)
})
it('in 3/5 restituisce false se nessuno ha 3 set', () => {
state.modalitaPartita = '3/5'
state.sp.set.home = 2
state.sp.set.guest = 2
setSetVinti(state, 2, 2)
expect(checkVittoriaPartita(state)).toBe(false)
})
it('in 3/5 restituisce true se guest ha 3 set', () => {
state.modalitaPartita = '3/5'
state.sp.set.guest = 3
setSetVinti(state, 0, 3)
expect(checkVittoriaPartita(state)).toBe(true)
})
it('in amichevole restituisce sempre false', () => {
state.modalitaPartita = 'amichevole'
setSetVinti(state, 5, 0)
expect(checkVittoriaPartita(state)).toBe(false)
})
})
// =============================================
@@ -338,27 +426,29 @@ describe('Game Logic (gameState.js)', () => {
// =============================================
describe('cambiaPalla', () => {
it('dovrebbe invertire il servizio a 0-0', () => {
expect(state.sp.servHome).toBe(true)
expect(servDi(state)).toBe(true)
const s = applyAction(state, { type: 'cambiaPalla' })
expect(s.sp.servHome).toBe(false)
expect(servDi(s)).toBe(false)
})
it('dovrebbe tornare a Home con doppio toggle', () => {
let s = applyAction(state, { type: 'cambiaPalla' })
s = applyAction(s, { type: 'cambiaPalla' })
expect(s.sp.servHome).toBe(true)
expect(servDi(s)).toBe(true)
})
it('non dovrebbe cambiare palla se il punteggio non è 0-0', () => {
state.sp.punt.home = 1
setPunteggio(state, 1, 0)
const prima = servDi(state)
const s = applyAction(state, { type: 'cambiaPalla' })
expect(s.sp.servHome).toBe(true)
expect(servDi(s)).toBe(prima)
})
it('non dovrebbe cambiare palla se Guest ha punti', () => {
state.sp.punt.guest = 3
setPunteggio(state, 0, 3)
const prima = servDi(state)
const s = applyAction(state, { type: 'cambiaPalla' })
expect(s.sp.servHome).toBe(true)
expect(servDi(s)).toBe(prima)
})
})
@@ -569,44 +659,37 @@ describe('Game Logic (gameState.js)', () => {
// =============================================
describe('checkVittoria', () => {
it('non dovrebbe dare vittoria a 24-24', () => {
state.sp.punt.home = 24
state.sp.punt.guest = 24
setPunteggio(state, 24, 24)
expect(checkVittoria(state)).toBe(false)
})
it('dovrebbe dare vittoria a 25-23', () => {
state.sp.punt.home = 25
state.sp.punt.guest = 23
setPunteggio(state, 25, 23)
expect(checkVittoria(state)).toBe(true)
})
it('non dovrebbe dare vittoria a 25-24 (serve 2 punti di scarto)', () => {
state.sp.punt.home = 25
state.sp.punt.guest = 24
setPunteggio(state, 25, 24)
expect(checkVittoria(state)).toBe(false)
})
it('dovrebbe dare vittoria a 26-24', () => {
state.sp.punt.home = 26
state.sp.punt.guest = 24
setPunteggio(state, 26, 24)
expect(checkVittoria(state)).toBe(true)
})
it('dovrebbe dare vittoria Guest a 25-20', () => {
state.sp.punt.home = 20
state.sp.punt.guest = 25
setPunteggio(state, 20, 25)
expect(checkVittoria(state)).toBe(true)
})
it('dovrebbe dare vittoria ai vantaggi (30-28)', () => {
state.sp.punt.home = 30
state.sp.punt.guest = 28
setPunteggio(state, 30, 28)
expect(checkVittoria(state)).toBe(true)
})
it('non dovrebbe dare vittoria ai vantaggi senza scarto (28-27)', () => {
state.sp.punt.home = 28
state.sp.punt.guest = 27
setPunteggio(state, 28, 27)
expect(checkVittoria(state)).toBe(false)
})
})
@@ -617,82 +700,64 @@ describe('Game Logic (gameState.js)', () => {
describe('Set decisivo', () => {
it('modalità 3/5: set decisivo dopo 4 set totali → vittoria a 15', () => {
state.modalitaPartita = "3/5"
state.sp.set.home = 2
state.sp.set.guest = 2
state.sp.punt.home = 15
state.sp.punt.guest = 10
setSetVinti(state, 2, 2)
setPunteggio(state, 15, 10)
expect(checkVittoria(state)).toBe(true)
})
it('modalità 3/5: non vittoria a 14-10 nel set decisivo', () => {
state.modalitaPartita = "3/5"
state.sp.set.home = 2
state.sp.set.guest = 2
state.sp.punt.home = 14
state.sp.punt.guest = 10
setSetVinti(state, 2, 2)
setPunteggio(state, 14, 10)
expect(checkVittoria(state)).toBe(false)
})
it('modalità 3/5: vittoria a 15-13 nel set decisivo', () => {
state.modalitaPartita = "3/5"
state.sp.set.home = 2
state.sp.set.guest = 2
state.sp.punt.home = 15
state.sp.punt.guest = 13
setSetVinti(state, 2, 2)
setPunteggio(state, 15, 13)
expect(checkVittoria(state)).toBe(true)
})
it('modalità 3/5: non vittoria a 15-14 nel set decisivo (serve scarto)', () => {
state.modalitaPartita = "3/5"
state.sp.set.home = 2
state.sp.set.guest = 2
state.sp.punt.home = 15
state.sp.punt.guest = 14
setSetVinti(state, 2, 2)
setPunteggio(state, 15, 14)
expect(checkVittoria(state)).toBe(false)
})
it('modalità 3/5: vittoria a 16-14 nel set decisivo', () => {
state.modalitaPartita = "3/5"
state.sp.set.home = 2
state.sp.set.guest = 2
state.sp.punt.home = 16
state.sp.punt.guest = 14
setSetVinti(state, 2, 2)
setPunteggio(state, 16, 14)
expect(checkVittoria(state)).toBe(true)
})
it('modalità 2/3: set decisivo dopo 2 set totali → vittoria a 15', () => {
state.modalitaPartita = "2/3"
state.sp.set.home = 1
state.sp.set.guest = 1
state.sp.punt.home = 15
state.sp.punt.guest = 10
setSetVinti(state, 1, 1)
setPunteggio(state, 15, 10)
expect(checkVittoria(state)).toBe(true)
})
it('modalità 2/3: non vittoria a 24-20 nel set decisivo (soglia 15)', () => {
it('modalità 2/3: non vittoria a 14-10 nel set decisivo (soglia 15)', () => {
state.modalitaPartita = "2/3"
state.sp.set.home = 1
state.sp.set.guest = 1
state.sp.punt.home = 14
state.sp.punt.guest = 10
setSetVinti(state, 1, 1)
setPunteggio(state, 14, 10)
expect(checkVittoria(state)).toBe(false)
})
it('modalità 2/3: set non decisivo (1-0) → soglia 25', () => {
state.modalitaPartita = "2/3"
state.sp.set.home = 1
state.sp.set.guest = 0
state.sp.punt.home = 15
state.sp.punt.guest = 10
setSetVinti(state, 1, 0)
setPunteggio(state, 15, 10)
expect(checkVittoria(state)).toBe(false)
})
it('modalità 3/5: set non decisivo (2-1) → soglia 25', () => {
state.modalitaPartita = "3/5"
state.sp.set.home = 2
state.sp.set.guest = 1
state.sp.punt.home = 15
state.sp.punt.guest = 10
setSetVinti(state, 2, 1)
setPunteggio(state, 15, 10)
expect(checkVittoria(state)).toBe(false)
})
})
@@ -702,15 +767,13 @@ describe('Game Logic (gameState.js)', () => {
// =============================================
describe('Reset', () => {
it('dovrebbe resettare punti e set a zero', () => {
state.sp.punt.home = 10
state.sp.punt.guest = 8
state.sp.set.home = 1
state.sp.set.guest = 1
setSetVinti(state, 1, 1)
setPunteggio(state, 10, 8)
const s = applyAction(state, { type: 'resetta' })
expect(s.sp.punt.home).toBe(0)
expect(s.sp.punt.guest).toBe(0)
expect(s.sp.set.home).toBe(0)
expect(s.sp.set.guest).toBe(0)
expect(puntDi(s).home).toBe(0)
expect(puntDi(s).guest).toBe(0)
expect(setDi(s).home).toBe(0)
expect(setDi(s).guest).toBe(0)
})
it('dovrebbe resettare formazioni a default', () => {
@@ -721,7 +784,7 @@ describe('Game Logic (gameState.js)', () => {
})
it('dovrebbe resettare la striscia a un set vuoto', () => {
state.sp.striscia = [{ serv: 'h', ris: 'hgh' }, { serv: 'h', ris: 'g' }]
state.sp.striscia = [{ serv: 'h', ris: 'hgh', vinc: 'h' }, { serv: 'h', ris: 'g', vinc: null }]
const s = applyAction(state, { type: 'resetta' })
expect(s.sp.striscia).toHaveLength(1)
expect(s.sp.striscia[0].ris).toBe('')
@@ -748,8 +811,8 @@ describe('Game Logic (gameState.js)', () => {
describe('Azione sconosciuta', () => {
it('dovrebbe restituire lo stato invariato con azione non riconosciuta', () => {
const s = applyAction(state, { type: 'azioneInesistente' })
expect(s.sp.punt.home).toBe(0)
expect(s.sp.punt.guest).toBe(0)
expect(puntDi(s).home).toBe(0)
expect(puntDi(s).guest).toBe(0)
})
})
})
+71
View File
@@ -0,0 +1,71 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
// Mock dell'I/O su disco: i test non toccano il filesystem reale né dipendono
// dal path relativo a src/.
vi.mock('fs', () => ({
readFileSync: vi.fn(),
writeFileSync: vi.fn(),
mkdirSync: vi.fn(),
existsSync: vi.fn(),
}))
import * as fs from 'fs'
import { loadState, saveState } from '../../src/persist.js'
import { createInitialState } from '../../src/gameState.js'
describe('Persistenza stato (persist.js)', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.restoreAllMocks()
})
describe('saveState', () => {
it('crea la directory e scrive lo stato serializzato', () => {
const state = createInitialState()
saveState(state)
expect(fs.mkdirSync).toHaveBeenCalledWith(expect.any(String), { recursive: true })
expect(fs.writeFileSync).toHaveBeenCalled()
const [, contenuto, encoding] = fs.writeFileSync.mock.calls[0]
expect(JSON.parse(contenuto)).toEqual(state)
expect(encoding).toBe('utf8')
})
it('non lancia eccezioni se la scrittura fallisce', () => {
fs.mkdirSync.mockImplementation(() => { throw new Error('EACCES') })
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
expect(() => saveState(createInitialState())).not.toThrow()
expect(errSpy).toHaveBeenCalled()
})
})
describe('loadState', () => {
it('legge e fa il parse di un file valido', () => {
const salvato = createInitialState()
salvato.sp.nomi.home = 'Squadra X'
fs.existsSync.mockReturnValue(true)
fs.readFileSync.mockReturnValue(JSON.stringify(salvato))
expect(loadState()).toEqual(salvato)
})
it('ritorna lo stato iniziale se il file non esiste', () => {
fs.existsSync.mockReturnValue(false)
expect(loadState()).toEqual(createInitialState())
})
it('ritorna lo stato iniziale se il JSON è corrotto', () => {
fs.existsSync.mockReturnValue(true)
fs.readFileSync.mockReturnValue('{ questo non è json')
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
expect(loadState()).toEqual(createInitialState())
expect(warnSpy).toHaveBeenCalled()
})
})
})
+110
View File
@@ -0,0 +1,110 @@
import { describe, it, expect } from 'vitest'
import { buildRefertoHtml } from '../../src/referto.js'
import { createInitialState } from '../../src/gameState.js'
// Data fissa per asserzioni deterministiche
const NOW = new Date('2026-03-14T20:30:00')
// Costruisce uno stato con una striscia di set arbitraria
function statoConSet(striscia, extra = {}) {
const state = createInitialState()
state.sp.striscia = striscia
state.sp.nomi = { home: 'Antoniana', guest: 'Rivali' }
return { ...state, ...extra }
}
describe('buildRefertoHtml (referto.js)', () => {
it('esclude i set _phantom dal referto', () => {
const striscia = [
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(20), vinc: 'h' },
{ serv: 'g', ris: '', vinc: 'g', _phantom: true },
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(18), vinc: 'h' },
]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
// due set reali → "Set 1" e "Set 2", mai "Set 3"
expect(html).toContain('Set 1')
expect(html).toContain('Set 2')
expect(html).not.toContain('Set 3')
})
it('calcola il punteggio finale di ogni set dalla ris', () => {
const striscia = [
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(20), vinc: 'h' },
{ serv: 'h', ris: '', vinc: null },
]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
// header del set: "Antoniana 25 · 20 Rivali"
expect(html).toContain('<strong>25</strong>')
expect(html).toContain('<strong>20</strong>')
})
it('conta i set vinti usando vinc', () => {
const striscia = [
{ serv: 'h', ris: '', vinc: 'h' },
{ serv: 'g', ris: '', vinc: 'g' },
{ serv: 'h', ris: '', vinc: 'h' },
{ serv: 'h', ris: '', vinc: null },
]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
// risultato 2 1
expect(html).toContain('2 1')
})
it('ricava il vincitore dal conteggio punti se vinc è nullo', () => {
const striscia = [
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(23), vinc: null },
{ serv: 'h', ris: '', vinc: null },
]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
// il primo set, pur con vinc null, conta come vinto da home → 1 0
expect(html).toContain('1 0')
})
it('include la progressione punto-punto con classi per squadra', () => {
const striscia = [
{ serv: 'h', ris: 'hhg', vinc: null },
]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
expect(html).toContain('punto-h')
expect(html).toContain('punto-g')
expect(html).toContain('1-0')
expect(html).toContain('2-0')
expect(html).toContain('2-1')
})
it('rende la formazione di partenza quando presente', () => {
const striscia = [
{
serv: 'h', ris: 'h', vinc: null,
formInizio: { home: ['4', '8', '15'], guest: ['16', '23', '42'] },
},
]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
expect(html).toContain('Formazione di partenza')
expect(html).toContain('>4<')
expect(html).toContain('>42<')
})
it('mostra "non disponibile" se manca formInizio', () => {
const striscia = [{ serv: 'h', ris: 'h', vinc: null }]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
expect(html).toContain('non disponibile')
})
it('mostra "Nessun punto registrato" per un set senza punti', () => {
const striscia = [{ serv: 'h', ris: '', vinc: null }]
const html = buildRefertoHtml(statoConSet(striscia), NOW)
expect(html).toContain('Nessun punto registrato')
})
it('header contiene nomi squadre, modalità e data iniettata', () => {
const striscia = [{ serv: 'h', ris: '', vinc: null }]
const state = statoConSet(striscia)
state.modalitaPartita = '2/3'
const html = buildRefertoHtml(state, NOW)
expect(html).toContain('Antoniana')
expect(html).toContain('Rivali')
expect(html).toContain('Modalità: 2/3')
expect(html).toContain('14/03/2026')
})
})
+37 -62
View File
@@ -1,14 +1,9 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import * as os from 'os'
import { getNetworkIPs, collectIPs, printServerInfo } from '../../src/server-utils.js'
vi.mock('os', async (importOriginal) => {
return {
...await importOriginal(),
networkInterfaces: vi.fn(() => ({}))
}
})
import { getNetworkIPs, printServerInfo } from '../../src/server-utils.js'
// Nota: gli IP vengono iniettati nei test (oggetto stile os.networkInterfaces o
// array di IP), così i risultati sono deterministici su qualsiasi piattaforma —
// incluso WSL, dove getNetworkIPs() userebbe altrimenti PowerShell.
describe('Server Utils', () => {
@@ -17,85 +12,73 @@ describe('Server Utils', () => {
})
// =============================================
// getNetworkIPs
// getNetworkIPs / collectIPs
// =============================================
describe('getNetworkIPs', () => {
it('dovrebbe restituire indirizzi IPv4 non-loopback', () => {
os.networkInterfaces.mockReturnValue({
eth0: [
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
]
})
expect(getNetworkIPs()).toEqual(['192.168.1.100'])
expect(getNetworkIPs({
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }]
})).toEqual(['192.168.1.100'])
})
it('dovrebbe escludere indirizzi loopback (internal)', () => {
os.networkInterfaces.mockReturnValue({
lo: [
{ family: 'IPv4', internal: true, address: '127.0.0.1' }
],
eth0: [
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
]
const ips = getNetworkIPs({
lo: [{ family: 'IPv4', internal: true, address: '127.0.0.1' }],
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }]
})
const ips = getNetworkIPs()
expect(ips).not.toContain('127.0.0.1')
expect(ips).toContain('192.168.1.100')
})
it('dovrebbe escludere indirizzi IPv6', () => {
os.networkInterfaces.mockReturnValue({
const ips = getNetworkIPs({
eth0: [
{ family: 'IPv6', internal: false, address: 'fe80::1' },
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
]
})
const ips = getNetworkIPs()
expect(ips).toEqual(['192.168.1.100'])
})
it('dovrebbe escludere bridge Docker 172.17.x.x', () => {
os.networkInterfaces.mockReturnValue({
docker0: [
{ family: 'IPv4', internal: false, address: '172.17.0.1' }
],
eth0: [
{ family: 'IPv4', internal: false, address: '10.0.0.5' }
]
const ips = getNetworkIPs({
docker0: [{ family: 'IPv4', internal: false, address: '172.17.0.1' }],
eth0: [{ family: 'IPv4', internal: false, address: '10.0.0.5' }]
})
const ips = getNetworkIPs()
expect(ips).not.toContain('172.17.0.1')
expect(ips).toContain('10.0.0.5')
})
it('dovrebbe escludere bridge Docker 172.18.x.x', () => {
os.networkInterfaces.mockReturnValue({
br0: [
{ family: 'IPv4', internal: false, address: '172.18.0.1' }
]
})
expect(getNetworkIPs()).toEqual([])
expect(getNetworkIPs({
br0: [{ family: 'IPv4', internal: false, address: '172.18.0.1' }]
})).toEqual([])
})
it('dovrebbe escludere indirizzi link-local 169.254.x.x', () => {
expect(getNetworkIPs({
eth0: [{ family: 'IPv4', internal: false, address: '169.254.1.1' }]
})).toEqual([])
})
it('dovrebbe restituire array vuoto se nessuna interfaccia disponibile', () => {
os.networkInterfaces.mockReturnValue({})
expect(getNetworkIPs()).toEqual([])
expect(getNetworkIPs({})).toEqual([])
})
it('dovrebbe restituire più indirizzi da interfacce diverse', () => {
os.networkInterfaces.mockReturnValue({
eth0: [
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
],
wlan0: [
{ family: 'IPv4', internal: false, address: '192.168.1.101' }
]
const ips = getNetworkIPs({
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }],
wlan0: [{ family: 'IPv4', internal: false, address: '192.168.1.101' }]
})
const ips = getNetworkIPs()
expect(ips).toHaveLength(2)
expect(ips).toContain('192.168.1.100')
expect(ips).toContain('192.168.1.101')
})
it('collectIPs gestisce input vuoto/undefined senza errori', () => {
expect(collectIPs()).toEqual([])
expect(collectIPs({})).toEqual([])
})
})
// =============================================
@@ -103,9 +86,8 @@ describe('Server Utils', () => {
// =============================================
describe('printServerInfo', () => {
it('dovrebbe stampare la porta di default (3000)', () => {
os.networkInterfaces.mockReturnValue({})
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
printServerInfo()
printServerInfo(3000, [])
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
expect(allLogs).toContain('3000')
expect(allLogs).toContain('/display')
@@ -114,22 +96,16 @@ describe('Server Utils', () => {
})
it('dovrebbe stampare la porta personalizzata', () => {
os.networkInterfaces.mockReturnValue({})
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
printServerInfo(8080)
printServerInfo(8080, [])
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
expect(allLogs).toContain('8080')
consoleSpy.mockRestore()
})
it('dovrebbe mostrare gli URL remoti se ci sono IP di rete', () => {
os.networkInterfaces.mockReturnValue({
eth0: [
{ family: 'IPv4', internal: false, address: '192.168.1.50' }
]
})
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
printServerInfo(3000)
printServerInfo(3000, ['192.168.1.50'])
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
expect(allLogs).toContain('192.168.1.50')
expect(allLogs).toContain('remoti')
@@ -137,9 +113,8 @@ describe('Server Utils', () => {
})
it('non dovrebbe mostrare sezione remoti se nessun IP di rete', () => {
os.networkInterfaces.mockReturnValue({})
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
printServerInfo(3000)
printServerInfo(3000, [])
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
expect(allLogs).not.toContain('remoti')
consoleSpy.mockRestore()