Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

54.7 KB
Raw Blame
// 生成 README 与网站用的界面截图:演示数据,不连接后台服务,不读真实剪贴板。
//
// 不要直接运行,用仓库根目录的 tools/screenshots/render.sh——
// 它会先从本机系统字体生成渲染用的字体文件,渲染完再加上 macOS 窗口外框。
//
// 放在 tool/ 而非 test/:它依赖本机字体,不该随 flutter test 在 CI 上跑。
// ignore_for_file: invalid_use_of_visible_for_testing_member
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:ui' show ImageByteFormat;

import 'package:pasteferry_ui/app_state.dart';
import 'package:pasteferry_ui/background_service.dart';
import 'package:pasteferry_ui/gen/pasteferry/v1/daemon.pb.dart';
import 'package:pasteferry_ui/i18n.dart';
import 'package:pasteferry_ui/main.dart';
import 'package:pasteferry_ui/picker/picker_host.dart';
import 'package:pasteferry_ui/picker/picker_state.dart';
import 'package:pasteferry_ui/picker/picker_view.dart';
import 'package:pasteferry_ui/icons.dart';
import 'package:pasteferry_ui/theme.dart';
import 'package:pasteferry_ui/theme_controller.dart';
import 'package:pasteferry_ui/theme_json.dart';
import 'package:pasteferry_ui/widgets/avatar.dart';
import 'package:pasteferry_ui/widgets/backdrop.dart';
import 'package:pasteferry_ui/widgets/friend_send_dialog.dart';
import 'package:pasteferry_ui/widgets/friends_dialog.dart';
import 'package:pasteferry_ui/widgets/nearby_dialog.dart';
import 'package:pasteferry_ui/widgets/pairing_dialog.dart';
import 'package:pasteferry_ui/widgets/send_dialog.dart';
import 'package:pasteferry_ui/widgets/web_share.dart';
import 'package:fixnum/fixnum.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

/// 截图的界面语言:zh、zh-tw、zh-hk、en 或 ja,由 render.sh 设置。演示数据里的文件名、文字也跟着换。
final _lang = Platform.environment['SCREENSHOT_LANG'] ?? 'zh';
final _locale = switch (_lang) { 'zh-tw' => zhHantTW, 'zh-hk' => zhHantHK, _ => Locale(_lang) };
String _t(String zh, String en, String ja, {required String tw, required String hk}) =>
    switch (_lang) { 'en' => en, 'ja' => ja, 'zh-tw' => tw, 'zh-hk' => hk, _ => zh };

/// 截图用哪套主题:SCREENSHOT_THEME=pixel 渲染像素主题,不设时是默认主题;
/// SCREENSHOT_THEME_FILE 指向一份主题 JSON 时用它(宣传片里展示自定义主题)
final AppThemeSpec _spec = switch (Platform.environment['SCREENSHOT_THEME_FILE']) {
  final String file => parseTheme(File(file).readAsStringSync()),
  null => AppThemeSpec.byId(Platform.environment['SCREENSHOT_THEME'] ?? ''),
};

/// 背景图:SCREENSHOT_BACKGROUND 指向一个目录,里面是后台服务导入后的 sharp.img、blur.img
/// 与分析结果 info.json;主题(SCREENSHOT_THEME_FILE)里的 background.image 要写它的 sha256
Future<void> _seedBackground(ThemeController theme) async {
  final dir = Platform.environment['SCREENSHOT_BACKGROUND'];
  if (dir == null) return;
  final info = jsonDecode(File('$dir/info.json').readAsStringSync()) as Map<String, dynamic>;
  if (!theme.themes.any((t) => t.id == _spec.id)) {
    theme.debugSeed(themes: [...theme.themes, ThemeChoice(id: _spec.id, names: const {}, builtin: false, spec: _spec)]);
  }
  theme.debugSeed(id: _spec.id);
  await theme.debugSeedBackground(
    info['SHA256'] as String,
    File('$dir/sharp.img').readAsBytesSync(),
    BackgroundInfo(
      sha256: info['SHA256'] as String,
      colors: (info['Colors'] as List).cast<String>(),
      minScrimDark: (info['MinScrimDark'] as num).toDouble(),
      minScrimLight: (info['MinScrimLight'] as num).toDouble(),
    ),
    blurred: File('$dir/blur.img').readAsBytesSync(),
  );
}

ThemeData _theme(Brightness brightness) =>
    buildTheme(brightness, spec: _spec, fontFamily: 'SF', fontFamilyFallback: _cjk);

/// 与 App 一样,主题规定了字号阶梯时把字号吸附上去。像素主题的场景与光标会一直动,
/// 截图时按「减少动态效果」只画静止的一帧,否则等不到画面静止
Widget _textScale(BuildContext context, Widget? child) {
  final scaled = ThemeTextScale(child: child!);
  if (!_spec.shape.pixel) return scaled;
  return MediaQuery(data: MediaQuery.of(context).copyWith(disableAnimations: true), child: scaled);
}

/// 汉字的字形跟着语言走:日文界面用冬青黑体,繁体用苹方的台湾、香港字形,否则用简体的苹方
final _cjk = switch (_lang) {
  'ja' => const ['Hiragino', 'PingFang'],
  'zh-tw' => const ['PingFangTC'],
  'zh-hk' => const ['PingFangHK'],
  _ => const ['PingFang'],
};

/// 生成配对码、搜索附近设备、搜索复制记录都不走后台服务,直接给固定的演示数据。
/// 手机网页传输二维码的演示点阵(41×41),由 client-core/internal/qrcode 对一个演示网址生成
const _demoQr = '1111111001111110001101110101011100111111110000010100110110101001100110011001000001101110100001100100010110101110011010111011011101000000001010101100111111100101110110111010110000101001101001000100001011101100000100000011111011101110101010010000011111111010101010101010101010101010111111100000000011001001101010011000110000000000101010100000101101100110011101100000100101101000110111111110000100100001000110110001100010000010010100001011001100001010111101110001000111001010100011011010001100011110111010001011110111000100111010110101100111001101101100101110100001010101000111010001111000001101111110011001100001011111001010100100000010011110101111001111000000000010010110110001100010010001001101001001010011001001100011101110111000111001011100111111101111010000100010110010101110110111101000011001001011011010101100101000101111110001010100011010010101101010110001000010001000111000110100010111010100001110100010001001100100111011001110100111111000101000101010101111101010111001000010100101011011111001011001100110011001111100101101000011001110000011000100100001000011111011100000100101100110100000000110001111010100011001000011011101111011010000001110000011101111010110000110001101111011011001000001000111111110011100110111010010001000011000111010100001111100000011001001101101100100011000101010011010101101111000100001100001101101100111011101001111110100000000011100001111010101110011010001101111111110000111010010010011101010101011011100000100111110011101001111101001000100011011101010010111101010000010010011111100110111010001011110110100011011000110011011101110101111111100000010101011001001111111000001001100101010101011111010001010001011111110111101100111011111110111100110011';

class _DemoState extends AppState {
  @override
  Future<WebShareInvite> newWebShareInvite(String address) async => WebShareInvite(
        id: 'demo',
        url: 'http://192.168.1.20:8790/#i=demo',
        expiresAtUnix: Int64(DateTime.now().add(const Duration(minutes: 10)).millisecondsSinceEpoch ~/ 1000),
        qrSize: 41,
        qrModules: [for (final c in _demoQr.codeUnits) c - 48],
      );

