Files
farolero/test/modelos/reconexion_sala_test.dart
T
FreeTLab 863690168c feat(multidispositivo): reconnection, impostor awareness and protocol fixes
Fixes
- SnapshotPartidaOnline no longer serializes esImpostor unless the game is
  over. Every phase change was broadcasting the impostor roles in clear to
  all clients.
- The impostor clue travels in its own field and only when the game enables
  it. It used to be sent as the category key ("todas" when no category was
  picked) and was overwritten by each phase snapshot.
- Eliminated players can no longer vote nor be voted for, on both the client
  screen and the host listener.
- Nearby listeners are removed on dispose; votes are no longer dispatched
  twice; votacionResultado no longer triggers two navigations.
- _jugadoresHostControlados handed the secret word to the host's own
  impostors in the payload.
- Impostor cap unified as maxImpostoresPara(n) for both modes, and the
  silent clamp in multi-device now reports the effective number.
- palabraAleatoria uses Random.secure.

Impostors know each other
- ConfigPartida.impostoresSeConocen, on by default. companerosImpostores is
  nullable: null means nothing to show, an empty list means "you are the
  only impostor".

Reconnection
- IdentidadDispositivo persists a stable device id; the host uses it as the
  clientId so a phone that drops and returns is the same client. Falls back
  to the endpointId for clients that do not send one.
- Usuario.absorbidoDe records the original owner when the host takes over a
  disconnected player, and they are handed back automatically on return.
- New solicitarResync/resync messages: the host replies with the current
  phase plus that client's own players, and the client jumps straight to the
  running phase.
- Readiness is keyed by clientId instead of endpointId, and a disconnected
  client no longer blocks the round.
- The retry gives up after three minutes, only reconnects to the host it
  belonged to, and is cancelled correctly while shutting down.

Per-word clue support
- The word bank loader accepts both the old list-of-strings format and the
  new one carrying a clue per word, falling back to the category clue.

Not verified on real devices: Nearby reconnection timing still needs two
Android phones.
2026-07-25 20:36:32 +02:00

157 lines
4.7 KiB
Dart

import 'package:farolero/modelos/sala_multijugador.dart';
import 'package:farolero/modelos/usuario.dart';
import 'package:flutter_test/flutter_test.dart';
EstadoSalaMultijugador _salaConCliente() {
final sala = EstadoSalaMultijugador.crear(
roomId: 'r1',
nombreSala: 'Sala',
hostClientId: 'host',
hostNombre: 'Ana',
);
sala.registrarCliente(
const ClienteSala(
clientId: 'dev-beto',
endpointId: 'ep-1',
nombre: 'Beto',
),
);
sala.usuarios['u-beto'] = Usuario(
id: 'u-beto',
nombre: 'Beto',
creadoPorClienteId: 'dev-beto',
clienteIdSeleccionado: 'dev-beto',
);
return sala;
}
void main() {
group('Reconexión de un cliente', () {
test('el mismo clientId con endpoint nuevo no crea un cliente duplicado', () {
final sala = _salaConCliente();
sala.registrarCliente(
const ClienteSala(
clientId: 'dev-beto',
endpointId: 'ep-2',
nombre: 'Beto',
),
);
expect(sala.clientes.length, 2, reason: 'host + Beto, sin duplicados');
expect(sala.clientes['dev-beto']!.endpointId, 'ep-2');
expect(sala.clientes['dev-beto']!.conectado, isTrue);
});
test('esReconexion distingue a quien vuelve de quien llega nuevo', () {
final sala = _salaConCliente();
expect(sala.esReconexion('dev-beto'), isTrue);
expect(sala.esReconexion('dev-cris'), isFalse);
});
test('en partida, desconectarse no libera a sus jugadores', () {
final sala = _salaConCliente();
sala.fase = FaseSalaMultijugador.enPartida;
sala.desconectarCliente('dev-beto');
expect(sala.usuarios['u-beto']!.clienteIdSeleccionado, 'dev-beto');
expect(sala.clientes['dev-beto']!.conectado, isFalse);
});
test('en lobby, desconectarse sí libera a sus jugadores', () {
final sala = _salaConCliente();
sala.desconectarCliente('dev-beto');
expect(sala.usuarios['u-beto']!.estaDisponible, isTrue);
});
});
group('Absorción por el host y devolución', () {
test('el host absorbe y queda constancia de quién era el dueño', () {
final sala = _salaConCliente();
sala.fase = FaseSalaMultijugador.enPartida;
sala.desconectarCliente('dev-beto');
final reasignados = sala.reasignarUsuariosDeCliente(
clientIdOrigen: 'dev-beto',
clientIdDestino: 'host',
);
expect(reasignados, 1);
expect(sala.usuarios['u-beto']!.clienteIdSeleccionado, 'host');
expect(sala.usuarios['u-beto']!.absorbidoDe, 'dev-beto');
expect(sala.usuariosAbsorbidosDe('dev-beto').single.id, 'u-beto');
});
test('al volver el móvil recupera a sus jugadores', () {
final sala = _salaConCliente();
sala.fase = FaseSalaMultijugador.enPartida;
sala.desconectarCliente('dev-beto');
sala.reasignarUsuariosDeCliente(
clientIdOrigen: 'dev-beto',
clientIdDestino: 'host',
);
sala.registrarCliente(
const ClienteSala(
clientId: 'dev-beto',
endpointId: 'ep-2',
nombre: 'Beto',
),
);
final devueltos = sala.devolverUsuariosAbsorbidos('dev-beto');
expect(devueltos, 1);
expect(sala.usuarios['u-beto']!.clienteIdSeleccionado, 'dev-beto');
expect(sala.usuarios['u-beto']!.absorbidoDe, isNull);
expect(sala.usuariosPorCliente('host'), isEmpty);
});
test('una doble absorción no pierde al dueño original', () {
final sala = _salaConCliente();
sala.fase = FaseSalaMultijugador.enPartida;
sala.reasignarUsuariosDeCliente(
clientIdOrigen: 'dev-beto',
clientIdDestino: 'host',
);
// El host vuelve a pasar por el mismo camino: no debe reescribir el
// origen a 'host' y dejar al usuario huérfano.
sala.reasignarUsuariosDeCliente(
clientIdOrigen: 'host',
clientIdDestino: 'host',
);
expect(sala.usuarios['u-beto']!.absorbidoDe, 'dev-beto');
});
test('no se devuelve nada a un cliente que no está en la sala', () {
final sala = _salaConCliente();
sala.reasignarUsuariosDeCliente(
clientIdOrigen: 'dev-beto',
clientIdDestino: 'host',
);
expect(sala.devolverUsuariosAbsorbidos('dev-fantasma'), 0);
});
});
group('Serialización', () {
test('absorbidoDe sobrevive al viaje JSON', () {
final sala = _salaConCliente();
sala.fase = FaseSalaMultijugador.enPartida;
sala.reasignarUsuariosDeCliente(
clientIdOrigen: 'dev-beto',
clientIdDestino: 'host',
);
final reparsed = EstadoSalaMultijugador.fromJson(sala.toJson());
expect(reparsed.usuarios['u-beto']!.absorbidoDe, 'dev-beto');
expect(reparsed.clientes['dev-beto']!.endpointId, 'ep-1');
});
});
}