refactor: rimuove terminal controller CLI

This commit is contained in:
2026-05-12 14:54:36 +02:00
parent 4bfc12fb00
commit b1a400cf81
6 changed files with 16 additions and 658 deletions
+2 -2
View File
@@ -12,7 +12,6 @@ e questo progetto aderisce al [Versionamento Semantico](https://semver.org/lang/
### Aggiunto
- Architettura client-server con WebSocket: server Express (`server.js`) + handler (`src/websocket-handler.js`) come unica fonte di verità; display e controller sono client separati sincronizzati in tempo reale
- Interfaccia display (`/display`) e controller (`/controller`) su porta singola `:3000`
- Terminal controller CLI (`cli.js`) per controllo da riga di comando via WebSocket
- Robustezza connessione WebSocket: reconnect automatico con backoff esponenziale, indicatore stato connessione sul display
- Supporto query parameter `?wsHost=` per scenari WSL2 / development remoto
- Validazione cambi giocatori già in formazione lato client
@@ -30,8 +29,9 @@ e questo progetto aderisce al [Versionamento Semantico](https://semver.org/lang/
- `nuovoSet` come azione dedicata per la progressione regolare tra set
### Rimosso
- Terminal controller CLI (`cli.js`)
- Dipendenze inutilizzate: `wave-ui`, `vue-router`, `concurrently`
- Script npm ridondanti: `preview`, `start`
- Script npm ridondanti: `preview`, `start`, `cli`, `cli:dev`
- Asset template Vite: `vite.svg`, `vue.svg`, `serve.png`
---
+10 -14
View File
@@ -4,15 +4,13 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
## Project Purpose
**Segnapunti Anto** is a real-time volleyball scoreboard PWA. An Express/WebSocket server hosts the game state; two separate web interfaces — a **display** (public scoreboard) and a **controller** (operator panel) — stay in sync via WebSocket. A terminal CLI (`cli.js`) provides an alternative controller interface.
**Segnapunti Anto** is a real-time volleyball scoreboard PWA. An Express/WebSocket server hosts the game state; two separate web interfaces — a **display** (public scoreboard) and a **controller** (operator panel) — stay in sync via WebSocket.
## Commands
```bash
npm run dev # Vite dev server — display: :5173, controller: :5173/controller.html (proxied on :3001)
npm run serve # Build + run production — display: :3000, controller: :3001
npm run cli # Terminal controller (connects to production :3000)
npm run cli:dev # Terminal controller (connects to dev :5173)
npm run dev # Vite dev server — display: :5173/display, controller: :5173/controller
npm run serve # Build + run production — display: :3000/display, controller: :3000/controller
npm run test # Vitest watch mode
npm run test:all # All Vitest suites once (unit, integration, component, stress)
@@ -28,19 +26,20 @@ npm run test:e2e:ui # Playwright with interactive UI
```
Controller (Vue) ──WebSocket──┐
Display (Vue) ──WebSocket──┤── websocket-handler.js ── gameState.js
CLI (Node) ──WebSocket──┘
│ │
│ └── persist.js ── .segnapunti/state.json
```
All game logic lives in `src/gameState.js` as three pure functions:
- `createInitialState()` — returns the initial state
- `applyAction(state, action)` — immutable reducer (deep-clones via `JSON.parse/stringify`)
- `applyAction(state, action)` — immutable reducer (deep-clones via `structuredClone`)
- `checkVittoria(state)` — volleyball win conditions (25-point sets with 2-point margin, 15-point final set)
`src/websocket-handler.js` receives actions, validates that the sender is a registered controller (not just a display), calls `applyAction`, then broadcasts the new state to all clients.
`src/websocket-handler.js` receives actions, validates that the sender is a registered controller (not just a display), calls `applyAction`, broadcasts the new state to all clients, then calls `onStateChange` to persist state to disk.
Two production HTTP servers are started by `server.js` (Express): port 3000 serves `dist/index.html` (display), port 3001 serves `dist/controller.html` (controller). Both share a single WebSocket endpoint at `/ws`.
`server.js` (Express) serves both interfaces on port 3000: `/display``dist/index.html`, `/controller``dist/controller.html`. Single WebSocket endpoint at `/ws`.
In development, `vite-plugin-websocket.js` is a custom Vite plugin that embeds the WebSocket server inside the Vite dev server and proxies port 3001 traffic back to Vite.
In development, `vite-plugin-websocket.js` embeds the WebSocket server inside the Vite dev server with URL rewrite middleware for `/display` and `/controller`.
## Key Design Constraints
@@ -48,6 +47,7 @@ In development, `vite-plugin-websocket.js` is a custom Vite plugin that embeds t
- **Role-based WebSocket** — clients register as `display` or `controller`; only controllers may send actions.
- **Immutable state** — `applyAction` never mutates; always returns a new state object.
- **Single-controller intent** — the design targets one active controller and one display; no conflict resolution exists for simultaneous controllers.
- **Persistent state** — `src/persist.js` saves state to `.segnapunti/state.json` after every action and loads it on server startup.
## Test Layout
@@ -60,7 +60,3 @@ In development, `vite-plugin-websocket.js` is a custom Vite plugin that embeds t
| E2E | `tests/e2e/` | Playwright (Chromium, Firefox, Mobile Chrome) |
E2E tests run serially (`workers: 1`) to avoid WebSocket state races. Run `npm run serve` before `npm run test:e2e`.
## Simplification Goals (Current Work)
The intended architecture is: **one host acts as both WebSocket server and display; one connected device acts as controller**. Complexity reduction should be evaluated against this constraint — anything that supports multi-controller scenarios, complex client topologies, or unneeded abstractions is a candidate for removal.
+3 -34
View File
@@ -1,4 +1,4 @@
# Segnapunti Anto
# Segnapunti
![Version](https://img.shields.io/badge/versione-2.0.0-blue)
![Node](https://img.shields.io/badge/node-%3E%3D18-green)
@@ -15,7 +15,6 @@ Segnapunti digitale in tempo reale per partite di pallavolo. Un server centrale
- [Guida utente](#guida-utente)
- [Funzionalità](#funzionalità)
- [Shortcuts tastiera](#shortcuts-tastiera)
- [Terminal Controller CLI](#terminal-controller-cli)
- [Deploy con Docker](#deploy-con-docker)
- [Sviluppo](#sviluppo)
- [Test](#test)
@@ -26,8 +25,8 @@ Segnapunti digitale in tempo reale per partite di pallavolo. Un server centrale
```
Controller (smartphone) ──WebSocket──┐
Display (schermo) ──WebSocket──┤── Server Node.js ── gameState.js
CLI (terminale) ──WebSocket──┘ │
── Server Node.js ── gameState.js
Display (schermo) ──WebSocket──┘ │
└── .segnapunti/state.json
```
@@ -132,34 +131,6 @@ http://<IP-del-server>:3000/controller
---
## Terminal Controller CLI
Alternativa al controller browser, utile da terminale.
```bash
npm run cli # server su porta 3000
npm run cli:dev # dev server su porta 5173
node cli.js <porta> # porta custom
```
| Comando | Alias | Effetto |
|---|---|---|
| `punto casa` | `+`, `pc` | Punto alla squadra di casa |
| `punto ospite` | `-`, `po` | Punto alla squadra ospite |
| `undo` | `u` | Annulla l'ultimo punto |
| `set casa` / `set ospite` | — | Incrementa contatore set |
| `serv` | — | Cambia servizio (solo a 0-0) |
| `nomi <casa> <ospite>` | — | Imposta i nomi delle squadre |
| `modalita 2/3` / `3/5` | — | Modalità partita |
| `reset` | — | Azzera la partita (chiede conferma) |
| `stato` | — | Mostra punteggio nel terminale |
| `help` | — | Lista comandi |
| `exit` | `q` | Chiude il CLI |
`Tab` per completamento automatico — `↑ ↓` per navigare nella history dei comandi.
---
## Deploy con Docker
### Prima installazione
@@ -219,8 +190,6 @@ Lo stato viene salvato in `.segnapunti/state.json` anche in modalità dev.
| `npm run dev` | Dev server con hot reload |
| `npm run build` | Build di produzione in `dist/` |
| `npm run serve` | Build + avvio server produzione |
| `npm run cli` | Terminal controller (porta 3000) |
| `npm run cli:dev` | Terminal controller (porta 5173) |
---
-302
View File
@@ -1,302 +0,0 @@
import { WebSocket } from 'ws';
import readline from 'readline';
// ---------------------------------------------------------------------------
// ANSI helpers
// ---------------------------------------------------------------------------
const c = {
reset: '\x1b[0m',
bold: '\x1b[1m',
dim: '\x1b[2m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
cyan: '\x1b[36m',
brightWhite: '\x1b[97m',
};
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
const port = process.argv[2] || process.env.PORT || 3000;
const url = `ws://localhost:${port}/ws`;
// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------
let currentState = null;
let connected = false;
// ---------------------------------------------------------------------------
// Startup banner
// ---------------------------------------------------------------------------
console.log(`\n${c.bold}${c.cyan} Segnapunti Anto — Terminal Controller${c.reset}`);
console.log(`${c.dim} Connessione a ${url}...${c.reset}\n`);
// ---------------------------------------------------------------------------
// WebSocket
// ---------------------------------------------------------------------------
const ws = new WebSocket(url);
ws.on('error', (err) => {
console.error(`${c.red}Errore di connessione: ${err.message}${c.reset}`);
console.error(`${c.dim}Assicurati che il server sia avviato su ${url}${c.reset}`);
process.exit(1);
});
ws.on('open', () => {
connected = true;
console.log(` ${c.green}Connesso.${c.reset} Digita ${c.bold}help${c.reset} per i comandi disponibili.\n`);
ws.send(JSON.stringify({ type: 'register', role: 'controller' }));
});
ws.on('message', (data) => {
try {
const msg = JSON.parse(data.toString());
if (msg.type === 'state') {
currentState = msg.state;
printState(currentState);
} else if (msg.type === 'error') {
clearLine();
console.error(` ${c.red}Errore server: ${msg.message}${c.reset}`);
}
} catch {
// ignora messaggi malformati
}
rl.prompt(true);
});
ws.on('close', () => {
console.log(`\n${c.dim} Connessione chiusa.${c.reset}\n`);
process.exit(0);
});
// ---------------------------------------------------------------------------
// Output helpers
// ---------------------------------------------------------------------------
/** Cancella la riga corrente del terminale (evita di sovrascrivere il prompt). */
function clearLine() {
process.stdout.write('\r\x1b[K');
}
function printState(s) {
if (!s) return;
const { nomi, punt, set, servHome } = s.sp;
const homeServ = servHome ? `${c.yellow}${c.reset}` : ' ';
const guestServ = !servHome ? `${c.yellow}${c.reset}` : ' ';
const homeName = nomi.home.padEnd(15);
const guestName = nomi.guest.padEnd(15);
const homeScore = String(punt.home).padStart(3);
const guestScore = String(punt.guest).padStart(3);
clearLine();
console.log(
` ${homeServ} ${c.bold}${homeName}${c.reset}` +
`${c.brightWhite}${homeScore}${c.reset} ${c.dim}(set ${set.home})${c.reset}` +
` ${c.dim}${c.reset} ` +
`${c.brightWhite}${guestScore}${c.reset} ${c.dim}(set ${set.guest})${c.reset}` +
` ${c.bold}${guestName}${c.reset} ${guestServ}` +
` ${c.dim}[${s.modalitaPartita}]${c.reset}`
);
}
function printHelp() {
console.log(`
${c.bold} Punteggio${c.reset}
${c.cyan}+${c.reset} / ${c.cyan}pc${c.reset} Punto casa (shortcut)
${c.cyan}-${c.reset} / ${c.cyan}po${c.reset} Punto ospite (shortcut)
${c.cyan}punto casa${c.reset} Punto casa
${c.cyan}punto ospite${c.reset} Punto ospite
${c.cyan}undo${c.reset} / ${c.cyan}u${c.reset} Annulla ultimo punto
${c.bold} Set${c.reset}
${c.cyan}set casa${c.reset} Incrementa set casa
${c.cyan}set ospite${c.reset} Incrementa set ospite
${c.bold} Partita${c.reset}
${c.cyan}serv${c.reset} Cambia servizio (solo se 0-0)
${c.cyan}reset${c.reset} Resetta la partita (chiede conferma)
${c.cyan}nomi <casa> <ospite>${c.reset} Imposta nomi squadre
${c.cyan}modalita 2/3${c.reset} Imposta modalità best-of-3
${c.cyan}modalita 3/5${c.reset} Imposta modalità best-of-5
${c.bold} Informazioni${c.reset}
${c.cyan}stato${c.reset} Mostra punteggio attuale
${c.cyan}help${c.reset} Mostra questo aiuto
${c.cyan}exit${c.reset} / ${c.cyan}q${c.reset} Esci
${c.dim}Suggerimento: usa Tab per il completamento automatico dei comandi.${c.reset}
`);
}
// ---------------------------------------------------------------------------
// Command dispatch
// ---------------------------------------------------------------------------
function sendAction(action) {
if (!connected || ws.readyState !== WebSocket.OPEN) {
console.error(` ${c.red}Non connesso al server.${c.reset}`);
rl.prompt();
return;
}
ws.send(JSON.stringify({ type: 'action', action }));
}
function parseCommand(line) {
const parts = line.trim().split(/\s+/);
const cmd = parts[0].toLowerCase();
switch (cmd) {
case '+':
case 'pc':
sendAction({ type: 'incPunt', team: 'home' });
break;
case '-':
case 'po':
sendAction({ type: 'incPunt', team: 'guest' });
break;
case 'punto': {
const team = parts[1]?.toLowerCase();
if (team === 'casa' || team === 'home') {
sendAction({ type: 'incPunt', team: 'home' });
} else if (team === 'ospite' || team === 'guest') {
sendAction({ type: 'incPunt', team: 'guest' });
} else {
console.error(` ${c.red}Uso: punto casa | punto ospite${c.reset}`);
rl.prompt();
}
break;
}
case 'undo':
case 'u':
sendAction({ type: 'decPunt' });
break;
case 'set': {
const team = parts[1]?.toLowerCase();
if (team === 'casa' || team === 'home') {
sendAction({ type: 'incSet', team: 'home' });
} else if (team === 'ospite' || team === 'guest') {
sendAction({ type: 'incSet', team: 'guest' });
} else {
console.error(` ${c.red}Uso: set casa | set ospite${c.reset}`);
rl.prompt();
}
break;
}
case 'serv':
sendAction({ type: 'cambiaPalla' });
break;
case 'reset':
rl.question(` ${c.yellow}Confermi reset partita? (s/N) ${c.reset}`, (answer) => {
if (answer.trim().toLowerCase() === 's') {
sendAction({ type: 'resetta' });
} else {
console.log(` ${c.dim}Reset annullato.${c.reset}`);
rl.prompt();
}
});
return;
case 'nomi': {
const home = parts[1];
const guest = parts[2];
if (!home) {
console.error(` ${c.red}Uso: nomi <casa> <ospite>${c.reset}`);
rl.prompt();
break;
}
const payload = { type: 'setNomi', home };
if (guest) payload.guest = guest;
sendAction(payload);
break;
}
case 'modalita': {
const m = parts[1];
if (m !== '2/3' && m !== '3/5') {
console.error(` ${c.red}Uso: modalita 2/3 | modalita 3/5${c.reset}`);
rl.prompt();
break;
}
sendAction({ type: 'setModalita', modalita: m });
break;
}
case 'stato':
printState(currentState);
rl.prompt();
break;
case 'help':
printHelp();
rl.prompt();
break;
case 'exit':
case 'q':
ws.close();
break;
default:
console.error(
` ${c.red}Comando non riconosciuto: "${cmd}"${c.reset}` +
` — digita ${c.bold}help${c.reset} per la lista`
);
rl.prompt();
}
}
// ---------------------------------------------------------------------------
// REPL
// ---------------------------------------------------------------------------
const TAB_COMPLETIONS = [
'+', '-', 'pc', 'po',
'punto casa', 'punto ospite',
'undo', 'u',
'set casa', 'set ospite',
'serv',
'reset',
'nomi',
'modalita 2/3', 'modalita 3/5',
'stato', 'help',
'exit', 'q',
];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> ',
historySize: 100,
completer(line) {
const hits = TAB_COMPLETIONS.filter((entry) => entry.startsWith(line));
return [hits.length ? hits : TAB_COMPLETIONS, line];
},
});
rl.on('line', (line) => {
if (!line.trim()) {
rl.prompt();
return;
}
parseCommand(line.trim());
});
rl.on('close', () => {
ws.close();
});
-2
View File
@@ -7,8 +7,6 @@
"dev": "vite",
"build": "vite build",
"serve": "vite build && node server.js",
"cli": "node cli.js",
"cli:dev": "node cli.js 5173",
"test": "vitest",
"test:unit": "vitest run tests/unit tests/integration",
"test:component": "vitest run tests/component",
-303
View File
@@ -1,303 +0,0 @@
import { vi, describe, it, expect, beforeAll, beforeEach } from 'vitest'
// vi.hoisted garantisce che refs sia disponibile nelle factory dei mock,
// che vengono hoistate prima degli import statici.
const refs = vi.hoisted(() => ({ ws: null, rl: null }))
// ---------------------------------------------------------------------------
// Mock: ws
// ---------------------------------------------------------------------------
vi.mock('ws', async () => {
const { EventEmitter } = await import('events')
class WebSocket extends EventEmitter {
static OPEN = 1
constructor() {
super()
this.readyState = 1
this.send = vi.fn()
this.close = vi.fn()
refs.ws = this
}
}
return { WebSocket }
})
// ---------------------------------------------------------------------------
// Mock: readline
// ---------------------------------------------------------------------------
vi.mock('readline', async () => {
const { EventEmitter } = await import('events')
return {
default: {
createInterface: vi.fn(() => {
const rl = new EventEmitter()
rl.prompt = vi.fn()
rl.question = vi.fn()
rl.close = vi.fn()
refs.rl = rl
return rl
}),
},
}
})
// ---------------------------------------------------------------------------
// Silenzia output e blocca process.exit durante i test
// ---------------------------------------------------------------------------
vi.spyOn(process, 'exit').mockImplementation(() => {})
vi.spyOn(process.stdout, 'write').mockReturnValue(true)
vi.spyOn(console, 'log').mockImplementation(() => {})
vi.spyOn(console, 'error').mockImplementation(() => {})
// ---------------------------------------------------------------------------
// Importa cli.js — esegue gli effetti collaterali con le dipendenze mockate
// ---------------------------------------------------------------------------
beforeAll(async () => {
await import('../../cli.js')
refs.ws.emit('open')
})
// ---------------------------------------------------------------------------
// Helper
// ---------------------------------------------------------------------------
function sendLine(line) {
refs.rl.emit('line', line)
}
function lastSent() {
const calls = refs.ws.send.mock.calls
return calls.length ? JSON.parse(calls[calls.length - 1][0]) : null
}
// ---------------------------------------------------------------------------
// Test
// ---------------------------------------------------------------------------
describe('CLI — Registrazione', () => {
it('invia register con ruolo controller all\'apertura del WebSocket', () => {
const call = refs.ws.send.mock.calls.find((c) => {
try { return JSON.parse(c[0]).type === 'register' } catch { return false }
})
expect(call).toBeDefined()
expect(JSON.parse(call[0])).toEqual({ type: 'register', role: 'controller' })
})
})
describe('CLI — Ricezione messaggi', () => {
beforeEach(() => {
refs.rl.prompt.mockClear()
vi.mocked(console.error).mockClear()
})
it('ripristina il prompt dopo un messaggio "state"', () => {
const state = {
sp: { nomi: { home: 'A', guest: 'B' }, punt: { home: 0, guest: 0 }, set: { home: 0, guest: 0 }, servHome: true },
modalitaPartita: '3/5',
}
refs.ws.emit('message', JSON.stringify({ type: 'state', state }))
expect(refs.rl.prompt).toHaveBeenCalled()
})
it('mostra un errore alla ricezione di un messaggio "error"', () => {
refs.ws.emit('message', JSON.stringify({ type: 'error', message: 'azione non valida' }))
expect(console.error).toHaveBeenCalled()
})
})
describe('CLI — Comandi punteggio', () => {
beforeEach(() => refs.ws.send.mockClear())
it('"+" → incPunt home', () => {
sendLine('+')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'home' } })
})
it('"pc" → incPunt home (shortcut)', () => {
sendLine('pc')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'home' } })
})
it('"-" → incPunt guest', () => {
sendLine('-')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'guest' } })
})
it('"po" → incPunt guest (shortcut)', () => {
sendLine('po')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'guest' } })
})
it('"punto casa" → incPunt home', () => {
sendLine('punto casa')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'home' } })
})
it('"punto ospite" → incPunt guest', () => {
sendLine('punto ospite')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'guest' } })
})
it('"punto" senza squadra → errore, nessun invio', () => {
sendLine('punto')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
it('"undo" → decPunt', () => {
sendLine('undo')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'decPunt' } })
})
it('"u" → decPunt (shortcut)', () => {
sendLine('u')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'decPunt' } })
})
})
describe('CLI — Comandi set', () => {
beforeEach(() => refs.ws.send.mockClear())
it('"set casa" → incSet home', () => {
sendLine('set casa')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incSet', team: 'home' } })
})
it('"set ospite" → incSet guest', () => {
sendLine('set ospite')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incSet', team: 'guest' } })
})
it('"set" senza squadra → errore, nessun invio', () => {
sendLine('set')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
})
describe('CLI — Comandi partita', () => {
beforeEach(() => refs.ws.send.mockClear())
it('"serv" → cambiaPalla', () => {
sendLine('serv')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'cambiaPalla' } })
})
it('"nomi <casa> <ospite>" → setNomi con entrambi i nomi', () => {
sendLine('nomi Antoniana Riviera')
expect(lastSent()).toMatchObject({
type: 'action',
action: { type: 'setNomi', home: 'Antoniana', guest: 'Riviera' },
})
})
it('"nomi <casa>" → setNomi con solo nome casa', () => {
sendLine('nomi Antoniana')
const sent = lastSent()
expect(sent).toMatchObject({ type: 'action', action: { type: 'setNomi', home: 'Antoniana' } })
expect(sent.action.guest).toBeUndefined()
})
it('"nomi" senza argomenti → errore, nessun invio', () => {
sendLine('nomi')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
it('"modalita 2/3" → setModalita 2/3', () => {
sendLine('modalita 2/3')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'setModalita', modalita: '2/3' } })
})
it('"modalita 3/5" → setModalita 3/5', () => {
sendLine('modalita 3/5')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'setModalita', modalita: '3/5' } })
})
it('"modalita" con valore non valido → errore, nessun invio', () => {
sendLine('modalita 4/7')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
})
describe('CLI — Comando reset (con conferma)', () => {
beforeEach(() => {
refs.ws.send.mockClear()
refs.rl.question.mockClear()
})
it('conferma "s" → invia resetta', () => {
refs.rl.question.mockImplementationOnce((_msg, cb) => cb('s'))
sendLine('reset')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'resetta' } })
})
it('risposta "n" → non invia nulla', () => {
refs.rl.question.mockImplementationOnce((_msg, cb) => cb('n'))
sendLine('reset')
expect(refs.ws.send).not.toHaveBeenCalled()
})
it('risposta vuota → non invia nulla', () => {
refs.rl.question.mockImplementationOnce((_msg, cb) => cb(''))
sendLine('reset')
expect(refs.ws.send).not.toHaveBeenCalled()
})
})
describe('CLI — Comandi informativi', () => {
beforeEach(() => {
refs.ws.send.mockClear()
vi.mocked(console.log).mockClear()
})
it('"help" → stampa aiuto, nessun invio', () => {
sendLine('help')
expect(console.log).toHaveBeenCalled()
expect(refs.ws.send).not.toHaveBeenCalled()
})
it('"stato" → nessun invio', () => {
sendLine('stato')
expect(refs.ws.send).not.toHaveBeenCalled()
})
it('comando sconosciuto → messaggio di errore, nessun invio', () => {
sendLine('xyzzy')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
it('riga vuota → nessun invio', () => {
refs.rl.emit('line', ' ')
expect(refs.ws.send).not.toHaveBeenCalled()
})
})
describe('CLI — Uscita', () => {
it('"exit" → chiude il WebSocket', () => {
refs.ws.close.mockClear()
sendLine('exit')
expect(refs.ws.close).toHaveBeenCalled()
})
it('"q" → chiude il WebSocket (shortcut)', () => {
refs.ws.close.mockClear()
sendLine('q')
expect(refs.ws.close).toHaveBeenCalled()
})
it('chiusura readline → chiude il WebSocket', () => {
refs.ws.close.mockClear()
refs.rl.emit('close')
expect(refs.ws.close).toHaveBeenCalled()
})
})