迁移 Flutter 端与训练脚本,模型/训练产物移出 git(遵循纯代码约定)
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:observer/auth/auth_screen.dart';
|
||||
import 'package:observer/auth/auth_view_model.dart';
|
||||
import 'package:observer/auth/session_store.dart';
|
||||
import 'package:observer/home/home_screen.dart';
|
||||
import 'package:observer/home/home_view_model.dart';
|
||||
import 'package:observer/payment/license_service.dart';
|
||||
import 'package:observer/payment/models.dart';
|
||||
import 'package:observer/payment/order_api.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
/// 测试环境无 secure storage 平台实现:mock 通道让读写立即返回空。
|
||||
void _mockSecureStorage() {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(
|
||||
const MethodChannel('plugins.it_nomads.com/flutter_secure_storage'),
|
||||
(call) async => null,
|
||||
);
|
||||
}
|
||||
|
||||
/// 内存版 SessionStore:不触平台通道(flutter_secure_storage 在测试环境不可用)
|
||||
class FakeSession extends SessionStore {
|
||||
String? token;
|
||||
String? phone;
|
||||
bool cleared = false;
|
||||
|
||||
@override
|
||||
Future<String?> readToken() async => token;
|
||||
@override
|
||||
Future<String?> readPhone() async => phone;
|
||||
@override
|
||||
Future<void> save(String phone, String token) async {
|
||||
this.phone = phone;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clear() async {
|
||||
token = null;
|
||||
phone = null;
|
||||
cleared = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// 内存版 OrderApi:登录/注册/授权全部本地返回,不触网络
|
||||
class FakeOrderApi extends OrderApi {
|
||||
final LicenseStatus licenseResult;
|
||||
FakeOrderApi(SessionStore s, {this.licenseResult = const LicenseStatus(active: false)})
|
||||
: super(sessionStore: s);
|
||||
|
||||
@override
|
||||
Future<void> register({required String phone, required String password}) async {}
|
||||
|
||||
@override
|
||||
Future<String> login({required String phone, required String password}) async =>
|
||||
'fake-token';
|
||||
|
||||
@override
|
||||
Future<LicenseStatus> fetchLicense() async => licenseResult;
|
||||
}
|
||||
|
||||
Widget _app(FakeSession session, FakeOrderApi api,
|
||||
{String initialRoute = '/login'}) {
|
||||
final authVm = AuthViewModel(orderApi: api, sessionStore: session);
|
||||
final homeVm = HomeViewModel(
|
||||
licenseService: LicenseService(orderApi: api, sessionStore: session));
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
Provider.value(value: session),
|
||||
ChangeNotifierProvider.value(value: authVm),
|
||||
ChangeNotifierProvider.value(value: homeVm),
|
||||
],
|
||||
child: MaterialApp(
|
||||
initialRoute: initialRoute,
|
||||
routes: {
|
||||
'/login': (_) => const AuthScreen(),
|
||||
'/home': (_) => const HomeScreen(),
|
||||
'/camera': (_) => const Scaffold(body: Text('camera-stub')),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _enterCredentials(WidgetTester tester, {required bool register}) async {
|
||||
if (register) {
|
||||
await tester.tap(find.text('没有账号?去注册'));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
await tester.enterText(find.widgetWithText(TextField, '手机号'), '13912345678');
|
||||
await tester.enterText(find.widgetWithText(TextField, '密码'), 'pass123456');
|
||||
}
|
||||
|
||||
void main() {
|
||||
setUp(_mockSecureStorage);
|
||||
|
||||
testWidgets('登录流程:输错密码提示错误,不跳转', (tester) async {
|
||||
final session = FakeSession();
|
||||
final api = FakeOrderApi(session);
|
||||
await tester.pumpWidget(_app(session, api));
|
||||
|
||||
await tester.tap(find.text('登录'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// 本地校验:空手机号提示
|
||||
expect(find.text('请输入手机号和至少 6 位密码'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('注册并登录成功 → 跳转主界面展示未充值', (tester) async {
|
||||
final session = FakeSession();
|
||||
final api = FakeOrderApi(session);
|
||||
await tester.pumpWidget(_app(session, api));
|
||||
|
||||
await _enterCredentials(tester, register: true);
|
||||
await tester.tap(find.text('注册并登录'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(HomeScreen), findsOneWidget);
|
||||
expect(session.token, 'fake-token');
|
||||
expect(session.phone, '13912345678');
|
||||
expect(find.textContaining('未充值'), findsOneWidget);
|
||||
expect(find.text('搜索'), findsOneWidget);
|
||||
expect(find.text('充值'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('搜索按钮强制服务端校验:未授权弹出拦截弹窗', (tester) async {
|
||||
final session = FakeSession()..token = 'fake-token';
|
||||
final api = FakeOrderApi(session); // active: false
|
||||
await tester.pumpWidget(_app(session, api, initialRoute: '/home'));
|
||||
|
||||
await tester.tap(find.text('搜索'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('授权不可用'), findsOneWidget);
|
||||
expect(find.text('去充值'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('搜索按钮校验通过 → 进入相机页', (tester) async {
|
||||
final session = FakeSession()..token = 'fake-token';
|
||||
final api = FakeOrderApi(
|
||||
session,
|
||||
licenseResult: LicenseStatus(
|
||||
active: true,
|
||||
expiresAt: DateTime.now().add(const Duration(days: 1))),
|
||||
);
|
||||
await tester.pumpWidget(_app(session, api, initialRoute: '/home'));
|
||||
|
||||
await tester.tap(find.text('搜索'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('camera-stub'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:observer/detection/coordinate_mapper.dart';
|
||||
|
||||
void main() {
|
||||
test('rotation90_center_mapsToViewCenter', () {
|
||||
final rect = CoordinateMapper.mapToView(
|
||||
0.4, 0.4, 0.6, 0.6,
|
||||
90, 1280, 720, 1080, 1920,
|
||||
);
|
||||
expect(rect.centerX, closeTo(540, 1));
|
||||
expect(rect.centerY, closeTo(960, 1));
|
||||
});
|
||||
|
||||
test('rotation90_corner_appliesFitCenterOffset', () {
|
||||
final rect = CoordinateMapper.mapToView(
|
||||
0, 0, 0.5, 0.5,
|
||||
90, 1280, 720,
|
||||
1080, 2400,
|
||||
);
|
||||
expect(rect.left, closeTo(540, 1));
|
||||
expect(rect.top, closeTo(240, 1));
|
||||
expect(rect.right, closeTo(1080, 1));
|
||||
expect(rect.bottom, closeTo(1200, 1));
|
||||
});
|
||||
|
||||
test('rotation0_keepsCoordinates', () {
|
||||
final rect = CoordinateMapper.mapToView(
|
||||
0.2, 0.3, 0.5, 0.7,
|
||||
0, 1080, 1920,
|
||||
1080, 1920,
|
||||
);
|
||||
expect(rect.left, closeTo(216, 1));
|
||||
expect(rect.top, closeTo(576, 1));
|
||||
expect(rect.right, closeTo(540, 1));
|
||||
expect(rect.bottom, closeTo(1344, 1));
|
||||
});
|
||||
|
||||
test('rotation180_flipsBothAxes', () {
|
||||
final rect = CoordinateMapper.mapToView(
|
||||
0.2, 0.2, 0.4, 0.4,
|
||||
180, 1080, 1920,
|
||||
1080, 1920,
|
||||
);
|
||||
expect(rect.left, closeTo(648, 1));
|
||||
expect(rect.top, closeTo(1152, 1));
|
||||
expect(rect.right, closeTo(864, 1));
|
||||
expect(rect.bottom, closeTo(1536, 1));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:observer/detection/detection_result.dart';
|
||||
import 'package:observer/detection/motion_aggregator.dart';
|
||||
|
||||
void main() {
|
||||
// 96x64 图,8x8 块 → 每块 12x8 像素
|
||||
|
||||
test('noMotion_returnsEmpty', () {
|
||||
final diff = List<int>.filled(96 * 64, 0);
|
||||
expect(MotionAggregator.aggregate(diff, 96, 64), isEmpty);
|
||||
});
|
||||
|
||||
test('singleBlockMotion_detectsRegion', () {
|
||||
final diff = List<int>.filled(96 * 64, 0);
|
||||
for (var y = 24; y < 32; y++) {
|
||||
for (var x = 24; x < 36; x++) {
|
||||
diff[y * 96 + x] = 1;
|
||||
}
|
||||
}
|
||||
final regions = MotionAggregator.aggregate(diff, 96, 64);
|
||||
expect(regions.length, 1);
|
||||
final r = regions[0];
|
||||
expect(r.left, lessThanOrEqualTo(24 / 96));
|
||||
expect(r.right, greaterThanOrEqualTo(36 / 96));
|
||||
expect(r.top, lessThanOrEqualTo(24 / 64));
|
||||
expect(r.bottom, greaterThanOrEqualTo(32 / 64));
|
||||
});
|
||||
|
||||
test('twoSeparateMotions_detectsTwoRegions', () {
|
||||
final diff = List<int>.filled(96 * 64, 0);
|
||||
for (var y = 0; y < 8; y++) {
|
||||
for (var x = 0; x < 12; x++) {
|
||||
diff[y * 96 + x] = 1;
|
||||
}
|
||||
}
|
||||
for (var y = 48; y < 64; y++) {
|
||||
for (var x = 72; x < 96; x++) {
|
||||
diff[y * 96 + x] = 1;
|
||||
}
|
||||
}
|
||||
expect(MotionAggregator.aggregate(diff, 96, 64).length, 2);
|
||||
});
|
||||
|
||||
test('globalNoise_filteredOut', () {
|
||||
final diff = List<int>.filled(96 * 64, 0);
|
||||
final rnd = math.Random(42);
|
||||
for (var i = 0; i < diff.length; i++) {
|
||||
if (rnd.nextDouble() < 0.1) diff[i] = 1;
|
||||
}
|
||||
expect(MotionAggregator.aggregate(diff, 96, 64), isEmpty);
|
||||
});
|
||||
|
||||
test('centerInRegion_matches', () {
|
||||
final box = DetectionResult(
|
||||
label: 'hare',
|
||||
score: 0.30,
|
||||
left: 0.2,
|
||||
top: 0.3,
|
||||
right: 0.4,
|
||||
bottom: 0.5);
|
||||
expect(MotionAggregator.centerInRegion(box, MotionRegion(0.1, 0.2, 0.5, 0.6)),
|
||||
isTrue);
|
||||
expect(MotionAggregator.centerInRegion(box, MotionRegion(0.6, 0.7, 0.9, 0.9)),
|
||||
isFalse);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:observer/detection/detection_result.dart';
|
||||
import 'package:observer/detection/nms.dart';
|
||||
|
||||
DetectionResult box(double l, double t, double r, double b, double score,
|
||||
{String label = 'x'}) =>
|
||||
DetectionResult(
|
||||
label: label, score: score, left: l, top: t, right: r, bottom: b);
|
||||
|
||||
void main() {
|
||||
test('overlappingBoxes_keepHighestScore', () {
|
||||
final a = box(0.1, 0.1, 0.5, 0.5, 0.8);
|
||||
final b = box(0.12, 0.12, 0.52, 0.52, 0.6);
|
||||
final result = nms([a, b], 0.45);
|
||||
expect(result.length, 1);
|
||||
expect(result[0].score, closeTo(0.8, 1e-6));
|
||||
});
|
||||
|
||||
test('separateBoxes_bothKept', () {
|
||||
final a = box(0.1, 0.1, 0.3, 0.3, 0.8);
|
||||
final b = box(0.7, 0.7, 0.9, 0.9, 0.6);
|
||||
expect(nms([a, b], 0.45).length, 2);
|
||||
});
|
||||
|
||||
test('lowScoreBox_suppressedByHigherScore', () {
|
||||
final a = box(0.1, 0.1, 0.5, 0.5, 0.9);
|
||||
final b = box(0.1, 0.1, 0.5, 0.5, 0.5);
|
||||
final result = nms([b, a], 0.45);
|
||||
expect(result.length, 1);
|
||||
expect(result[0].score, greaterThan(0.5));
|
||||
});
|
||||
|
||||
test('iou_nonOverlapping_isZero', () {
|
||||
final a = box(0.0, 0.0, 0.2, 0.2, 1);
|
||||
final b = box(0.8, 0.8, 1.0, 1.0, 1);
|
||||
expect(iou(a, b), closeTo(0, 1e-6));
|
||||
});
|
||||
|
||||
test('iou_identicalBoxes_isOne', () {
|
||||
final a = box(0.1, 0.1, 0.5, 0.5, 1);
|
||||
expect(iou(a, a), closeTo(1, 1e-6));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:observer/payment/models.dart';
|
||||
|
||||
void main() {
|
||||
test('LicenseStatus.isActive 未过期为 true', () {
|
||||
final license = LicenseStatus(
|
||||
active: true,
|
||||
expiresAt: DateTime.now().add(const Duration(days: 1)),
|
||||
);
|
||||
expect(license.isActive, isTrue);
|
||||
});
|
||||
|
||||
test('LicenseStatus.isActive 已过期为 false', () {
|
||||
final license = LicenseStatus(
|
||||
active: true,
|
||||
expiresAt: DateTime.now().subtract(const Duration(days: 1)),
|
||||
);
|
||||
expect(license.isActive, isFalse);
|
||||
});
|
||||
|
||||
test('LicenseStatus.isActive active=false 恒为 false', () {
|
||||
final license = LicenseStatus(
|
||||
active: false,
|
||||
expiresAt: DateTime.now().add(const Duration(days: 1)),
|
||||
);
|
||||
expect(license.isActive, isFalse);
|
||||
});
|
||||
|
||||
test('LicenseStatus.fromJson 解析', () {
|
||||
final license = LicenseStatus.fromJson({
|
||||
'active': true,
|
||||
'expiresAt': '2026-08-29T23:59:59+08:00',
|
||||
});
|
||||
expect(license.active, isTrue);
|
||||
expect(license.expiresAt, isNotNull);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user