// connect_button_test.dart — 连接键三态展示与回调
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pangolin_vpn/state/connection_provider.dart';
import 'package:pangolin_vpn/widgets/connect_button.dart';
import 'package:pangolin_vpn/widgets/pangolin_icons.dart';

import '../helpers/harness.dart';

void main() {
  setUpAll(disableGoogleFontsFetching);

  Widget button(VpnPhase phase, {Duration elapsed = Duration.zero, VoidCallback? onTap}) => wrapThemed(
        ConnectButton(
          phase: phase,
          elapsed: elapsed,
          offLabel: '点击连接',
          secureLabel: '已加密',
          onTap: onTap ?? () {},
        ),
      );

  testWidgets('off 态:power 图标 + 点击连接', (tester) async {
    await tester.pumpWidget(button(VpnPhase.off));
    await tester.pump();
    expect(find.byIcon(PangolinIcons.power), findsOneWidget);
    expect(find.text('点击连接'), findsOneWidget);
  });

  testWidgets('connecting 态:loader 图标', (tester) async {
    await tester.pumpWidget(button(VpnPhase.connecting));
    await tester.pump(const Duration(milliseconds: 100));
    expect(find.byIcon(PangolinIcons.loader), findsOneWidget);
  });

  testWidgets('on 态:盾勾 + 计时 + 已加密', (tester) async {
    await tester.pumpWidget(button(VpnPhase.on, elapsed: const Duration(seconds: 5)));
    await tester.pump();
    expect(find.byIcon(PangolinIcons.shieldCheck), findsOneWidget);
    expect(find.text('00:00:05'), findsOneWidget);
    expect(find.text('已加密'), findsOneWidget);
  });

  testWidgets('点击触发回调', (tester) async {
    var tapped = 0;
    await tester.pumpWidget(button(VpnPhase.off, onTap: () => tapped++));
    await tester.pump();
    await tester.tap(find.byType(ConnectButton));
    expect(tapped, 1);
  });
}
