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:
83
tests/integration/websocket.test.js
Normal file
83
tests/integration/websocket.test.js
Normal 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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user