2026-02-12 19:33:29 +01:00
|
|
|
import { describe, it, expect, vi, afterEach } from 'vitest'
|
2026-06-21 00:36:02 +02:00
|
|
|
import { getNetworkIPs, collectIPs, printServerInfo } from '../../src/server-utils.js'
|
2026-02-12 15:13:04 +01:00
|
|
|
|
2026-06-21 00:36:02 +02:00
|
|
|
// Nota: gli IP vengono iniettati nei test (oggetto stile os.networkInterfaces o
|
|
|
|
|
// array di IP), così i risultati sono deterministici su qualsiasi piattaforma —
|
|
|
|
|
// incluso WSL, dove getNetworkIPs() userebbe altrimenti PowerShell.
|
2026-02-12 15:13:04 +01:00
|
|
|
|
|
|
|
|
describe('Server Utils', () => {
|
|
|
|
|
|
2026-02-12 19:33:29 +01:00
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// =============================================
|
2026-06-21 00:36:02 +02:00
|
|
|
// getNetworkIPs / collectIPs
|
2026-02-12 19:33:29 +01:00
|
|
|
// =============================================
|
|
|
|
|
describe('getNetworkIPs', () => {
|
|
|
|
|
it('dovrebbe restituire indirizzi IPv4 non-loopback', () => {
|
2026-06-21 00:36:02 +02:00
|
|
|
expect(getNetworkIPs({
|
|
|
|
|
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }]
|
|
|
|
|
})).toEqual(['192.168.1.100'])
|
2026-02-12 19:33:29 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('dovrebbe escludere indirizzi loopback (internal)', () => {
|
2026-06-21 00:36:02 +02:00
|
|
|
const ips = getNetworkIPs({
|
|
|
|
|
lo: [{ family: 'IPv4', internal: true, address: '127.0.0.1' }],
|
|
|
|
|
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }]
|
2026-02-12 19:33:29 +01:00
|
|
|
})
|
|
|
|
|
expect(ips).not.toContain('127.0.0.1')
|
|
|
|
|
expect(ips).toContain('192.168.1.100')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('dovrebbe escludere indirizzi IPv6', () => {
|
2026-06-21 00:36:02 +02:00
|
|
|
const ips = getNetworkIPs({
|
2026-02-12 19:33:29 +01:00
|
|
|
eth0: [
|
|
|
|
|
{ family: 'IPv6', internal: false, address: 'fe80::1' },
|
|
|
|
|
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
expect(ips).toEqual(['192.168.1.100'])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('dovrebbe escludere bridge Docker 172.17.x.x', () => {
|
2026-06-21 00:36:02 +02:00
|
|
|
const ips = getNetworkIPs({
|
|
|
|
|
docker0: [{ family: 'IPv4', internal: false, address: '172.17.0.1' }],
|
|
|
|
|
eth0: [{ family: 'IPv4', internal: false, address: '10.0.0.5' }]
|
2026-02-12 19:33:29 +01:00
|
|
|
})
|
|
|
|
|
expect(ips).not.toContain('172.17.0.1')
|
|
|
|
|
expect(ips).toContain('10.0.0.5')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('dovrebbe escludere bridge Docker 172.18.x.x', () => {
|
2026-06-21 00:36:02 +02:00
|
|
|
expect(getNetworkIPs({
|
|
|
|
|
br0: [{ family: 'IPv4', internal: false, address: '172.18.0.1' }]
|
|
|
|
|
})).toEqual([])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('dovrebbe escludere indirizzi link-local 169.254.x.x', () => {
|
|
|
|
|
expect(getNetworkIPs({
|
|
|
|
|
eth0: [{ family: 'IPv4', internal: false, address: '169.254.1.1' }]
|
|
|
|
|
})).toEqual([])
|
2026-02-12 19:33:29 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('dovrebbe restituire array vuoto se nessuna interfaccia disponibile', () => {
|
2026-06-21 00:36:02 +02:00
|
|
|
expect(getNetworkIPs({})).toEqual([])
|
2026-02-12 19:33:29 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('dovrebbe restituire più indirizzi da interfacce diverse', () => {
|
2026-06-21 00:36:02 +02:00
|
|
|
const ips = getNetworkIPs({
|
|
|
|
|
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }],
|
|
|
|
|
wlan0: [{ family: 'IPv4', internal: false, address: '192.168.1.101' }]
|
2026-02-12 19:33:29 +01:00
|
|
|
})
|
|
|
|
|
expect(ips).toHaveLength(2)
|
|
|
|
|
expect(ips).toContain('192.168.1.100')
|
|
|
|
|
expect(ips).toContain('192.168.1.101')
|
|
|
|
|
})
|
2026-06-21 00:36:02 +02:00
|
|
|
|
|
|
|
|
it('collectIPs gestisce input vuoto/undefined senza errori', () => {
|
|
|
|
|
expect(collectIPs()).toEqual([])
|
|
|
|
|
expect(collectIPs({})).toEqual([])
|
|
|
|
|
})
|
2026-02-12 19:33:29 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// =============================================
|
|
|
|
|
// printServerInfo
|
|
|
|
|
// =============================================
|
|
|
|
|
describe('printServerInfo', () => {
|
2026-05-12 12:22:43 +02:00
|
|
|
it('dovrebbe stampare la porta di default (3000)', () => {
|
2026-02-12 19:33:29 +01:00
|
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
2026-06-21 00:36:02 +02:00
|
|
|
printServerInfo(3000, [])
|
2026-02-12 19:33:29 +01:00
|
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
2026-05-12 12:22:43 +02:00
|
|
|
expect(allLogs).toContain('3000')
|
|
|
|
|
expect(allLogs).toContain('/display')
|
|
|
|
|
expect(allLogs).toContain('/controller')
|
2026-02-12 19:33:29 +01:00
|
|
|
consoleSpy.mockRestore()
|
|
|
|
|
})
|
2026-02-12 15:13:04 +01:00
|
|
|
|
2026-05-12 12:22:43 +02:00
|
|
|
it('dovrebbe stampare la porta personalizzata', () => {
|
2026-02-12 19:33:29 +01:00
|
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
2026-06-21 00:36:02 +02:00
|
|
|
printServerInfo(8080, [])
|
2026-02-12 19:33:29 +01:00
|
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
2026-05-12 12:22:43 +02:00
|
|
|
expect(allLogs).toContain('8080')
|
2026-02-12 19:33:29 +01:00
|
|
|
consoleSpy.mockRestore()
|
|
|
|
|
})
|
2026-02-12 15:13:04 +01:00
|
|
|
|
2026-02-12 19:33:29 +01:00
|
|
|
it('dovrebbe mostrare gli URL remoti se ci sono IP di rete', () => {
|
|
|
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
2026-06-21 00:36:02 +02:00
|
|
|
printServerInfo(3000, ['192.168.1.50'])
|
2026-02-12 19:33:29 +01:00
|
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
|
|
|
|
expect(allLogs).toContain('192.168.1.50')
|
|
|
|
|
expect(allLogs).toContain('remoti')
|
|
|
|
|
consoleSpy.mockRestore()
|
|
|
|
|
})
|
2026-02-12 15:13:04 +01:00
|
|
|
|
2026-02-12 19:33:29 +01:00
|
|
|
it('non dovrebbe mostrare sezione remoti se nessun IP di rete', () => {
|
|
|
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
2026-06-21 00:36:02 +02:00
|
|
|
printServerInfo(3000, [])
|
2026-02-12 19:33:29 +01:00
|
|
|
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
|
|
|
|
expect(allLogs).not.toContain('remoti')
|
|
|
|
|
consoleSpy.mockRestore()
|
|
|
|
|
})
|
2026-02-12 15:13:04 +01:00
|
|
|
})
|
|
|
|
|
})
|