// Author: fruit.com.cn import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:fruitshorts/app.dart'; import 'package:fruitshorts/core/config/app_config.dart'; import 'package:fruitshorts/core/data/remote/api_client.dart'; import 'package:fruitshorts/core/data/remote/catalog_api.dart'; import 'package:integration_test/integration_test.dart'; /// 设备侧集成测试:**在手机进程里**真实请求开发机上的 app-api。 /// /// 跑法(红线第 10 条要求的"设备侧取证"): /// ``` /// IP=$(ipconfig getifaddr en0) /// flutter test integration_test -d --dart-define=API_BASE_URL=http://$IP:8081 /// ``` /// 断言失败时会把实际使用的地址一起打出来,便于区分"地址不对"还是"设备连不上"。 void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); final baseUrl = AppConfig.apiBaseUrl; testWidgets('should_fetch_home_feed_and_drama_detail_when_device_reaches_backend', ( tester, ) async { debugPrint('[device] API_BASE_URL=$baseUrl'); final client = ApiClient(baseUrl: baseUrl); addTearDown(client.close); final api = CatalogApi(client); final feed = await api.homeFeed(); expect( feed.dramas, isNotEmpty, reason: '设备侧拿不到首页列表(API_BASE_URL=$baseUrl)', ); debugPrint('[device] 首页分区=${feed.sections.length} 剧目=${feed.dramas.length}'); final card = feed.dramas.first; expect(card.id, isNotEmpty, reason: '设备侧首页卡片 id 为空'); final detail = await api.dramaDetail(card.id); expect(detail.id, card.id, reason: '详情返回的 id 与卡片不一致'); expect( detail.episodes, isNotEmpty, reason: '设备侧拿到的详情没有剧集(dramaId=${card.id})', ); debugPrint('[device] 详情 id=${detail.id} 剧集=${detail.episodes.length}'); }); testWidgets('should_render_home_drama_list_when_app_boots_on_device', ( tester, ) async { // 用真 App(真网络、真目录控制器):首页底部标签栏出现即代表"剧目列表加载成功", // 也就是用户在真机上遇到的那个「剧目列表加载失败」已经消失。 await tester.pumpWidget(const LumaDramaApp()); var loaded = false; for (var attempt = 0; attempt < 60 && !loaded; attempt++) { await tester.pump(const Duration(milliseconds: 500)); loaded = find.byKey(const Key('home-tabs-bar')).evaluate().isNotEmpty; } expect( loaded, isTrue, reason: '首页 30 秒内没渲染出剧目列表(API_BASE_URL=$baseUrl)', ); debugPrint('[device] 首页剧目列表已渲染:API_BASE_URL=$baseUrl'); }); }