Files
pipelines/app/test/features/settings/health/health_screen_test.dart
T

100 lines
3.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/settings/health/health_screen.dart';
class _Gateway implements ApiGateway {
int calls = 0;
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls++;
return {
'server_stats': {
'sampled_at': DateTime.now().toUtc().toIso8601String(),
'host': {
'hostname': 'test-host',
'platform': 'Linux',
'platform_version': '1',
},
'cpu': {'usage_percent': 90, 'logical_cores': 4},
'memory': {'used_percent': 20, 'total_bytes': 100, 'used_bytes': 20},
'storage': [
{'mount': '/secure', 'error': 'Storage metrics unavailable'},
],
'gpu': {'available': false, 'reason': 'No GPU'},
'data': {},
'process': {},
},
};
}
}
Future<void> _mount(WidgetTester tester, _Gateway gateway) async {
tester.view.physicalSize = const Size(800, 1200);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(MaterialApp(home: HealthScreen(gateway: gateway)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
}
void main() {
testWidgets('renders honest health sections and threshold meter', (
tester,
) async {
final gateway = _Gateway();
await _mount(tester, gateway);
expect(find.text('test-host'), findsOneWidget);
expect(find.text('Storage metrics unavailable'), findsOneWidget);
expect(find.text('Overall 90.0%'), findsOneWidget);
final meter = tester.widget<LinearProgressIndicator>(
find.byType(LinearProgressIndicator).first,
);
final context = tester.element(find.byType(HealthScreen));
expect(meter.color, Theme.of(context).colorScheme.tertiary);
expect(tester.takeException(), isNull);
});
testWidgets('auto-refreshes only while the page is visible', (tester) async {
final gateway = _Gateway();
await _mount(tester, gateway);
expect(gateway.calls, 1);
await tester.pump(const Duration(seconds: 5));
expect(gateway.calls, 2);
final context = tester.element(find.byType(HealthScreen));
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (_) => const Scaffold(body: Text('Covered')),
),
);
await tester.pump(const Duration(milliseconds: 500));
final coveredCalls = gateway.calls;
await tester.pump(const Duration(seconds: 6));
expect(gateway.calls, coveredCalls);
});
testWidgets('stops refreshing after disposal and while backgrounded', (
tester,
) async {
final gateway = _Gateway();
await _mount(tester, gateway);
final observer =
tester.state(find.byType(HealthScreen)) as WidgetsBindingObserver;
observer.didChangeAppLifecycleState(AppLifecycleState.paused);
await tester.pump(const Duration(seconds: 12));
expect(gateway.calls, 1);
observer.didChangeAppLifecycleState(AppLifecycleState.resumed);
await tester.pump();
await tester.pump(const Duration(seconds: 5));
expect(gateway.calls, 2);
await tester.pumpWidget(const SizedBox());
await tester.pump(const Duration(seconds: 12));
expect(gateway.calls, 2);
});
}