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

88 lines
2.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:todo/ui/attachment_picking.dart';
void main() {
testWidgets('the button passes picked files on, ignores a cancel', (
tester,
) async {
final got = <List<XFile>>[];
var result = <XFile>[];
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: AttachButton(onFiles: got.add, picking: (_) async => result),
),
),
);
await tester.tap(find.byTooltip('Attach'));
await tester.pump();
expect(got, isEmpty);
result = [XFile('/picked/a.pdf')];
await tester.tap(find.byTooltip('Attach'));
await tester.pump();
expect(got.single.single.name, 'a.pdf');
});
Future<List<XFile>?> openSheet(
WidgetTester tester,
List<AttachmentPickSource> asked,
) async {
List<XFile>? result;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => TextButton(
onPressed: () async => result = await pickAttachments(
context,
fromPhone: (source) async {
asked.add(source);
return [XFile('/picked/${source.name}')];
},
),
child: const Text('pick'),
),
),
),
),
);
await tester.tap(find.text('pick'));
await tester.pumpAndSettle();
return result;
}
Future<void> choose(WidgetTester tester, String entry) async {
await tester.tap(find.text('pick'));
await tester.pumpAndSettle();
await tester.tap(find.text(entry));
await tester.pumpAndSettle();
}
testWidgets('the phone sheet offers Camera, Gallery and File', (
tester,
) async {
final asked = <AttachmentPickSource>[];
await openSheet(tester, asked);
expect(find.text('Camera'), findsOneWidget);
expect(find.text('File'), findsOneWidget);
await tester.tap(find.text('Gallery'));
await tester.pumpAndSettle();
await choose(tester, 'Camera');
await choose(tester, 'File');
expect(asked, [
AttachmentPickSource.gallery,
AttachmentPickSource.camera,
AttachmentPickSource.file,
]);
});
testWidgets('dismissing the sheet picks nothing', (tester) async {
final asked = <AttachmentPickSource>[];
await openSheet(tester, asked);
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(asked, isEmpty);
});
}