- The desktop's rescans run for the whole session and each one targets the archive at Settings › Archive's folder (as the Import and Archive screens do), so an archive created on the first run, chosen in Settings, relinked or imported with settings is rescanned from the next tick on, and Rescan every always reaches a live timer. The folder of the start is reopened (relink, interrupted renames) as soon as its archive.json is there, so an unplugged drive or a folder without an archive no longer stops polling for the session. - ArchiveService.RefreshSettingsAsync reads archive.json and registers the archive again when its name, templates or proxy size changed elsewhere (a server's admin panel, another PC); null when the file cannot be read, is newer or belongs to another archive. Desktop rescans and the server's polling call it first, so imports, uploads and the admin panel follow the shared settings without a restart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
144 lines
6.6 KiB
C#
144 lines
6.6 KiB
C#
using System.IO;
|
||
using Titano.App.Infrastructure;
|
||
using Titano.App.Tests.Uploader;
|
||
using Titano.Core.Model;
|
||
using Titano.Data;
|
||
using Titano.Data.Archiving;
|
||
using Titano.Testing;
|
||
|
||
namespace Titano.App.Tests.Infrastructure;
|
||
|
||
public sealed class AppServicesTests
|
||
{
|
||
/// <summary>Dialogs that answer like a user who cancels everything.</summary>
|
||
private sealed class NoDialogs : IAppDialogs
|
||
{
|
||
public string? PickFolder(string title, string? initialFolder = null) => null;
|
||
public string? PickJsonFile(string title) => null;
|
||
public bool Confirm(ConfirmRequest request) => false;
|
||
public bool SignIn(string server, string? username, Func<string, string, Task<string?>> signIn) => false;
|
||
}
|
||
|
||
[Test]
|
||
[WindowsOnly]
|
||
public void ExitStopsPollingAndTheEngineWhileTheUiThreadWaits()
|
||
{
|
||
var environment = AppEnvironment.ForCapture();
|
||
try
|
||
{
|
||
SingleThreadedContext.Run(async () =>
|
||
{
|
||
var root = Path.Combine(environment.DataDirectory, "archive");
|
||
await using (var setup = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(environment.DataDirectory, "setup.db"))))
|
||
await new ArchiveService(setup).CreateAsync(root, "Exit");
|
||
var services = new AppServices(environment, new AppSettings { Language = "en", LastArchivePath = root }, () => { }, new NoDialogs());
|
||
await services.OpenAsync(environment.DatabasePath);
|
||
// The archive was reopened and its first rescan queued: polling now waits for the next one.
|
||
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
|
||
while ((await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items.Count == 0)
|
||
{
|
||
Assert.True(DateTime.UtcNow < until, "no rescan queued");
|
||
await Task.Delay(50);
|
||
}
|
||
|
||
// App.OnExit blocks the UI thread until the services stop: nothing they await may continue on it.
|
||
Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped");
|
||
});
|
||
}
|
||
finally
|
||
{
|
||
environment.DeleteCaptureData();
|
||
}
|
||
}
|
||
|
||
/// <summary>BRIEF §7: polling follows the archive folder, also one chosen after the start (first run, Settings, relink).</summary>
|
||
[Test]
|
||
[WindowsOnly]
|
||
public void RescansFollowTheArchiveFolderChosenAfterTheStart()
|
||
{
|
||
var environment = AppEnvironment.ForCapture();
|
||
try
|
||
{
|
||
SingleThreadedContext.Run(async () =>
|
||
{
|
||
// The first run: the index opens before any archive folder is set.
|
||
var settings = new AppSettings { Language = "en" };
|
||
var services = new AppServices(environment, settings, () => { }, new NoDialogs());
|
||
try
|
||
{
|
||
await services.OpenAsync(environment.DatabasePath);
|
||
Assert.Null(await services.RescanAsync());
|
||
// The rescans run all the same: Settings › Archive › Rescan every reaches them.
|
||
settings.ScanIntervalMinutes = 30;
|
||
services.SaveSettings();
|
||
Assert.Equal<TimeSpan?>(TimeSpan.FromMinutes(30), services.PollingPeriod);
|
||
|
||
// The first-run dialog creates an archive: the next rescan is of it.
|
||
await services.ArchiveSetup.CreateAsync(Path.Combine(environment.DataDirectory, "first"));
|
||
Assert.Equal("first", (await services.RescanAsync())?.Name);
|
||
|
||
// Settings › Archive moves to another archive: the next rescan is of that one, no longer of the first.
|
||
var second = Path.Combine(environment.DataDirectory, "second");
|
||
await new ArchiveService(services.Database!).CreateAsync(second, "Second");
|
||
settings.LastArchivePath = second;
|
||
services.SaveSettings();
|
||
var rescanned = Assert.NotNull(await services.RescanAsync());
|
||
Assert.Equal("Second", rescanned.Name);
|
||
Assert.Contains((await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items, j => j.TargetIds.Contains(rescanned.Id));
|
||
}
|
||
finally
|
||
{
|
||
Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped");
|
||
}
|
||
});
|
||
}
|
||
finally
|
||
{
|
||
environment.DeleteCaptureData();
|
||
}
|
||
}
|
||
|
||
/// <summary>BRIEF §6.2: a restore staged in Settings › Advanced recovers an index that no longer opens.</summary>
|
||
[Test]
|
||
[WindowsOnly]
|
||
public void AStagedRestoreRecoversAnIndexThatNoLongerOpens()
|
||
{
|
||
var environment = AppEnvironment.ForCapture();
|
||
try
|
||
{
|
||
SingleThreadedContext.Run(async () =>
|
||
{
|
||
await using (var db = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(environment.DatabasePath)))
|
||
{
|
||
await db.Archives.UpsertAsync(new Archive { Id = Guid.CreateVersion7(), Name = "Kept", RootPath = @"C:\Kept", Created = DateTimeOffset.UtcNow });
|
||
var backup = await db.BackupAsync(environment.BackupDirectory, DatabaseBackups.Keep);
|
||
await DatabaseBackups.StageRestoreAsync(backup, environment.DatabasePath);
|
||
}
|
||
// The index is damaged beyond opening ("file is not a database").
|
||
File.Delete(environment.DatabasePath + "-wal");
|
||
File.Delete(environment.DatabasePath + "-shm");
|
||
await File.WriteAllBytesAsync(environment.DatabasePath, [.. Enumerable.Repeat((byte)0x5A, 8192)]);
|
||
|
||
var services = new AppServices(environment, new AppSettings { Language = "en" }, () => { }, new NoDialogs());
|
||
try
|
||
{
|
||
await services.OpenAsync(environment.DatabasePath);
|
||
|
||
Assert.Null(services.OpenError);
|
||
Assert.Equal(StagedRestoreResult.Restored, services.StagedRestore);
|
||
Assert.SequenceEqual(["Kept"], (await services.Database!.Archives.ListAsync()).Select(a => a.Name));
|
||
Assert.False(DatabaseBackups.HasStagedRestore(environment.DatabasePath));
|
||
}
|
||
finally
|
||
{
|
||
Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped");
|
||
}
|
||
});
|
||
}
|
||
finally
|
||
{
|
||
environment.DeleteCaptureData();
|
||
}
|
||
}
|
||
}
|