merge: rc.14 parcel layout
This commit is contained in:
commit
e97df55840
24 files changed
+605
-21
No files matched your search
@@ -0,0 +1,180 @@
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Principal;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Archiving;
|
||||
|
||||
[Category("archive")]
|
||||
public sealed class ArchiveStructureTests(TestContext context)
|
||||
{
|
||||
private ArchivePaths NewArchive() => new(context.CreateTempDirectory("structure"));
|
||||
|
||||
[Test]
|
||||
public void AnEmptyFolderGetsTheWholeLayout()
|
||||
{
|
||||
var archive = NewArchive();
|
||||
|
||||
var report = ArchiveStructure.Ensure(archive);
|
||||
|
||||
Assert.True(report.IsComplete);
|
||||
Assert.Null(report.Message);
|
||||
foreach (var folder in ArchiveStructure.Folders(archive)) Assert.True(Directory.Exists(folder), folder);
|
||||
Assert.SequenceEqual(ArchiveStructure.Folders(archive).Skip(1), report.Created);
|
||||
Assert.Empty(ArchiveStructure.Missing(archive));
|
||||
string[] expected = [".chronos", ".chronos/locks", ".chronos/journal", ".chronos/staging", ".chronos/trash", ".chronos/trash/photos", ".chronos/trash/videos"];
|
||||
Assert.SequenceEqual(expected, report.Created.Select(archive.ToRelative));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AMissingRootIsCreatedOnlyForANewArchive()
|
||||
{
|
||||
var parent = context.CreateTempDirectory("parent");
|
||||
var archive = new ArchivePaths(Path.Combine(parent, "Archive"));
|
||||
|
||||
var refused = ArchiveStructure.Ensure(archive);
|
||||
Assert.Equal(ArchiveStructureProblem.Unavailable, refused.Problem);
|
||||
Assert.Equal(archive.Root, refused.Folder);
|
||||
Assert.False(Directory.Exists(archive.Root), "an archive that is not there is never made up");
|
||||
|
||||
var created = ArchiveStructure.Ensure(archive, createRoot: true);
|
||||
Assert.True(created.IsComplete);
|
||||
Assert.Equal(archive.Root, created.Created[0]);
|
||||
Assert.Empty(ArchiveStructure.Missing(archive));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnExistingLayoutAndItsFilesAreLeftAsTheyAre()
|
||||
{
|
||||
var archive = NewArchive();
|
||||
Assert.True(ArchiveStructure.Ensure(archive).IsComplete);
|
||||
// Files where an archive keeps them, and the user's own: none may change, nor their times.
|
||||
string[] files =
|
||||
[
|
||||
archive.ManifestPath,
|
||||
Path.Combine(archive.TrashPhotosDirectory, "entry.json"),
|
||||
Path.Combine(archive.StagingDirectory, "copy.tmp"),
|
||||
Path.Combine(archive.Root, "2026", "Harbour", "originals", "IMG_0001.JPG"),
|
||||
Path.Combine(archive.Root, "notes.txt"),
|
||||
];
|
||||
var written = new DateTime(2026, 1, 2, 3, 4, 5, DateTimeKind.Utc);
|
||||
for (var i = 0; i < files.Length; i++)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(files[i])!);
|
||||
File.WriteAllBytes(files[i], [(byte)i, 1, 2, 3]);
|
||||
File.SetLastWriteTimeUtc(files[i], written);
|
||||
}
|
||||
var folders = Directory.GetDirectories(archive.Root, "*", SearchOption.AllDirectories).Order(StringComparer.Ordinal).ToList();
|
||||
|
||||
var again = ArchiveStructure.Ensure(archive);
|
||||
|
||||
Assert.True(again.IsComplete);
|
||||
Assert.Empty(again.Created);
|
||||
for (var i = 0; i < files.Length; i++)
|
||||
{
|
||||
Assert.BytesEqual([(byte)i, 1, 2, 3], File.ReadAllBytes(files[i]));
|
||||
Assert.Equal(written, File.GetLastWriteTimeUtc(files[i]));
|
||||
}
|
||||
Assert.SequenceEqual(folders, Directory.GetDirectories(archive.Root, "*", SearchOption.AllDirectories).Order(StringComparer.Ordinal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OnlyTheMissingFoldersAreCreated()
|
||||
{
|
||||
var archive = NewArchive();
|
||||
Directory.CreateDirectory(archive.LocksDirectory);
|
||||
File.WriteAllText(Path.Combine(archive.LocksDirectory, "lease"), "held");
|
||||
|
||||
var report = ArchiveStructure.Ensure(archive);
|
||||
|
||||
Assert.True(report.IsComplete);
|
||||
Assert.False(report.Created.Contains(archive.MetaDirectory) || report.Created.Contains(archive.LocksDirectory));
|
||||
Assert.True(report.Created.Contains(archive.TrashVideosDirectory));
|
||||
Assert.Equal("held", File.ReadAllText(Path.Combine(archive.LocksDirectory, "lease")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFileWhereAFolderBelongsIsReportedNotReplaced()
|
||||
{
|
||||
var archive = NewArchive();
|
||||
Directory.CreateDirectory(archive.MetaDirectory);
|
||||
File.WriteAllText(archive.TrashDirectory, "mine");
|
||||
|
||||
var report = ArchiveStructure.Ensure(archive);
|
||||
|
||||
Assert.Equal(ArchiveStructureProblem.Failed, report.Problem);
|
||||
Assert.Equal(archive.TrashDirectory, report.Folder);
|
||||
Assert.Equal("mine", File.ReadAllText(archive.TrashDirectory));
|
||||
Assert.True(Directory.Exists(archive.StagingDirectory), "the folders before it are made");
|
||||
Assert.Equal(Loc.L("Archive.Structure.Failed", archive.TrashDirectory), report.Message);
|
||||
Assert.Equal(report.Message, new ArchiveStructureException(report).Message);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EachFailureSaysWhy()
|
||||
{
|
||||
var archive = NewArchive();
|
||||
(Exception Thrown, ArchiveStructureProblem Expected, string Key)[] cases =
|
||||
[
|
||||
(new UnauthorizedAccessException("denied"), ArchiveStructureProblem.Denied, "Archive.Structure.Denied"),
|
||||
(new IOException("write protected", unchecked((int)0x80070013)), ArchiveStructureProblem.ReadOnly, "Archive.Structure.ReadOnly"),
|
||||
(new IOException("not ready", unchecked((int)0x80070015)), ArchiveStructureProblem.Unavailable, "Archive.Structure.Unavailable"),
|
||||
(new DirectoryNotFoundException("gone"), ArchiveStructureProblem.Unavailable, "Archive.Structure.Unavailable"),
|
||||
(new IOException("disk full", unchecked((int)0x80070070)), ArchiveStructureProblem.Failed, "Archive.Structure.Failed"),
|
||||
];
|
||||
foreach (var (thrown, expected, key) in cases)
|
||||
{
|
||||
var report = ArchiveStructure.Ensure(archive, createDirectory: _ => throw thrown);
|
||||
Assert.Equal(expected, report.Problem);
|
||||
Assert.Equal(archive.MetaDirectory, report.Folder);
|
||||
Assert.Equal(thrown.Message, report.Detail);
|
||||
Assert.Empty(report.Created);
|
||||
Assert.Equal(Loc.L(key, archive.MetaDirectory), report.Message);
|
||||
}
|
||||
Assert.False(Directory.Exists(archive.MetaDirectory));
|
||||
if (!OperatingSystem.IsWindows())
|
||||
Assert.Equal(ArchiveStructureProblem.ReadOnly, ArchiveStructure.Classify(new IOException("read-only file system", 30)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFolderChronosMayNotWriteReportsTheError()
|
||||
{
|
||||
var archive = NewArchive();
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
var info = new DirectoryInfo(archive.Root);
|
||||
var me = WindowsIdentity.GetCurrent().User!;
|
||||
var deny = new FileSystemAccessRule(me, FileSystemRights.CreateDirectories | FileSystemRights.CreateFiles, AccessControlType.Deny);
|
||||
var security = info.GetAccessControl();
|
||||
security.AddAccessRule(deny);
|
||||
info.SetAccessControl(security);
|
||||
try
|
||||
{
|
||||
var report = ArchiveStructure.Ensure(archive);
|
||||
Assert.Equal(ArchiveStructureProblem.Denied, report.Problem);
|
||||
Assert.Equal(archive.MetaDirectory, report.Folder);
|
||||
Assert.False(Directory.Exists(archive.MetaDirectory));
|
||||
}
|
||||
finally
|
||||
{
|
||||
security.RemoveAccessRule(deny);
|
||||
info.SetAccessControl(security);
|
||||
}
|
||||
return;
|
||||
}
|
||||
File.SetUnixFileMode(archive.Root, UnixFileMode.UserRead | UnixFileMode.UserExecute);
|
||||
try
|
||||
{
|
||||
var report = ArchiveStructure.Ensure(archive);
|
||||
// An account that ignores permissions (root in a CI container) writes anyway; the cases above cover the mapping.
|
||||
if (report.IsComplete) Assert.Inconclusive("this account writes into read-only folders");
|
||||
Assert.Equal(ArchiveStructureProblem.Denied, report.Problem);
|
||||
Assert.Equal(archive.MetaDirectory, report.Folder);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.SetUnixFileMode(archive.Root, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Archiving;
|
||||
|
||||
/// <summary>Every archive the service creates, adopts or opens holds its whole layout (BRIEF §7, owner request 2026-11-06).</summary>
|
||||
[Category("archive")]
|
||||
public sealed class ArchiveStructureServiceTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task ANewArchiveGetsItsWholeLayout()
|
||||
{
|
||||
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation);
|
||||
var service = new ArchiveService(db);
|
||||
var root = Path.Combine(context.CreateTempDirectory("new"), "Chronos", "Archive");
|
||||
|
||||
var archive = await service.AdoptAsync(root, "Fresh", context.Cancellation);
|
||||
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
Assert.FileExists(paths.ManifestPath);
|
||||
Assert.Empty(ArchiveStructure.Missing(paths));
|
||||
Assert.True(Directory.Exists(paths.TrashPhotosDirectory) && Directory.Exists(paths.TrashVideosDirectory));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OpeningAnArchiveMakesTheFoldersItLacksAndTouchesNothingElse()
|
||||
{
|
||||
await using var fx = await ArchiveFixture.CreateAsync(context);
|
||||
var paths = fx.Paths;
|
||||
var kept = Path.Combine(paths.StagingDirectory, "copy.tmp");
|
||||
File.WriteAllText(kept, "staged");
|
||||
var manifest = File.ReadAllBytes(paths.ManifestPath);
|
||||
// An archive an older Chronos or a copy left without its trash, journal and locks.
|
||||
Directory.Delete(paths.TrashDirectory, recursive: true);
|
||||
Directory.Delete(paths.JournalDirectory);
|
||||
Directory.Delete(paths.LocksDirectory, recursive: true);
|
||||
|
||||
await fx.Service.OpenAsync(paths.Root, fx.Ct);
|
||||
|
||||
Assert.Empty(ArchiveStructure.Missing(paths));
|
||||
Assert.Equal("staged", File.ReadAllText(kept));
|
||||
Assert.BytesEqual(manifest, File.ReadAllBytes(paths.ManifestPath));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnArchiveThatCannotBeLaidOutIsNotCreated()
|
||||
{
|
||||
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation);
|
||||
var service = new ArchiveService(db);
|
||||
var root = context.CreateTempDirectory("blocked");
|
||||
// A file where the archive's metadata folder belongs: it stays, and the archive is refused with the reason.
|
||||
var meta = new ArchivePaths(root).MetaDirectory;
|
||||
File.WriteAllText(meta, "not a folder");
|
||||
|
||||
var error = await Assert.ThrowsAsync<ArchiveStructureException>(() => service.CreateAsync(root, "Blocked", cancellationToken: context.Cancellation));
|
||||
|
||||
Assert.Equal(ArchiveStructureProblem.Failed, error.Report.Problem);
|
||||
Assert.Equal(meta, error.Report.Folder);
|
||||
Assert.Equal(error.Report.Message, error.Message);
|
||||
Assert.Equal("not a folder", File.ReadAllText(meta));
|
||||
Assert.Empty(await db.Archives.ListAsync(context.Cancellation));
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex
|
||||
Assert.SequenceEqual([frames[1].Id, frames[3].Id], plan.Frames.Select(f => f.Id));
|
||||
Assert.Equal(frames[1].Size + frames[3].Size, plan.Bytes);
|
||||
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
|
||||
Assert.False(Directory.Exists(FrameTrash.Directory(fx.Paths)), "a dry run moves nothing");
|
||||
Assert.False(Directory.EnumerateFileSystemEntries(FrameTrash.Directory(fx.Paths)).Any(), "a dry run moves nothing");
|
||||
foreach (var frame in frames) Assert.FileExists(fx.Paths.FromRelative(frame.RelativePath));
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,11 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
return sequences;
|
||||
}
|
||||
|
||||
/// <summary>What the trash holds besides its photos and videos areas, which every archive has (empty here).</summary>
|
||||
private static IEnumerable<string> TrashContents(ArchivePaths paths) =>
|
||||
Directory.EnumerateFileSystemEntries(paths.TrashDirectory, "*", SearchOption.AllDirectories)
|
||||
.Where(e => e != paths.TrashPhotosDirectory && e != paths.TrashVideosDirectory);
|
||||
|
||||
private static (int Files, long Bytes) OnDisk(string folder)
|
||||
{
|
||||
var files = new DirectoryInfo(folder).EnumerateFiles("*", SearchOption.AllDirectories).ToList();
|
||||
@@ -109,7 +114,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
Assert.Equal(6, plan.Frames);
|
||||
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
|
||||
Assert.True(Directory.Exists(folder), "a dry run moves nothing");
|
||||
Assert.False(Directory.Exists(SequenceTrash.TrashDirectory(fx.Paths)));
|
||||
Assert.Empty(TrashContents(fx.Paths));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -166,7 +171,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
|
||||
Assert.Equal(render.OutputPath, (await fx.Db.Renders.ListBySequenceAsync(victim.Id)).Single().OutputPath);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
Assert.Empty(Directory.EnumerateFileSystemEntries(SequenceTrash.TrashDirectory(fx.Paths)));
|
||||
Assert.Empty(TrashContents(fx.Paths));
|
||||
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
|
||||
|
||||
// Deleted again: the trash takes it again under the same name.
|
||||
@@ -360,7 +365,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
|
||||
Assert.True(Directory.Exists(folder), "the folder is back where it was");
|
||||
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
|
||||
Assert.Empty(Directory.EnumerateFileSystemEntries(SequenceTrash.TrashDirectory(fx.Paths)));
|
||||
Assert.Empty(TrashContents(fx.Paths));
|
||||
await HealSequencesAsync(fx, "DELETE");
|
||||
Assert.Count(1, await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user