import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/settings/health/health_controller.dart'; class _Gateway implements ApiGateway { final calls = []; JsonMap reply; _Gateway(this.reply); @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async { calls.add(type); expect(fields, isEmpty); return reply; } } void main() { test( 'reads the server_stats envelope without inventing missing values', () async { final gateway = _Gateway({ 'server_stats': { 'host': {'hostname': 'fixture-host'}, 'cpu': {'usage_percent': 42.5}, }, }); final controller = HealthController(gateway); await controller.load(); expect(gateway.calls, ['server_stats']); expect(controller.stats!['host']['hostname'], 'fixture-host'); expect(controller.error, isNull); controller.dispose(); }, ); test('malformed or unsupported replies are actionable errors', () async { final controller = HealthController( _Gateway({'error': 'Unknown command: server_stats'}), ); await controller.load(); expect(controller.stats, isNull); expect(controller.error, contains('Unknown command')); controller.dispose(); }); test('a section error remains available for inline rendering', () async { final controller = HealthController( _Gateway({ 'server_stats': { 'gpu': {'error': 'GPU metrics unavailable'}, }, }), ); await controller.load(); expect(controller.stats!['gpu']['error'], 'GPU metrics unavailable'); controller.dispose(); }); test('does not overlap requests or notify after disposal', () async { final completer = Completer(); final gateway = _PendingGateway(completer.future); final controller = HealthController(gateway); var notifications = 0; controller.addListener(() => notifications++); final first = controller.load(); await controller.load(); expect(gateway.calls, 1); controller.dispose(); completer.complete({'server_stats': {}}); await first; expect(notifications, 1); }); } class _PendingGateway implements ApiGateway { _PendingGateway(this.result); final Future result; int calls = 0; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) { calls++; return result; } }