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:
@@ -2,6 +2,10 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import ControllerPage from '../../src/components/ControllerPage.vue'
|
||||
import { generaReferto } from '../../src/referto.js'
|
||||
|
||||
// Il referto apre una finestra/print: lo mockiamo per testarne solo l'invocazione.
|
||||
vi.mock('../../src/referto.js', () => ({ generaReferto: vi.fn() }))
|
||||
|
||||
// Mock globale WebSocket per jsdom
|
||||
class MockWebSocket {
|
||||
@@ -25,6 +29,21 @@ class MockWebSocket {
|
||||
|
||||
vi.stubGlobal('WebSocket', MockWebSocket)
|
||||
|
||||
// Forza l'orientamento portrait → il controller usa il layout "mobile"
|
||||
// (con .team-pts, .btn-ctrl, ecc.) su cui questi test fanno asserzioni.
|
||||
Object.defineProperty(window, 'innerWidth', { value: 400, writable: true, configurable: true })
|
||||
Object.defineProperty(window, 'innerHeight', { value: 800, writable: true, configurable: true })
|
||||
|
||||
// Imposta il punteggio del set in corso costruendo una ris coerente.
|
||||
// `serv` ('h'|'g') controlla l'ultimo punto, quindi chi risulta al servizio.
|
||||
function setScore(wrapper, home, guest, serv = 'h') {
|
||||
const altro = serv === 'h' ? 'g' : 'h'
|
||||
const nAltro = serv === 'h' ? guest : home
|
||||
const nServ = serv === 'h' ? home : guest
|
||||
// mette per ultimo il carattere del battitore desiderato
|
||||
wrapper.vm.state.sp.striscia.at(-1).ris = altro.repeat(nAltro) + serv.repeat(nServ)
|
||||
}
|
||||
|
||||
// Helper per creare il componente con stato personalizzato
|
||||
function mountController(stateOverrides = {}) {
|
||||
const wrapper = mount(ControllerPage, {
|
||||
@@ -105,7 +124,7 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe essere disabilitato se il punteggio non è 0-0', async () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.sp.punt.home = 5
|
||||
setScore(wrapper, 5, 0)
|
||||
await wrapper.vm.$nextTick()
|
||||
const btn = wrapper.findAll('.btn-ctrl').find(b => b.text().includes('Cambio Palla'))
|
||||
expect(btn.attributes('disabled')).toBeDefined()
|
||||
@@ -194,8 +213,7 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe generare "N pari" a punteggio uguale', () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.sp.punt.home = 5
|
||||
wrapper.vm.state.sp.punt.guest = 5
|
||||
setScore(wrapper, 5, 5)
|
||||
wrapper.vm.wsConnected = true
|
||||
wrapper.vm.ws = { readyState: 1, send: vi.fn() }
|
||||
wrapper.vm.speak()
|
||||
@@ -205,9 +223,7 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe annunciare prima il punteggio di chi batte (home serve)', () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.sp.punt.home = 15
|
||||
wrapper.vm.state.sp.punt.guest = 10
|
||||
wrapper.vm.state.sp.servHome = true
|
||||
setScore(wrapper, 15, 10, 'h')
|
||||
wrapper.vm.wsConnected = true
|
||||
wrapper.vm.ws = { readyState: 1, send: vi.fn() }
|
||||
wrapper.vm.speak()
|
||||
@@ -217,9 +233,7 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe annunciare prima il punteggio di chi batte (guest serve)', () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.sp.punt.home = 10
|
||||
wrapper.vm.state.sp.punt.guest = 15
|
||||
wrapper.vm.state.sp.servHome = false
|
||||
setScore(wrapper, 10, 15, 'g')
|
||||
wrapper.vm.wsConnected = true
|
||||
wrapper.vm.ws = { readyState: 1, send: vi.fn() }
|
||||
wrapper.vm.speak()
|
||||
@@ -228,6 +242,51 @@ describe('ControllerPage.vue', () => {
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// REFERTO (modal PARTITA FINITA)
|
||||
// =============================================
|
||||
describe('Referto', () => {
|
||||
// Porta il componente allo stato "partita finita" per home in 2/3
|
||||
function setPartitaFinita(wrapper) {
|
||||
wrapper.vm.state.modalitaPartita = '2/3'
|
||||
wrapper.vm.state.sp.striscia = [
|
||||
{ serv: 'h', ris: '', vinc: 'h' },
|
||||
{ serv: 'h', ris: '', vinc: null },
|
||||
]
|
||||
wrapper.vm.setVintoTeam = 'home'
|
||||
wrapper.vm.showSetVinto = true
|
||||
}
|
||||
|
||||
it('mostra il bottone REFERTO quando la partita è finita', async () => {
|
||||
const wrapper = mountController()
|
||||
setPartitaFinita(wrapper)
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.vm.isPartitaFinita).toBe(true)
|
||||
const btn = wrapper.findAll('.btn-secondary').find(b => b.text().includes('REFERTO'))
|
||||
expect(btn).toBeDefined()
|
||||
})
|
||||
|
||||
it('il click su REFERTO invoca generaReferto con lo stato', async () => {
|
||||
const wrapper = mountController()
|
||||
setPartitaFinita(wrapper)
|
||||
await wrapper.vm.$nextTick()
|
||||
const btn = wrapper.findAll('.btn-secondary').find(b => b.text().includes('REFERTO'))
|
||||
await btn.trigger('click')
|
||||
expect(generaReferto).toHaveBeenCalledWith(wrapper.vm.state)
|
||||
})
|
||||
|
||||
it('NON mostra il bottone REFERTO a set vinto (partita non finita)', async () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.modalitaPartita = '3/5'
|
||||
wrapper.vm.setVintoTeam = 'home'
|
||||
wrapper.vm.showSetVinto = true
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.vm.isPartitaFinita).toBe(false)
|
||||
const btn = wrapper.findAll('.btn-secondary').find(b => b.text().includes('REFERTO'))
|
||||
expect(btn).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// BARRA CONNESSIONE
|
||||
// =============================================
|
||||
|
||||
@@ -75,8 +75,8 @@ describe('DisplayPage.vue', () => {
|
||||
|
||||
it('dovrebbe aggiornare il punteggio quando lo stato cambia', async () => {
|
||||
const wrapper = mountDisplay()
|
||||
wrapper.vm.state.sp.punt.home = 15
|
||||
wrapper.vm.state.sp.punt.guest = 12
|
||||
// il punteggio si ricava dalla striscia: 15 punti home + 12 guest
|
||||
wrapper.vm.state.sp.striscia.at(-1).ris = 'h'.repeat(15) + 'g'.repeat(12)
|
||||
await wrapper.vm.$nextTick()
|
||||
const punti = wrapper.findAll('.punt')
|
||||
expect(punti[0].text()).toBe('15')
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { createWsMixin } from '../../src/wsMixin.js'
|
||||
import { createInitialState } from '../../src/gameState.js'
|
||||
|
||||
// WebSocket mock controllabile: i gestori (onopen/onmessage/onclose) vengono
|
||||
// assegnati dal mixin e li invochiamo manualmente nei test.
|
||||
class MockWebSocket {
|
||||
static CONNECTING = 0
|
||||
static OPEN = 1
|
||||
static CLOSING = 2
|
||||
static CLOSED = 3
|
||||
constructor(url) {
|
||||
this.url = url
|
||||
this.readyState = MockWebSocket.CONNECTING
|
||||
this.send = vi.fn()
|
||||
this.close = vi.fn()
|
||||
MockWebSocket.instances.push(this)
|
||||
}
|
||||
}
|
||||
MockWebSocket.instances = []
|
||||
|
||||
vi.stubGlobal('WebSocket', MockWebSocket)
|
||||
|
||||
// Monta un componente che usa il mixin. `extra` permette di aggiungere hook.
|
||||
function mountWith(role = 'controller', extra = {}) {
|
||||
const Comp = {
|
||||
mixins: [createWsMixin(role)],
|
||||
template: '<div></div>',
|
||||
...extra,
|
||||
}
|
||||
return mount(Comp)
|
||||
}
|
||||
|
||||
function ultimaWs() {
|
||||
return MockWebSocket.instances.at(-1)
|
||||
}
|
||||
|
||||
describe('createWsMixin (wsMixin.js)', () => {
|
||||
beforeEach(() => {
|
||||
MockWebSocket.instances = []
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('computed derivati', () => {
|
||||
it('punt/servHome/set delegano alle funzioni pure sulla striscia', async () => {
|
||||
const wrapper = mountWith()
|
||||
wrapper.vm.state.sp.striscia = [
|
||||
{ serv: 'h', ris: 'h', vinc: 'h' },
|
||||
{ serv: 'h', ris: 'h'.repeat(10) + 'g'.repeat(8), vinc: null },
|
||||
]
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.vm.punt).toEqual({ home: 10, guest: 8 })
|
||||
expect(wrapper.vm.set).toEqual({ home: 1, guest: 0 })
|
||||
// ultimo punto 'g' → serve guest
|
||||
expect(wrapper.vm.servHome).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('connessione', () => {
|
||||
it('apre una WebSocket verso /ws al mount', () => {
|
||||
mountWith()
|
||||
const ws = ultimaWs()
|
||||
expect(ws).toBeDefined()
|
||||
expect(ws.url).toMatch(/^ws:\/\/.+\/ws$/)
|
||||
})
|
||||
|
||||
it('invia il messaggio register all\'apertura', () => {
|
||||
const wrapper = mountWith('controller')
|
||||
const ws = ultimaWs()
|
||||
ws.readyState = MockWebSocket.OPEN
|
||||
ws.onopen()
|
||||
expect(ws.send).toHaveBeenCalledTimes(1)
|
||||
const msg = JSON.parse(ws.send.mock.calls[0][0])
|
||||
expect(msg).toEqual({ type: 'register', role: 'controller' })
|
||||
expect(wrapper.vm.wsConnected).toBe(true)
|
||||
})
|
||||
|
||||
it('un messaggio "state" aggiorna lo stato locale', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
const nuovo = createInitialState()
|
||||
nuovo.sp.nomi.home = 'Nuova Squadra'
|
||||
ws.onmessage({ data: JSON.stringify({ type: 'state', state: nuovo }) })
|
||||
expect(wrapper.vm.state.sp.nomi.home).toBe('Nuova Squadra')
|
||||
})
|
||||
|
||||
it('un messaggio non-state invoca l\'hook onWsMessage', () => {
|
||||
const onWsMessage = vi.fn()
|
||||
const wrapper = mountWith('display', { methods: { onWsMessage } })
|
||||
const ws = ultimaWs()
|
||||
ws.onmessage({ data: JSON.stringify({ type: 'speak', text: 'ciao' }) })
|
||||
expect(onWsMessage).toHaveBeenCalledWith({ type: 'speak', text: 'ciao' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('riconnessione', () => {
|
||||
it('scheduleReconnect usa backoff esponenziale con cap a 30s', () => {
|
||||
const wrapper = mountWith()
|
||||
const delays = []
|
||||
const spy = vi.spyOn(globalThis, 'setTimeout').mockImplementation(() => 123)
|
||||
// intercetta i delay leggendoli dalle chiamate
|
||||
spy.mockImplementation((_fn, d) => { delays.push(d); return 123 })
|
||||
|
||||
wrapper.vm.reconnectAttempts = 0
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect() // 1000
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect() // 2000
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect() // 4000
|
||||
expect(delays).toEqual([1000, 2000, 4000])
|
||||
|
||||
// attempts alto → cap a 30000
|
||||
delays.length = 0
|
||||
wrapper.vm.reconnectAttempts = 20
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect()
|
||||
expect(delays[0]).toBe(30000)
|
||||
|
||||
spy.mockRestore()
|
||||
})
|
||||
|
||||
it('non riconnette su chiusura pulita (1000/1001)', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
const spy = vi.spyOn(wrapper.vm, 'scheduleReconnect')
|
||||
ws.onclose({ code: 1000 })
|
||||
ws.onclose({ code: 1001 })
|
||||
expect(spy).not.toHaveBeenCalled()
|
||||
expect(wrapper.vm.wsConnected).toBe(false)
|
||||
})
|
||||
|
||||
it('riconnette su chiusura anomala (es. 1006)', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
const spy = vi.spyOn(wrapper.vm, 'scheduleReconnect')
|
||||
ws.onclose({ code: 1006 })
|
||||
expect(spy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('sendWs', () => {
|
||||
it('ritorna false se non connesso', () => {
|
||||
const wrapper = mountWith()
|
||||
wrapper.vm.wsConnected = false
|
||||
expect(wrapper.vm.sendWs({ type: 'action' })).toBe(false)
|
||||
})
|
||||
|
||||
it('serializza e invia se connesso e aperto', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
ws.readyState = MockWebSocket.OPEN
|
||||
wrapper.vm.wsConnected = true
|
||||
const ok = wrapper.vm.sendWs({ type: 'action', action: { type: 'incPunt' } })
|
||||
expect(ok).toBe(true)
|
||||
const inviato = JSON.parse(ws.send.mock.calls.at(-1)[0])
|
||||
expect(inviato.type).toBe('action')
|
||||
})
|
||||
})
|
||||
|
||||
describe('cleanup', () => {
|
||||
it('beforeUnmount chiude la WebSocket', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
wrapper.unmount()
|
||||
expect(ws.close).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user