  @override
  Future<void> closeWebShareInvite(String id) async {}

  @override
  Future<PairingCode> createPairingCode() async => (code: 'K7MP2X', addresses: ['192.168.1.20']);

  @override
  Future<PairingCode> createFriendCode() async => (code: 'F7K3QXM', addresses: ['192.168.1.20']);

  // 「所有人」模式下附近的陌生设备:截图里不放
  @override
  Stream<List<Stranger>> watchStrangers() => StreamController<List<Stranger>>().stream;

  @override
  Future<ListHistoryResponse> requestSearch(String query) async =>
      ListHistoryResponse(records: records.where((r) => matchesSearch(r, query)), searched: true);

  // 悬停提示框里的全文
  @override
  Future<ClipDetail> clipDetail(String clipId) async => ClipDetail(text: _meetingNotes, textLength: _meetingNotes.length);

  // 不关闭的流:后台服务那边的订阅一直开着,结束就表示后台服务断开了
  @override
  Stream<List<NearbyDevice>> watchNearby() => (StreamController<List<NearbyDevice>>()
        ..add([
          NearbyDevice(id: 'n1', name: _t('设计部的 PC', 'Design PC', 'デザイン部の PC', tw: '設計部的 PC', hk: '設計部的 PC'), platform: 'windows'),
          NearbyDevice(id: 'n2', name: 'MacBook Air', platform: 'darwin', paired: true),
          NearbyDevice(id: 'n3', name: _t('会议室的 iMac', 'Meeting room iMac', '会議室の iMac', tw: '會議室的 iMac', hk: '會議室的 iMac'), platform: 'darwin'),
        ]))
      .stream;
}

Future<void> _loadFonts() async {
  final dir = Platform.environment['SCREENSHOT_FONTS'];
  if (dir == null) throw StateError('用 tools/screenshots/render.sh 运行,它会设置 SCREENSHOT_FONTS');

  Future<void> family(String name, List<String> files) async {
    final loader = FontLoader(name);
    for (final f in files) {
      loader.addFont(Future.value(ByteData.sublistView(File('$dir/$f').readAsBytesSync())));
    }
    await loader.load();
  }

  await family('SF', ['SF-Regular.ttf', 'SF-Medium.ttf', 'SF-Semibold.ttf']);
  await family('PingFang', ['PingFang-Regular.ttf', 'PingFang-Medium.ttf', 'PingFang-Semibold.ttf']);
  await family('PingFangTC', ['PingFangTC-Regular.ttf', 'PingFangTC-Medium.ttf', 'PingFangTC-Semibold.ttf']);
  await family('PingFangHK', ['PingFangHK-Regular.ttf', 'PingFangHK-Medium.ttf', 'PingFangHK-Semibold.ttf']);
  await family('Hiragino', ['Hiragino-Regular.ttf', 'Hiragino-Medium.ttf', 'Hiragino-Semibold.ttf']);
  await family(monoFamily, ['Menlo-Regular.ttf', 'Menlo-Bold.ttf']);
  // 图标字体随依赖包打进测试资源,但测试环境不会自动加载
  final icons = FontLoader('packages/cupertino_icons/CupertinoIcons')
    ..addFont(rootBundle.load('packages/cupertino_icons/assets/CupertinoIcons.ttf'));
  await icons.load();
  // 官方服务等少数地方用的是 Material 图标
  final material = FontLoader('MaterialIcons')..addFont(rootBundle.load('fonts/MaterialIcons-Regular.otf'));
  await material.load();
  // 像素主题的字体与图标(App 自带的资源)
  for (final (name, file) in const [
    ('FusionPixel', 'FusionPixel12.otf'),
    ('FusionPixelMono', 'FusionPixel12Mono.otf'),
    ('PixelIcons', 'PixelIcons.ttf'),
  ]) {
    await (FontLoader(name)..addFont(rootBundle.load('assets/fonts/$file'))).load();
  }
}

final _now = DateTime.now();
Int64 _ago(Duration d) => Int64(_now.subtract(d).millisecondsSinceEpoch ~/ 1000);

ClipRecord _clip(
  String id,
  ClipKind kind, {
  String preview = '',
  bool outgoing = false,
  String from = 'MacBook Air',
  int size = 0,
  required Duration ago,
  ClipStatus status = ClipStatus.CLIP_STATUS_READY,
  List<ClipItem> items = const [],
}) =>
    ClipRecord(
      id: id,
      kind: kind,
      status: status,
      outgoing: outgoing,
      originDeviceName: outgoing ? 'MacBook Pro' : from,
      textPreview: preview,
      totalSize: Int64(size),
      createdAtUnix: _ago(ago),
      items: items,
    );

const _mb = 1024 * 1024;
final _quarterly = _t('季度汇报-终版.key', 'Q3 review - final.key', '四半期報告_最終版.key',
    tw: '季度報告-定稿.key', hk: '季度報告-最終版.key');
final _meeting = _t('会议结论:周二灰度 10%,观察两天没问题周四全量;回滚预案见文档第 3 节',
    'Decision: ship to 10% on Tuesday, everyone on Thursday if two days look clean. Rollback plan in section 3',
    '決定事項:火曜に 10% へ先行公開、2 日問題なければ木曜に全体公開。切り戻し手順は資料の第 3 節',
    tw: '會議結論:週二先開放 10%,觀察兩天沒問題就在週四全面上線;回滾方案見文件第 3 節',
    hk: '會議結論:星期二先開放 10%,觀察兩日沒問題便於星期四全面推出;回滾方案見文件第 3 節');
// 悬停提示框里显示的全文:比列表里那一行长,换了行
final _meetingNotes = _meeting + _t(
    '\n\n负责人:前端 小林,后端 老周,测试 阿杰\n发布窗口:周二 10:00–11:00\n监控:错误率超过 0.5% 立即回滚',
    '\n\nOwners: web Lin, backend Zhou, QA Jay\nRelease window: Tuesday 10:00–11:00\nMonitoring: roll back at once if errors exceed 0.5%',
    '\n\n担当:フロント 林、バックエンド 周、テスト 杰\n公開時間:火曜 10:00–11:00\n監視:エラー率 0.5% 超えで即切り戻し',
    tw: '\n\n負責人:前端 小林,後端 老周,測試 阿杰\n發布時段:週二 10:00–11:00\n監控:錯誤率超過 0.5% 立即回滾',
    hk: '\n\n負責人:前端 小林,後端 老周,測試 阿杰\n發佈時段:星期二 10:00–11:00\n監察:錯誤率超過 0.5% 立即回滾');

ClipRecord _favorite(ClipRecord r, Duration ago) => r
  ..favorite = true
  ..favoritedAtUnix = _ago(ago);

