// flutter_test_config.dart — 全测试前置（Flutter 自动应用于 test/ 下所有测试）。
//
// 启用 PangolinFonts.useBundled，并用 FontLoader 注册 test/fonts/ 的真实字体，
// 让 widget/golden 测试用真字体渲染、且不触发 google_fonts 运行时拉取（避免抛异常）。
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pangolin_vpn/pangolin_theme.dart';

Future<void> testExecutable(FutureOr<void> Function() testMain) async {
  TestWidgetsFlutterBinding.ensureInitialized();
  PangolinFonts.useBundled = true;
  await _load('Sora', const ['test/fonts/Sora-Regular.ttf', 'test/fonts/Sora-SemiBold.ttf', 'test/fonts/Sora-Bold.ttf']);
  await _load('Manrope', const [
    'test/fonts/Manrope-Regular.ttf',
    'test/fonts/Manrope-Medium.ttf',
    'test/fonts/Manrope-SemiBold.ttf',
    'test/fonts/Manrope-Bold.ttf',
  ]);
  await _load('JetBrains Mono', const ['test/fonts/JetBrainsMono-Regular.ttf']);
  // Lucide 图标字体（family 须带 package 前缀以匹配 PangolinIcons 的 fontPackage）。
  await _load('packages/lucide_icons/Lucide', const ['packages/lucide_icons_patched/fonts/lucide.ttf']);
  await testMain();
}

Future<void> _load(String family, List<String> paths) async {
  final loader = FontLoader(family);
  for (final p in paths) {
    final file = File(p);
    if (file.existsSync()) {
      final bytes = file.readAsBytesSync();
      loader.addFont(Future.value(ByteData.view(bytes.buffer, bytes.offsetInBytes, bytes.lengthInBytes)));
    }
  }
  await loader.load();
}
