Add server health page with native controls and bounded refresh
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
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 = <String>[];
|
||||
JsonMap reply;
|
||||
_Gateway(this.reply);
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
@override
|
||||
Future<JsonMap> 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<JsonMap>();
|
||||
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<JsonMap> result;
|
||||
int calls = 0;
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
calls++;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/settings/health/health_models.dart';
|
||||
import 'package:pipelines_app/features/settings/health/health_widgets.dart';
|
||||
|
||||
void main() {
|
||||
test('missing usage remains unknown', () {
|
||||
expect(healthUsageRatio(null, 100), isNull);
|
||||
expect(healthUsageRatio(5, null), isNull);
|
||||
expect(healthUsageRatio(5, 0), isNull);
|
||||
expect(healthUsageRatio(150, 100), 1);
|
||||
});
|
||||
testWidgets('meter applies consistent high and critical thresholds', (
|
||||
tester,
|
||||
) async {
|
||||
final scheme = ColorScheme.fromSeed(seedColor: Colors.blue);
|
||||
for (final (value, color) in [
|
||||
(0.799, scheme.primary),
|
||||
(0.8, scheme.tertiary),
|
||||
(0.949, scheme.tertiary),
|
||||
(0.95, scheme.error),
|
||||
]) {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(colorScheme: scheme),
|
||||
home: Scaffold(
|
||||
body: HealthMeter(label: 'CPU', value: value),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<LinearProgressIndicator>(
|
||||
find.byType(LinearProgressIndicator),
|
||||
)
|
||||
.color,
|
||||
color,
|
||||
);
|
||||
}
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(body: HealthMeter(label: 'CPU', value: null)),
|
||||
),
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<LinearProgressIndicator>(find.byType(LinearProgressIndicator))
|
||||
.value,
|
||||
0,
|
||||
);
|
||||
expect(find.text('CPU —'), findsOneWidget);
|
||||
});
|
||||
testWidgets('meter label fits narrow large text', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: MediaQuery(
|
||||
data: const MediaQueryData(textScaler: TextScaler.linear(2)),
|
||||
child: const SizedBox(
|
||||
width: 200,
|
||||
child: HealthMeter(label: 'Utilization', value: 0.97),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user