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

149 lines
4.1 KiB
Dart

import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:todo/attachments/image_store_api.dart';
import 'package:todo/attachments/image_store_scope.dart';
import 'package:todo/ui/image_attach.dart';
import '../fake_image_store.dart';
void main() {
test('the controller inserts only while an editor is bound', () {
final controller = ImageAttachController();
final got = <List<String>>[];
expect(controller.insert(['a']), isFalse);
final unbind = controller.bind(got.add);
expect(controller.insert([]), isFalse);
expect(controller.insert(['a']), isTrue);
// A later editor's bind wins; the earlier unbind no longer detaches it.
final unbindSecond = controller.bind(got.add);
unbind();
expect(controller.insert(['b']), isTrue);
unbindSecond();
expect(controller.insert(['c']), isFalse);
expect(got, [
['a'],
['b'],
]);
});
Future<BuildContext> pumpScope(WidgetTester tester, ImageStore store) async {
late BuildContext context;
await tester.pumpWidget(
ImageStoreScope(
store: store,
child: MaterialApp(
home: Scaffold(
body: Builder(
builder: (c) {
context = c;
return const SizedBox();
},
),
),
),
),
);
return context;
}
testWidgets('attaches in order and inserts every link', (tester) async {
final store = FakeImageStore();
final context = await pumpScope(tester, store);
final controller = ImageAttachController();
final inserted = <List<String>>[];
controller.bind(inserted.add);
var asked = 0;
await attachImages(
context,
noteId: () {
asked++;
return 'n1';
},
target: controller,
images: [
Uint8List.fromList([1]),
Uint8List.fromList([2]),
],
);
expect(asked, 2);
expect(store.attached.values.map((b) => b.first), [1, 2]);
expect(inserted.single, [
for (final ref in store.attached.keys) ref.imageMarkdown,
]);
await tester.pump();
expect(find.byType(SnackBar), findsNothing);
});
testWidgets('reports one non-image', (tester) async {
final store = FakeImageStore();
final context = await pumpScope(tester, store);
await attachImages(
context,
noteId: () => 'n1',
target: ImageAttachController(),
images: [
Uint8List.fromList([0]),
],
);
await tester.pump();
expect(find.text('Not an image'), findsOneWidget);
});
testWidgets('counts dropped non-images together', (tester) async {
final store = FakeImageStore();
final context = await pumpScope(tester, store);
await attachImages(
context,
noteId: () => 'n1',
target: ImageAttachController(),
images: [
Uint8List.fromList([0]),
],
rejected: 2,
);
await tester.pump();
expect(find.text('3 files were not images'), findsOneWidget);
});
testWidgets('a store failure is shown and stops the batch', (tester) async {
final store = FakeImageStore()
..failure = const ImageStoreException('helper down');
final context = await pumpScope(tester, store);
await attachImages(
context,
noteId: () => 'n1',
target: ImageAttachController(),
images: [
Uint8List.fromList([1]),
Uint8List.fromList([2]),
],
);
await tester.pump();
expect(find.text('helper down'), findsOneWidget);
});
testWidgets('nothing is shown once the screen closed mid-attach', (
tester,
) async {
final gate = Completer<void>();
final store = FakeImageStore()..gate = gate.future;
final context = await pumpScope(tester, store);
final pending = attachImages(
context,
noteId: () => 'n1',
target: ImageAttachController(),
images: [
Uint8List.fromList([0]),
],
);
await tester.pumpWidget(const SizedBox());
gate.complete();
await pending;
await tester.pump();
expect(find.byType(SnackBar), findsNothing);
});
}