import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pluriwave/servicios/servicio_audio.dart';
import 'package:pluriwave/widgets/visualizador_audio.dart';

void main() {
  group('VisualizadorAudio lifecycle', () {
    testWidgets('ignora eventos de estado después de dispose', (tester) async {
      final controller = StreamController<EstadoReproduccion>.broadcast();

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: VisualizadorAudio(estadoStream: controller.stream),
          ),
        ),
      );

      await tester.pumpWidget(const SizedBox.shrink());
      controller.add(EstadoReproduccion.reproduciendo);
      await tester.pump();

      expect(tester.takeException(), isNull);
      await controller.close();
    });

    testWidgets('ignora eventos de estado después de dispose en indicador', (tester) async {
      final controller = StreamController<EstadoReproduccion>.broadcast();

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: IndicadorReproduccion(estadoStream: controller.stream),
          ),
        ),
      );

      await tester.pumpWidget(const SizedBox.shrink());
      controller.add(EstadoReproduccion.reproduciendo);
      await tester.pump();

      expect(tester.takeException(), isNull);
      await controller.close();
    });
  });
}
