todo-app/test/images/image_cache_store_vanish_test.dart
Krzysztof kuhy Rudnicki 9a8e7aefd2
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Fix flaky image_store_io_test "without picker cop…
Todo: 2961b5ff
2026-11-09 08:37:03 +01:00

70 lines
1.9 KiB
Dart

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:todo/images/attachment_ref.dart';
import 'package:todo/images/image_cache_store.dart';
import 'fake_dufs.dart';
/// A file that `existsSync()` still sees but that is gone by the time its
/// length is read: the window a teardown or a note deletion can hit while an
/// upload pass is between the two calls.
class _VanishingFile implements File {
_VanishingFile(this.path);
@override
final String path;
@override
bool existsSync() => true;
@override
Future<int> length() async =>
throw PathNotFoundException(path, const OSError('gone', 2));
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
/// Hands out a [_VanishingFile] for exactly one path, real files otherwise.
final class _VanishOverrides extends IOOverrides {
_VanishOverrides(this.vanishing);
final String vanishing;
@override
File createFile(String path) =>
path == vanishing ? _VanishingFile(path) : super.createFile(path);
}
void main() {
late Directory root;
late ImageCacheStore store;
late FakeDufs dufs;
final a = AttachmentRef.tryParse('n1/a.jpg')!;
final b = AttachmentRef.tryParse('n2/b.jpg')!;
final bytes = Uint8List.fromList([1, 2, 3]);
setUp(() {
root = Directory.systemTemp.createTempSync('image_cache_vanish');
store = ImageCacheStore(root);
dufs = FakeDufs();
});
tearDown(() => root.deleteSync(recursive: true));
test('a file vanishing mid-pass is skipped; the rest still upload', () async {
await store.put(a, bytes);
await store.put(b, bytes);
final report = await IOOverrides.runWithIOOverrides(
() => store.reconcile({}, dufs.client()),
_VanishOverrides(store.fileFor(a).path),
);
expect(report.uploaded, 1);
expect(dufs.files.keys, ['n2/b.jpg']);
expect(store.isPending(a), isTrue);
expect(store.isPending(b), isFalse);
});
}