/// 收藏页的演示数据:常用的文字、链接、文件。收藏页按收藏时间从新到旧排。
/// 例子里不用编出来的公司名、真实的地址
List<ClipRecord> _favorites() => <ClipRecord>[
      _favorite(
          _clip('f1', ClipKind.CLIP_KIND_TEXT,
              preview: _t('周报模板:本周完成 / 下周计划 / 需要协助',
                  'Weekly report: done this week / next week / blockers',
                  '週報テンプレート:今週の成果/来週の予定/相談事項',
                  tw: '週報範本:本週完成 / 下週計畫 / 需要協助',
                  hk: '週報範本:本週完成 / 下週計劃 / 需要協助'),
              outgoing: true, size: 72, ago: const Duration(days: 12)),
          const Duration(days: 12)),
      _favorite(
          _clip('f2', ClipKind.CLIP_KIND_TEXT,
              preview: _t('收到,我看一下,今天下班前给你答复。', 'Got it — I’ll take a look and get back to you today.',
                  '承知しました。確認して本日中にお返事します。',
                  tw: '收到,我看一下,今天下班前給你答覆。', hk: '收到,我睇一睇,今日放工前覆你。'),
              outgoing: true, size: 54, ago: const Duration(days: 6)),
          const Duration(days: 6)),
      _favorite(
          _clip('f3', ClipKind.CLIP_KIND_TEXT,
              preview: 'https://pasteferry.cc/download/', outgoing: true, size: 31, ago: const Duration(days: 4))
            ..isLink = true,
          const Duration(days: 4)),
      _favorite(
          _clip('f4', ClipKind.CLIP_KIND_FILE,
              size: (1.2 * _mb).round(), ago: const Duration(days: 9),
              items: [ClipItem(name: _t('营业执照.pdf', 'certificate-of-incorporation.pdf', '登記簿謄本.pdf',
                  tw: '公司登記證明.pdf', hk: '商業登記證.pdf'), size: Int64((1.2 * _mb).round()))]),
          const Duration(days: 3)),
      _favorite(
          _clip('f5', ClipKind.CLIP_KIND_TEXT,
              preview: 'ssh [email protected] -p 2222', outgoing: true, size: 32, ago: const Duration(days: 20)),
          const Duration(days: 2)),
    ].reversed.toList();

AppState _demo({LinkState link = LinkState.online,
    ServiceState? service, Device? pending, bool empty = false,
    String server = 'wss://sync.example.com/signal'}) {
  final state = _DemoState();
  state.debugSeed(
    link: link,
    serviceState: service,
    pendingPairing: pending,
    status: Status(
      version: '2.5.4',
      deviceName: 'MacBook Pro',
      signalingConnected: link == LinkState.online && server.isNotEmpty,
      serverConfigured: server.isNotEmpty,
      lanEnabled: true,
      clipboardPermission: ClipboardPermission.CLIPBOARD_PERMISSION_ALWAYS_ALLOW,
      cacheBytesUsed: Int64(412 * _mb),
      peersOnline: 2,
    ),
    config: Config(
      signalingUrl: server,
      deviceName: 'MacBook Pro',
      autoSyncThresholdBytes: Int64(50 * _mb),
      historyTtlSeconds: Int64(3 * 86400),
      cacheTtlSeconds: Int64(86400),
      syncText: true,
      syncHtml: true,
      syncImage: true,
      syncFile: true,
      autoApplyToClipboard: true,
    ),
    self: Device(id: 'self', name: 'MacBook Pro', platform: 'darwin', publicKeyFingerprint: 'H3QD-7KXA-M2PV'),
    peers: empty ? const [] : [
      Device(
        id: 'air',
        name: 'MacBook Air',
        platform: 'darwin',
        online: true,
        connection: ConnectionKind.CONNECTION_KIND_DIRECT,
        publicKeyFingerprint: 'R8NF-2WTC-QL5J',
      ),
      Device(
        id: 'mini',
        name: 'Mac mini',
        platform: 'darwin',
        online: true,
        connection: ConnectionKind.CONNECTION_KIND_RELAY,
        publicKeyFingerprint: 'B6YE-X4MH-9DKS',
      ),
      Device(id: 'imac', name: 'iMac', platform: 'darwin', publicKeyFingerprint: 'T2VA-LP7Q-8ZRN'),
    ],
    favorites: empty ? const [] : _favorites(),
    records: empty ? const [] : [
      _clip('1', ClipKind.CLIP_KIND_TEXT,
          preview: 'git push origin release/1.0 && gh release create v1.0.0',
          outgoing: true, size: 54, ago: const Duration(seconds: 4)),
      _clip('2', ClipKind.CLIP_KIND_IMAGE,
          preview: _t('图片 2.4 MB', 'Image 2.4 MB', '画像 2.4 MB', tw: '圖片 2.4 MB', hk: '圖片 2.4 MB'), size: (2.4 * _mb).round(), ago: const Duration(minutes: 2),
          items: [ClipItem(name: 'image.png', size: Int64((2.4 * _mb).round()))]),
      _clip('3', ClipKind.CLIP_KIND_FILE,
          size: (38.2 * _mb).round(), ago: const Duration(minutes: 9),
          items: [ClipItem(name: _quarterly, size: Int64((38.2 * _mb).round()))]),
      _clip('4', ClipKind.CLIP_KIND_FILE,
          size: (1.3 * 1024 * _mb).round(), ago: const Duration(minutes: 14),
          from: 'Mac mini', status: ClipStatus.CLIP_STATUS_REMOTE_ONLY,
          items: [ClipItem(name: _t('发布演示录屏.mov', 'launch-demo.mov', '発表デモ.mov', tw: '發表會示範錄影.mov', hk: '發佈會示範錄影.mov'), size: Int64((1.3 * 1024 * _mb).round()))]),
      _clip('5', ClipKind.CLIP_KIND_HTML,
          preview: _meeting, from: 'Mac mini', size: 2310, ago: const Duration(minutes: 26)),
      _clip('6', ClipKind.CLIP_KIND_FILE,
          outgoing: true, size: 126 * _mb, ago: const Duration(minutes: 41),
          items: [ClipItem(name: 'design-assets', size: Int64(126 * _mb), isDir: true)]),
      _clip('7', ClipKind.CLIP_KIND_FILE,
          size: (18.6 * _mb).round(), ago: const Duration(hours: 1),
          status: ClipStatus.CLIP_STATUS_FETCHING,
          items: [ClipItem(name: _t('合同扫描件.pdf', 'signed-contract.pdf', '契約書_署名済み.pdf', tw: '合約掃描檔.pdf', hk: '合約掃描本.pdf'), size: Int64((18.6 * _mb).round()))]),
      _clip('8', ClipKind.CLIP_KIND_TEXT,
          preview: 'https://github.com/baiyuze/pasteferry/releases', outgoing: true, size: 44,
          ago: const Duration(hours: 3))
        ..isLink = true,
      _clip('9', ClipKind.CLIP_KIND_FILE,
          size: (4.1 * _mb).round(), ago: const Duration(hours: 5),
          items: [
            ClipItem(name: 'IMG_2041.HEIC', size: Int64((1.4 * _mb).round())),
            ClipItem(name: 'IMG_2042.HEIC', size: Int64((1.3 * _mb).round())),
            ClipItem(name: 'IMG_2043.HEIC', size: Int64((1.4 * _mb).round())),
          ]),
      _clip('10', ClipKind.CLIP_KIND_IMAGE,
          preview: _t('图片 864 KB', 'Image 864 KB', '画像 864 KB', tw: '圖片 864 KB', hk: '圖片 864 KB'), outgoing: true, size: 864 * 1024, ago: const Duration(days: 1),
          items: [ClipItem(name: 'image.png', size: Int64(864 * 1024))]),
    ],
  );
  return state;
}

