Files
oott/frontend/test/unit/api_error_test.dart
T
rzuastiandClaude Opus 4.8 a51b9c06dc Display unknown values as a dash and centralise repeated literals
Backend: notifications now show a plain "-" for an absent name, vendor, or
device type (was empty string / "(unknown)" / "Unknown"), via a single
UNKNOWN_PLACEHOLDER constant.

Frontend:
- Empty/unknown values render as an em dash everywhere, centralised in a new
  Placeholders.emptyValue constant (replaces inline '—' and '(unknown)').
- Route paths moved to a new Routes class, used by the router and every
  navigation call site.
- Device event type modelled as a DeviceEventType enum mirroring the backend
  (NewDevice/DeviceSeen) instead of bare string comparisons.
- Hardcoded EdgeInsets/SizedBox spacing replaced with existing Insets tokens.

Tests and formatting updated; all backend and frontend tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 16:07:05 -04:00

87 lines
2.5 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:frontend/utils/api/api_error.dart';
void main() {
final options = RequestOptions(path: '/x');
DioException dio(DioExceptionType type, {int? status}) => DioException(
requestOptions: options,
type: type,
response: status == null
? null
: Response(requestOptions: options, statusCode: status),
);
test('maps shape errors', () {
expect(
dioErrorToUserMessage(TypeError()),
'Unexpected response shape from backend.',
);
expect(
dioErrorToUserMessage(const FormatException('bad')),
'Unexpected response shape from backend.',
);
});
test('maps non-Dio errors to a generic message', () {
expect(dioErrorToUserMessage(Exception('boom')), 'Unexpected error.');
});
test('maps timeouts', () {
expect(
dioErrorToUserMessage(dio(DioExceptionType.connectionTimeout)),
'Backend did not respond in time.',
);
expect(
dioErrorToUserMessage(dio(DioExceptionType.receiveTimeout)),
'Backend did not respond in time.',
);
});
test('maps connection and certificate errors', () {
expect(
dioErrorToUserMessage(dio(DioExceptionType.connectionError)),
'Cannot reach backend. Check the base URL and your network.',
);
expect(
dioErrorToUserMessage(dio(DioExceptionType.badCertificate)),
'Backend TLS certificate could not be verified.',
);
});
test('maps bad responses by status code', () {
expect(
dioErrorToUserMessage(dio(DioExceptionType.badResponse, status: 401)),
'Authentication failed. Check your API key in Settings.',
);
expect(
dioErrorToUserMessage(dio(DioExceptionType.badResponse, status: 403)),
'Authentication failed. Check your API key in Settings.',
);
expect(
dioErrorToUserMessage(dio(DioExceptionType.badResponse, status: 404)),
'Not found.',
);
expect(
dioErrorToUserMessage(dio(DioExceptionType.badResponse, status: 503)),
'Backend error (status 503). Please try again.',
);
expect(
dioErrorToUserMessage(dio(DioExceptionType.badResponse, status: 418)),
'Unexpected response from backend (status 418).',
);
});
test('maps cancellation and unknown errors', () {
expect(
dioErrorToUserMessage(dio(DioExceptionType.cancel)),
'Request canceled.',
);
expect(
dioErrorToUserMessage(dio(DioExceptionType.unknown)),
'Unexpected error contacting backend.',
);
});
}