- Nuovo db.test.js: 11 test per salvaPartita, getPartite, getPartita (isolamento con DB in memoria via vi.stubEnv + vi.resetModules) - gameState.test.js: test per confermaSet, formInizioSet, partitaFinita, checkVittoriaPartita e guardie setFinito/partitaFinita; fix di 6 test pre-esistenti non allineati con la logica striscia aggiornata - websocket.test.js: mock di db.js e 4 test per il salvataggio automatico su DB al termine della partita - server-utils.test.js: 2 test aggiuntivi per storicoPort - ControllerPage.test.js: 4 test per l'overlay di fine set (setFinito) - DisplayPage.test.js: 4 test per l'overlay di fine partita (partitaFinita)
160 lines
6.0 KiB
JavaScript
160 lines
6.0 KiB
JavaScript
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
import * as os from 'os'
|
|
|
|
vi.mock('os', async (importOriginal) => {
|
|
return {
|
|
...await importOriginal(),
|
|
networkInterfaces: vi.fn(() => ({}))
|
|
}
|
|
})
|
|
|
|
import { getNetworkIPs, printServerInfo } from '../../src/server-utils.js'
|
|
|
|
describe('Server Utils', () => {
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
// =============================================
|
|
// getNetworkIPs
|
|
// =============================================
|
|
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'])
|
|
})
|
|
|
|
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()
|
|
expect(ips).not.toContain('127.0.0.1')
|
|
expect(ips).toContain('192.168.1.100')
|
|
})
|
|
|
|
it('dovrebbe escludere indirizzi IPv6', () => {
|
|
os.networkInterfaces.mockReturnValue({
|
|
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()
|
|
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([])
|
|
})
|
|
|
|
it('dovrebbe restituire array vuoto se nessuna interfaccia disponibile', () => {
|
|
os.networkInterfaces.mockReturnValue({})
|
|
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()
|
|
expect(ips).toHaveLength(2)
|
|
expect(ips).toContain('192.168.1.100')
|
|
expect(ips).toContain('192.168.1.101')
|
|
})
|
|
})
|
|
|
|
// =============================================
|
|
// printServerInfo
|
|
// =============================================
|
|
describe('printServerInfo', () => {
|
|
it('dovrebbe stampare le porte corrette (default)', () => {
|
|
os.networkInterfaces.mockReturnValue({})
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
printServerInfo()
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
|
expect(allLogs).toContain('5173')
|
|
expect(allLogs).toContain('3001')
|
|
expect(allLogs).toContain('3002')
|
|
consoleSpy.mockRestore()
|
|
})
|
|
|
|
it('dovrebbe stampare le porte personalizzate', () => {
|
|
os.networkInterfaces.mockReturnValue({})
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
printServerInfo(3000, 4000)
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
|
expect(allLogs).toContain('3000')
|
|
expect(allLogs).toContain('4000')
|
|
consoleSpy.mockRestore()
|
|
})
|
|
|
|
it('dovrebbe stampare storicoPort personalizzato', () => {
|
|
os.networkInterfaces.mockReturnValue({})
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
printServerInfo(3000, 3001, 5000)
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
|
expect(allLogs).toContain('5000')
|
|
consoleSpy.mockRestore()
|
|
})
|
|
|
|
it('dovrebbe mostrare gli URL remoti per controller e storico', () => {
|
|
os.networkInterfaces.mockReturnValue({
|
|
eth0: [
|
|
{ family: 'IPv4', internal: false, address: '192.168.1.50' }
|
|
]
|
|
})
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
printServerInfo(3000, 3001, 3002)
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
|
expect(allLogs).toContain('192.168.1.50')
|
|
expect(allLogs).toContain('3001')
|
|
expect(allLogs).toContain('3002')
|
|
consoleSpy.mockRestore()
|
|
})
|
|
|
|
it('non dovrebbe mostrare sezione remoti se nessun IP di rete', () => {
|
|
os.networkInterfaces.mockReturnValue({})
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
printServerInfo(3000, 3001)
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
|
expect(allLogs).not.toContain('remoti')
|
|
consoleSpy.mockRestore()
|
|
})
|
|
})
|
|
})
|