Files
Chronos/tests/Chronos.Data.Tests/Workspaces/WorkspaceHomesTests.cs
T
Alby96andClaude Opus 5.5 4d697a4255 feat: stable server id and per-home folder layout (owner review 5 P3.1)
The desktop keyed every server by the address it was reached at, so one
NAS reached by name and by IP, or after an address change, looked like two
servers, and all servers shared one workspace and one cache. Each server
now announces a stable id, and the desktop's folders are sorted into one
home per server, so that every server can have its own archive of
downloaded originals (or5 answers Q1).

- The server makes a Guid v7 once, keeps it as the app setting server.id
  (so it survives restarts and restores, on SQLite and Postgres) and
  announces it as ServerInfo.ServerId. Older clients ignore the field, and
  older servers read as null.
- ServerIdentity keys a server as id:<N> when it announces an id, and by
  its address otherwise. Address keys are kept as aliases.
- HomeFolders names the homes: local, servers/<id> or
  servers/addr-<hash16>. The hash equals the 1.0 proxy cache folder name.
  It also names the per-server originals archive root.
- WorkspaceHomes splits the workspace into one format-1 store per home
  under a format-2 parent manifest, which 1.0 refuses. A 1.0 workspace
  stays one store until it is sorted.
- HomeLayoutMigration plans as a dry run and applies through a journal:
  - only same-folder renames, each checked by inventory;
  - a clash keeps the copy saved last and sends the other to the trash;
  - it resumes after a crash at any step;
  - it skips leased projects;
  - it defers an offline workspace or one being moved, while the cache
    part still runs.
- RekeyAsync moves an alias home to the id home: whole folders, or
  project-by-project merges. It also rewrites working states under their
  lease, and moves sync_links (the newer row wins a clash).
- ProxyCache, the job root resolver, WorkspaceMover and WorkspaceCleaner
  understand the split layout. The new layout is opt-in through
  SyncOptions.ServersDirectory, so the app sees no change until it is
  wired.

Refs #66

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:15:11 +01:00

