TermRemoteCtl/apps/mobile_app/test/features/pairing/pairing_controller_test.dart

58 lines
1.6 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:term_remote_ctl/core/network/agent_api_client.dart';
import 'package:term_remote_ctl/features/pairing/pairing_controller.dart';
void main() {
test('redeemCode updates state on success', () async {
final client = _FakeAgentApiClient();
final controller = PairingController(client);
final future = controller.redeemCode('123456', 'tablet');
expect(controller.state, const AsyncLoading<void>());
await future;
expect(client.redeemCalls, 1);
expect(client.lastCode, '123456');
expect(client.lastDeviceName, 'tablet');
expect(controller.state, const AsyncData<void>(null));
});
test('redeemCode exposes api failures as AsyncError', () async {
final client = _FakeAgentApiClient(shouldThrow: true);
final controller = PairingController(client);
final future = controller.redeemCode('123456', 'tablet');
expect(controller.state, const AsyncLoading<void>());
await future;
expect(controller.state, isA<AsyncError<void>>());
});
}
class _FakeAgentApiClient extends AgentApiClient {
_FakeAgentApiClient({this.shouldThrow = false}) : super(Uri.parse('https://host:9443'));
final bool shouldThrow;
int redeemCalls = 0;
String? lastCode;
String? lastDeviceName;
@override
Future<void> redeemPairingCode({
required String code,
required String deviceName,
}) async {
redeemCalls += 1;
lastCode = code;
lastDeviceName = deviceName;
if (shouldThrow) {
throw StateError('boom');
}
}
}