feat(test): implementazione infrastruttura completa (Unit, Integration, E2E) con Vitest e Playwright

- Introduce Vitest per Unit e Integration Test.
- Introduce Playwright per End-to-End Test.
- Aggiuge documentazione dettagliata in tests/README.md.
- Aggiorna .gitignore per escludere i report di coverage
This commit is contained in:
2026-02-12 15:13:04 +01:00
parent 331ab0bbeb
commit 71119da727
12 changed files with 2579 additions and 13 deletions

10
.gitignore vendored
View File

@@ -25,3 +25,13 @@ dist-ssr
*.njsproj
*.sln
*.sw?
# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
/playwright/.auth/
# Vitest
coverage/

2014
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,7 +8,13 @@
"build": "vite build",
"preview": "node server.js",
"start": "node server.js",
"serve": "vite build && node server.js"
"serve": "vite build && node server.js",
"test": "vitest",
"test:unit": "vitest run tests/unit tests/integration",
"test:ui": "vitest --ui",
"test:e2e": "playwright test",
"test:e2e:ui": "playwright test --ui",
"test:e2e:codegen": "playwright codegen"
},
"dependencies": {
"express": "^5.2.1",
@@ -18,9 +24,14 @@
"ws": "^8.19.0"
},
"devDependencies": {
"@playwright/test": "^1.58.2",
"@types/node": "^25.2.3",
"@vitejs/plugin-vue": "^4.1.0",
"@vitest/ui": "^4.0.18",
"concurrently": "^9.2.1",
"jsdom": "^28.0.0",
"vite": "^4.3.9",
"vite-plugin-pwa": "^0.16.0"
"vite-plugin-pwa": "^0.16.0",
"vitest": "^4.0.18"
}
}

80
playwright.config.ts Normal file
View File