173 lines
9.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.Core.IO;
using Chronos.Core.Localization;
using Chronos.Core.Project;
using Chronos.Data.Sync;
using Chronos.Data.Tests.Homes;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Workspaces;
/// <summary>The workspace split into one store per home (owner review 5, #66).</summary>
[Category("workspace")]
public sealed class WorkspaceHomesTests(TestContext context)
{
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
[Test]
public void ANewWorkspaceIsSplitAndEachHomeGetsItsStore()
{
var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace");
var homes = new WorkspaceHomes(root, new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]));
Assert.True(homes.IsSplit);
Assert.True(WorkspaceHomes.IsSplitRoot(root));
Assert.Equal((WorkspaceHomes.ParentFormatVersion, WorkspaceHomes.HomesLayout), (homes.Manifest.FormatVersion, homes.Manifest.Layout));
Assert.Empty(homes.Homes());
// A 1.0 Chronos (store format 1) refuses the parent: it never shows the workspace empty.
Assert.Throws<InvalidDataException>(() => new WorkspaceStore(root));
var local = homes.StoreFor(HomeFixtures.Local());
Assert.Equal(Path.Combine(homes.Root, "local"), local.Root);
var server = homes.StoreFor(HomeFixtures.Server(HomeFixtures.Nas));
Assert.Equal(Path.Combine(homes.Root, "servers", Id.ToString("N")), server.Root);
Assert.Same(server, homes.StoreFor(HomeFixtures.Server(ServerIdentity.IdKey(Id))));
var record = Assert.NotNull(ServerHomeRecord.Read(server.Root));
Assert.Equal((ServerIdentity.IdKey(Id), (Guid?)Id), (record.Key, record.Id));
var other = homes.StoreFor(HomeFixtures.Server(HomeFixtures.Other));
Assert.Equal(Path.Combine(homes.Root, "servers", HomeFolders.ServerFolder(HomeFixtures.Other)), other.Root);
Assert.Equal(HomeFixtures.Other, Assert.NotNull(ServerHomeRecord.Read(other.Root)).Address);
Assert.NotEqual(local.Id, server.Id);
var described = Assert.NotNull(homes.Describe(HomeFixtures.Nas, "Studio NAS", HomeFixtures.Nas, "AA:BB", DateTimeOffset.UtcNow));
Assert.Equal(("Studio NAS", HomeFixtures.Nas, "AA:BB"), (described.Name, described.Address, described.TlsFingerprint));
Assert.Equal(record.FirstSeen, described.FirstSeen);
var listed = homes.Homes();
Assert.Equal(3, listed.Count);
Assert.True(listed[0].IsLocal);
Assert.Contains(listed, h => h.ServerKey == ServerIdentity.IdKey(Id) && h.Server?.Name == "Studio NAS");
}
[Test]
public void ProjectsAreFoundInEveryHomeAndTheTrashIsListedPerHome()
{
var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace");
var homes = new WorkspaceHomes(root);
var a = HomeFixtures.Project(homes.StoreFor(HomeFixtures.Local()).Root, HomeFixtures.Local());
var serverHome = HomeFixtures.Server(HomeFixtures.Nas);
var b = HomeFixtures.Project(homes.StoreFor(serverHome).Root, serverHome);
Assert.Equal(homes.LocalRoot, Path.GetDirectoryName(Assert.NotNull(homes.Find(a)).Folder));
Assert.Equal(homes.ServerRoot(HomeFixtures.Nas), Path.GetDirectoryName(Assert.NotNull(homes.Find(b)).Folder));
Assert.Null(homes.Find(Guid.CreateVersion7()));
Assert.Equal(2, homes.List().Count);
Assert.Equal(2, homes.ListBundles().Count);
HomeFixtures.TrashEntry(homes.ServerRoot(HomeFixtures.Nas), b, serverHome);
var trash = homes.Trash();
Assert.Equal(2, trash.Count);
Assert.Equal(1, trash.Sum(t => t.Entries.Count));
}
[Test]
public void A10WorkspaceStaysOneStoreUntilItIsSorted()
{
var root = HomeFixtures.LegacyWorkspace(context.CreateTempDirectory("ws"));
var a = HomeFixtures.Project(root, HomeFixtures.Local());
var homes = new WorkspaceHomes(root);
Assert.False(homes.IsSplit);
Assert.Equal(1, homes.Manifest.FormatVersion);
Assert.Equal(homes.Root, homes.StoreFor(HomeFixtures.Server(HomeFixtures.Nas)).Root);
Assert.Equal(homes.Root, homes.Local.Root);
Assert.True(Assert.NotNull(homes.Find(a)).Exists);
Assert.True(homes.Homes().Single().IsLegacy);
Assert.False(Directory.Exists(homes.LocalRoot), "nothing is made beside the 1.0 projects");
// A folder with projects and no manifest is a 1.0 workspace too, never a new split one.
var bare = Path.Combine(context.CreateTempDirectory("bare"), "workspace");
HomeFixtures.Project(bare, HomeFixtures.Local());
Assert.False(new WorkspaceHomes(bare).IsSplit);
}
[Test]
public void AProjectNotSortedYetIsListedWhereItIs()
{
var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace");
_ = new WorkspaceHomes(root);
var left = HomeFixtures.Project(root, HomeFixtures.Local());
var homes = new WorkspaceHomes(root);
Assert.True(homes.IsSplit);
Assert.Equal(homes.Root, Path.GetDirectoryName(Assert.NotNull(homes.Find(left)).Folder));
Assert.True(homes.Homes()[0].IsLegacy);
Assert.Equal(homes.LocalRoot, homes.Local.Root);
}
[Test]
public void AJobQueuedBeforeTheSortingFindsItsProjectInItsHome()
{
var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace");
var homes = new WorkspaceHomes(root);
var home = HomeFixtures.Server(HomeFixtures.Nas);
var id = HomeFixtures.Project(homes.ServerRoot(HomeFixtures.Nas), home);
_ = homes.StoreFor(home);
// Queued with the 1.0 folder (the parent): found in the server's home.
Assert.Equal(homes.ServerRoot(HomeFixtures.Nas), WorkspaceJobRoots.Resolve(root, id, null, "Errors.Workspace.NotCheckedOut"));
// Queued with the home of the address, which was re-keyed to the id since.
var rekeyed = Path.Combine(root, "servers", Id.ToString("N"));
Directory.Move(homes.ServerRoot(HomeFixtures.Nas), rekeyed);
Assert.Equal(rekeyed, WorkspaceJobRoots.Resolve(Path.Combine(root, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas)), id, null, "Errors.Workspace.NotCheckedOut"));
// With the parent in use now (Settings › Workspace) and an older folder named.
Assert.Equal(rekeyed, WorkspaceJobRoots.Resolve(Path.Combine(context.CreateTempDirectory("gone"), "workspace"), id, () => root, "Errors.Workspace.NotCheckedOut"));
// While the parent is being moved nothing runs.
File.WriteAllText(Path.Combine(root, WorkspaceMover.JournalFile), "{}");
var moving = Assert.Throws<InvalidOperationException>(() => WorkspaceJobRoots.Resolve(rekeyed, id, null, "Errors.Workspace.NotCheckedOut"));
Assert.Equal(Loc.L("Errors.Workspace.Moving"), moving.Message);
File.Delete(Path.Combine(root, WorkspaceMover.JournalFile));
Assert.Throws<InvalidOperationException>(() => WorkspaceJobRoots.Resolve(root, Guid.CreateVersion7(), null, "Errors.Workspace.NotCheckedOut"));
}
[Test]
public void SameServerGoesThroughTheAliases()
{
var identity = new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]);
var options = new WorkspaceJobOptions(new Chronos.Data.Processing.ProcessingOptions(), () => false);
Assert.True(options.IsSameServer("https://NAS.local:8443", HomeFixtures.Nas));
Assert.False(options.IsSameServer(ServerIdentity.IdKey(Id), HomeFixtures.Nas));
var aliased = options with { SameServer = identity.Same };
Assert.True(aliased.IsSameServer(ServerIdentity.IdKey(Id), HomeFixtures.Nas));
Assert.False(aliased.IsSameServer(ServerIdentity.IdKey(Id), HomeFixtures.Other));
}
[Test]
public async Task MovingASplitWorkspaceMovesEveryHome()
{
foreach (var sameVolume in new[] { true, false })
{
var parent = context.CreateTempDirectory("move");
var from = Path.Combine(parent, "workspace");
var homes = new WorkspaceHomes(from);
var serverHome = HomeFixtures.Server(HomeFixtures.Nas);
var ids = new[]
{
HomeFixtures.Project(homes.StoreFor(HomeFixtures.Local()).Root, HomeFixtures.Local()),
HomeFixtures.Project(homes.StoreFor(serverHome).Root, serverHome),
HomeFixtures.Project(homes.StoreFor(HomeFixtures.Server(HomeFixtures.Other)).Root, HomeFixtures.Server(HomeFixtures.Other)),
};
var before = HomeFixtures.Manifest(from);
var to = Path.Combine(parent, "moved");
var mover = new WorkspaceMover(from, to) { SameVolume = sameVolume };
var survey = mover.Survey();
Assert.Equal(3, survey.Projects.Count);
Assert.SequenceEqual(new[] { "local", "servers", WorkspaceStore.ManifestFile }, survey.Entries);
var lines = new List<string>();
await mover.MoveAsync(survey.Fingerprint, log: lines.Add, cancellationToken: context.Cancellation);
Assert.False(Directory.Exists(from), "the old folder went");
var moved = new WorkspaceHomes(to);
Assert.True(moved.IsSplit);
Assert.Equal(3, moved.List().Count);
foreach (var id in ids) Assert.NotNull(moved.Find(id));
Assert.SequenceEqual(before, HomeFixtures.Manifest(to));
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", mover.To, 3, survey.Files), lines);
}
}
}