eb37f8319f
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
72 lines
2.4 KiB
JavaScript
72 lines
2.4 KiB
JavaScript
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()
|
|
})
|
|
})
|
|
})
|