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

89 lines
2.9 KiB
Dart

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:todo/attachments/image_store_api.dart';
import 'package:todo/ui/images/file_opening.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
/// Records the URL instead of opening a browser.
class _FakeUrlLauncher extends UrlLauncherPlatform
with MockPlatformInterfaceMixin {
final List<(String, String?)> launched = [];
bool succeed = true;
@override
final LinkDelegate? linkDelegate = null;
@override
Future<bool> launchUrl(String url, LaunchOptions options) async {
launched.add((url, options.webOnlyWindowName));
return succeed;
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const channel = MethodChannel('open_file');
final calls = <Map<Object?, Object?>>[];
void answer(int type, String message) {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
calls.add(call.arguments as Map<Object?, Object?>);
return jsonEncode({'type': type, 'message': message});
});
}
setUp(calls.clear);
tearDown(
() => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null),
);
test('a local file goes to the app that handles its type', () async {
answer(0, 'done');
expect(
await openAttachedFile(const LocalImageSource('/c/a.pdf'), 'a.pdf'),
isNull,
);
expect(calls.single['file_path'], '/c/a.pdf');
});
test('no handler or a failure says so', () async {
answer(-1, 'No APP found');
expect(
await openAttachedFile(const LocalImageSource('/c/a.xyz'), 'a.xyz'),
'No app on this phone opens a.xyz',
);
answer(-4, 'File opened incorrectly');
expect(
await openAttachedFile(const LocalImageSource('/c/a.pdf'), 'a.pdf'),
'Could not open a.pdf: File opened incorrectly',
);
});
test('the desktop opens the wrapper copy, named, in a new tab', () async {
final launcher = _FakeUrlLauncher();
UrlLauncherPlatform.instance = launcher;
const source = NetworkImageSource(
'http://localhost:8730/images/n/u.pdf?r=2',
);
expect(await openAttachedFile(source, 'Raport v2.pdf'), isNull);
final (url, window) = launcher.launched.single;
expect(Uri.parse(url).queryParameters, {'r': '2', 'name': 'Raport v2.pdf'});
expect(window, '_blank');
launcher.succeed = false;
expect(await openAttachedFile(source, 'a.pdf'), 'Could not open a.pdf');
});
test('a file not on this device cannot be opened', () async {
expect(
await openAttachedFile(const MissingImageSource(), 'a.pdf'),
'a.pdf is not on this device yet',
);
});
}