/// 隔空传送页的演示数据:同一局域网、跨网络中转、离线的设备,收到的与发出的各种状态。
AppState _sendDemo() {
  final base = _demo();
  final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  Int64 ago(int s) => Int64(now - s);
  const incoming = TransferDirection.TRANSFER_DIRECTION_INCOMING;
  const outgoing = TransferDirection.TRANSFER_DIRECTION_OUTGOING;
  final photos = _t('照片 2026-10', 'Photos 2026-10', '写真 2026-10', tw: '照片 2026-10', hk: '相片 2026-10');
  final contract = _t('合同扫描件.pdf', 'signed-contract.pdf', '契約書_署名済み.pdf', tw: '合約掃描檔.pdf', hk: '合約掃描本.pdf');
  final windows = _t('Windows 台式机', 'Windows Desktop', 'Windows デスクトップ', tw: 'Windows 桌機', hk: 'Windows 桌面電腦');
  final ubuntu = _t('Ubuntu 笔记本', 'Ubuntu Laptop', 'Ubuntu ノート', tw: 'Ubuntu 筆電', hk: 'Ubuntu 手提電腦');
  base.debugSeed(
    status: base.status!..officialUsage = OfficialUsage(available: true, remainingBytes: Int64(62 * _mb)),
    config: base.config!..serviceMode = ServiceMode.SERVICE_MODE_OFFICIAL,
    self: base.self!,
    peers: [
      Device(id: 'air', name: 'MacBook Air', platform: 'darwin', online: true, lanReachable: true,
          connection: ConnectionKind.CONNECTION_KIND_DIRECT, receivesFiles: true),
      Device(id: 'win', name: windows, platform: 'windows', online: true,
          connection: ConnectionKind.CONNECTION_KIND_RELAY, receivesFiles: true),
      Device(id: 'ubuntu', name: ubuntu, platform: 'linux', online: true, lanReachable: true,
          connection: ConnectionKind.CONNECTION_KIND_DIRECT, receivesFiles: true),
      Device(id: 'imac', name: 'iMac', platform: 'darwin', receivesFiles: true),
      Device(id: 'mini', name: 'Mac mini', platform: 'darwin', receivesFiles: true),
    ],
    transfers: [
      Transfer(id: 'r1', direction: incoming, deviceId: 'air', deviceName: 'MacBook Air', status: 'active',
          items: [ClipItem(name: photos, isDir: true, size: Int64(2150 * _mb))],
          totalBytes: Int64(2150 * _mb), doneBytes: Int64(1462 * _mb), fileCount: Int64(412),
          bytesPerSecond: 42.0 * _mb, currentFile: '$photos/IMG_2041.HEIC', currentIndex: Int64(281),
          createdAtUnix: ago(40)),
      Transfer(id: 'r2', direction: incoming, deviceId: 'air', deviceName: 'MacBook Air', status: 'done',
          items: [ClipItem(name: contract), ClipItem(name: 'notes.txt'), ClipItem(name: 'IMG_2042.HEIC')],
          paths: ['/a', '/b', '/c'], present: [true, true, true], totalBytes: Int64((18.4 * _mb).round()),
          fileCount: Int64(3), createdAtUnix: ago(200), finishedAtUnix: ago(180), seen: true),
      Transfer(id: 'r3', direction: incoming, deviceId: 'win', deviceName: windows, status: 'done',
          items: [ClipItem(name: 'PasteFerry-Setup.exe')], paths: ['/d'], present: [false],
          totalBytes: Int64(42 * _mb), fileCount: Int64(1), createdAtUnix: ago(90000), finishedAtUnix: ago(90000),
          seen: true),
      Transfer(id: 's1', direction: outgoing, deviceId: 'air', deviceName: 'MacBook Air', status: 'done',
          items: [ClipItem(name: 'design-assets', isDir: true)], paths: ['/e'], totalBytes: Int64(126 * _mb),
          fileCount: Int64(58), createdAtUnix: ago(4000), finishedAtUnix: ago(3990)),
    ],
  );
  return base;
}

/// 好友(2.6.0)的演示数据:本机的名字,三个好友(局域网、需要对方打开服务、离线),
/// 与好友之间的传输带备注。人名都是演示用的,不对应真实的人
AppState _friendsDemo() {
  final base = _sendDemo();
  final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  Int64 ago(int s) => Int64(now - s);
  const incoming = TransferDirection.TRANSFER_DIRECTION_INCOMING;
  const outgoing = TransferDirection.TRANSFER_DIRECTION_OUTGOING;
  final xm = _t('小明', 'Alex', '佐藤', tw: '小明', hk: '小明');
  final aj = _t('阿杰', 'Jordan', '田中', tw: '阿傑', hk: '阿傑');
  final wj = _t('王姐', 'Sam', '鈴木', tw: '王姐', hk: '王姐');
  final contract = _t('合同扫描件.pdf', 'signed-contract.pdf', '契約書_署名済み.pdf', tw: '合約掃描檔.pdf', hk: '合約掃描本.pdf');
  final note = _t('合同第 3 页要签字', 'Please sign page 3', '3 ページ目に署名してください', tw: '合約第 3 頁要簽名', hk: '合約第 3 頁要簽名');
  final quote = _t('报价单.png', 'quote.png', '見積書.png', tw: '報價單.png', hk: '報價單.png');
  final quoteNote = _t('看下这个价格', 'Does this price work?', 'この価格で大丈夫?', tw: '看一下這個價格', hk: '看看這個價錢');
  final winDesk = _t('Win 台式机', 'Windows PC', 'Windows PC', tw: 'Win 桌機', hk: 'Win 桌面電腦');
  final friends = [
    Friend(id: 'f-xm', displayName: xm, deviceName: 'MacBook Air', platform: 'darwin', online: true, lanReachable: true,
        connection: ConnectionKind.CONNECTION_KIND_DIRECT, featuresKnown: true, receivesFiles: true,
        addedAtUnix: ago(86400 * 3), lastActivityUnix: ago(300)),
    Friend(id: 'f-aj', displayName: aj, deviceName: winDesk, platform: 'windows', reachability: 'peer_no_service',
        featuresKnown: true, receivesFiles: true, addedAtUnix: ago(86400 * 9), lastActivityUnix: ago(86400)),
    Friend(id: 'f-wj', displayName: wj, deviceName: 'MacBook Pro', platform: 'darwin', featuresKnown: true,
        receivesFiles: true, addedAtUnix: ago(86400 * 20), lastActivityUnix: ago(86400 * 6)),
  ];
  final transfers = [
    Transfer(id: 'fr1', direction: incoming, deviceId: 'f-xm', deviceName: '$xm · MacBook Air', status: 'done',
        peerRole: 'friend', note: quoteNote, items: [ClipItem(name: quote, size: Int64(640 * 1024))],
        paths: ['/q'], present: [true], totalBytes: Int64(640 * 1024), fileCount: Int64(1),
        createdAtUnix: ago(320), finishedAtUnix: ago(300)),
    Transfer(id: 'fs1', direction: outgoing, deviceId: 'f-xm', deviceName: '$xm · MacBook Air', status: 'done',
        peerRole: 'friend', note: note, items: [ClipItem(name: contract), ClipItem(name: 'notes.txt')],
        paths: ['/c'], totalBytes: Int64((18.4 * _mb).round()), fileCount: Int64(2),
        createdAtUnix: ago(120), finishedAtUnix: ago(110)),
    ...base.transfers(incoming),
    ...base.transfers(outgoing),
  ]..sort((a, b) => b.createdAtUnix.compareTo(a.createdAtUnix));
  base.debugSeed(
    status: base.status!,
    config: base.config!..receiveFrom = 'friends',
    self: base.self!,
    peers: base.peers,
    friends: friends,
    profile: Profile(displayName: _t('光光', 'Robin', '高橋', tw: '光光', hk: '光光'), deviceId: 'self'),
    transfers: transfers,
  );
  return base;
}

