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

98 lines
3.2 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:image/image.dart' as img;
import 'package:todo/attachments/image_store_scope.dart';
import 'package:todo/images/attachment_ref.dart';
import 'package:todo/ui/images/image_embed.dart';
import 'package:todo/ui/images/attachment_strip.dart';
import 'package:todo/ui/images/image_viewer_screen.dart';
import '../fake_image_store.dart';
void main() {
final ref = AttachmentRef.create('n1');
late FakeImageStore store;
late Directory dir;
setUp(() {
store = FakeImageStore();
dir = Directory.systemTemp.createTempSync('embed');
final file = File('${dir.path}/a.jpg')
..writeAsBytesSync(img.encodeJpg(img.Image(width: 4, height: 4)));
store.localPath = file.path;
});
tearDown(() => dir.deleteSync(recursive: true));
Future<void> pump(WidgetTester tester, Widget child) => tester.pumpWidget(
ImageStoreScope(
store: store,
child: MaterialApp(
home: Scaffold(body: SizedBox(width: 200, height: 200, child: child)),
),
),
);
testWidgets('a local image renders and opens full screen', (tester) async {
await pump(tester, ImageEmbed(ref: ref));
expect(find.byType(Image), findsOneWidget);
expect(find.byTooltip(kImageWaitingToUpload), findsNothing);
await tester.tap(find.byType(ImageEmbed));
await tester.pumpAndSettle();
expect(find.byType(ImageViewerScreen), findsOneWidget);
expect(find.byType(InteractiveViewer), findsOneWidget);
});
testWidgets('a queued image carries the upload badge', (tester) async {
store.pending.add(ref);
await pump(tester, ImageEmbed(ref: ref));
expect(find.byTooltip(kImageWaitingToUpload), findsOneWidget);
});
testWidgets('a missing image is a placeholder and not tappable', (
tester,
) async {
store.missing.add(ref);
await pump(tester, ImageEmbed(ref: ref));
expect(find.text(kImageNotDownloaded), findsOneWidget);
await tester.tap(find.byType(ImageEmbed));
await tester.pumpAndSettle();
expect(find.byType(ImageViewerScreen), findsNothing);
});
testWidgets('a broken file or url falls back to the placeholder', (
tester,
) async {
store.localPath = '${dir.path}/gone.jpg';
await pump(tester, ImageEmbed(ref: ref));
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 50)),
);
await tester.pump();
expect(find.text(kImageNotDownloaded), findsOneWidget);
store.network = true;
store.changed();
await tester.pump();
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 50)),
);
await tester.pump();
expect(find.text(kImageNotDownloaded), findsOneWidget);
});
testWidgets('the strip shows one thumbnail per linked image', (tester) async {
final other = AttachmentRef.create('n2');
await pump(
tester,
AttachmentStrip(text: 'x\n${ref.imageMarkdown}\n${other.imageMarkdown}'),
);
expect(find.byType(ImageEmbed), findsNWidgets(2));
});
testWidgets('the strip is empty without images', (tester) async {
await pump(tester, const AttachmentStrip(text: 'no images'));
expect(find.byType(ImageEmbed), findsNothing);
});
}