Files
kooltab/test/kooltap_api_client_test.dart
T
2026-10-08 04:12:36 +02:00

93 lines
2.4 KiB
Dart

import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:kooltap/models/remote_command.dart';
import 'package:kooltap/services/kooltap_api_client.dart';
KoolTapApiClient _clientReturning(http.Response response) {
return KoolTapApiClient(
client: MockClient((_) async => response),
baseUrl: 'https://api.test',
);
}
http.Response _error(int status, String code, String message) {
return http.Response(
jsonEncode({
'error': {'code': code, 'message': message},
}),
status,
);
}
void main() {
group('registerDevice', () {
test('surfaces the server reason for a rejected code', () async {
final client = _clientReturning(
_error(
401,
'invalid_registration_code',
'Invalid or expired registration code',
),
);
await expectLater(
client.registerDevice(
venueId: 'venue',
deviceName: 'Bar',
registrationCode: 'WRONG',
),
throwsA(
isA<ApiException>()
.having((e) => e, 'type', isNot(isA<UnauthorizedException>()))
.having((e) => e.code, 'code', 'invalid_registration_code')
.having(
(e) => e.message,
'message',
'Invalid or expired registration code',
),
),
);
});
test('falls back to the reason phrase for a non-JSON error', () async {
final client = _clientReturning(
http.Response('<html>', 502, reasonPhrase: 'Bad Gateway'),
);
await expectLater(
client.registerDevice(
venueId: 'venue',
deviceName: 'Bar',
registrationCode: 'CODE',
),
throwsA(
isA<ApiException>().having(
(e) => e.message,
'message',
'Bad Gateway',
),
),
);
});
});
group('sync', () {
test('treats a 401 as a revoked device token', () async {
final client = _clientReturning(
_error(401, 'invalid_token', 'Token revoked'),
);
await expectLater(
client.sync(
token: 'token',
request: const SyncRequest(deviceId: 'device', openTabs: []),
),
throwsA(isA<UnauthorizedException>()),
);
});
});
}