final _boundary = GlobalKey();

Future<void> _shoot(
  WidgetTester tester,
  String name, {
  required AppState state,
  Section section = Section.history,
  Brightness brightness = Brightness.light,
  Future<void> Function()? arrange,
  bool settle = true,
  Size window = const Size(960, 640),
}) async {
  // 默认 960×640 的窗口,按 Retina 2 倍渲染
  tester.view.physicalSize = window * 2;
  tester.view.devicePixelRatio = 2;
  addTearDown(tester.view.reset);
  // 平台覆盖必须在测试体内复原:框架在执行 tearDown 之前就会检查它
  debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
  // 测试框架默认把阴影画成实心描边(便于图片比对),截图要真实的模糊阴影
  debugDisableShadows = false;
  try {
    await _render(tester, name, state: state, section: section, brightness: brightness,
        arrange: arrange, settle: settle);
  } finally {
    debugDefaultTargetPlatformOverride = null;
    debugDisableShadows = true;
  }
}

Future<void> _render(
  WidgetTester tester,
  String name, {
  required AppState state,
  required Section section,
  required Brightness brightness,
  required Future<void> Function()? arrange,
  required bool settle,
}) async {

  // 背景图要真的解码、取样(算遮罩浓度),在测试的假时钟外面做
  await tester.runAsync(() => _seedBackground(state.theme));
  final theme = _theme(brightness);
  await tester.pumpWidget(RepaintBoundary(
    key: _boundary,
    child: AppScope(
      state: state,
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        builder: (context, child) =>
            _textScale(context, ThemedBackdrop(controller: state.theme, window: 'main', child: child!)),
        theme: theme,
        locale: _locale,
        supportedLocales: supportedLocales,
        localizationsDelegates: AppLocalizations.localizationsDelegates,
        home: Shell(initialSection: section),
      ),
    ),
  ));
  await tester.pump();
  // 图片解码是真实的异步操作,在测试的假时钟里不会自己完成
  await tester.runAsync(() async {
    for (final el in find.byType(Image).evaluate()) {
      await precacheImage((el.widget as Image).image, el);
    }
  });
  await tester.pump();
  if (arrange != null) await arrange();
  // 「拉取中」的进度条是无限动画,有它在的页面不能等动画结束
  settle ? await tester.pumpAndSettle() : await tester.pump(const Duration(milliseconds: 400));
  await _capture(tester, name);
}

/// 自己按 2 倍截取:matchesGoldenFile 只按 1 倍输出,放到 Retina 屏上会糊。
Future<void> _capture(WidgetTester tester, String name) async {
  final boundary = tester.renderObject<RenderRepaintBoundary>(find.byKey(_boundary));
  await tester.runAsync(() async {
    final image = await boundary.toImage(pixelRatio: 2);
    final png = await image.toByteData(format: ImageByteFormat.png);
    // SCREENSHOT_OUT:另一个流程(宣传片)同时在渲染时,各写各的目录
    File('${Platform.environment['SCREENSHOT_OUT'] ?? 'tool/screenshots/out'}/$name.png')
      ..createSync(recursive: true)
      ..writeAsBytesSync(png!.buffer.asUint8List());
  });
}

