mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-10 19:27:01 +01:00
A ref is about to name any file stored under todo-images/, not only a re-encoded JPEG. Mechanical rename of the type and its file; no behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
2.4 KiB
Dart
75 lines
2.4 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:todo/images/attachment_ref.dart';
|
|
|
|
void main() {
|
|
const rel = 'note-1/0815ff8a-203e-4f54-9cc5-8a58b27982ec.jpg';
|
|
final ref = AttachmentRef.tryParse(rel)!;
|
|
|
|
test('a ref exposes its parts, url and markdown', () {
|
|
expect(ref.noteId, 'note-1');
|
|
expect(ref.file, '0815ff8a-203e-4f54-9cc5-8a58b27982ec.jpg');
|
|
expect(ref.relativePath, rel);
|
|
expect(ref.url, '$kDufsBaseUrl/$kImagesDir/$rel');
|
|
expect(ref.markdown, '');
|
|
});
|
|
|
|
test('create files a fresh uuid jpg under the note', () {
|
|
final a = AttachmentRef.create('n1');
|
|
final b = AttachmentRef.create('n1');
|
|
expect(a.noteId, 'n1');
|
|
expect(a.file, endsWith('.jpg'));
|
|
expect(a, isNot(b));
|
|
});
|
|
|
|
test('create refuses an unsafe note id', () {
|
|
expect(() => AttachmentRef.create('../x'), throwsArgumentError);
|
|
});
|
|
|
|
test('tryParse rejects traversal, nesting and other extensions', () {
|
|
for (final bad in [
|
|
'../x/y.jpg',
|
|
'a/../b.jpg',
|
|
'a/b/c.jpg',
|
|
'a/b.png',
|
|
'a/.jpg',
|
|
'/a/b.jpg',
|
|
'a b/c.jpg',
|
|
]) {
|
|
expect(AttachmentRef.tryParse(bad), isNull, reason: bad);
|
|
}
|
|
});
|
|
|
|
test('refs compare by path, so sets dedupe', () {
|
|
expect({ref, AttachmentRef.tryParse(rel)!}, hasLength(1));
|
|
});
|
|
|
|
test('imageRefsIn finds every dufs image link, in order, once', () {
|
|
final other = AttachmentRef.create('n2');
|
|
final text =
|
|
'hi ${ref.markdown} and\n${other.markdown}\n${ref.markdown}\n'
|
|
' ';
|
|
expect(imageRefsIn(text).toList(), [ref, other]);
|
|
expect(imageRefsIn('no images'), isEmpty);
|
|
});
|
|
|
|
test('imageRefOfLine only matches a line that is just one image', () {
|
|
expect(imageRefOfLine(' ${ref.markdown} '), ref);
|
|
expect(imageRefOfLine('see ${ref.markdown}'), isNull);
|
|
expect(imageRefOfLine(''), ref);
|
|
expect(imageRefOfLine('plain'), isNull);
|
|
});
|
|
|
|
test('backlog paths round-trip exactly', () {
|
|
final text = 'a\n${ref.markdown}\n';
|
|
final disk = toBacklogImagePaths(text);
|
|
expect(disk, contains(''));
|
|
expect(disk, isNot(contains(kDufsBaseUrl)));
|
|
expect(fromBacklogImagePaths(disk), text);
|
|
});
|
|
|
|
test('a bare path outside an image link is left alone', () {
|
|
const text = 'see $kBacklogImagesRoot/$rel';
|
|
expect(fromBacklogImagePaths(text), text);
|
|
});
|
|
}
|