import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:openchamber/api/client.dart'; import 'package:openchamber/api/transport.dart'; import 'package:openchamber/l10n.dart'; import 'package:openchamber/screens/archived_sessions_screen.dart'; import 'package:openchamber/state.dart'; class _FakeTransport implements Transport { final List> archived; _FakeTransport(this.archived); @override Future send(TransportRequest r) async { Object? body; if (r.path == '/api/session') { body = {'data': archived}; } else if (r.path == '/api/project') { body = [ {'id': 'p1', 'canonical': '/home/me/alpha', 'vcs': 'git'}, {'id': 'p2', 'canonical': '/home/me/beta', 'vcs': 'git'}, ]; } else if (r.path == '/api/config/settings') { body = {'projects': []}; } else if (r.path == '/api/fs/home') { body = {'home': '/home/me'}; } else if (r.method == 'PATCH') { archived.removeWhere((s) => s['id'] == r.path.split('/').last); body = {'data': {}}; } else if (r.method == 'DELETE') { archived.removeWhere((s) => s['id'] == r.path.split('/').last); body = {}; } else { body = {}; } return TransportResponse( 200, const {'content-type': 'application/json'}, Stream.value(Uint8List.fromList(utf8.encode(jsonEncode(body)))), ); } @override Future webSocket(String path, {String? rawQuery, Map headers = const {}}) async => throw UnimplementedError(); @override void setBearerToken(String? token) {} @override void close() {} } Map _session( String id, String title, String directory, int archived) => { 'id': id, 'title': title, 'time': {'created': archived - 1000, 'updated': archived - 500, 'archived': archived}, 'location': {'directory': directory}, }; void main() { setUp(() => L10n.instance.apply(AppLanguage.en)); tearDown(() => L10n.instance.apply(AppLanguage.zh)); testWidgets('archived sessions are listed, filtered and restored', (tester) async { final app = AppState(); addTearDown(app.dispose); final transport = _FakeTransport([ _session('a1', 'Old alpha', '/home/me/alpha', 1000), _session('b1', 'Old beta', '/home/me/beta', 2000), ]); app.client = Client(Uri.parse('http://test'), transport: transport); await tester.pumpWidget(MaterialApp(home: ArchivedSessionsScreen(app: app))); await tester.pumpAndSettle(); expect(find.text('Archive'), findsOneWidget); expect(find.text('2 archived sessions'), findsOneWidget); expect(find.text('Old alpha'), findsOneWidget); expect(find.text('Old beta'), findsOneWidget); expect(find.text('All projects'), findsOneWidget); // The shared project sheet narrows the archive; "All projects" brings both // back. await tester.tap(find.text('All projects')); await tester.pumpAndSettle(); await tester.tap(find.text('/home/me/alpha')); await tester.pumpAndSettle(); expect(find.text('Old alpha'), findsOneWidget); expect(find.text('Old beta'), findsNothing); expect(find.text('1 archived session'), findsOneWidget); await tester.tap(find.text('alpha')); await tester.pumpAndSettle(); await tester.tap(find.text('All projects')); await tester.pumpAndSettle(); expect(find.text('Old beta'), findsOneWidget); // Search across projects. await tester.enterText(find.byType(TextField), 'beta'); await tester.pumpAndSettle(); expect(find.text('Old alpha'), findsNothing); expect(find.text('Old beta'), findsOneWidget); expect(find.text('1 archived session'), findsOneWidget); // Restoring removes the row from the archive and from the list. await tester.tap(find.byIcon(Icons.unarchive_outlined)); await tester.pumpAndSettle(); expect(find.text('Old beta'), findsNothing); expect(find.text('Session restored'), findsOneWidget); expect(app.archivedSessions.map((s) => s.id), ['a1']); }); testWidgets('the project sheet filters the archive', (tester) async { final app = AppState(); addTearDown(app.dispose); final transport = _FakeTransport([ _session('a1', 'Old alpha', '/home/me/alpha', 1000), _session('b1', 'Old beta', '/home/me/beta', 2000), ]); app.client = Client(Uri.parse('http://test'), transport: transport); await tester.pumpWidget(MaterialApp(home: ArchivedSessionsScreen(app: app))); await tester.pumpAndSettle(); await tester.tap(find.text('All projects')); await tester.pumpAndSettle(); await tester.tap(find.text('/home/me/beta')); await tester.pumpAndSettle(); expect(find.text('Old beta'), findsOneWidget); expect(find.text('Old alpha'), findsNothing); }); }