todo-app/test/capture_screen_harness.dart
Krzysztof kuhy Rudnicki 9f2ffdb8a9
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
Attach any file to a note, of any size, not only images
📎 now picks any file (Android: Camera / Gallery / File; desktop: an
unfiltered dialog) and the desktop page accepts any dropped file. Photo and
screenshot formats up to 64 MB are still re-encoded and shown inline;
everything else is stored byte for byte on dufs beside the images and linked
as [name](url), shown as a row with its size that opens in the platform's
app (Android) or a new browser tab (desktop). BACKLOG.md rewrites file links
to ~/data/cloud/todo-images/ like image links.

There is no size cap, so no layer holds a file in memory: uploads and
downloads stream through DufsImageClient, writes land in a .tmp renamed when
whole (an interrupted download no longer leaves a truncated file that looks
done), the desktop page POSTs the browser's own Blob with fetch, and Android's
"File" uses file_picker, because file_selector's Android side reads the whole
file into memory on the UI thread. Verified with a 1 GB file end to end on
desktop (peak RSS +36 MB) and on an API 36 emulator.

The wrapper serves attachments from the origin that holds the GitHub token
and the notes, so only script-free types open inline (nosniff), the rest
download; writes carrying another site's Origin are refused (this also
closes cross-site overwrites of the backup files); and attachment routes run
concurrently so a long upload or a playing video cannot stall the page.

Also: cupertino_icons 2.0.0 (unused, but the freshness gate blocks the
pubspec otherwise), open_file 4.0.0 and file_picker 13.1.0 pinned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 12:17:06 +02:00

114 lines
4.4 KiB
Dart

/// The shared harness for the capture-screen test files.
///
/// `capture_screen_test.dart` was split by concern for the 250-line cap.
/// [pumpCapture] closes over the `WidgetTester`'s view and addTearDown, so it
/// takes the tester as a parameter rather than being a closure inside main().
///
/// Deliberately NOT named `*_test.dart`: the runner would collect it and fail
/// on the missing `main()`.
library;
import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:todo/analytics/analytics_service.dart';
import 'package:todo/attachments/image_store_api.dart';
import 'package:todo/attachments/image_store_scope.dart';
import 'package:todo/data/app_settings.dart';
import 'package:todo/data/note.dart';
import 'package:todo/data/note_template.dart';
import 'package:todo/sync/local_backup.dart';
import 'package:todo/sync/notes_markdown.dart';
import 'package:todo/ui/attachment_picking.dart';
import 'package:todo/ui/capture_screen.dart';
import 'package:todo/ui/github_mirror_screen.dart';
import 'package:todo/ui/settings_screen.dart';
import 'fake_note_repository.dart';
import 'fake_secure_storage.dart';
// A real CRDT DB schedules sqflite timers that never drain under the
// widget tester's fake clock, so these tests inject a timer-free fake.
// (NOTE: avoid pumpAndSettle — the autofocused field's cursor blink never
// settles; pump explicit frames instead.)
Future<FakeNoteRepository> pumpCapture(
WidgetTester tester, {
Map<String, Object> prefs = const {},
http.Client? httpClient,
List<Note> seed = const [],
FakeNoteRepository? repository,
LocalBackup? localBackup,
Future<FirebaseRestClient?> Function()? firebaseFactory,
// Defaults to true: these tests exercise today's full UI (priority/status
// dropdowns, sync status text, Guided). Tests for the new default-off
// behavior pass advancedMode: false explicitly.
ValueNotifier<AppSettings>? appSettings,
AnalyticsService? analytics,
ImageStore? imageStore,
AttachmentPicking? attachmentPicking,
}) async {
SharedPreferences.setMockInitialValues(prefs);
installFakeSecureStorage();
// Tall surface so a pushed settings screen builds its whole ListView.
tester.view.physicalSize = const Size(1200, 2800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final repo = repository ?? FakeNoteRepository(seed);
addTearDown(repo.close);
// Default to an in-memory, no-op backup so tests never touch real disk
// (the production backup writes ~/src/todo/BACKLOG.md on the Linux test host).
final backup =
localBackup ??
LocalBackup(
fetch: repo.listNotes,
reader: () async => null,
writer: (_) async {},
debounce: Duration.zero,
);
final app = MaterialApp(
home: CaptureScreen(
repository: repo,
appSettings:
appSettings ?? ValueNotifier(const AppSettings(advancedMode: true)),
analytics: analytics,
httpClient: httpClient,
// Both injected so the widget never reaches for the platform: the
// real factories want the OS keystore and an application-support
// directory, neither of which exists under `flutter test`.
firebaseFactory: firebaseFactory ?? () async => null,
stateStore: InMemorySyncStateStore(),
localBackup: backup,
attachmentPicking: attachmentPicking,
),
);
await tester.pumpWidget(
imageStore == null ? app : ImageStoreScope(store: imageStore, child: app),
);
await tester.pump(); // flush initial stream + settings load
return repo;
}
// Seeds a fully configured GitHub sync so the configured `_sync` path runs.
const configuredPrefs = {
'sync.owner': 'o',
'sync.repo': 'r',
'sync.token': 'tok',
};
// A MockClient that records request methods and answers the sync flow:
// repo-exists GET (not /contents/) → 200, the (empty) log listing → 404,
// and the device's own PUT → 200.
MockClient recordingMock(List<String> methods) => MockClient((req) async {
methods.add(req.method);
if (req.method == 'PUT') return http.Response('{}', 200);
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);
return http.Response('', 404);
});