From bbc7f244fa8e59f6299ba3f8c86d21b3a0772c1a Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 11:17:55 +0000 Subject: [PATCH] fix(data): never let the compile_options probe block opening an index Any failure of the informational PRAGMA compile_options probe is logged and gives an empty list; before, anything but a SqliteException was cached by the lazy value and rethrown by every later open. Tests now cover the decode-tier fields end to end: the Library's preview-only chip and its plural tooltip, server cards and frames carrying previewOnlyCount and the preview size over the API. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Sqlite/SqliteDatabase.cs | 53 +++++++++++-------- .../Library/RemoteCardsTests.cs | 22 ++++++++ .../Sqlite/SqliteBindingTests.cs | 9 ++++ .../Titano.Server.Tests/Api/SequenceTests.cs | 37 +++++++++++++ 4 files changed, 100 insertions(+), 21 deletions(-) diff --git a/src/Titano.Data/Sqlite/SqliteDatabase.cs b/src/Titano.Data/Sqlite/SqliteDatabase.cs index f9b56736..c7286df0 100644 --- a/src/Titano.Data/Sqlite/SqliteDatabase.cs +++ b/src/Titano.Data/Sqlite/SqliteDatabase.cs @@ -37,7 +37,7 @@ public sealed class SqliteDatabase : IDatabase internal const int BusyTimeoutMs = 5000; private const string LogCategory = "Data.Sqlite"; private static readonly TimeSpan WriteLockTimeout = TimeSpan.FromSeconds(60); - private static readonly Lazy> ProbedCompileOptions = new(ProbeCompileOptions); + private static readonly Lazy> ProbedCompileOptions = new(() => ProbeCompileOptions(ReadCompileOptions)); private readonly string _filename; private readonly bool _memory; @@ -275,34 +275,45 @@ public sealed class SqliteDatabase : IDatabase throw new NotSupportedException($"SQLite {version / 1_000_000}.{version / 1000 % 1000}.{version % 1000} is too old; 3.31 or newer is required."); } - private static IReadOnlyList ProbeCompileOptions() + /// + /// The options returns, logged; any failure is logged and gives none, since the probe is + /// informational and must never stop an index from opening (a failure escaping the lazy value would be rethrown by + /// every later open). + /// + internal static IReadOnlyList ProbeCompileOptions(Func> read) { - var options = new List(); try { - using var connection = SqliteConnection.Open(":memory:", isMemory: true, BusyTimeoutMs); - var statement = connection.Rent("PRAGMA compile_options"); - try - { - var columns = statement.ReadColumns(); - int rc; - while ((rc = statement.Step()) == SQLITE_ROW) - { - if (statement.ReadRow(columns).GetStringOrNull(0) is { Length: > 0 } option) options.Add(option); - } - if (rc != SQLITE_DONE) throw connection.Error(rc); - } - finally - { - connection.Return(statement); - } + var options = read(); + Log.Info(LogCategory, $"SQLite {LibraryVersion}; compile options: {string.Join(' ', options)}"); + return options; } - catch (SqliteException ex) + catch (Exception ex) when (ex is not OutOfMemoryException) { Log.Warning(LogCategory, "PRAGMA compile_options could not be read", ex); return []; } - Log.Info(LogCategory, $"SQLite {LibraryVersion}; compile options: {string.Join(' ', options)}"); + } + + private static List ReadCompileOptions() + { + var options = new List(); + using var connection = SqliteConnection.Open(":memory:", isMemory: true, BusyTimeoutMs); + var statement = connection.Rent("PRAGMA compile_options"); + try + { + var columns = statement.ReadColumns(); + int rc; + while ((rc = statement.Step()) == SQLITE_ROW) + { + if (statement.ReadRow(columns).GetStringOrNull(0) is { Length: > 0 } option) options.Add(option); + } + if (rc != SQLITE_DONE) throw connection.Error(rc); + } + finally + { + connection.Return(statement); + } return options; } diff --git a/tests/Titano.App.Tests/Library/RemoteCardsTests.cs b/tests/Titano.App.Tests/Library/RemoteCardsTests.cs index 64202d20..9fe96252 100644 --- a/tests/Titano.App.Tests/Library/RemoteCardsTests.cs +++ b/tests/Titano.App.Tests/Library/RemoteCardsTests.cs @@ -1,9 +1,12 @@ +using Titano.App.Infrastructure; using Titano.App.Library; +using Titano.Core.Localization; using Titano.Core.Model; using Titano.Data.Repositories; using Titano.Data.Sync; using Titano.Protocol; using Titano.Testing; +using Titano.Ui.Controls; namespace Titano.App.Tests.Library; @@ -70,6 +73,25 @@ public sealed class RemoteCardsTests Assert.Equal(SyncState.RemoteOnly, card.SyncState); } + [Test] + public void PreviewOnlyFramesShowAChipWithTheirCount() + { + // A server sequence of CR3s it reads only through their previews (BRIEF ยง8.2 tier 2). + var link = Link("Aurora"); + var dto = new SequenceDto(link.RemoteId, "Aurora", "Aurora", "2026/2026-02-20 Nikon Z 8", "NIKON CORPORATION", "NIKON Z 8", null, + new DateTime(2026, 2, 20, 21, 0, 0), new DateTime(2026, 2, 21, 1, 8, 0), 1_860, 8.0, 50_000_000_000, (int)SequenceStatus.AnalyzedLight, 0, null, + [], 0, null, 1, 1, 0, null, DateTimeOffset.UtcNow, PreviewOnlyCount: 1_860); + var card = RemoteCards.Build([new SyncEntry(null, link, SyncStatus.RemoteOnly)], new Dictionary { [dto.Id] = dto }).Single(); + Assert.Equal(1_860, card.PreviewOnlyCount); + Assert.Equal(1_860, RemoteCards.ToSequence(dto).PreviewOnlyCount); + + var chip = SequenceItem.BuildStatuses(card, analysing: false).Single(s => s.Kind == StatusKind.PreviewOnly); + Assert.Equal(Formatting.Count(1_860), chip.Text); + Assert.Equal(Loc.Plural("Tooltips.Library.Status.PreviewOnly", 1_860), chip.ToolTip); + Assert.NotEqual(Loc.Plural("Tooltips.Library.Status.PreviewOnly", 1), chip.ToolTip, "plural forms"); + Assert.False(SequenceItem.BuildStatuses(card with { PreviewOnlyCount = 0 }, analysing: false).Any(s => s.Kind == StatusKind.PreviewOnly), "no chip without them"); + } + [Test] public void CardsSortAmongThePagesAndFollowTheFilter() { diff --git a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs index 4fe19f6e..ef68a17a 100644 --- a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs +++ b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs @@ -28,6 +28,15 @@ public sealed class SqliteBindingTests(TestContext context) : DatabaseTest(conte if (!OperatingSystem.IsWindows()) Assert.Contains(options, o => o.StartsWith("THREADSAFE=", StringComparison.Ordinal)); } + [Test] + public void AProbeThatFailsInAnyWayGivesNoOptionsInsteadOfBlockingTheIndex() + { + // A build that returns no statement for the pragma, a column it cannot read, a SQLite error: none escapes. + Assert.Empty(SqliteDatabase.ProbeCompileOptions(() => throw new ArgumentException("No statement in 'PRAGMA compile_options'."))); + Assert.Empty(SqliteDatabase.ProbeCompileOptions(() => throw new InvalidOperationException("Column type 5 is not text."))); + Assert.SequenceEqual(["THREADSAFE=1"], SqliteDatabase.ProbeCompileOptions(() => ["THREADSAFE=1"])); + } + [Test] public async Task ConnectionsUseTheBriefPragmas() { diff --git a/tests/Titano.Server.Tests/Api/SequenceTests.cs b/tests/Titano.Server.Tests/Api/SequenceTests.cs index b160c278..ca46a573 100644 --- a/tests/Titano.Server.Tests/Api/SequenceTests.cs +++ b/tests/Titano.Server.Tests/Api/SequenceTests.cs @@ -228,4 +228,41 @@ public sealed class SequenceTests(TestContext context) using var analysis = await editor.GetAsync($"/api/v1/sequences/{id}/analysis"); Assert.Equal("analysis", (await analysis.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details); } + + [Test] + public async Task CardsAndFramesCarryTheDecodeTierFields() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var first = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 50), "t0.CR3", sessionKey: "t"); + await Uploads.UploadAsync(editor, Uploads.Photo(1001, 51), "t1.CR3", sessionKey: "t"); + var db = server.Database; + // The follow-up jobs of the upload (thumbnails, proxies, light analysis) rewrite the rows: let them finish first. + for (var waited = 0; waited < 600; waited++) + { + if ((await db.Jobs.ListAsync(Titano.Core.Model.JobState.Queued)).Items.Count == 0 + && (await db.Jobs.ListAsync(Titano.Core.Model.JobState.Running)).Items.Count == 0) break; + await Task.Delay(50, context.Cancellation); + } + // What a light analysis on the server records for CR3s it reads only through their previews. + var frames = await db.Frames.ListBySequenceAsync(first.SequenceId); + await db.Frames.UpdateTiersAsync([.. frames.Select(f => new Titano.Data.Repositories.FrameTierFields(f.Id, Titano.Core.Model.FrameFlags.PreviewOnly, 1620, 1080))]); + var sequence = (await db.Sequences.GetAsync(first.SequenceId))!; + await db.Sequences.UpsertAsync(sequence with { PreviewOnlyCount = 2 }); + + // The Library cards read the count (the chip), the remote editor the preview size (the output cap). + using var list = await editor.GetAsync("/api/v1/sequences"); + await list.ExpectAsync(HttpStatusCode.OK); + Assert.Contains("\"previewOnlyCount\":2", await list.Content.ReadAsStringAsync()); + using var listed = await editor.GetAsync("/api/v1/sequences"); + Assert.Equal(2, (await listed.ReadAsync(ProtocolJson.Default.PageSequenceDto)).Items.Single(s => s.Id == first.SequenceId).PreviewOnlyCount); + using var page = await editor.GetAsync($"/api/v1/sequences/{first.SequenceId}/frames"); + await page.ExpectAsync(HttpStatusCode.OK); + var json = await page.Content.ReadAsStringAsync(); + Assert.Contains("\"previewWidth\":1620", json); + Assert.Contains("\"previewHeight\":1080", json); + using var typed = await editor.GetAsync($"/api/v1/sequences/{first.SequenceId}/frames"); + var dtos = (await typed.ReadAsync(ProtocolJson.Default.PageFrameDto)).Items; + Assert.True(dtos.All(f => (f.PreviewWidth, f.PreviewHeight, f.Flags) == (1620, 1080, (long)Titano.Core.Model.FrameFlags.PreviewOnly)), "frame DTOs"); + } }