Files
flutterApp/test/device_emergency_stop_test.dart

142 lines
4.7 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:maibu_satabot_v2/core/env/env_config.dart';
import 'package:maibu_satabot_v2/core/error/failure.dart';
import 'package:maibu_satabot_v2/features/device_emergency_stop/data/datasources/emergency_stop_remote_datasource.dart';
import 'package:maibu_satabot_v2/features/device_emergency_stop/data/repositories/device_emergency_stop_repository_impl.dart';
import 'package:maibu_satabot_v2/features/device_emergency_stop/domain/entities/emergency_stop_target.dart';
import 'package:maibu_satabot_v2/features/device_emergency_stop/domain/usecases/request_device_emergency_stop_usecase.dart';
void main() {
late Dio dio;
late RequestDeviceEmergencyStopUseCase request;
late List<RequestOptions> requests;
dynamic body;
int status = 200;
DioExceptionType? errorType;
setUp(() {
requests = [];
body = {'code': 200};
status = 200;
errorType = null;
dio = Dio();
// Resolve locally: these tests never contact a backend or a machine.
dio.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) {
requests.add(options);
if (errorType != null) {
handler.reject(
DioException(requestOptions: options, type: errorType!),
);
} else {
handler.resolve(
Response<dynamic>(
requestOptions: options,
statusCode: status,
data: body,
),
);
}
},
),
);
request = RequestDeviceEmergencyStopUseCase(
DeviceEmergencyStopRepositoryImpl(EmergencyStopRemoteDatasource(dio)),
);
});
tearDown(() => dio.close(force: true));
for (final target in EmergencyStopTarget.values) {
test(
'${target.name} uses GET query parameters in selected environment',
() async {
expect(requests, isEmpty);
final result = await request(' DEVICE-1 ', target);
expect(result.isRight(), isTrue);
final options = requests.single;
expect(options.method, 'GET');
expect(options.data, isNull);
expect(options.queryParameters, {
'deviceId': 'DEVICE-1',
'eStop': target == EmergencyStopTarget.engage ? 1 : 0,
});
expect(options.uri.path, '/iot/host/eStopChange');
expect(options.uri.origin, EnvConfig.baseUrl);
const environment = String.fromEnvironment('ENV', defaultValue: 'test');
expect(options.uri.port, environment == 'prod' ? 8081 : 59003);
expect(options.headers['Cache-Control'], 'no-store');
// API request completion has no device-state result attached to it.
},
);
}
test('empty device ID fails without sending a request', () async {
final result = await request(' ', EmergencyStopTarget.engage);
expect(result.isLeft(), isTrue);
expect(requests, isEmpty);
});
test(
'string business success code follows existing host convention',
() async {
body = {'code': '200', 'data': true};
expect(
(await request('DEVICE-1', EmergencyStopTarget.engage)).isRight(),
isTrue,
);
},
);
final invalidResponses = <String, dynamic>{
'empty response': null,
'text response': 'ok',
'missing business code': {'data': true},
'business rejection': {'code': 500},
'explicit failure': {'code': 200, 'data': false},
'top-level rejection': {'code': 200, 'accepted': false},
'device rejection': {
'code': 200,
'data': {'accepted': false},
},
};
for (final response in invalidResponses.entries) {
test('${response.key} is not reported as accepted', () async {
body = response.value;
final result = await request('DEVICE-1', EmergencyStopTarget.release);
expect(result.isLeft(), isTrue);
result.fold(
(failure) => expect(failure, isA<ServerFailure>()),
(_) => fail('Unexpected success'),
);
});
}
test('HTTP failure is not masked by a successful business code', () async {
status = 500;
expect(
(await request('DEVICE-1', EmergencyStopTarget.engage)).isLeft(),
isTrue,
);
});
for (final type in [
DioExceptionType.connectionTimeout,
DioExceptionType.sendTimeout,
DioExceptionType.receiveTimeout,
DioExceptionType.connectionError,
]) {
test('${type.name} remains unconfirmed and is not retried', () async {
errorType = type;
final result = await request('DEVICE-1', EmergencyStopTarget.release);
result.fold(
(failure) => expect(failure, isA<NetworkFailure>()),
(_) => fail('Unexpected success'),
);
expect(requests, hasLength(1));
});
}
}