todo-app/test/fake_image_store.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

102 lines
3.0 KiB
Dart

/// An in-memory [ImageStore] for widget tests.
///
/// Deliberately NOT named `*_test.dart`: the runner would collect it.
library;
import 'dart:async';
import 'dart:typed_data';
import 'package:todo/attachments/image_store_api.dart';
import 'package:todo/images/attachment_ref.dart';
/// Records attaches and reconciles; every image is "local" at a fake path
/// unless listed in [missing].
class FakeImageStore extends ImageStore {
/// Bytes accepted per ref, in attach order.
final Map<AttachmentRef, Uint8List> attached = {};
/// Files accepted through [attachFile], in order.
final List<XFile> attachedFiles = [];
/// What [sizeOf] reports for an attachment on this device.
int size = 2048;
/// Note texts passed to each reconcile.
final List<List<String>> reconciles = [];
/// Refs reported as not on this device.
final Set<AttachmentRef> missing = {};
/// Refs reported as waiting to upload.
final Set<AttachmentRef> pending = {};
/// Inputs whose first byte is this are "not an image".
int notImageMarker = 0;
/// When set, attach throws this.
ImageStoreException? failure;
/// Where local images appear to live, e.g. a real temp file.
String localPath = '/nonexistent/image.jpg';
/// Returns [NetworkImageSource]s instead of local ones.
bool network = false;
/// When set, attach waits for it first (to act mid-attach).
Future<void>? gate;
@override
Future<AttachmentRef?> attach(String noteId, Uint8List bytes) async {
await gate;
final failure = this.failure;
if (failure != null) throw failure;
if (bytes.isNotEmpty && bytes.first == notImageMarker) return null;
final ref = AttachmentRef.create(noteId);
attached[ref] = bytes;
return ref;
}
/// Files whose name ends in `.png` or `.jpg` come back as images.
@override
Future<Attachment> attachFile(String noteId, XFile file) async {
await gate;
final failure = this.failure;
if (failure != null) throw failure;
attachedFiles.add(file);
final isImage = file.name.endsWith('.png') || file.name.endsWith('.jpg');
return Attachment(
ref: isImage
? AttachmentRef.create(noteId)
: AttachmentRef.forFile(noteId, file.name),
name: file.name,
isImage: isImage,
);
}
@override
Future<int?> sizeOf(AttachmentRef ref) async =>
missing.contains(ref) ? null : size;
@override
Future<void> reconcile(Iterable<String> noteTexts) async =>
reconciles.add(noteTexts.toList());
@override
ImageSource sourceFor(AttachmentRef ref) {
if (missing.contains(ref)) return const MissingImageSource();
return network
? NetworkImageSource('http://localhost/images/$ref')
: LocalImageSource(localPath);
}
@override
bool isPending(AttachmentRef ref) => pending.contains(ref);
@override
Future<ImageStoreStatus> status() async =>
ImageStoreStatus(configured: true, pending: pending.length);
/// Lets a test fire a change notification.
void changed() => notifyListeners();
}