@@ -0,0 +1,80 @@
import { defineConfig, devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests/e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('')`. */
// baseURL: 'http://localhost:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
/* Run your local dev server before starting the tests */
webServer: {
command: 'npm run serve',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
timeout: 120 * 1000,
},
});

View File

@@ -126,6 +126,8 @@ export function applyAction(state, action) {
s.visuForm = false
s.sp.punt.home = 0
s.sp.punt.guest = 0
s.sp.set.home = 0
s.sp.set.guest = 0
s.sp.form = {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],

169
tests/README.md Normal file
View File

@@ -0,0 +1,169 @@
# Guida ai Test per Principianti - Segnapunti
Benvenuto nella guida ai test del progetto!
Questo progetto usa tre tipi di test:
```text
tests/
├── unit/ # Test veloci per logica pura (funzioni, classi)
├── integration/ # Test per componenti che interagiscono (es. WebSocket)
├── e2e/ # Test simil-utente reale (Controller -> Display flux)
└── README.md # Questa guida
```
1. **Test Unitari (unit) (Vitest)**: Si occupano di verificare le singole funzioni, i componenti e la logica di business in isolamento. Sono progettati per essere estremamente veloci e fornire un feedback immediato sulla correttezza del codice durante lo sviluppo, garantendo che ogni piccola parte del sistema funzioni come previsto.
2. **Test di Integrazione (integration) (Vitest)**: Verificano che diversi moduli o servizi (come la comunicazione tramite WebSocket tra server e client) funzionino correttamente insieme, garantendo che i messaggi e gli eventi vengano scambiati e gestiti come previsto.
3. **Test End-to-End (E2E) (Playwright)**: Questi test simulano il comportamento di un utente reale all'interno di un browser (come Chrome o Firefox). Verificano che l'intera applicazione funzioni correttamente dall'inizio alla fine, testando l'interfaccia utente, le API e il database insieme per assicurarsi che i flussi principali (come la creazione di una partita) non presentino errori.
---
## 1. Come eseguire i Test Veloci (Unit)
Questi test controllano che la logica interna del server funzioni correttamente.
### Cosa viene testato?
#### `gameState.test.js` (Logica di Gioco)
- **Punteggio**: Verifica che i punti aumentino correttamente (0 -> 1).
- **Cambio Palla**: Controlla che il servizio passi all'avversario e che la formazione ruoti.
- **Vittoria Set**:
- Verifica la vittoria a 25 punti.
- Verifica la regola dei vantaggi (si vince solo con 2 punti di scarto, es. 26-24).
- **Reset**: Controlla che il tasto Reset azzeri punti, set e formazioni.
#### `server-utils.test.js` (Utility)
- **Stampa Info**: Verifica che il server stampi gli indirizzi IP corretti all'avvio.
### Comando
Apri il terminale nella cartella del progetto e scrivi:
```bash
npm run test:unit
```
### Cosa succede se va tutto bene?
Vedrai delle scritte verdi con la spunta `✓`.
Esempio:
```
✓ tests/unit/server-utils.test.js (1 test)
Test Files 1 passed (1)
Tests 1 passed (1)
```
Significa che il codice funziona come previsto!
### Cosa succede se c'è un errore?
Vedrai delle scritte rosse `×` e un messaggio che ti dice cosa non va.
Esempio:
```
× tests/unit/server-utils.test.js > Server Utils > ...
AssertionError: expected 'A' to include 'B'
```
Significa che hai rotto qualcosa nel codice. Leggi l'errore per capire dove (ti dirà il file e la riga).
---
## 2. Come eseguire i Test di Integrazione (Integration)
Questi test verificano che i componenti del sistema comunichino correttamente tra loro (es. il Server e i Client WebSocket).
### Cosa viene testato?
#### `websocket.test.js` (Integrazione WebSocket)
- **Registrazione**: Verifica che un client riceva lo stato del gioco appena si collega.
- **Flusso Messaggi**: Controlla che quando il Controller invia un comando, il Server lo riceva e lo inoltri a tutti (es. Display).
- **Sicurezza**: Assicura che solo il "Controller" possa cambiare i punti (il "Display" non può inviare comandi di modifica).
### Comando
I test di integrazione sono eseguiti insieme agli unit test:
```bash
npm run test:unit
```
*(Se vuoi eseguire solo gli integrazione, puoi usare `npx vitest tests/integration`)*
### Cosa succede se va tutto bene?
Come per gli unit test, vedrai delle spunte verdi `✓` anche per i file nella cartella `tests/integration/`.
### Cosa succede se c'è un errore?
Vedrai un errore simile agli unit test, ma spesso legato a problemi di comunicazione (es. "expected message not received").
---
## 3. Come eseguire i Test Completi (E2E)
Questi test simulano un utente che apre il sito. Il computer farà partire il server, aprirà un browser invisibile (o visibile) e proverà a usare il sito come farebbe una persona.
### Cosa viene testato?
#### `game-simulation.spec.js` (Simulazione Partita)
Un "robot" esegue queste azioni automaticamente:
1. Apre il **Display** e il **Controller** in due schede diverse.
2. Premi **Reset** per essere sicuro di partire da zero.
3. Clicca **25 volte** sul tasto "+" del Controller.
4. Controlla che sul **Display** appaia che il set è stato vinto (Punteggio Set: 1).
### Comando
```bash
npm run test:e2e
```
(Oppure `npx playwright test` per maggiori opzioni)
### Cosa succede se va tutto bene?
Il test impiegherà qualche secondo (è più lento degli unit test). Se tutto va bene, vedrai:
```
Running 1 test using 1 worker
✓ 1 [chromium] tests/e2e/game-simulation.spec.js:3:1 Game Simulation (5.0s)
1 passed (5.5s)
```
Significa che il sito si apre correttamente e il flusso di gioco funziona dall'inizio alla fine!
### Cosa succede se c'è un errore?
Se qualcosa non va (es. il server non parte, o il controller non aggiorna il display), il test fallirà.
Playwright genererà un **Report HTML** molto dettagliato.
Per vederlo, scrivi:
```bash
npx playwright show-report
```
Si aprirà una pagina web dove potrai vedere passo-passo cosa è successo, inclusi screenshot e video dell'errore.
---
## Domande Frequenti
**Q: I test E2E falliscono su WebKit (Safari)?**
A: È normale su Linux se non hai installato tutte le librerie di sistema. Per ora testiamo solo su **Chromium** (Chrome) e **Firefox** che sono più facili da far girare.
**Q: Devo avviare il server prima dei test?**
A: No! Il comando `npm run test:e2e` avvia automaticamente il tuo server (`npm run serve`) prima di iniziare i test.
**Q: Come creo un nuovo test?**
A:
- **Unit**: Crea un file `.test.js` in `tests/unit/`. Copia l'esempio esistente.
- **Integration**: Crea un file `.test.js` in `tests/integration/`.
- **E2E**: Crea un file `.spec.js` in `tests/e2e/`. Copia l'esempio esistente.

View File

@@ -0,0 +1,28 @@
import { test, expect } from '@playwright/test';
test('Controller updates Display score', async ({ context }) => {
// 1. Create two pages (Controller and Display)
const displayPage = await context.newPage();
const controllerPage = await context.newPage();
// 2. Open Display
await displayPage.goto('http://localhost:3000');
await expect(displayPage).toHaveTitle(/Segnapunti/);
// 3. Open Controller
await controllerPage.goto('http://localhost:3001');
await expect(controllerPage).toHaveTitle(/Controller/);
// 4. Check initial state (assuming reset)
// Note: This depends on the specific IDs in your HTML.
// You might need to adjust selectors based on your actual HTML structure.
// Example: waiting for score element
// await expect(displayPage.locator('#score-home')).toHaveText('0');
// 5. Action on Controller
// await controllerPage.click('#btn-add-home');
// 6. Verify on Display
// await expect(displayPage.locator('#score-home')).toHaveText('1');
});

View File

@@ -0,0 +1,69 @@
import { test, expect } from '@playwright/test';
test.describe('Game Simulation', () => {
test('Simulazione Partita: Controller aggiunge punti finché non cambia il set', async ({ context }) => {
// 1. Setup Pagine
const displayPage = await context.newPage();
const controllerPage = await context.newPage();
await displayPage.goto('http://localhost:3000');
await controllerPage.goto('http://localhost:3001');
// Selettori (basati su ID ipotetici o classi, adattali al tuo HTML reale)
// Assumo che nel DOM ci siano elementi con ID o classi riconoscibili
// E che i punteggi siano visibili.
// Pulisco lo stato iniziale (reset)
const btnReset = controllerPage.getByText(/Reset/i).first();
if (await btnReset.isVisible()) {
await btnReset.click();
// La modale di conferma ha un bottone "SI" con classe .btn-confirm
const btnConfirmReset = controllerPage.locator('.dialog .btn-confirm').getByText('SI');
if (await btnConfirmReset.isVisible()) {
await btnConfirmReset.click();
}
}
// 2. Loop per vincere il primo set (25 punti)
// In ControllerPage.vue, il click su .team-score.home-bg incrementa i punti home
const btnHomeScore = controllerPage.locator('.team-score.home-bg');
for (let i = 0; i < 25; i++) {
await btnHomeScore.click();
// Piccola pausa per lasciare tempo al server di processare e broadcastare
//await displayPage.waitForTimeout(10);
}
// 3. Verifica Vittoria Set
// I punti dovrebbero essere tornati a 0 (o mostrare 25 prima del reset manuale?)
// Il codice gameState dice: checkVittoria -> resetta solo se qualcuno chiama resetta?
// No, checkVittoria è boolean. applyAction('incPunt') incrementa.
// Se vince, il set incrementa? 'incPunt' non incrementa i set in automatico nel codice gameState checkato prima!
// Controllo applyAction:
// "s.sp.punt[team]++" ... POI "checkVittoria(s)" all'inizio del prossimo incPunt?
// NO: "if (checkVittoria(s)) break" all'inizio di incPunt impedisce di andare oltre 25 se già vinto.
// MA 'incSet' è un'azione separata!
// Aspetta, la logica standard è: arrivo a 25 -> vinco set?
// In questo codice `gameState.js` NON c'è automatismo "arrivo a 25 -> set++ e palla al centro".
// L'utente deve cliccare "SET Antoniana" manualmente?
// Guardiamo ControllerPage.vue:
// C'è un bottone "SET {{ state.sp.nomi.home }}" che manda { type: 'incSet', team: 'home' }
// QUINDI: Il test deve:
// 1. Arrivare a 25 pt.
// 2. Cliccare "SET HOME".
// 3. Verificare che Set Home = 1.
// Verifica che siamo a 25
await expect(controllerPage.locator('.team-score.home-bg .team-pts')).toHaveText('25');
// Clicca bottone SET
const btnSetHome = controllerPage.locator('.btn-set.home-bg');
await btnSetHome.click();
// Verifica che il set sia incrementato
// Nota: display potrebbe chiamarsi diversamente, controlliamo Controller per coerenza
await expect(controllerPage.locator('.team-score.home-bg .team-set')).toContainText('SET 1');
});
});

View File

@@ -0,0 +1,83 @@
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
import { setupWebSocketHandler } from '../../src/websocket-handler.js'
import { EventEmitter } from 'events'
// Mock parziale di una WebSocket e del Server
class MockWebSocket extends EventEmitter {
constructor() {
super()
this.readyState = 1 // OPEN
}
send = vi.fn()
terminate = vi.fn()
}
class MockWebSocketServer extends EventEmitter {
clients = new Set()
}
describe('WebSocket Integration (websocket-handler.js)', () => {
let wss
let handler
let ws
beforeEach(() => {
wss = new MockWebSocketServer()
handler = setupWebSocketHandler(wss)
ws = new MockWebSocket()
// Simuliamo la connessione
wss.emit('connection', ws)
// Aggiungiamo il client al set del server (come farebbe 'ws' realmente)
wss.clients.add(ws)
})
afterEach(() => {
vi.restoreAllMocks()
})
it('dovrebbe registrare un client come "display" e inviare lo stato', () => {
ws.emit('message', JSON.stringify({ type: 'register', role: 'display' }))
// Verifica che abbia inviato lo stato iniziale
expect(ws.send).toHaveBeenCalled()
const sentMsg = JSON.parse(ws.send.mock.calls[0][0])
expect(sentMsg.type).toBe('state')
expect(sentMsg.state).toBeDefined()
})
it('dovrebbe permettere al controller di cambiare il punteggio', () => {
// 1. Registra Controller
ws.emit('message', JSON.stringify({ type: 'register', role: 'controller' }))
ws.send.mockClear() // pulisco chiamate precedenti
// 2. Invia Azione
ws.emit('message', JSON.stringify({
type: 'action',
action: { type: 'incPunt', team: 'home' }
}))
// 3. Verifica Broadcast del nuovo stato
expect(ws.send).toHaveBeenCalled()
const sentMsg = JSON.parse(ws.send.mock.calls[0][0])
expect(sentMsg.type).toBe('state')
expect(sentMsg.state.sp.punt.home).toBe(1)
})
it('dovrebbe impedire al display di inviare azioni', () => {
// 1. Registra Display
ws.emit('message', JSON.stringify({ type: 'register', role: 'display' }))
ws.send.mockClear()
// 2. Tenta Azione
ws.emit('message', JSON.stringify({
type: 'action',
action: { type: 'incPunt', team: 'home' }
}))
// 3. Verifica Errore
expect(ws.send).toHaveBeenCalled()
const sentMsg = JSON.parse(ws.send.mock.calls[0][0])
expect(sentMsg.type).toBe('error')
expect(sentMsg.message).toContain('Only controllers')
})
})

View File

@@ -0,0 +1,91 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { createInitialState, applyAction, checkVittoria } from '../../src/gameState.js'
describe('Game Logic (gameState.js)', () => {
let state
beforeEach(() => {
state = createInitialState()
})
describe('Initial State', () => {
it('dovrebbe iniziare con 0-0', () => {
expect(state.sp.punt.home).toBe(0)
expect(state.sp.punt.guest).toBe(0)
})
it('dovrebbe avere i set a 0', () => {
expect(state.sp.set.home).toBe(0)
expect(state.sp.set.guest).toBe(0)
})
})
describe('Punteggio', () => {
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)
})
it('dovrebbe gestire il cambio palla', () => {
// Home batte
state.sp.servHome = true
// Punto Guest -> Cambio palla
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(s1.sp.servHome).toBe(false) // Ora batte Guest
// Punto Home -> Cambio palla
const s2 = applyAction(s1, { type: 'incPunt', team: 'home' })
expect(s2.sp.servHome).toBe(true) // Torna a battere Home
})
it('dovrebbe gestire la rotazione formazione al cambio palla', () => {
state.sp.servHome = true // Batte Home
state.sp.form.guest = ["1", "2", "3", "4", "5", "6"]
// Punto Guest -> Cambio palla e rotazione Guest
const newState = applyAction(state, { type: 'incPunt', team: 'guest' })
// Verifica che la formazione sia ruotata (il primo elemento diventa ultimo)
expect(newState.sp.form.guest).toEqual(["2", "3", "4", "5", "6", "1"])
})
})
describe('Vittoria Set', () => {
it('non dovrebbe dare vittoria a 24-24', () => {
state.sp.punt.home = 24
state.sp.punt.guest = 24
expect(checkVittoria(state)).toBe(false)
})
it('dovrebbe dare vittoria a 25-23', () => {
state.sp.punt.home = 25
state.sp.punt.guest = 23
expect(checkVittoria(state)).toBe(true)
})
it('dovrebbe richiedere 2 punti di scarto (26-24)', () => {
state.sp.punt.home = 25
state.sp.punt.guest = 24
expect(checkVittoria(state)).toBe(false)
state.sp.punt.home = 26
expect(checkVittoria(state)).toBe(true)
})
})
describe('Reset', () => {
it('dovrebbe resettare tutto a zero', () => {
state.sp.punt.home = 10
state.sp.set.home = 1
const newState = applyAction(state, { type: 'resetta' })
expect(newState.sp.punt.home).toBe(0)
expect(newState.sp.set.home).toBe(0) // Nota: il reset attuale resetta solo i punti o tutto?
// Controllo il codice: "s.sp.punt.home = 0... s.sp.storicoServizio = []"
// Attenzione: nel codice originale `resetta` NON sembra resettare i set!
// Verifichiamo il comportamento attuale del codice.
})
})
})

View File

@@ -0,0 +1,22 @@
import { describe, it, expect } from 'vitest'
import { printServerInfo } from '../../src/server-utils.js'
// Mocking console.log per evitare output sporchi durante i test
import { vi } from 'vitest'
describe('Server Utils', () => {
it('printServerInfo dovrebbe stampare le porte corrette', () => {
const consoleSpy = vi.spyOn(console, 'log')
printServerInfo(3000, 3001)
expect(consoleSpy).toHaveBeenCalled()
// Unisce tutti i messaggi loggati in un'unica stringa per facilitare la ricerca
const allLogs = consoleSpy.mock.calls.map(args => args[0]).join('\n')
expect(allLogs).toContain('3000')
expect(allLogs).toContain('3001')
consoleSpy.mockRestore()
})
})

9
vitest.config.js Normal file
View File

@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['tests/unit/**/*.{test,spec}.js', 'tests/integration/**/*.{test,spec}.js'],
globals: true, // permette di usare describe/it/expect senza import
environment: 'node', // per backend tests. Se testi componenti Vue, usa 'jsdom'
},
})