void main() {
  setUpAll(() async {
    TestWidgetsFlutterBinding.ensureInitialized();
    appL10n = lookupAppLocalizations(_locale);
    AppIcons.pixel = _spec.pixelIcons;
    await _loadFonts();
  });

  testWidgets('history', (tester) async {
    await _shoot(tester, 'history', state: _demo(), settle: false, arrange: () async {
      // 悬停在一条记录上,展示「放入剪贴板」
      final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
      await mouse.addPointer(location: Offset.zero);
      addTearDown(mouse.removePointer);
      await mouse.moveTo(tester.getCenter(find.text(_quarterly)));
      await tester.pump();
    });
  });

  testWidgets('history-dark', (tester) async {
    await _shoot(tester, 'history-dark',
        state: _demo(), brightness: Brightness.dark, settle: false);
  });

  // 网站「功能」页:鼠标停在一条长文字上,弹出提示框显示全文
  testWidgets('hover-dark', (tester) async {
    await _shoot(tester, 'hover-dark', state: _demo(), brightness: Brightness.dark, settle: false,
        arrange: () async {
      final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
      await mouse.addPointer(location: Offset.zero);
      addTearDown(mouse.removePointer);
      await mouse.moveTo(tester.getCenter(find.text(_meeting)));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 600));
      await tester.pump();
      await tester.pump();
    });
  });

  testWidgets('favorites-dark', (tester) async {
    await _shoot(tester, 'favorites-dark', state: _demo(), section: Section.favorites,
        brightness: Brightness.dark);
  });

  // 快捷粘贴窗口:单独渲染,透明背景,自带圆角与阴影
  testWidgets('card-picker-dark', (tester) async {
    await _shootPicker(tester, 'card-picker-dark');
  });

  testWidgets('card-picker-search-dark', (tester) async {
    await _shootPicker(tester, 'card-picker-search-dark', query: 'docker');
  });

  // 窗口要够宽,传输列表才在雷达右边(默认的排法)
  for (final dark in [false, true]) {
    testWidgets(dark ? 'send-dark' : 'send', (tester) async {
      await _shoot(tester, dark ? 'send-dark' : 'send',
          state: _sendDemo(),
          section: Section.send,
          brightness: dark ? Brightness.dark : Brightness.light,
          window: const Size(1180, 720),
          settle: false, // 雷达的波纹一直在动
          arrange: () async => tester.pump(const Duration(milliseconds: 900)));
    });
  }

  for (final (name, size) in [
    ('send-small-short', const Size(760, 520)),
    ('send-small-tall', const Size(760, 760)),
  ]) {
    testWidgets(name, (tester) async {
      await _shoot(tester, name, state: _sendDemo(), section: Section.send, settle: false, window: size);
    });
  }

  testWidgets('send-dialog-dark', (tester) async {
    await _shoot(tester, 'send-dialog-dark', state: _sendDemo(), section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      showDialog<void>(
        context: tester.element(find.byType(Shell)),
        builder: (_) => const SendDialog(deviceId: 'air'),
      );
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 400));
    });
  });

  testWidgets('friends-send-dark', (tester) async {
    await _shoot(tester, 'friends-send-dark', state: _friendsDemo(), section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720));
  });

  testWidgets('friends-list-dark', (tester) async {
    await _shoot(tester, 'friends-list-dark', state: _friendsDemo(), section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      showFriendsDialog(tester.element(find.byType(Shell)));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 400));
    });
  });

  testWidgets('friend-page-dark', (tester) async {
    final state = _friendsDemo();
    await _shoot(tester, 'friend-page-dark', state: state, section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      showFriendHistory(tester.element(find.byType(Shell)), state.friends.first);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 400));
    });
  });

  testWidgets('friends-page-dark', (tester) async {
    await _shoot(tester, 'friends-page-dark', state: _friendsDemo(), section: Section.friends, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720));
  });

  testWidgets('receive-from-menu-dark', (tester) async {
    await _shoot(tester, 'receive-from-menu-dark', state: _friendsDemo(), section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      await tester.tap(find.byIcon(AppIcons.chevronDown).last);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 400));
    });
  });

  testWidgets('avatar-menu-dark', (tester) async {
    await _shoot(tester, 'avatar-menu-dark', state: _friendsDemo(), section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      await tester.tap(find.byType(SelfAvatarButton).first);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 400));
    });
  });

  testWidgets('add-friend-dark', (tester) async {
    await _shoot(tester, 'add-friend-dark', state: _friendsDemo(), section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      showAddFriendDialog(tester.element(find.byType(Shell)));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 400));
    });
  });

  testWidgets('friend-send-dialog-dark', (tester) async {
    final dir = Directory.systemTemp.createTempSync('pf-shot');
    final files = <String>[];
    for (final (name, size) in [
      (_t('合同扫描件.pdf', 'signed-contract.pdf', '契約書_署名済み.pdf', tw: '合約掃描檔.pdf', hk: '合約掃描本.pdf'), 2400000),
      (_t('附件', 'attachments', '添付ファイル', tw: '附件', hk: '附件'), 0),
    ]) {
      final path = '${dir.path}/$name';
      if (size == 0) {
        Directory(path).createSync();
        File('$path/a.bin').writeAsBytesSync(List.filled(1500000, 0));
      } else {
        File(path).writeAsBytesSync(List.filled(size, 0));
      }
      files.add(path);
    }
    final state = _friendsDemo();
    await _shoot(tester, 'friend-send-dialog-dark', state: state, section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      final f = state.friends.first;
      showDialog<void>(
        context: tester.element(find.byType(Shell)),
        builder: (_) => FriendSendDialog(target: SendTarget.friend(f), initial: files),
      );
      await tester.pump();
      await tester.runAsync(() => Future<void>.delayed(const Duration(milliseconds: 200)));
      await tester.pump(const Duration(milliseconds: 400));
      await tester.enterText(find.byType(TextField),
          _t('合同第 3 页要签字', 'Please sign page 3', '3 ページ目に署名してください', tw: '合約第 3 頁要簽名', hk: '合約第 3 頁要簽名'));
      await tester.pump();
    });
  });

  testWidgets('incoming-dark', (tester) async {
    final state = _friendsDemo();
    final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
    final xm = state.friends.first;
    state.debugSeed(
      status: state.status!,
      config: state.config!,
      self: state.self!,
      peers: state.peers,
      friends: state.friends,
      profile: state.profile,
      transfers: [
        Transfer(id: 'ask', direction: TransferDirection.TRANSFER_DIRECTION_INCOMING, deviceId: xm.id,
            deviceName: '${xm.displayName} · ${xm.deviceName}', status: 'asking', peerRole: 'friend',
            note: _t('合同第 3 页要签字', 'Please sign page 3', '3 ページ目に署名してください', tw: '合約第 3 頁要簽名', hk: '合約第 3 頁要簽名'),
            items: [
              ClipItem(name: _t('合同扫描件.pdf', 'signed-contract.pdf', '契約書_署名済み.pdf', tw: '合約掃描檔.pdf', hk: '合約掃描本.pdf')),
              ClipItem(name: 'notes.txt'),
              ClipItem(name: 'IMG_2042.HEIC'),
            ],
            totalBytes: Int64((18.4 * _mb).round()), createdAtUnix: Int64(now - 3)),
        ...state.transfers(TransferDirection.TRANSFER_DIRECTION_INCOMING),
      ],
    );
    await _shoot(tester, 'incoming-dark', state: state, section: Section.send, settle: false,
        brightness: Brightness.dark, window: const Size(1180, 720), arrange: () async {
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 400));
    });
  });

  testWidgets('devices', (tester) async {
    await _shoot(tester, 'devices', state: _demo(), section: Section.devices);
  });

  // 手机网页传输:设备页里的开关与已登记的手机,加上二维码窗口
  for (final dark in [false, true]) {
    testWidgets(dark ? 'web-share-dark' : 'web-share', (tester) async {
      // 在演示数据上加手机网页传输的状态:已配对的设备照常显示
      final state = _demo();
      state.debugSeed(
        status: state.status!,
        config: state.config!,
        self: state.self!,
        peers: state.peers,
        webShare: WebShareStatus(enabled: true, listening: true, port: 8790, addresses: [
          '192.168.1.20'
        ], sessions: [
          WebSession(id: 's1', name: 'iPhone', createdAtUnix: _ago(const Duration(days: 2)), lastSeenUnix: _ago(const Duration(minutes: 3))),
        ]),
      );
      await _shoot(tester, dark ? 'web-share-dark' : 'web-share',
          state: state,
          section: Section.devices,
          brightness: dark ? Brightness.dark : Brightness.light,
          window: const Size(960, 720), arrange: () async {
        showWebShareDialog(tester.element(find.byType(Shell)));
      });
    });
  }

  testWidgets('pairing', (tester) async {
    await _shoot(tester, 'pairing', state: _demo(), section: Section.devices, arrange: () async {
      showDialog<void>(
        context: tester.element(find.byType(Shell)),
        builder: (_) => const PairingDialog(),
      );
    });
  });

  for (final dark in [false, true]) {
    testWidgets(dark ? 'nearby-dark' : 'nearby', (tester) async {
      await _shoot(tester, dark ? 'nearby-dark' : 'nearby',
          state: _demo(),
          section: Section.devices,
          brightness: dark ? Brightness.dark : Brightness.light,
          settle: false, // 雷达的波纹一直在动
          arrange: () async {
        showDialog<void>(
          context: tester.element(find.byType(Shell)),
          builder: (_) => const NearbyDialog(),
        );
        await tester.pump();
        await tester.pump();
        await tester.pump(const Duration(milliseconds: 300));
        await tester.pump(const Duration(milliseconds: 900));
      });
    });
  }

  testWidgets('verify', (tester) async {
    await _shoot(tester, 'verify',
        section: Section.devices,
        state: _demo(
          pending: Device(
            id: 'new',
            name: 'MacBook Air',
            platform: 'darwin',
            publicKeyFingerprint: 'R8NF-2WTC-QL5J',
            pairingSession: 's1',
          ),
        ));
  });

  testWidgets('settings', (tester) async {
    await _shoot(tester, 'settings', state: _demo(), section: Section.settings);
  });

  // 「外观」:自带的两套主题加两套演示用的自定义主题
  AppState appearanceDemo() {
    final state = _demo()..requestPage('settings:appearance');
    final sunset = parseTheme({
      'id': 'sunset-pixel',
      'base': 'pixel',
      'modes': {
        'dark': {'accent': '#FF8A5B', 'background': '#16110F', 'sidebar': '#100C0A', 'surface': '#1E1714', 'selection': '#2A201B', 'border': '#3A2D27'},
        'light': {'accent': '#C2410C'},
      },
    });
    final mint = parseTheme({
      'id': 'mint',
      'base': 'default',
      'modes': {
        'dark': {'accent': '#34D399'},
        'light': {'accent': '#047857'},
      },
    });
    state.theme.debugSeed(themes: [
      for (final e in AppThemeSpec.builtins.entries) ThemeChoice(id: e.key, names: const {}, builtin: true, spec: e.value),
      ThemeChoice(id: 'sunset-pixel', names: {'zh-Hans': '落日像素', 'en': 'Sunset Pixel', 'ja': 'サンセット', 'zh-Hant-TW': '落日像素', 'zh-Hant-HK': '落日像素'}, builtin: false, spec: sunset),
      ThemeChoice(id: 'mint', names: {'zh-Hans': '薄荷', 'en': 'Mint', 'ja': 'ミント', 'zh-Hant-TW': '薄荷', 'zh-Hant-HK': '薄荷'}, builtin: false, spec: mint),
    ], id: _spec.id);
    return state;
  }

  testWidgets('appearance', (tester) async {
    await _shoot(tester, 'appearance', state: appearanceDemo(), section: Section.settings);
  });

  // 「背景图」一节在外观页最下面;配合 SCREENSHOT_BACKGROUND 看设置了背景图的样子
  for (final dark in [false, true]) {
    testWidgets(dark ? 'appearance-background-dark' : 'appearance-background', (tester) async {
      await _shoot(tester, dark ? 'appearance-background-dark' : 'appearance-background',
          state: appearanceDemo(),
          section: Section.settings,
          brightness: dark ? Brightness.dark : Brightness.light, arrange: () async {
        await tester.ensureVisible(find.text(_t('背景图', 'Background image', '背景画像', tw: '背景圖', hk: '背景圖')).first);
        await tester.pump();
      });
    });
  }

  testWidgets('appearance-dark', (tester) async {
    await _shoot(tester, 'appearance-dark', state: appearanceDemo(), section: Section.settings, brightness: Brightness.dark);
  });

  AppState officialDemo() {
    final state = _demo();
    final bjt = _now.toUtc().add(const Duration(hours: 8));
    final reset = DateTime.utc(bjt.year, bjt.month, bjt.day + 1).subtract(const Duration(hours: 8));
    state.debugSeed(
      status: state.status!.deepCopy()
        ..deviceId = 'demo-device-7k2m9q4v'
        ..officialUsage = OfficialUsage(
          available: true,
          uploadBytes: Int64(12340000),
          downloadBytes: Int64(7660000),
          limitBytes: Int64(100000000),
          remainingBytes: Int64(80000000),
          updatedAtUnix: Int64(_now.millisecondsSinceEpoch ~/ 1000),
          resetAtUnix: Int64(reset.millisecondsSinceEpoch ~/ 1000),
        ),
      config: state.config!.deepCopy()
        ..serviceMode = ServiceMode.SERVICE_MODE_OFFICIAL
        ..signalingUrl = '',
      self: state.self!,
      peers: state.peers,
      records: state.records,
    );
    return state;
  }

  testWidgets('official-settings-dark', (tester) async {
    // 服务器设置在「连接」这一类里
    await _shoot(tester, 'official-settings-dark', state: officialDemo()..requestPage('settings:connection'),
        section: Section.settings, brightness: Brightness.dark);
  });

  testWidgets('official-usage-dark', (tester) async {
    await _shoot(tester, 'official-usage-dark', state: officialDemo(),
        section: Section.official, brightness: Brightness.dark);
  });

  // 网站「在局域网里部署服务器」:服务器地址填局域网 IP,已连接
  testWidgets('settings-lan-dark', (tester) async {
    // 服务器设置在「连接」这一类里
    await _shoot(tester, 'settings-lan-dark',
        state: _demo(server: 'ws://192.168.1.20:8787/signal')..requestPage('settings:connection'),
        section: Section.settings, brightness: Brightness.dark);
  });

  // 网站「部署到公网服务器」:服务器地址填公网 IP(文档专用的示例地址段)
  testWidgets('settings-cloud-dark', (tester) async {
    // 服务器设置在「连接」这一类里
    await _shoot(tester, 'settings-cloud-dark',
        state: _demo(server: 'ws://203.0.113.10:8787/signal')..requestPage('settings:connection'),
        section: Section.settings, brightness: Brightness.dark);
  });

  // 网站「快速开始」:第一次打开、生成配对码,深色界面配深色网页
  testWidgets('welcome-dark', (tester) async {
    final state = _demo(link: LinkState.offline, service: ServiceState.notInstalled, empty: true);
    await _shoot(tester, 'welcome-dark', state: state, brightness: Brightness.dark);
  });

  testWidgets('pairing-dark', (tester) async {
    await _shoot(tester, 'pairing-dark', state: _demo(), section: Section.devices,
        brightness: Brightness.dark, arrange: () async {
      showDialog<void>(
        context: tester.element(find.byType(Shell)),
        builder: (_) => const PairingDialog(),
      );
    });
  });

  // 网站「同一网络」的跨网段一节与「Tailscale」:输入配对码,展开地址框,填上对方显示的地址
  for (final (name, address) in [('pairing-join-dark', '192.168.1.20'), ('pairing-join-ts-dark', '100.92.41.17')]) {
    testWidgets(name, (tester) async {
      await _shoot(tester, name, state: _demo(), section: Section.devices,
          brightness: Brightness.dark, arrange: () async {
        showDialog<void>(
          context: tester.element(find.byType(Shell)),
          builder: (_) => const PairingDialog(),
        );
        await tester.pumpAndSettle();
        await tester.tap(find.text(appL10n.enterCode));
        await tester.pumpAndSettle();
        await tester.enterText(find.byType(TextField).last, 'K7MP2X');
        await tester.tap(find.text(appL10n.differentSubnet));
        await tester.pumpAndSettle();
        await tester.enterText(find.byType(TextField).last, address);
      });
    });
  }

  // 网站上与文字并排展示的对话框:单独渲染,透明背景,保留对话框自己的圆角与阴影
  for (final dark in [false, true]) {
    testWidgets(dark ? 'card-verify-dark' : 'card-verify', (tester) async {
      tester.view.physicalSize = const Size(1120, 800);
      tester.view.devicePixelRatio = 2;
      addTearDown(tester.view.reset);
      debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
      debugDisableShadows = false;
      try {
        final brightness = dark ? Brightness.dark : Brightness.light;
        await tester.pumpWidget(RepaintBoundary(
          key: _boundary,
          // 对话框要同时显示本机的指纹,从 AppScope 里取
          child: AppScope(
            state: _demo(),
            child: MaterialApp(
              debugShowCheckedModeBanner: false,
              builder: _textScale,
              theme: _theme(brightness),
              locale: _locale,
              supportedLocales: supportedLocales,
              localizationsDelegates: AppLocalizations.localizationsDelegates,
              home: Scaffold(
                backgroundColor: Colors.transparent,
                body: Center(
                  child: PairingConfirmDialog(
                    peer: Device(name: 'MacBook Air', publicKeyFingerprint: 'R8NF-2WTC-QL5J'),
                  ),
                ),
              ),
            ),
          ),
        ));
        await tester.pumpAndSettle();
        await _capture(tester, dark ? 'card-verify-dark' : 'card-verify');
      } finally {
        debugDefaultTargetPlatformOverride = null;
        debugDisableShadows = true;
      }
    });
  }

  testWidgets('welcome', (tester) async {
    // 首次打开时还没有任何记录和设备
    final state = _demo(link: LinkState.offline, service: ServiceState.notInstalled, empty: true);
    await _shoot(tester, 'welcome', state: state);
  });
}

