209 lines
6.8 KiB
Dart
209 lines
6.8 KiB
Dart
|
|
import 'dart:async';
|
||
|
|
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||
|
|
import 'package:flutter_test/flutter_test.dart';
|
||
|
|
import 'package:fpdart/fpdart.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/error/failure.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||
|
|
import 'package:maibu_satabot_v2/features/v2/device_list/data/models/robot_data_model.dart';
|
||
|
|
import 'package:maibu_satabot_v2/features/v2/device_list/domain/entities/bind_device_entities.dart';
|
||
|
|
import 'package:maibu_satabot_v2/features/v2/device_list/domain/repositories/bind_device_repository.dart';
|
||
|
|
import 'package:maibu_satabot_v2/features/v2/device_list/domain/usecases/bind_device_usecases.dart';
|
||
|
|
import 'package:maibu_satabot_v2/features/v2/device_list/presentation/pages/robot_ownership_page_v2.dart';
|
||
|
|
|
||
|
|
import 'support/region_test_localizations.dart';
|
||
|
|
|
||
|
|
class FakeOwnershipRepository implements BindDeviceRepository {
|
||
|
|
List<UserSimpleEntity> users = const [
|
||
|
|
UserSimpleEntity(id: 99, name: '其他人员'),
|
||
|
|
UserSimpleEntity(id: 7, name: '设备负责人'),
|
||
|
|
];
|
||
|
|
final requestedSites = <int>[];
|
||
|
|
final pending = <int, Completer<Either<Failure, List<UserSimpleEntity>>>>{};
|
||
|
|
bool fail = false;
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<Either<Failure, List<OrgEntity>>> getOrgList() async =>
|
||
|
|
const Right([]);
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<Either<Failure, List<UserSimpleEntity>>> getUsersBySiteId(
|
||
|
|
int siteId,
|
||
|
|
) async {
|
||
|
|
requestedSites.add(siteId);
|
||
|
|
return pending[siteId]?.future ??
|
||
|
|
(fail ? Left(Failure('网络错误')) : Right(users));
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||
|
|
}
|
||
|
|
|
||
|
|
RobotDataModel robot({int? userId = 7, String? userName, int? siteId}) =>
|
||
|
|
RobotDataModel(
|
||
|
|
name: 'MC700-test',
|
||
|
|
id: 'SN-1',
|
||
|
|
type: 'MC700',
|
||
|
|
status: '在线',
|
||
|
|
battery: 80,
|
||
|
|
task: '待机中',
|
||
|
|
userId: userId,
|
||
|
|
userName: userName,
|
||
|
|
siteId: siteId,
|
||
|
|
);
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||
|
|
setUpAll(RegionTestLocalizations.loadAssets);
|
||
|
|
late FakeOwnershipRepository repository;
|
||
|
|
late AppUserCubit user;
|
||
|
|
|
||
|
|
setUp(() {
|
||
|
|
repository = FakeOwnershipRepository();
|
||
|
|
user = AppUserCubit();
|
||
|
|
sl.registerSingleton(GetOrgListUseCase(repository));
|
||
|
|
sl.registerSingleton(GetUsersBySiteUseCase(repository));
|
||
|
|
});
|
||
|
|
|
||
|
|
tearDown(() async {
|
||
|
|
await user.close();
|
||
|
|
await sl.reset();
|
||
|
|
});
|
||
|
|
|
||
|
|
Future<void> mount(
|
||
|
|
WidgetTester tester,
|
||
|
|
RobotDataModel device, {
|
||
|
|
int siteId = 18,
|
||
|
|
}) async {
|
||
|
|
await tester.pumpWidget(
|
||
|
|
BlocProvider.value(
|
||
|
|
value: user,
|
||
|
|
child: MaterialApp(
|
||
|
|
locale: AppLocalizations.chinese,
|
||
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
||
|
|
localizationsDelegates: const [
|
||
|
|
RegionTestLocalizations(),
|
||
|
|
GlobalMaterialLocalizations.delegate,
|
||
|
|
GlobalWidgetsLocalizations.delegate,
|
||
|
|
GlobalCupertinoLocalizations.delegate,
|
||
|
|
],
|
||
|
|
home: RobotOwnershipPageV2(robot: device, siteId: siteId),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
await tester.pumpAndSettle();
|
||
|
|
}
|
||
|
|
|
||
|
|
testWidgets(
|
||
|
|
'matches the device owner ID using the existing site user query',
|
||
|
|
(tester) async {
|
||
|
|
await mount(tester, robot());
|
||
|
|
expect(repository.requestedSites, [18]);
|
||
|
|
expect(find.text('设备负责人'), findsOneWidget);
|
||
|
|
expect(find.textContaining('其他人员'), findsNothing);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
testWidgets('matches the legacy tenantId returned by the real device API', (
|
||
|
|
tester,
|
||
|
|
) async {
|
||
|
|
repository.users = const [UserSimpleEntity(id: 185, name: '旧接口负责人')];
|
||
|
|
final device = RobotDataModel.fromJson({
|
||
|
|
'deviceId': 288,
|
||
|
|
'deviceName': 'MC700-test',
|
||
|
|
'deviceAlias': '设备部',
|
||
|
|
'siteId': 23,
|
||
|
|
'tenantId': 185,
|
||
|
|
'tenantName': '',
|
||
|
|
'onlineStatus': 0,
|
||
|
|
});
|
||
|
|
await mount(tester, device);
|
||
|
|
expect(repository.requestedSites, [23]);
|
||
|
|
expect(find.text('旧接口负责人'), findsOneWidget);
|
||
|
|
});
|
||
|
|
|
||
|
|
testWidgets('negative legacy tenantId does not resolve an unrelated user', (
|
||
|
|
tester,
|
||
|
|
) async {
|
||
|
|
final device = RobotDataModel.fromJson({
|
||
|
|
'deviceId': 303,
|
||
|
|
'deviceName': 'MC700-test',
|
||
|
|
'siteId': 18,
|
||
|
|
'tenantId': -1,
|
||
|
|
'onlineStatus': 1,
|
||
|
|
});
|
||
|
|
await mount(tester, device);
|
||
|
|
expect(repository.requestedSites, isEmpty);
|
||
|
|
expect(find.text('设备负责人'), findsNothing);
|
||
|
|
expect(find.text('未知'), findsWidgets);
|
||
|
|
});
|
||
|
|
|
||
|
|
testWidgets('prefers the owner name already returned with the device', (
|
||
|
|
tester,
|
||
|
|
) async {
|
||
|
|
await mount(tester, robot(userName: '接口负责人'));
|
||
|
|
expect(repository.requestedSites, isEmpty);
|
||
|
|
expect(find.text('接口负责人'), findsOneWidget);
|
||
|
|
});
|
||
|
|
|
||
|
|
testWidgets('queries the device site instead of another selected site', (
|
||
|
|
tester,
|
||
|
|
) async {
|
||
|
|
await mount(tester, robot(siteId: 19));
|
||
|
|
expect(repository.requestedSites, [19]);
|
||
|
|
expect(find.text('设备负责人'), findsOneWidget);
|
||
|
|
});
|
||
|
|
|
||
|
|
testWidgets('does not guess the owner when the device has no user ID', (
|
||
|
|
tester,
|
||
|
|
) async {
|
||
|
|
await mount(tester, robot(userId: null));
|
||
|
|
expect(repository.requestedSites, isEmpty);
|
||
|
|
expect(find.text('设备负责人'), findsNothing);
|
||
|
|
expect(find.text('未知'), findsWidgets);
|
||
|
|
});
|
||
|
|
|
||
|
|
testWidgets(
|
||
|
|
'does not display another user when the owner is missing from the list',
|
||
|
|
(tester) async {
|
||
|
|
repository.users = const [UserSimpleEntity(id: 99, name: '其他人员')];
|
||
|
|
await mount(tester, robot());
|
||
|
|
expect(find.textContaining('其他人员'), findsNothing);
|
||
|
|
expect(find.text('未知'), findsWidgets);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
testWidgets('a failed user query preserves the unknown display', (
|
||
|
|
tester,
|
||
|
|
) async {
|
||
|
|
repository.fail = true;
|
||
|
|
await mount(tester, robot());
|
||
|
|
expect(find.text('设备负责人'), findsNothing);
|
||
|
|
expect(find.text('未知'), findsWidgets);
|
||
|
|
expect(tester.takeException(), isNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
testWidgets('an old site response cannot overwrite the current owner', (
|
||
|
|
tester,
|
||
|
|
) async {
|
||
|
|
repository.pending[18] = Completer();
|
||
|
|
repository.pending[19] = Completer();
|
||
|
|
await mount(tester, robot(siteId: 18));
|
||
|
|
await mount(tester, robot(userId: 8, siteId: 19));
|
||
|
|
repository.pending[19]!.complete(
|
||
|
|
const Right([UserSimpleEntity(id: 8, name: '新负责人')]),
|
||
|
|
);
|
||
|
|
await tester.pumpAndSettle();
|
||
|
|
repository.pending[18]!.complete(
|
||
|
|
const Right([UserSimpleEntity(id: 7, name: '旧负责人')]),
|
||
|
|
);
|
||
|
|
await tester.pumpAndSettle();
|
||
|
|
expect(find.text('新负责人'), findsOneWidget);
|
||
|
|
expect(find.textContaining('旧负责人'), findsNothing);
|
||
|
|
});
|
||
|
|
}
|