import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:health_app/models/ble_device.dart';
import 'package:health_app/pages/device/device_sync_ui_logic.dart';

void main() {
  test('unsupported bound devices are labelled without pretending to sync', () {
    expect(deviceSyncAvailabilityLabel(BleDeviceType.glucose), '暂不支持自动同步');
    expect(deviceSyncAvailabilityLabel(BleDeviceType.bloodPressure), isNull);
  });

  test('device sync errors keep connection and upload failures distinct', () {
    expect(
      deviceSyncErrorMessage(
        const DeviceSyncFailure(DeviceSyncFailureStage.permission),
      ),
      contains('蓝牙权限'),
    );
    expect(
      deviceSyncErrorMessage(
        const DeviceSyncFailure(DeviceSyncFailureStage.upload),
      ),
      contains('上传'),
    );
  });

  test('bound device page keeps the automatic scan indicator', () {
    final page = File(
      'lib/pages/device/device_management_page.dart',
    ).readAsStringSync();

    expect(page, contains('AnimationController('));
    expect(page, contains('child: _ScanIndicator('));
    expect(page, contains('scanning: _scanning'));
  });

  test('profile exposes a real edit route and keeps phone read only', () {
    final profile = File(
      'lib/pages/profile/profile_page.dart',
    ).readAsStringSync();
    final editor = File(
      'lib/pages/profile/profile_edit_page.dart',
    ).readAsStringSync();
    final router = File('lib/core/app_router.dart').readAsStringSync();

    expect(profile, contains("pushRoute(ref, 'profileEdit')"));
    expect(editor, contains('手机号不可修改'));
    expect(router, contains("case 'profileEdit':"));
  });

  test(
    'drawer does not refetch history when only current conversation changes',
    () {
      final provider = File(
        'lib/providers/conversation_history_provider.dart',
      ).readAsStringSync();
      final drawer = File('lib/widgets/health_drawer.dart').readAsStringSync();

      expect(
        provider,
        isNot(
          contains(
            'ref.watch(chatProvider.select((state) => state.conversationId))',
          ),
        ),
      );
      expect(drawer, contains('currentConversationId'));
    },
  );
}