// ─────────────────────────── 快捷粘贴窗口 ───────────────────────────

const _pickerChannel = MethodChannel('pasteferry/picker');

/// 快捷窗口的演示数据,照 design/quick-paste-and-favorites.md 第 2.6 节的示意。不连后台服务。
class _DemoPicker extends PickerState {
  _DemoPicker() : super(host: PickerHost(_pickerChannel));

  @override
  Future<ListHistoryResponse> requestSearch(String query, PickerTab tab) async => ListHistoryResponse(searched: false);

  @override
  Future<ClipDetail> clipDetail(String clipId) async => ClipDetail();

  @override
  Future<GetImagePreviewResponse> imagePreview(String clipId, {int index = 0}) async => GetImagePreviewResponse();
}

ClipRecord _pick(String id, String text, Duration ago, {bool link = false, bool favorited = false}) =>
    _clip(id, ClipKind.CLIP_KIND_TEXT, preview: text, outgoing: true, size: text.length, ago: ago)
      ..isLink = link
      ..favorited = favorited;

List<ClipRecord> _pickerRecords() => [
      _pick('p1', _t('会议改到周四下午 3 点,地点不变', 'Meeting moved to Thursday 3 pm, same room',
          '打ち合わせは木曜 15 時に変更、場所は同じ',
          tw: '會議改到週四下午 3 點,地點不變', hk: '會議改到星期四下午 3 點,地點不變'), const Duration(seconds: 8)),
      _pick('p2', 'https://github.com/baiyuze/pasteferry', const Duration(minutes: 2), link: true),
      _clip('p3', ClipKind.CLIP_KIND_IMAGE,
          preview: _t('截图 14.32.png', 'Screenshot 14.32.png', 'スクリーンショット 14.32.png',
              tw: '截圖 14.32.png', hk: '截圖 14.32.png'),
          from: _t('Windows 台式机', 'Windows PC', 'Windows デスクトップ', tw: 'Windows 桌機', hk: 'Windows 桌面電腦'),
          size: 412 * 1024, ago: const Duration(minutes: 5),
          items: [ClipItem(name: _t('截图 14.32.png', 'Screenshot 14.32.png', 'スクリーンショット 14.32.png',
              tw: '截圖 14.32.png', hk: '截圖 14.32.png'), size: Int64(412 * 1024))])
        ..favorited = true,
      _clip('p4', ClipKind.CLIP_KIND_FILE, outgoing: true, size: (6.4 * _mb).round(), ago: const Duration(minutes: 52),
          items: [
            ClipItem(name: _t('合同扫描件.pdf', 'signed-contract.pdf', '契約書_署名済み.pdf', tw: '合約掃描檔.pdf', hk: '合約掃描本.pdf'),
                size: Int64((2.2 * _mb).round())),
            ClipItem(name: _t('报价单.xlsx', 'quote.xlsx', '見積書.xlsx', tw: '報價單.xlsx', hk: '報價單.xlsx'), size: Int64(96 * 1024)),
            ClipItem(name: _t('附件.zip', 'attachments.zip', '添付.zip', tw: '附件.zip', hk: '附件.zip'), size: Int64((4.1 * _mb).round())),
          ]),
      _pick('p5', 'docker compose up -d --build', const Duration(hours: 1, minutes: 5)),
      _clip('p6', ClipKind.CLIP_KIND_FILE, status: ClipStatus.CLIP_STATUS_REMOTE_ONLY,
          size: 812 * _mb, ago: const Duration(hours: 20),
          items: [ClipItem(name: 'PasteFerry.dmg', size: Int64(812 * _mb))]),
      _pick('p7', _t('快递单号:7712 3456 0098', 'Tracking number: 7712 3456 0098', '追跡番号:7712 3456 0098',
          tw: '物流單號:7712 3456 0098', hk: '速遞單號:7712 3456 0098'),
          const Duration(hours: 22), favorited: true),
      _pick('p8', "SELECT id, status FROM orders WHERE created_at > now() - interval '1 day'", const Duration(hours: 23)),
      _pick('p9', 'pnpm install', const Duration(hours: 26)),
      _pick('p10', 'git push origin main', const Duration(hours: 27)),
      _pick('p11', 'docker logs -f gateway', const Duration(days: 2)),
      _pick('p12', _t('把 docker 镜像源换成国内的', 'switch the docker registry mirror', 'docker のミラーを切り替える',
          tw: '把 docker 映像來源換成本地的', hk: '把 docker 鏡像源換成本地的'), const Duration(days: 2, hours: 3)),
    ];

