using System.IO; using Titano.App.Library; using Titano.Core.Localization; using Titano.Data; using Titano.Data.Repositories; using Titano.Testing; namespace Titano.App.Tests.Library; /// Saved views of the Library (BRIEF ยง12.2): a name for filters and sort, kept as app-scope settings rows. public sealed class SavedViewTests(TestContext context) { private static readonly LibraryFilter Night = new() { Text = " night ", Year = 2026, Camera = "ILCE-7M4", Status = StatusFilter.NotAnalysed, Tag = "stars", Origin = OriginFilter.Local, Sort = SequenceSort.Size, }; [Test] public void AViewKeepsFiltersAndSortAndRecognisesThem() { var view = SavedView.Of(Guid.CreateVersion7(), " Night, 4K, not analysed ", Night); Assert.Equal("Night, 4K, not analysed", view.Name); Assert.Equal("night", view.Text); Assert.Equal(Night with { Text = "night" }, view.ToFilter()); Assert.True(view.Matches(Night)); Assert.True(view.Matches(Night with { Text = "NIGHT" }), "the search compares trimmed and ignoring case"); Assert.False(view.Matches(Night with { Sort = SequenceSort.Newest }), "the sort is part of the view"); Assert.False(view.Matches(Night with { Origin = OriginFilter.Any })); Assert.False(view.Matches(Night with { Tag = null })); var blank = SavedView.Of(Guid.CreateVersion7(), "All", new LibraryFilter { Text = " " }); Assert.Null(blank.Text); Assert.True(blank.Matches(new LibraryFilter())); } [Test] public void NamesAreShortUniqueAndPrintable() { string[] others = ["Night", "Holy grail"]; Assert.Equal(Loc.L("Library.Views.Error.Empty"), SavedView.Validate(" ", others)); Assert.Equal(Loc.L("Library.Views.Error.Long", SavedView.MaxName), SavedView.Validate(new string('a', SavedView.MaxName + 1), others)); Assert.Equal(Loc.L("Library.Views.Error.Long", SavedView.MaxName), SavedView.Validate("Night\tsky", others)); Assert.Equal(Loc.L("Library.Views.Error.Taken"), SavedView.Validate(" night ", others)); Assert.Null(SavedView.Validate("Night, 4K", others)); Assert.Null(SavedView.Validate(new string('a', SavedView.MaxName), others)); } [Test] public async Task ViewsRoundTripThroughTheSettingsTable() { await using var db = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "titano.db")), context.Cancellation); var store = new SavedViewStore(db.Settings); Assert.Empty(await store.ListAsync()); var night = SavedView.Of(Guid.CreateVersion7(), "Night", Night); var grail = SavedView.Of(Guid.CreateVersion7(), "holy grail", new LibraryFilter { Tag = "holy grail", Sort = SequenceSort.Oldest }); await store.SaveAsync(night); await store.SaveAsync(grail); // Other app settings and a damaged row live next to the views and are not views. await db.Settings.SetJsonAsync(SettingScope.App, null, "library.layout", "2"); await db.Settings.SetJsonAsync(SettingScope.App, null, "library.view.broken", "\"not a view\""); var listed = await store.ListAsync(); Assert.SequenceEqual(["holy grail", "Night"], listed.Select(v => v.Name), "by name, ignoring case"); Assert.Equal(night, listed[1]); Assert.Equal(grail, listed[0]); await store.SaveAsync(night with { Name = "Night sky" }); Assert.SequenceEqual(["holy grail", "Night sky"], (await store.ListAsync()).Select(v => v.Name), "a rename keeps one row"); Assert.True(await store.DeleteAsync(grail.Id)); Assert.False(await store.DeleteAsync(grail.Id)); Assert.Equal(night.Id, (await store.ListAsync()).Single().Id); } }