Verify storage controls and provide isolated browser fixture

This commit is contained in:
2026-11-06 21:28:37 +00:00
parent c9e1124baa
commit 4f2c981a13
4 changed files with 398 additions and 0 deletions
@@ -0,0 +1,133 @@
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/settings/storage/storage_controller.dart';
import 'package:pipelines_app/features/settings/storage/storage_models.dart';
import '../../../../tool/storage_probe_fixture.dart';
class _Pending extends StorageProbeGateway {
final plan = Completer<JsonMap>();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) =>
type == 'preview_storage_cleanup'
? plan.future
: super.call(type, fields);
}
class _Malformed extends StorageProbeGateway {
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async => {
'storage': {},
};
}
void main() {
const policy = StoragePolicy(
maxAgeDays: 30,
maxBytes: 10737418240,
includeWorktrees: true,
);
test('missing capability is explicit, not a fabricated zero total', () async {
final gateway = StorageProbeGateway()..unsupported = true;
final controller = StorageController(gateway);
await controller.load();
expect(controller.overview, isNull);
expect(controller.error, contains('Unknown command'));
expect(controller.busy, isFalse);
controller.dispose();
});
test('malformed overview cannot display fabricated zero usage', () async {
final controller = StorageController(_Malformed());
await controller.load();
expect(controller.overview, isNull);
expect(controller.error, isNotNull);
controller.dispose();
});
test('server policy save preserves scope and explicit limits', () async {
final gateway = StorageProbeGateway();
final controller = StorageController(gateway);
await controller.save(policy);
expect(gateway.calls.single.fields['storage_policy'], policy.toJson());
expect(controller.overview!['policy'], policy.toJson());
expect(controller.notice, contains('saved on this server'));
controller.dispose();
});
test('apply requires reviewed token and edit invalidates it', () async {
final gateway = StorageProbeGateway();
final controller = StorageController(gateway);
await controller.apply(policy);
expect(gateway.calls, isEmpty);
expect(controller.error, contains('Preview cleanup again'));
await controller.plan(policy);
expect(controller.preview!['preview_token'], 'fixture-preview-token');
controller.invalidatePreview();
await controller.apply(policy);
expect(
gateway.calls.where((call) => call.type == 'apply_storage_cleanup'),
isEmpty,
);
controller.dispose();
});
test('late preview after form edit cannot become actionable', () async {
final gateway = _Pending();
final controller = StorageController(gateway);
final pending = controller.plan(policy);
controller.invalidatePreview();
gateway.plan.complete({'storage_cleanup': gateway.cleanup});
await pending;
expect(controller.preview, isNull);
controller.dispose();
});
test('apply sends exact token and retains partial cleanup errors', () async {
final gateway = StorageProbeGateway();
final controller = StorageController(gateway);
await controller.plan(policy);
await controller.apply(policy);
final apply = gateway.calls.singleWhere(
(call) => call.type == 'apply_storage_cleanup',
);
expect(apply.fields, {
'storage_policy': policy.toJson(),
'storage_cleanup_token': 'fixture-preview-token',
});
expect(controller.preview, isNull);
expect(controller.result!['removed_count'], 1);
expect(
controller.result!['errors'],
contains('Worktree became active and was kept.'),
);
expect(controller.overview!['partial'], true);
controller.dispose();
});
test('policy validation rejects unsafe and nonfinite bounds', () {
for (final size in ['NaN', 'Infinity', '-2']) {
expect(
() => parseStoragePolicy(
enabled: true,
days: '',
gib: size,
includeWorktrees: false,
),
throwsFormatException,
);
}
expect(
() => parseStoragePolicy(
enabled: true,
days: '',
gib: '',
includeWorktrees: false,
),
throwsFormatException,
);
expect(
parseStoragePolicy(
enabled: false,
days: '',
gib: '',
includeWorktrees: false,
).maxBytes,
0,
);
});
}
@@ -0,0 +1,125 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/settings/storage/storage_screen.dart';
import 'package:pipelines_app/features/settings/storage/storage_controls.dart';
import '../../../../tool/storage_probe_fixture.dart';
Future<void> _mount(
WidgetTester tester,
StorageProbeGateway gateway,
bool cupertino, {
double width = 900,
double scale = 1,
}) async {
tester.view.physicalSize = Size(width, 1100);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final screen = StorageScreen(gateway: gateway);
Widget builder(BuildContext context, Widget? child) => MediaQuery(
data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)),
child: child!,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(builder: builder, home: screen)
: MaterialApp(builder: builder, home: screen),
);
await tester.pumpAndSettle();
}
Future<void> _tap(WidgetTester tester, String label) async {
final finder = find.text(label);
await tester.ensureVisible(finder);
await tester.pumpAndSettle();
await tester.tap(finder);
await tester.pumpAndSettle();
}
Finder _field(String label) => find.descendant(
of: find.byWidgetPredicate(
(widget) => widget is StorageInput && widget.label == label,
),
matching: find.byType(EditableText),
);
void main() {
for (final cupertino in [false, true]) {
final style = cupertino ? 'Cupertino' : 'Material';
testWidgets(
'$style usage and partial measurement remain honest at 2x narrow',
(tester) async {
final gateway = StorageProbeGateway();
await _mount(tester, gateway, cupertino, width: 560, scale: 2);
expect(find.text('At least 15.0 GiB used'), findsOneWidget);
expect(
find.text('Measurement is incomplete. See details below.'),
findsOneWidget,
);
expect(find.text('Database'), findsOneWidget);
expect(find.textContaining('6 November 2026'), findsOneWidget);
expect(find.text('Worktrees'), findsOneWidget);
await _tap(tester, 'Preview cleanup');
expect(find.text('Cleanup preview'), findsOneWidget);
expect(
find.text('Protected · worktree active-item · 4.00 GiB'),
findsOneWidget,
);
expect(tester.takeException(), isNull);
},
);
testWidgets(
'$style cleanup cancellation does not mutate and confirmation uses token',
(tester) async {
final gateway = StorageProbeGateway();
await _mount(tester, gateway, cupertino);
await _tap(tester, 'Preview cleanup');
await _tap(tester, 'Apply cleanup');
expect(find.text('Apply cleanup?'), findsOneWidget);
await _tap(tester, 'Cancel');
expect(
gateway.calls.where((call) => call.type == 'apply_storage_cleanup'),
isEmpty,
);
await _tap(tester, 'Apply cleanup');
await _tap(tester, 'Remove items');
expect(
gateway.calls
.singleWhere((call) => call.type == 'apply_storage_cleanup')
.fields['storage_cleanup_token'],
'fixture-preview-token',
);
expect(find.text('Cleanup result'), findsOneWidget);
expect(
find.text('Worktree became active and was kept.'),
findsOneWidget,
);
expect(tester.takeException(), isNull);
},
);
testWidgets('$style invalid limits do not send a policy', (tester) async {
final gateway = StorageProbeGateway();
await _mount(tester, gateway, cupertino);
await tester.ensureVisible(_field('Retain data for days'));
await tester.enterText(_field('Retain data for days'), '-1');
await _tap(tester, 'Save policy');
expect(
find.textContaining('Enter a whole number of days'),
findsOneWidget,
);
expect(
gateway.calls.where((call) => call.type == 'set_storage_policy'),
isEmpty,
);
});
testWidgets(
'$style unsupported server has an actionable error and no fake usage',
(tester) async {
final gateway = StorageProbeGateway()..unsupported = true;
await _mount(tester, gateway, cupertino);
expect(find.text('Unknown command: get_storage'), findsOneWidget);
expect(find.text('Server storage'), findsNothing);
expect(find.text('Refresh storage'), findsOneWidget);
},
);
}
}
+35
View File
@@ -0,0 +1,35 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pipelines_app/features/settings/storage/storage_screen.dart';
import 'storage_probe_fixture.dart';
void main() {
final query = Uri.base.queryParameters;
final cupertino = query['platform'] == 'cupertino';
final dark = query['theme'] == 'dark';
final scale = double.tryParse(query['scale'] ?? '') ?? 1;
final screen = StorageScreen(
gateway: StorageProbeGateway()..unsupported = query['unsupported'] == '1',
);
Widget scaled(BuildContext context, Widget? child) => MediaQuery(
data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)),
child: child!,
);
runApp(
cupertino
? CupertinoApp(
theme: CupertinoThemeData(
brightness: dark ? Brightness.dark : Brightness.light,
),
builder: scaled,
home: screen,
)
: MaterialApp(
theme: ThemeData(
brightness: dark ? Brightness.dark : Brightness.light,
),
builder: scaled,
home: screen,
),
);
}
+105
View File
@@ -0,0 +1,105 @@
import 'package:pipelines_app/core/api_gateway.dart';
class StorageProbeGateway implements ApiGateway {
final calls = <({String type, JsonMap fields})>[];
JsonMap policy = {
'enabled': false,
'max_age_days': 30,
'max_bytes': 10737418240,
'include_worktrees': true,
};
bool unsupported = false;
@override
Stream<JsonMap> get frames => const Stream.empty();
JsonMap get overview => {
'measured_at': '2026-11-06T12:34:56Z',
'automatic_interval_seconds': 3600,
'total_bytes': 16106127360,
'partial': true,
'policy': policy,
'categories': [
{
'id': 'database',
'label': 'Database',
'bytes': 1073741824,
'files': 3,
'path': '/fixture/data/pipelines.db',
},
{
'id': 'artifacts',
'label': 'Run artifacts',
'bytes': 5368709120,
'files': 148,
'path': '/fixture/data/artifacts',
},
{
'id': 'worktrees',
'label': 'Worktrees',
'bytes': 9663676416,
'files': 10432,
'path': '/fixture/repos/worktrees',
},
],
'warnings': ['One inaccessible directory was excluded.'],
};
JsonMap get cleanup => {
'policy': policy,
'total_bytes': 16106127360,
'reclaimable_bytes': 3221225472,
'target_bytes': 10737418240,
'target_reached': false,
'candidate_count': 2,
'protected_count': 1,
'preview_token': 'fixture-preview-token',
'items': [
{
'kind': 'run',
'id': '42',
'bytes': 1073741824,
'reason': 'Older than retention age',
'protected': false,
},
{
'kind': 'worktree',
'id': 'completed-item',
'path': '/fixture/repos/worktrees/completed-item',
'bytes': 2147483648,
'reason': 'Clean and merged',
'protected': false,
},
{
'kind': 'worktree',
'id': 'active-item',
'bytes': 4294967296,
'reason': 'Active session',
'protected': true,
},
],
'applied': false,
};
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add((type: type, fields: fields));
if (unsupported) return {'error': 'Unknown command: get_storage'};
switch (type) {
case 'get_storage':
return {'storage': overview};
case 'set_storage_policy':
policy = Map<String, dynamic>.from(fields['storage_policy'] as Map);
return {'storage': overview};
case 'preview_storage_cleanup':
return {'storage_cleanup': cleanup};
case 'apply_storage_cleanup':
return {
'storage_cleanup': {
...cleanup,
'applied': true,
'removed_count': 1,
'removed_bytes': 1073741824,
'errors': ['Worktree became active and was kept.'],
},
};
}
return {};
}
}