Files
Chronos/tests/Titano.Data.Tests/Archiving/IndexMaintenanceTests.cs
T
Claude 3e146c9f9f feat(settings): make every Settings row real; archive settings, index backups and restore
Settings (UI-01, UI-02, UI-03, UI-09, RF-4):
- New Import, Analysis and Formats pages, in the order of Settings.html. Import holds
  the defaults of new cards (session rules, subfolders, verification, duplicates,
  after-import actions); Analysis the deep-analysis width cap, night and star-field
  thresholds and deep analysis after import or scan; Formats the decode tier of every
  format on this PC with a notice and Store install link per missing codec extension.
- Archive gains thumbnail size and rescan interval; its templates, proxy size and
  thumbnail size are the open archive's own (archive.json) and new archives' defaults.
- Rendering quality, parallelism and background priority now drive the jobs: quality
  maps to preparation size, read-ahead and default flow quality; parallelism sets the
  engine's workers live and caps per-job threads; background priority raises the UI
  thread while Titano is in front.
- Advanced gains Restore (daily backups, dry-run summary, destructive confirmation,
  staged for the next start) and Reset all.
- The naming preview's sample title is a catalog string.

Engine (ENG-03, ENG-04, ENG-06):
- ArchiveService.GetSettingsAsync/UpdateSettingsAsync rewrite archive.json atomically
  under a lease, then the index row; the server endpoint and the desktop use them, and
  the desktop creates archives with Settings › Archive. ThumbnailPolicy in archive.json
  sizes the thumbnails.
- DatabaseBackups: daily VACUUM INTO copies kept x3 on the desktop and on a server on
  SQLite (DatabaseBackupService), staged restore checked and preceded by a fresh copy.
- Maintenance vacuums the SQLite index when 10 % or more is free (dry run reports it).
- JobEngine.SetWorkers; ProcessingOptions.Tune applies host tuning per job.

Archive setup toasts say why in a localized sentence (RF-1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 10:06:34 +00:00

81 lines
4.2 KiB
C#

using System.Globalization;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
using Titano.Testing;
namespace Titano.Data.Tests.Archiving;
/// <summary>Maintenance vacuums the SQLite index (BRIEF §12.5 "SQLite vacuum"); a dry run only reports the reclaimable space.</summary>
[Category("archive")]
public sealed class IndexMaintenanceTests(TestContext context)
{
[Test]
public async Task ADryRunReportsTheFreePagesAndARunGivesThemBack()
{
await using var fx = await ArchiveFixture.CreateAsync(context);
await FreeSpaceAsync(fx.Db, fx.Ct);
var before = (await fx.Db.SpaceAsync(fx.Ct))!;
Assert.True(before.ReclaimableBytes >= before.FileBytes * 0.3, $"free pages made ({before.ReclaimableBytes} of {before.FileBytes})");
var dry = await fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: true, 0, _ => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((before.ReclaimableBytes, 0L), (dry.IndexReclaimable, dry.IndexReclaimed));
Assert.Equal(before.ReclaimableBytes, (await fx.Db.SpaceAsync(fx.Ct))!.ReclaimableBytes);
var run = await fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: false, 0, _ => ValueTask.CompletedTask, fx.Ct);
Assert.Greater(run.IndexReclaimed, 0L);
var after = (await fx.Db.SpaceAsync(fx.Ct))!;
Assert.Equal(0L, after.ReclaimableBytes);
Assert.Less(after.FileBytes, before.FileBytes);
// The index still answers after the rewrite.
Assert.NotNull(await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct));
}
[Test]
public async Task AFewFreePagesAreNotWorthARewrite()
{
await using var fx = await ArchiveFixture.CreateAsync(context);
var space = (await fx.Db.SpaceAsync(fx.Ct))!;
Assert.True(space.ReclaimableBytes < space.FileBytes * ArchiveMaintenance.IndexVacuumRatio, "a fresh index has almost no free pages");
var run = await fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: false, 0, _ => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((space.ReclaimableBytes, 0L), (run.IndexReclaimable, run.IndexReclaimed));
}
[Test]
public async Task TheMaintenanceJobLogsTheVacuum()
{
await using var fx = await ArchiveFixture.CreateAsync(context);
await FreeSpaceAsync(fx.Db, fx.Ct);
var engine = await fx.StartEngineAsync();
var dry = await engine.EnqueueAsync(ArchiveJobs.Maintenance(fx.Archive, dryRun: true), cancellationToken: fx.Ct);
var dryDone = await fx.WaitForAsync(dry.Id, JobState.Done);
Assert.True(HasLine(dryDone.LogTail, "Archive.Log.IndexReclaimable"), dryDone.LogTail ?? "no log");
Assert.False(HasLine(dryDone.LogTail, "Archive.Log.IndexVacuumed"), "a dry run rewrites nothing");
var run = await engine.EnqueueAsync(ArchiveJobs.Maintenance(fx.Archive), cancellationToken: fx.Ct);
var runDone = await fx.WaitForAsync(run.Id, JobState.Done);
Assert.Equal(5L, runDone.ProgressTotal);
Assert.True(HasLine(runDone.LogTail, "Archive.Log.IndexVacuumed"), runDone.LogTail ?? "no log");
Assert.Equal(0L, (await fx.Db.SpaceAsync(fx.Ct))!.ReclaimableBytes);
}
/// <summary>A log line made from the one-argument sentence <paramref name="key"/>, whatever its size argument.</summary>
private static bool HasLine(string? log, string key)
{
var parts = Loc.L(key, "\0").Split('\0');
return (log ?? string.Empty).Split('\n').Any(l => l.Contains(parts[0], StringComparison.Ordinal) && l.TrimEnd().EndsWith(parts[1].TrimEnd(), StringComparison.Ordinal));
}
/// <summary>Writes about 2 MB of rows and deletes them: SQLite keeps the pages as free pages until a vacuum.</summary>
internal static async Task FreeSpaceAsync(TitanoDatabase db, CancellationToken cancellationToken)
{
var filler = new string('x', 4000);
for (var i = 0; i < 500; i++)
await db.Settings.SetJsonAsync(SettingScope.App, null, "filler." + i.ToString(CultureInfo.InvariantCulture), "\"" + filler + "\"", cancellationToken);
for (var i = 0; i < 500; i++) await db.Settings.DeleteAsync(SettingScope.App, null, "filler." + i.ToString(CultureInfo.InvariantCulture), cancellationToken);
}
}