mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-08-23 08:30:21 +08:00

Instead we're going to move back to standard exceptions. Using a custom Result class has created far far more problems - The Stacktraces aren't always right - Sometimes one forgets to check the Result error - All other exception throwing code needing to be converted to Results - Non idiomatic Dart code I think it's better to just go back to exceptions. They have their problems, but overall, I think it's a better approach.
178 lines
5.3 KiB
Dart
178 lines
5.3 KiB
Dart
/*
|
|
* SPDX-FileCopyrightText: 2019-2021 Vishesh Handa <me@vhanda.in>
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
import 'dart:math';
|
|
|
|
import 'package:gitjournal/core/file/file.dart';
|
|
import 'package:gitjournal/core/file/file_storage.dart';
|
|
import 'package:gitjournal/core/folder/notes_folder_config.dart';
|
|
import 'package:gitjournal/core/folder/notes_folder_fs.dart';
|
|
import 'package:gitjournal/core/folder/sorted_notes_folder.dart';
|
|
import 'package:gitjournal/core/folder/sorting_mode.dart';
|
|
import 'package:gitjournal/core/note_storage.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:test/test.dart';
|
|
import 'package:universal_io/io.dart' as io;
|
|
|
|
import 'lib.dart';
|
|
|
|
void main() {
|
|
setUpAll(gjSetupAllTests);
|
|
|
|
group('Sorted Notes Folder Test', () {
|
|
late String repoPath;
|
|
late io.Directory tempDir;
|
|
late NotesFolderFS folder;
|
|
late NotesFolderConfig config;
|
|
late FileStorage fileStorage;
|
|
|
|
final gitDt = DateTime.now();
|
|
|
|
setUp(() async {
|
|
tempDir =
|
|
await io.Directory.systemTemp.createTemp('__sorted_folder_test__');
|
|
repoPath = tempDir.path + p.separator;
|
|
|
|
SharedPreferences.setMockInitialValues({});
|
|
config = NotesFolderConfig('', await SharedPreferences.getInstance());
|
|
fileStorage = await FileStorage.fake(repoPath);
|
|
|
|
folder = NotesFolderFS.root(config, fileStorage);
|
|
|
|
var random = Random();
|
|
for (var i = 0; i < 5; i++) {
|
|
var path = p.join(folder.folderPath, "${random.nextInt(1000)}.md");
|
|
var note =
|
|
await NoteStorage.load(File.short(path, repoPath, gitDt), folder);
|
|
note = note.copyWith(
|
|
modified: DateTime(2020, 1, 10 + (i * 2)),
|
|
body: "$i\n",
|
|
);
|
|
await NoteStorage.save(note);
|
|
}
|
|
await folder.loadRecursively();
|
|
});
|
|
|
|
tearDown(() async {
|
|
tempDir.deleteSync(recursive: true);
|
|
});
|
|
|
|
test('Should load the notes sorted', () async {
|
|
var sf = SortedNotesFolder(
|
|
folder: folder,
|
|
sortingMode:
|
|
SortingMode(SortingField.Modified, SortingOrder.Descending),
|
|
);
|
|
expect(sf.hasNotes, true);
|
|
expect(sf.isEmpty, false);
|
|
expect(sf.name.startsWith("__sorted_folder_test__"), true);
|
|
expect(sf.subFolders.length, 0);
|
|
expect(sf.notes.length, 5);
|
|
|
|
expect(sf.notes[0].body, "4\n");
|
|
expect(sf.notes[1].body, "3\n");
|
|
expect(sf.notes[2].body, "2\n");
|
|
expect(sf.notes[3].body, "1\n");
|
|
expect(sf.notes[4].body, "0\n");
|
|
});
|
|
|
|
test('Should on modification remains sorted', () async {
|
|
var sf = SortedNotesFolder(
|
|
folder: folder,
|
|
sortingMode:
|
|
SortingMode(SortingField.Modified, SortingOrder.Descending),
|
|
);
|
|
|
|
var i = sf.notes.indexWhere((n) => n.body == "1\n");
|
|
sf.notes[i] = sf.notes[i].copyWith(modified: DateTime(2020, 2, 1));
|
|
|
|
expect(sf.notes[0].body, "1\n");
|
|
expect(sf.notes[1].body, "4\n");
|
|
expect(sf.notes[2].body, "3\n");
|
|
expect(sf.notes[3].body, "2\n");
|
|
expect(sf.notes[4].body, "0\n");
|
|
});
|
|
|
|
test('Should add new note correctly', () async {
|
|
var sf = SortedNotesFolder(
|
|
folder: folder,
|
|
sortingMode:
|
|
SortingMode(SortingField.Modified, SortingOrder.Descending),
|
|
);
|
|
|
|
var fNew = File.short('new.md', repoPath, gitDt);
|
|
var note = await NoteStorage.load(fNew, folder);
|
|
folder.add(note);
|
|
|
|
note = note.copyWith(
|
|
modified: DateTime(2020, 2, 1),
|
|
body: "new\n",
|
|
);
|
|
await NoteStorage.save(note);
|
|
|
|
expect(sf.notes.length, 6);
|
|
|
|
expect(sf.notes[0].body, "new\n");
|
|
expect(sf.notes[1].body, "4\n");
|
|
expect(sf.notes[2].body, "3\n");
|
|
expect(sf.notes[3].body, "2\n");
|
|
expect(sf.notes[4].body, "1\n");
|
|
expect(sf.notes[5].body, "0\n");
|
|
});
|
|
|
|
test('Should add new note to end works correctly', () async {
|
|
var sf = SortedNotesFolder(
|
|
folder: folder,
|
|
sortingMode:
|
|
SortingMode(SortingField.Modified, SortingOrder.Descending),
|
|
);
|
|
|
|
var fNew = File.short('new.md', repoPath, gitDt);
|
|
var note = await NoteStorage.load(fNew, folder);
|
|
folder.add(note);
|
|
|
|
note = note.copyWith(
|
|
modified: DateTime(2020, 1, 1),
|
|
body: "new\n",
|
|
);
|
|
await NoteStorage.save(note);
|
|
|
|
expect(sf.notes.length, 6);
|
|
|
|
expect(sf.notes[0].body, "4\n");
|
|
expect(sf.notes[1].body, "3\n");
|
|
expect(sf.notes[2].body, "2\n");
|
|
expect(sf.notes[3].body, "1\n");
|
|
expect(sf.notes[4].body, "0\n");
|
|
expect(sf.notes[5].body, "new\n");
|
|
});
|
|
|
|
test('If still sorted while loading the notes', () async {
|
|
var folder = NotesFolderFS.root(config, fileStorage);
|
|
var sf = SortedNotesFolder(
|
|
folder: folder,
|
|
sortingMode:
|
|
SortingMode(SortingField.Modified, SortingOrder.Descending),
|
|
);
|
|
|
|
await folder.loadRecursively();
|
|
|
|
expect(sf.hasNotes, true);
|
|
expect(sf.isEmpty, false);
|
|
expect(sf.name.startsWith("__sorted_folder_test__"), true);
|
|
expect(sf.subFolders.length, 0);
|
|
expect(sf.notes.length, 5);
|
|
|
|
expect(sf.notes[0].body, "4\n");
|
|
expect(sf.notes[1].body, "3\n");
|
|
expect(sf.notes[2].body, "2\n");
|
|
expect(sf.notes[3].body, "1\n");
|
|
expect(sf.notes[4].body, "0\n");
|
|
});
|
|
}, skip: true);
|
|
}
|