Files
Chronos/tests/Chronos.Server.Tests/Hosting/ArchiveStructureStartupTests.cs
T
Alby96andClaude Opus 5.5 a5facf0b5b feat(archive): lay out the archive's whole folder tree wherever its root is
The owner asked (2026-11-06) that whatever path is given as the archive
root, on the server in Docker or on Windows, Chronos creates inside it
every folder the archive keeps. Until now only .chronos was made up
front; locks, journal, staging, trash and its photos and videos areas
appeared on first use, and an archive copied or made by an older build
could lack them.

ArchiveStructure.Ensure (Core) creates the missing folders of BRIEF §7
idempotently, never touching existing folders or files, and reports why
it could not (drive or share unavailable, permission denied, read-only,
a file in the way) as one localized sentence. ArchiveService runs it
when it creates an archive (refused with the reason when the tree cannot
be made) and at every open. The server checks it at start-up and on
each dashboard read: it logs a warning, the dashboard shows an "Archive
incomplete" chip with the reason, and /health reports archive
"incomplete". The desktop runs it when an archive is created, adopted or
opened at start, and a folder typed in Settings that is not an archive
yet becomes one in place, as Welcome's Create does; a writable archive
whose tree cannot be completed is refused with the reason.

Refs #31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 17:05:57 +01:00

79 lines
3.6 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Microsoft.Extensions.DependencyInjection;
using Chronos.Core.Archiving;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Protocol;
using Chronos.Server.Storage;
using Chronos.Testing;
namespace Chronos.Server.Tests.Hosting;
/// <summary>The server lays out its archive folder whole at start (owner request 2026-11-06) and says when it cannot.</summary>
[Category("server")]
public sealed class ArchiveStructureStartupTests(TestContext context)
{
[Test]
public async Task StartingTheServerLaysOutTheArchiveFolder()
{
await using var server = await TestServer.StartAsync(context);
var paths = new ArchivePaths(server.Options.ArchivePath);
Assert.FileExists(paths.ManifestPath);
Assert.Empty(ArchiveStructure.Missing(paths));
Assert.True(Assert.NotNull(server.Services.GetRequiredService<ServerArchive>().Structure).IsComplete);
var health = Assert.NotNull(await server.Client.GetFromJsonAsync("/health", ProtocolJson.Default.HealthStatus));
Assert.Equal("ok", health.Checks["archive"]);
}
[Test]
public async Task AnArchiveMissingFoldersGetsThemAtStart()
{
var root = Path.Combine(context.CreateTempDirectory("existing"), "archive");
await using (var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation))
await new ArchiveService(db).CreateAsync(root, "Older", cancellationToken: context.Cancellation);
var paths = new ArchivePaths(root);
Directory.Delete(paths.TrashDirectory, recursive: true);
Directory.Delete(paths.StagingDirectory);
var notes = Path.Combine(root, "notes.txt");
File.WriteAllText(notes, "mine");
await using var server = await TestServer.StartAsync(context, o => o.ArchivePath = root);
Assert.Empty(ArchiveStructure.Missing(paths));
Assert.Equal("mine", File.ReadAllText(notes));
}
[Test]
public async Task TheDashboardWarnsWhileAFolderCannotBeCreated()
{
await using var server = await TestServer.StartAsync(context);
var admin = await server.AdminAsync();
var paths = new ArchivePaths(server.Options.ArchivePath);
Directory.Delete(paths.TrashVideosDirectory);
File.WriteAllText(paths.TrashVideosDirectory, "in the way");
var warned = await DashboardAsync(admin);
var problem = Assert.NotNull(warned.ArchiveProblem);
Assert.Equal(("failed", paths.TrashVideosDirectory), (problem.Code, problem.Folder));
var health = Assert.NotNull(await server.Client.GetFromJsonAsync("/health", ProtocolJson.Default.HealthStatus));
Assert.Equal(("ok", "incomplete"), (health.Status, health.Checks["archive"]));
Assert.Equal("in the way", File.ReadAllText(paths.TrashVideosDirectory));
// Once the file is out of the way, the next look at the dashboard makes the folder and the warning goes.
File.Delete(paths.TrashVideosDirectory);
Assert.Null((await DashboardAsync(admin)).ArchiveProblem);
Assert.True(Directory.Exists(paths.TrashVideosDirectory));
health = Assert.NotNull(await server.Client.GetFromJsonAsync("/health", ProtocolJson.Default.HealthStatus));
Assert.Equal("ok", health.Checks["archive"]);
}
private static async Task<DashboardDto> DashboardAsync(HttpClient admin)
{
using var response = await admin.GetAsync("/api/v1/server/dashboard");
await response.ExpectAsync(HttpStatusCode.OK);
return await response.ReadAsync(ProtocolJson.Default.DashboardDto);
}
}