Future<void> _shootPicker(WidgetTester tester, String name, {String query = ''}) async {
  // 窗口 560×460,四周留出阴影的位置
  const pad = 40.0;
  tester.view.physicalSize = const Size((560 + pad * 2) * 2, (460 + pad * 2) * 2);
  tester.view.devicePixelRatio = 2;
  tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
  addTearDown(tester.view.reset);
  addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
  TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(_pickerChannel, (c) async {
    return switch (c.method) {
      'pasteStatus' => {'available': true, 'reason': ''},
      _ => null,
    };
  });
  debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
  debugDisableShadows = false;
  try {
    final s = _DemoPicker()
      ..debugSeed(
        config: Config(language: _lang == 'zh' ? 'zh-Hans' : _locale.toLanguageTag()),
        records: _pickerRecords(),
        favorites: _favorites(),
      );
    final theme = _theme(Brightness.dark).copyWith(
      scaffoldBackgroundColor: Colors.transparent,
      canvasColor: Colors.transparent,
    );
    await tester.pumpWidget(RepaintBoundary(
      key: _boundary,
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        builder: _textScale,
        color: Colors.transparent,
        theme: theme,
        locale: _locale,
        supportedLocales: supportedLocales,
        localizationsDelegates: AppLocalizations.localizationsDelegates,
        home: Center(
          child: Container(
            width: 560,
            height: 460,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(_spec.shape.lg),
              boxShadow: const [
                BoxShadow(color: Color(0x73000000), blurRadius: 36, offset: Offset(0, 14)),
                BoxShadow(color: Color(0x40000000), blurRadius: 3),
              ],
            ),
            child: PickerView(state: s),
          ),
        ),
      ),
    ));
    await tester.pump();
    if (query.isNotEmpty) {
      await tester.enterText(find.byType(TextField), query);
      await tester.pump(const Duration(milliseconds: 300));
    }
    await tester.pumpAndSettle();
    await _capture(tester, name);
  } finally {
    debugDefaultTargetPlatformOverride = null;
    debugDisableShadows = true;
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(_pickerChannel, null);
  }
}