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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-15 13:15:11 +01:00
1 parent 58ed737310
commit 4d697a4255
27 files changed
+3082 -38

No files matched your search

@@ -0,0 +1,90 @@
using System.Security.Cryptography;
using System.Text;
using Chronos.Core.IO;
using Chronos.Testing;
namespace Chronos.Core.Tests.Foundation;
/// <summary>The folders of the homes (owner review 5, #66): this PC's, a server's by id, a server's by address hash.</summary>
public sealed class HomeFoldersTests(TestContext context)
{
private static readonly Guid ServerId = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
[Test]
public void AnIdKeyNamesItsFolderByTheId()
{
var key = HomeFolders.IdKey(ServerId);
Assert.Equal("id:0192a4c17b3e7cc08a512f9d0e6b1a01", key);
Assert.Equal("0192a4c17b3e7cc08a512f9d0e6b1a01", HomeFolders.ServerFolder(key));
Assert.Equal(ServerId, HomeFolders.IdOfKey(key));
Assert.Equal(ServerId, HomeFolders.IdOfKey("ID:0192A4C17B3E7CC08A512F9D0E6B1A01"));
Assert.Equal(ServerId, HomeFolders.IdOfFolder(HomeFolders.ServerFolder(key)));
Assert.Throws<ArgumentException>(() => HomeFolders.IdKey(Guid.Empty));
}
[Test]
public void AnAddressKeyNamesItsFolderAsTheProxyCacheOf10Did()
{
const string key = "https://nas.local:8443";
// ProxyCache 1.0: Convert.ToHexStringLower(SHA256(UTF-8 key))[..16].
var hash = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(key)))[..16];
Assert.Equal(hash, HomeFolders.AddressHash(key));
Assert.Equal("addr-" + hash, HomeFolders.ServerFolder(key));
Assert.Null(HomeFolders.IdOfKey(key));
Assert.True(HomeFolders.IsAddressFolder("addr-" + hash));
Assert.True(HomeFolders.IsServerFolder("addr-" + hash));
Assert.Null(HomeFolders.IdOfFolder("addr-" + hash));
// "id:" with no id after it is an address key like any other.
Assert.Equal("addr-" + HomeFolders.AddressHash("id:nothing"), HomeFolders.ServerFolder("id:nothing"));
}
[Test]
public void OnlyAHomeFolderNameIsAServerHome()
{
Assert.True(HomeFolders.IsServerFolder(ServerId.ToString("N")));
foreach (var name in new[] { null, "", "local", "servers", ".trash", ServerId.ToString("N").ToUpperInvariant(), ServerId.ToString("D"), "addr-", "addr-0123",
"addr-0123456789ABCDEF", "addr-0123456789abcdefa", "xaddr-0123456789abcdef", Guid.Empty.ToString("N") })
Assert.False(HomeFolders.IsServerFolder(name), name ?? "null");
}
[Test]
public void HomePathsLieUnderTheirRoot()
{
var root = context.CreateTempDirectory("homes");
Assert.Equal(Path.Combine(root, "local"), HomeFolders.LocalPath(root));
Assert.Equal(Path.Combine(root, "servers", ServerId.ToString("N")), HomeFolders.ServerPath(root, HomeFolders.IdKey(ServerId)));
Assert.Equal(Path.Combine(root, "servers", "addr-" + HomeFolders.AddressHash("https://x")), HomeFolders.ServerPath(root, "https://x"));
}
[Test]
public void TheOriginalsOfAServerGetAnArchiveBesideThisPcs()
{
var parent = context.CreateTempDirectory("originals");
var archive = Path.Combine(parent, "ChronosArchive");
Directory.CreateDirectory(archive);
Assert.Equal(Path.Combine(parent, "ChronosArchive - Studio NAS"), HomeFolders.OriginalsArchiveRoot(archive, "Studio NAS", Path.Combine(parent, "fallback")));
// Taken: " (2)"; a name Windows refuses is made safe.
Directory.CreateDirectory(Path.Combine(parent, "ChronosArchive - Studio NAS"));
Assert.Equal(Path.Combine(parent, "ChronosArchive - Studio NAS (2)"), HomeFolders.OriginalsArchiveRoot(archive + Path.DirectorySeparatorChar, "Studio NAS", "unused"));
Assert.Equal(Path.Combine(parent, "ChronosArchive - A B C"), HomeFolders.OriginalsArchiveRoot(archive, "A:B/C", "unused"));
Assert.Equal(Path.Combine(parent, "ChronosArchive - Taken (3)"),
HomeFolders.OriginalsArchiveRoot(archive, "Taken", "unused", path => !path.EndsWith("(3)", StringComparison.Ordinal)));
// No archive of this PC's: beside where its first one would go.
var fallback = Path.Combine(parent, "Pictures", "ChronosArchive");
Assert.Equal(Path.Combine(parent, "Pictures", "ChronosArchive - NAS"), HomeFolders.OriginalsArchiveRoot(null, "NAS", fallback));
}
[Test]
public void ASafeNameIsAFolderNameEveryDriveTakes()
{
Assert.Equal("Studio NAS", HomeFolders.SafeName(" Studio \t NAS. "));
Assert.Equal("a b", HomeFolders.SafeName("a<>:\"|?*b"));
Assert.Equal("CON_", HomeFolders.SafeName("con".ToUpperInvariant()));
Assert.Equal("nul.box_", HomeFolders.SafeName("nul.box"));
Assert.Equal("Server", HomeFolders.SafeName("..."));
Assert.Equal("Server", HomeFolders.SafeName(null));
Assert.Equal(HomeFolders.MaxNameLength, HomeFolders.SafeName(new string('x', 200)).Length);
}
}
@@ -0,0 +1,122 @@
using System.Security.Cryptography;
using System.Text;
using Chronos.Core.Archiving;
using Chronos.Core.Project;
using Chronos.Data.Workspaces;
namespace Chronos.Data.Tests.Homes;
/// <summary>Workspaces and caches laid out as 1.0 left them, for the sorting into homes (owner review 5, #66).</summary>
internal static class HomeFixtures
{
public const string Nas = "https://nas.local:8443";
public const string NasByIp = "https://192.168.1.5:8443";
public const string Other = "https://other.example:8443";
/// <summary>A working state of <paramref name="home"/> written into <c>&lt;root&gt;/&lt;id&gt;</c> with some media, versions and a render.</summary>
public static Guid Project(string root, BundleHome home, string name = "Main", DateTimeOffset? saved = null, Guid? id = null, int proxies = 3)
{
var projectId = id ?? Guid.CreateVersion7();
var folder = Path.Combine(root, projectId.ToString("N"));
Directory.CreateDirectory(folder);
var archive = home.IsArchive
? new BundleArchive(home.ArchiveId ?? Guid.CreateVersion7(), "This PC", @"D:\ChronosArchive")
: new BundleArchive(Data.Projects.ServerProjectHome.ArchiveIdOf(home.Server!), "Studio NAS", null);
var bundle = new ProjectBundle(archive, new BundleSequence(home.SequenceId, "2026/2026-11-15 Harbour", "Harbour", proxies), new ProjectSettings(),
saved ?? DateTimeOffset.UtcNow)
{
Workspace = new BundleWorkspace(projectId, name, true, home, null, new BundleMedia(BundleMedia.Proxies, 1)),
};
bundle.Save(Path.Combine(folder, WorkspaceProject.BundleFile));
var media = Path.Combine(folder, WorkspaceProject.MediaFolder, WorkspaceProject.ProxiesFolder);
Directory.CreateDirectory(media);
for (var i = 0; i < proxies; i++) File.WriteAllBytes(Path.Combine(media, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(projectId, i, 500 + i));
Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.VersionsFolder));
File.WriteAllText(Path.Combine(folder, WorkspaceProject.VersionsFolder, "1-" + Guid.CreateVersion7().ToString("N") + ".json"), "{}");
Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.ExportsFolder));
File.WriteAllBytes(Path.Combine(folder, WorkspaceProject.ExportsFolder, "render.mp4"), Bytes(projectId, 99, 2048));
return projectId;
}
public static BundleHome Local() => new(BundleHome.ArchiveKind, Guid.CreateVersion7(), Guid.CreateVersion7());
public static BundleHome Server(string key) => new(BundleHome.ServerKind, Guid.CreateVersion7(), null, key);
/// <summary>A 1.0 workspace: its manifest (format 1) and the folder.</summary>
public static string LegacyWorkspace(string parent)
{
var root = Path.Combine(parent, "workspace");
_ = new WorkspaceStore(root);
return root;
}
/// <summary>A trash entry of <paramref name="projectId"/> holding a working state, as a removal leaves it.</summary>
public static string TrashEntry(string root, Guid projectId, BundleHome home)
{
var trash = Path.Combine(root, WorkspaceTrash.FolderName);
var staging = Path.Combine(trash, "staging");
Directory.CreateDirectory(staging);
Project(staging, home, id: projectId, proxies: 1);
var entry = Path.Combine(trash, projectId.ToString("N") + "-638000000000000000");
Directory.Move(Path.Combine(staging, projectId.ToString("N")), entry);
Directory.Delete(staging);
return entry;
}
/// <summary>The 1.0 proxy cache of <paramref name="key"/>'s sequence <paramref name="remoteId"/>.</summary>
public static string LegacyProxies(string cache, string key, Guid remoteId, int frames = 2)
{
var folder = Data.Sync.ProxyCache.LegacyFolder(Path.Combine(cache, "proxies"), key, remoteId);
Directory.CreateDirectory(folder);
File.WriteAllText(Path.Combine(folder, "remote.json"), "{}");
for (var i = 0; i < frames; i++) File.WriteAllBytes(Path.Combine(folder, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(remoteId, i, 300));
return folder;
}
/// <summary>A live lease on a project folder, as another Chronos working on the same workspace holds it.</summary>
public static LeaseFile Hold(string projectFolder) =>
LeaseFile.TryAcquire(projectFolder, ".lock", "workspace@other", TimeSpan.FromMinutes(5)) ?? throw new InvalidOperationException("The lease is held.");
/// <summary>
/// The SHA-256 of every file under <paramref name="root"/> by its name relative to the project or entry it belongs to,
/// whatever home folder holds it: the same before and after a sorting that only renames (the test-only proof).
/// </summary>
public static SortedDictionary<string, string> Manifest(string root)
{
var manifest = new SortedDictionary<string, string>(StringComparer.Ordinal);
if (!Directory.Exists(root)) return manifest;
foreach (var file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories))
{
var relative = Path.GetRelativePath(root, file).Replace('\\', '/');
var name = Path.GetFileName(file);
if (name is WorkspaceStore.ManifestFile or "server.json" && !relative.Contains('/', StringComparison.Ordinal)) continue;
if (name is WorkspaceStore.ManifestFile or "server.json" && IsHomeLevel(relative)) continue;
manifest[Strip(relative)] = Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(file))) + "|" + new FileInfo(file).Length;
}
return manifest;
}
/// <summary>A path without the home folders it lies in (<c>local/</c>, <c>servers/&lt;folder&gt;/</c>).</summary>
private static string Strip(string relative)
{
if (relative.StartsWith("local/", StringComparison.Ordinal)) return relative["local/".Length..];
if (relative.StartsWith("servers/", StringComparison.Ordinal))
{
var rest = relative["servers/".Length..];
var slash = rest.IndexOf('/', StringComparison.Ordinal);
return slash < 0 ? rest : rest[(slash + 1)..];
}
return relative;
}
private static bool IsHomeLevel(string relative) =>
relative.Split('/') is { Length: 2 } parts && parts[0] == "local" || relative.Split('/') is { Length: 3 } more && more[0] == "servers";
private static byte[] Bytes(Guid seed, int index, int length)
{
var bytes = new byte[length];
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(seed.ToString("N") + index));
for (var i = 0; i < length; i++) bytes[i] = hash[i % hash.Length];
return bytes;
}
}
@@ -0,0 +1,297 @@
using Chronos.Core.IO;
using Chronos.Core.Project;
using Chronos.Data.Homes;
using Chronos.Data.Sync;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Homes;
/// <summary>The one-time sorting of a 1.0 workspace and cache into homes (owner review 5, #66).</summary>
[Category("homes")]
public sealed class HomeLayoutMigrationTests(TestContext context)
{
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
/// <summary>A 1.0 data folder: a workspace with local, server and damaged projects and a trash, and a cache with proxies, render-node files and a staged import.</summary>
private sealed class Legacy
{
public required string Data { get; init; }
public required string Workspace { get; init; }
public required string Cache { get; init; }
public required Guid Local { get; init; }
public required Guid OnNas { get; init; }
public required Guid OnOther { get; init; }
public required Guid Damaged { get; init; }
public required Guid NasRemote { get; init; }
public HomeLayoutMigration Migration(ServerIdentity? identity = null, Action<string, string>? cut = null) =>
new(new HomeLayoutOptions
{
DataDirectory = Data,
WorkspaceRoot = Workspace,
CacheRoot = Cache,
Identity = identity ?? ServerIdentity.None,
CurrentServer = HomeFixtures.Nas,
})
{ Cut = cut };
}
private Legacy Make()
{
var data = context.CreateTempDirectory("data");
var workspace = HomeFixtures.LegacyWorkspace(data);
var local = HomeFixtures.Project(workspace, HomeFixtures.Local());
var onNas = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Nas));
var onOther = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Other));
var damaged = Guid.CreateVersion7();
Directory.CreateDirectory(Path.Combine(workspace, damaged.ToString("N")));
File.WriteAllText(Path.Combine(workspace, damaged.ToString("N"), WorkspaceProject.BundleFile), "{ not json");
HomeFixtures.TrashEntry(workspace, onNas, HomeFixtures.Server(HomeFixtures.Nas));
File.WriteAllText(Path.Combine(workspace, WorkspaceTrash.FolderName, "loose.txt"), "kept");
var cache = Path.Combine(data, "cache");
var nasRemote = Guid.CreateVersion7();
HomeFixtures.LegacyProxies(cache, HomeFixtures.Nas, nasRemote);
HomeFixtures.LegacyProxies(cache, HomeFixtures.Other, Guid.CreateVersion7());
Directory.CreateDirectory(Path.Combine(cache, "render-node", "job1"));
File.WriteAllText(Path.Combine(cache, "render-node", "job1", "frame.jpg"), "rn");
Directory.CreateDirectory(Path.Combine(cache, "server-import"));
File.WriteAllText(Path.Combine(cache, "server-import", "card.part"), "import");
return new Legacy
{
Data = data,
Workspace = workspace,
Cache = cache,
Local = local,
OnNas = onNas,
OnOther = onOther,
Damaged = damaged,
NasRemote = nasRemote,
};
}
/// <summary>Every file and folder under <paramref name="root"/> with its size and time: what a dry run must leave as it was.</summary>
private static List<string> Snapshot(string root) =>
[.. new DirectoryInfo(root).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
.Select(e => Path.GetRelativePath(root, e.FullName) + "|" + (e is FileInfo f ? f.Length + "|" + f.LastWriteTimeUtc.Ticks : "dir"))
.Order(StringComparer.Ordinal)];
private static string Home(string workspace, string key) => Path.Combine(workspace, "servers", HomeFolders.ServerFolder(key));
private static void AssertSorted(Legacy legacy, string nasFolder)
{
var ws = legacy.Workspace;
Assert.True(WorkspaceHomes.IsSplitRoot(ws));
Assert.FileExists(Path.Combine(ws, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile));
Assert.FileExists(Path.Combine(ws, "local", legacy.Damaged.ToString("N"), WorkspaceProject.BundleFile));
Assert.FileExists(Path.Combine(ws, "servers", nasFolder, legacy.OnNas.ToString("N"), WorkspaceProject.BundleFile));
Assert.FileExists(Path.Combine(Home(ws, HomeFixtures.Other), legacy.OnOther.ToString("N"), WorkspaceProject.BundleFile));
Assert.FileExists(Path.Combine(ws, "local", WorkspaceTrash.FolderName, "loose.txt"));
Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(ws, "servers", nasFolder, WorkspaceTrash.FolderName)));
Assert.False(Directory.EnumerateDirectories(ws).Any(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)), "no project is left in the parent");
Assert.False(Directory.Exists(Path.Combine(ws, WorkspaceTrash.FolderName)), "the parent's emptied trash went");
Assert.Equal("Studio NAS", Assert.NotNull(ServerHomeRecord.Read(Path.Combine(ws, "servers", nasFolder))).Name);
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "proxies", legacy.NasRemote.ToString("N"), "remote.json"));
Assert.True(Directory.Exists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Other), "proxies")));
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "render-node", "job1", "frame.jpg"));
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "import", "card.part"));
Assert.False(Directory.Exists(Path.Combine(legacy.Cache, "render-node")));
Assert.FileMissing(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
var homes = new WorkspaceHomes(ws);
Assert.True(homes.IsSplit);
Assert.Equal(4, homes.List().Count);
}
[Test]
public async Task ThePlanWritesNothing()
{
var legacy = Make();
var before = Snapshot(legacy.Data);
var plan = await legacy.Migration().PlanAsync(context.Cancellation);
Assert.SequenceEqual(before, Snapshot(legacy.Data));
Assert.True(plan.SplitsWorkspace);
Assert.Null(plan.Deferred);
Assert.Equal(4, plan.Projects);
Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Trash));
Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Proxies));
Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.RenderNode));
Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.Import));
var damaged = plan.Entries.Single(e => e.Source.EndsWith(legacy.Damaged.ToString("N"), StringComparison.Ordinal));
Assert.Equal(Path.Combine(legacy.Workspace, "local", legacy.Damaged.ToString("N")), damaged.Target);
Assert.Equal(plan.Fingerprint, (await legacy.Migration().PlanAsync(context.Cancellation)).Fingerprint);
}
[Test]
public async Task SortingMovesEveryHomeAndRenamesOnly()
{
var legacy = Make();
var before = HomeFixtures.Manifest(legacy.Workspace);
var cacheBefore = HomeFixtures.Manifest(legacy.Cache);
var result = await legacy.Migration().ApplyAsync(context.Cancellation);
Assert.Equal((4, 0, false, true), (result.ProjectsMoved, result.InUse, result.Resumed, result.Split));
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
// Every file is there with the same name relative to its project and the same bytes: the sorting only renamed.
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace));
Assert.SequenceEqual(cacheBefore.Values.Order(), HomeFixtures.Manifest(legacy.Cache).Values.Order());
// A second run does nothing.
var again = await legacy.Migration().ApplyAsync(context.Cancellation);
Assert.Equal(HomeLayoutResult.Nothing, again);
Assert.True((await legacy.Migration().PlanAsync(context.Cancellation)).IsEmpty);
}
[Test]
public async Task AServerKnownByIdGetsItsIdHome()
{
var legacy = Make();
await legacy.Migration(new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])])).ApplyAsync(context.Cancellation);
AssertSorted(legacy, Id.ToString("N"));
Assert.Equal(ServerIdentity.IdKey(Id), Assert.NotNull(ServerHomeRecord.Read(Path.Combine(legacy.Workspace, "servers", Id.ToString("N")))).Key);
}
[Test]
public async Task ACrashAtAnyStepIsTakenUpToTheSameEnd()
{
var entries = (await Make().Migration().PlanAsync(context.Cancellation)).Entries.Count;
var cuts = new List<(string Step, int Occurrence)> { ("journal", 1), ("commit", 1) };
for (var i = 1; i <= entries; i++) cuts.AddRange([("rename", i), ("renamed", i)]);
foreach (var (step, occurrence) in cuts)
{
var legacy = Make();
var before = HomeFixtures.Manifest(legacy.Workspace);
var seen = 0;
var crashing = legacy.Migration(cut: (name, _) =>
{
if (name == step && ++seen == occurrence) throw new IOException("cut at " + step);
});
await Assert.ThrowsAsync<IOException>(() => crashing.ApplyAsync(context.Cancellation));
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
var resumed = await legacy.Migration().ApplyAsync(context.Cancellation);
Assert.True(resumed.Resumed, $"{step} #{occurrence}");
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace), $"{step} #{occurrence}");
Assert.Equal(HomeLayoutResult.Nothing, await legacy.Migration().ApplyAsync(context.Cancellation));
}
}
[Test]
public async Task AProjectInUseIsLeftAndMovedAtALaterStart()
{
var legacy = Make();
var held = HomeFixtures.Hold(Path.Combine(legacy.Workspace, legacy.Local.ToString("N")));
HomeLayoutResult result;
using (held)
{
var plan = await legacy.Migration().PlanAsync(context.Cancellation);
Assert.SequenceEqual(new[] { legacy.Local }, plan.InUse);
result = await legacy.Migration().ApplyAsync(context.Cancellation);
Assert.Equal((3, 1, true), (result.ProjectsMoved, result.InUse, result.Split));
Assert.True(WorkspaceHomes.IsSplitRoot(legacy.Workspace));
var homes = new WorkspaceHomes(legacy.Workspace);
Assert.Equal(homes.Root, Path.GetDirectoryName(Assert.NotNull(homes.Find(legacy.Local)).Folder));
Assert.Equal(4, homes.List().Count);
}
result = await legacy.Migration().ApplyAsync(context.Cancellation);
Assert.Equal((1, 0), (result.ProjectsMoved, result.InUse));
Assert.FileExists(Path.Combine(legacy.Workspace, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile));
Assert.Null(new WorkspaceHomes(legacy.Workspace).Homes().FirstOrDefault(h => h.IsLegacy));
}
[Test]
public async Task AnUnfinishedMoveDefersTheWorkspaceWhileTheCacheIsSorted()
{
var legacy = Make();
File.WriteAllText(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile), "{}");
var before = Snapshot(legacy.Workspace);
var result = await legacy.Migration().ApplyAsync(context.Cancellation);
Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Moving, result.Deferred);
Assert.False(result.Split);
Assert.SequenceEqual(before, Snapshot(legacy.Workspace));
Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "proxies", legacy.NasRemote.ToString("N"), "remote.json"));
File.Delete(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile));
await legacy.Migration().ApplyAsync(context.Cancellation);
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
}
[Test]
public async Task AWorkspaceThatWentOfflineWaitsWithItsJournal()
{
var legacy = Make();
var before = HomeFixtures.Manifest(legacy.Workspace);
await Assert.ThrowsAsync<IOException>(() => legacy.Migration(cut: (name, _) =>
{
if (name == "journal") throw new IOException("cut");
}).ApplyAsync(context.Cancellation));
// The drive goes before the next start.
var away = legacy.Workspace + "-unplugged";
Directory.Move(legacy.Workspace, away);
var waiting = await legacy.Migration().ApplyAsync(context.Cancellation);
Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Offline, waiting.Deferred);
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "import", "card.part"));
// Back: the workspace is sorted, the cache left as it is now.
Directory.Move(away, legacy.Workspace);
await legacy.Migration().ApplyAsync(context.Cancellation);
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace));
}
[Test]
public async Task AProjectInBothPlacesKeepsTheCopySavedLastAndTrashesTheOther()
{
var data = context.CreateTempDirectory("data");
var workspace = HomeFixtures.LegacyWorkspace(data);
var id = Guid.CreateVersion7();
var home = HomeFixtures.Local();
HomeFixtures.Project(workspace, home, "newer", DateTimeOffset.UtcNow, id);
HomeFixtures.Project(Path.Combine(workspace, "local"), home, "older", DateTimeOffset.UtcNow.AddDays(-1), id);
var files = Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories).Count(f => Path.GetFileName(f) != WorkspaceStore.ManifestFile);
var result = await new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = workspace }).ApplyAsync(context.Cancellation);
Assert.Equal(1, result.Clashes);
var kept = ProjectBundle.Load(Path.Combine(workspace, "local", id.ToString("N"), WorkspaceProject.BundleFile));
Assert.Equal("newer", kept.Workspace!.Name);
var trashed = Assert.NotNull(Directory.EnumerateDirectories(Path.Combine(workspace, "local", WorkspaceTrash.FolderName)).SingleOrDefault());
Assert.Equal("older", ProjectBundle.Load(Path.Combine(trashed, WorkspaceProject.BundleFile)).Workspace!.Name);
Assert.Equal(files, Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories)
.Count(f => Path.GetFileName(f) is not (WorkspaceStore.ManifestFile or "server.json")), "nothing was deleted");
}
[Test]
public async Task ARenameThatDoesNotCheckStopsWithItsJournal()
{
var legacy = Make();
var tampered = false;
var migration = legacy.Migration(cut: (name, path) =>
{
if (name != "renamed" || tampered || !Directory.Exists(path)) return;
tampered = true;
File.WriteAllText(Path.Combine(path, "stray.txt"), "written meanwhile");
});
await Assert.ThrowsAsync<HomeLayoutException>(() => migration.ApplyAsync(context.Cancellation));
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
// The next start takes it up; the entry found renamed is taken as moved.
await legacy.Migration().ApplyAsync(context.Cancellation);
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
}
[Test]
public async Task AWorkspaceNeverMadeOrAlreadySortedHasNothingToDo()
{
var data = context.CreateTempDirectory("data");
var options = new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = Path.Combine(data, "workspace"), CacheRoot = Path.Combine(data, "cache") };
Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty);
Assert.Equal(HomeLayoutResult.Nothing, await new HomeLayoutMigration(options).ApplyAsync(context.Cancellation));
Assert.False(Directory.Exists(options.WorkspaceRoot));
_ = new WorkspaceHomes(options.WorkspaceRoot);
Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty);
// An empty 1.0 workspace is split by its manifest alone.
var empty = HomeFixtures.LegacyWorkspace(context.CreateTempDirectory("empty"));
var split = await new HomeLayoutMigration(options with { WorkspaceRoot = empty }).ApplyAsync(context.Cancellation);
Assert.True(split.Split);
Assert.True(WorkspaceHomes.IsSplitRoot(empty));
Assert.Equal(WorkspaceStore.FormatVersion, new WorkspaceStore(Path.Combine(empty, "local")).Manifest.FormatVersion);
}
}
@@ -0,0 +1,215 @@
using Chronos.Core.IO;
using Chronos.Core.Project;
using Chronos.Data.Homes;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Data.Tests.Repositories;
using Chronos.Data.Workspaces;
using Chronos.Testing;
namespace Chronos.Data.Tests.Homes;
/// <summary>A server's home moves from its address to its id when it first announces one (owner review 5, #66).</summary>
[Category("homes")]
public sealed class HomeRekeyTests(TestContext context)
{
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
private static string IdFolder => Id.ToString("N");
private (string Data, WorkspaceHomes Homes, string Cache) Make()
{
var data = context.CreateTempDirectory("data");
return (data, new WorkspaceHomes(Path.Combine(data, "workspace")), Path.Combine(data, "cache"));
}
private static HomeLayoutMigration Migration(string data, WorkspaceHomes homes, string cache) =>
new(new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = homes.Root, CacheRoot = cache });
private static string AddressHome(string root, string key) => Path.Combine(root, "servers", HomeFolders.ServerFolder(key));
private static ProjectBundle Bundle(string home, Guid id) => ProjectBundle.Load(Path.Combine(home, id.ToString("N"), WorkspaceProject.BundleFile));
[Test]
public async Task TheHomeOfTheAddressBecomesTheHomeOfTheId()
{
var (data, homes, cache) = Make();
var home = HomeFixtures.Server(HomeFixtures.Nas);
var store = homes.StoreFor(home);
var (a, b) = (HomeFixtures.Project(store.Root, home), HomeFixtures.Project(store.Root, HomeFixtures.Server(HomeFixtures.Nas)));
HomeFixtures.TrashEntry(store.Root, a, home);
var before = HomeFixtures.Manifest(homes.Root);
var (sorted, old) = (Guid.CreateVersion7(), Guid.CreateVersion7());
var cached = Path.Combine(AddressHome(cache, HomeFixtures.Nas), "proxies", sorted.ToString("N"));
Directory.CreateDirectory(cached);
File.WriteAllText(Path.Combine(cached, "remote.json"), "{}");
HomeFixtures.LegacyProxies(cache, HomeFixtures.Nas, old);
var result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
Assert.Equal(new HomeRekeyResult(2, 0, 0, 2, 0, 0, 0), result);
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
Assert.False(Directory.Exists(AddressHome(homes.Root, HomeFixtures.Nas)));
var idKey = ServerIdentity.IdKey(Id);
foreach (var id in new[] { a, b })
{
var bundle = Bundle(idHome, id);
Assert.Equal(idKey, bundle.Workspace!.Home!.Server);
Assert.Equal(ServerProjectHome.ArchiveIdOf(idKey), bundle.Archive.Id);
}
var record = Assert.NotNull(ServerHomeRecord.Read(idHome));
Assert.Equal((idKey, (Guid?)Id, HomeFixtures.Nas), (record.Key, record.Id, record.Address));
Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(idHome, WorkspaceTrash.FolderName)));
// Only the working states were rewritten: every other file is as it was.
var after = HomeFixtures.Manifest(homes.Root);
Assert.SequenceEqual(before.Keys, after.Keys);
Assert.SequenceEqual(before.Where(f => !f.Key.EndsWith(WorkspaceProject.BundleFile, StringComparison.Ordinal)),
after.Where(f => !f.Key.EndsWith(WorkspaceProject.BundleFile, StringComparison.Ordinal)));
Assert.FileExists(Path.Combine(cache, "servers", IdFolder, "proxies", sorted.ToString("N"), "remote.json"));
Assert.FileExists(Path.Combine(cache, "servers", IdFolder, "proxies", old.ToString("N"), "remote.json"));
Assert.False(Directory.Exists(AddressHome(cache, HomeFixtures.Nas)));
// The store now finds the id home by either key.
var identity = new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]);
Assert.Equal(idHome, new WorkspaceHomes(homes.Root, identity).ServerRoot(HomeFixtures.Nas));
// Again: nothing left to do.
Assert.Equal(new HomeRekeyResult(0, 0, 0, 0, 0, 0, 0), await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation));
}
[Test]
public async Task TwoAddressesOfOneServerMergeAndTheOlderCopyGoesToTheTrash()
{
var (data, homes, cache) = Make();
var byName = HomeFixtures.Server(HomeFixtures.Nas);
var byIp = HomeFixtures.Server(HomeFixtures.NasByIp);
var first = homes.StoreFor(byName).Root;
var second = homes.StoreFor(byIp).Root;
var both = Guid.CreateVersion7();
HomeFixtures.Project(first, byName, "older", DateTimeOffset.UtcNow.AddDays(-1), both);
HomeFixtures.Project(second, byIp, "newer", DateTimeOffset.UtcNow, both);
var onlyFirst = HomeFixtures.Project(first, byName);
var onlySecond = HomeFixtures.Project(second, byIp);
HomeFixtures.TrashEntry(first, onlyFirst, byName);
HomeFixtures.TrashEntry(second, onlyFirst, byIp);
var files = Directory.EnumerateFiles(homes.Root, "*", SearchOption.AllDirectories).Count(IsProjectFile);
// Each address had a proxy cache too.
Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.Nas), "proxies", "a"));
Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.NasByIp), "proxies", "a"));
Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.NasByIp), "proxies", "b"));
var result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas, HomeFixtures.NasByIp], context.Cancellation);
Assert.Equal((2, 2, 1, 3), (result.HomesMoved, result.ProjectsMoved, result.ProjectsTrashed, result.BundlesRewritten));
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
Assert.Equal("newer", Bundle(idHome, both).Workspace!.Name);
foreach (var id in new[] { onlyFirst, onlySecond }) Assert.FileExists(Path.Combine(idHome, id.ToString("N"), WorkspaceProject.BundleFile));
var trash = Directory.EnumerateDirectories(Path.Combine(idHome, WorkspaceTrash.FolderName)).ToList();
Assert.Equal(3, trash.Count);
Assert.Contains(trash, t => ProjectBundle.Load(Path.Combine(t, WorkspaceProject.BundleFile)).Workspace!.Name == "older");
// The second address's home held only its manifest and record afterwards: it went. Nothing else was deleted.
Assert.False(Directory.Exists(second));
Assert.Equal(files, Directory.EnumerateFiles(homes.Root, "*", SearchOption.AllDirectories).Count(IsProjectFile));
Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(homes.Root, "servers")));
Assert.True(Directory.Exists(Path.Combine(cache, "servers", IdFolder, "proxies", "b")));
}
[Test]
public async Task AProjectInUseStaysAndKeepsItsAliasWhichStillMatches()
{
var (data, homes, cache) = Make();
var home = HomeFixtures.Server(HomeFixtures.Nas);
var root = homes.StoreFor(home).Root;
var (held, free) = (HomeFixtures.Project(root, home), HomeFixtures.Project(root, HomeFixtures.Server(HomeFixtures.Nas)));
HomeRekeyResult result;
using (HomeFixtures.Hold(Path.Combine(root, held.ToString("N"))))
{
// A project in use keeps the whole home where it is until a later re-key; the id home is made beside it.
_ = new WorkspaceStore(Path.Combine(homes.Root, "servers", IdFolder));
result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
}
Assert.Equal((0, 1, 1), (result.HomesMoved, result.ProjectsMoved, result.BundlesRewritten));
Assert.FileExists(Path.Combine(root, held.ToString("N"), WorkspaceProject.BundleFile));
Assert.Equal(HomeFixtures.Nas, Bundle(root, held).Workspace!.Home!.Server);
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
Assert.Equal(ServerIdentity.IdKey(Id), Bundle(idHome, free).Workspace!.Home!.Server);
Assert.True(ServerProjectHome.IsArchiveIdOf(Bundle(root, held).Archive.Id, [ServerIdentity.IdKey(Id), HomeFixtures.Nas]));
Assert.NotNull(homes.Find(held));
result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
Assert.Equal((1, 1), (result.ProjectsMoved, result.BundlesRewritten));
Assert.False(Directory.Exists(root));
Assert.Equal(ServerIdentity.IdKey(Id), Bundle(idHome, held).Workspace!.Home!.Server);
}
[Test]
public async Task AWorkingStateInUseInTheIdHomeIsLeftNamingTheAlias()
{
var (data, homes, cache) = Make();
var home = HomeFixtures.Server(HomeFixtures.Nas);
var root = homes.StoreFor(home).Root;
var id = HomeFixtures.Project(root, home);
// Renamed whole first (nothing held), then held while its working state would be rewritten.
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
Directory.Move(root, idHome);
HomeRekeyResult result;
using (HomeFixtures.Hold(Path.Combine(idHome, id.ToString("N"))))
result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
Assert.Equal((0, 1), (result.BundlesRewritten, result.BundlesInUse));
Assert.Equal(HomeFixtures.Nas, Bundle(idHome, id).Workspace!.Home!.Server);
Assert.Equal(ServerIdentity.IdKey(Id), Assert.NotNull(ServerHomeRecord.Read(idHome)).Key);
}
[Test]
public async Task AnIdKeyIsNeverAnAliasAndAnEmptyIdIsRefused()
{
var (data, homes, cache) = Make();
await Assert.ThrowsAsync<ArgumentException>(() => Migration(data, homes, cache).RekeyAsync(Guid.Empty, [HomeFixtures.Nas], context.Cancellation));
var other = ServerIdentity.IdKey(Guid.CreateVersion7());
var otherHome = homes.StoreFor(HomeFixtures.Server(other)).Root;
Assert.Equal(new HomeRekeyResult(0, 0, 0, 0, 0, 0, 0), await Migration(data, homes, cache).RekeyAsync(Id, [other], context.Cancellation));
Assert.True(Directory.Exists(otherHome));
}
private static bool IsProjectFile(string path) => Path.GetFileName(path) is not (WorkspaceStore.ManifestFile or HomeFolders.ServerFile);
}
/// <summary>A re-key moves the server's links in the index too.</summary>
[Category("homes")]
public abstract class HomeRekeyLinkTests(TestContext context) : RepositoryTest(context)
{
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
[Test]
public async Task TheLinksOfEveryAliasMoveToTheId()
{
var db = await OpenChronosAsync();
var store = new SyncLinkStore(db);
foreach (var key in new[] { HomeFixtures.Nas, HomeFixtures.NasByIp, HomeFixtures.Other })
{
await store.UpsertAsync(new SyncLink
{
Server = key,
RemoteId = Guid.CreateVersion7(),
Title = "Harbour",
RemoteUpdatedAt = TestData.Now,
SyncedAt = TestData.Now,
}, Ct);
}
var data = Context.CreateTempDirectory("data");
var migration = new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = data, Database = db });
var result = await migration.RekeyAsync(Id, [HomeFixtures.Nas, HomeFixtures.NasByIp], Ct);
Assert.Equal((2, 0), (result.LinksMoved, result.LinksDropped));
Assert.Count(2, await store.ListAsync(ServerIdentity.IdKey(Id), Ct));
Assert.Count(1, await store.ListAsync(HomeFixtures.Other, Ct));
Assert.Empty(await store.ListAsync(HomeFixtures.Nas, Ct));
}
}
public sealed class SqliteHomeRekeyLinkTests(TestContext context) : HomeRekeyLinkTests(context)
{
protected override Backend Backend => Backend.SqliteMemory;
}
public sealed class PostgresHomeRekeyLinkTests(TestContext context) : HomeRekeyLinkTests(context)
{
protected override Backend Backend => Backend.Postgres;
}
@@ -0,0 +1,58 @@
using Chronos.Core.IO;
using Chronos.Data.Sync;
using Chronos.Data.Tests.Homes;
using Chronos.Testing;
namespace Chronos.Data.Tests.Sync;
/// <summary>Where the proxies of a server's sequence live (owner review 5, #66): its home in the cache, else where an earlier layout left them.</summary>
[Category("sync")]
public sealed class ProxyCacheTests(TestContext context)
{
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
[Test]
public void WithoutAServersRootTheLayoutIs10s()
{
var root = context.CreateTempDirectory("proxies");
var remote = Guid.CreateVersion7();
var cache = new ProxyCache(root);
Assert.Equal(Path.Combine(root, HomeFolders.AddressHash(HomeFixtures.Nas), remote.ToString("N")), cache.Folder(HomeFixtures.Nas, remote));
}
[Test]
public void ANewSequenceGoesToItsServersHomeAndAnOldOneIsReadWhereItIs()
{
var cacheRoot = context.CreateTempDirectory("cache");
var (legacyRoot, servers) = (Path.Combine(cacheRoot, "proxies"), Path.Combine(cacheRoot, "servers"));
var identity = new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]);
var cache = new ProxyCache(legacyRoot, servers, () => identity);
var idKey = ServerIdentity.IdKey(Id);
var fresh = Guid.CreateVersion7();
Assert.Equal(Path.Combine(servers, Id.ToString("N"), "proxies", fresh.ToString("N")), cache.Folder(idKey, fresh));
Assert.Equal(cache.Folder(idKey, fresh), cache.Folder(HomeFixtures.Nas, fresh), "an alias names the same home");
// A sequence cached by 1.0 under the address is used where it is until it is moved.
var old = Guid.CreateVersion7();
var legacy = HomeFixtures.LegacyProxies(cacheRoot, HomeFixtures.Nas, old);
Assert.Equal(legacy, cache.Folder(idKey, old));
Assert.True(cache.Exists(idKey, old));
// In the home of the address (sorted before the server announced its id): used there too.
var sorted = Guid.CreateVersion7();
var byAddress = Path.Combine(servers, HomeFolders.ServerFolder(HomeFixtures.Nas), "proxies", sorted.ToString("N"));
Directory.CreateDirectory(byAddress);
Assert.Equal(byAddress, cache.Folder(idKey, sorted));
// Once in its home, that is where it is.
var home = Path.Combine(servers, Id.ToString("N"), "proxies", old.ToString("N"));
Directory.CreateDirectory(Path.GetDirectoryName(home)!);
Directory.Move(legacy, home);
Assert.Equal(home, cache.Folder(HomeFixtures.Nas, old));
// A server known by its address only has an address home.
var other = Guid.CreateVersion7();
Assert.Equal(Path.Combine(servers, HomeFolders.ServerFolder(HomeFixtures.Other), "proxies", other.ToString("N")), cache.Folder(HomeFixtures.Other, other));
}
}
@@ -0,0 +1,84 @@
using Chronos.Core.IO;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.Data.Tests.Sync;
/// <summary>How the desktop names a server (owner review 5, #66): by its announced id, its address keys kept as aliases.</summary>
[Category("sync")]
public sealed class ServerIdentityTests
{
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
private static ServerInfo Info(Guid? id) => new("Studio NAS", "1.1.0", 1, [], null, [], new ServerLimits(1, 1), null, null, id);
[Test]
public void AServerThatAnnouncesAnIdIsKeyedByIt()
{
var key = ServerIdentity.Key(Info(Id), new Uri("https://NAS.local:8443/"));
Assert.Equal("id:" + Id.ToString("N"), key);
Assert.True(ServerIdentity.IsIdKey(key));
Assert.Equal(Id, ServerIdentity.IdOf(key));
// Another address of the same server: the same key.
Assert.Equal(key, ServerIdentity.Key(Info(Id), new Uri("https://192.168.1.5:8443")));
}
[Test]
public void AnOlderServerKeepsItsAddressKey()
{
var address = new Uri("https://NAS.local:8443/");
Assert.Equal("https://nas.local:8443", ServerIdentity.Key(Info(null), address));
Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key((ServerInfo?)null, address));
Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key(Guid.Empty, address));
Assert.False(ServerIdentity.IsIdKey(SyncService.ServerKey(address)));
}
[Test]
public void AliasesNameTheServerOfTheirId()
{
var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443", "https://192.168.1.5:8443"])]);
var key = ServerIdentity.IdKey(Id);
Assert.Equal(key, identity.Canonical("https://NAS.local:8443"));
Assert.Equal(key, identity.Canonical(key.ToUpperInvariant()));
Assert.Equal("https://other:1", identity.Canonical("https://other:1"));
Assert.True(identity.Same("https://nas.local:8443", "https://192.168.1.5:8443"));
Assert.True(identity.Same(key, "https://192.168.1.5:8443"));
Assert.False(identity.Same(key, "https://other:1"));
Assert.True(identity.Same(null, null));
Assert.False(identity.Same(key, null));
Assert.Equal(Id.ToString("N"), identity.FolderName("https://192.168.1.5:8443"));
Assert.Equal(HomeFolders.ServerFolder("https://other:1"), identity.FolderName("https://other:1"));
Assert.SequenceEqual(new[] { key, "https://nas.local:8443", "https://192.168.1.5:8443" }, identity.KeysOf("https://192.168.1.5:8443"));
Assert.SequenceEqual(new[] { "https://other:1" }, identity.KeysOf("https://other:1"));
Assert.Equal(2, identity.AliasesOf(Id).Count);
// Without the table every key is its own.
Assert.False(ServerIdentity.None.Same(key, "https://nas.local:8443"));
}
[Test]
public void AnAddressChangeKeepsTheKeyOnceTheAliasIsKnown()
{
var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443"])]);
var widened = identity.With(Id, "https://nas.local:9443");
Assert.True(widened.Same("https://nas.local:8443", "https://nas.local:9443"));
Assert.Same(widened, widened.With(Id, "HTTPS://nas.local:9443"));
Assert.False(identity.Same("https://nas.local:8443", "https://nas.local:9443"), "the table it came from is unchanged");
var other = Guid.CreateVersion7();
Assert.Equal(ServerIdentity.IdKey(other), widened.With(other, "https://second:1").Canonical("https://second:1"));
// An address two ids claim stays with the first one listed.
var clash = new ServerIdentity([new KnownServerKey(Id, ["https://x"]), new KnownServerKey(other, ["https://x"])]);
Assert.Equal(ServerIdentity.IdKey(Id), clash.Canonical("https://x"));
}
[Test]
public void TheArchiveIdOfAnyAliasIsTheServers()
{
string[] keys = [ServerIdentity.IdKey(Id), "https://nas.local:8443"];
Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://nas.local:8443"), keys));
Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf(keys[0]), keys));
Assert.False(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://other:1"), keys));
}
}
@@ -0,0 +1,72 @@
using Chronos.Data.Sync;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Data.Tests.Repositories;
using Chronos.Testing;
namespace Chronos.Data.Tests.Sync;
/// <summary>A server's links move from its address key to its id key (owner review 5, #66), the newer row winning a clash.</summary>
[Category("sync")]
public abstract class SyncLinkRekeyTests(TestContext context) : RepositoryTest(context)
{
private const string Address = "https://nas.local:8443";
private static readonly string IdKey = ServerIdentity.IdKey(Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"));
private static SyncLink Link(string server, Guid remoteId, Guid? localId, DateTimeOffset? synced, string title = "Harbour") => new()
{
Server = server,
RemoteId = remoteId,
LocalId = localId,
Title = title,
RemoteUpdatedAt = TestData.Now,
SyncedAt = synced,
};
[Test]
public async Task TheLinksOfAnAddressMoveToTheIdAndTheNewerRowWinsAClash()
{
var db = await OpenChronosAsync();
var archive = TestData.Archive();
await db.Archives.UpsertAsync(archive, Ct);
var (a, b, c) = (TestData.Sequence(archive.Id), TestData.Sequence(archive.Id), TestData.Sequence(archive.Id));
foreach (var sequence in new[] { a, b, c }) await db.Sequences.UpsertAsync(sequence, Ct);
var store = new SyncLinkStore(db);
var (plain, sameRemote, sameLocal, stale) = (Guid.CreateVersion7(), Guid.CreateVersion7(), Guid.CreateVersion7(), Guid.CreateVersion7());
// Only under the address: it moves.
await store.UpsertAsync(Link(Address, plain, a.Id, TestData.Now), Ct);
// Under both keys for one server sequence: the address's row is newer, so it replaces the id's.
await store.UpsertAsync(Link(Address, sameRemote, null, TestData.Now.AddHours(2), "newer"), Ct);
await store.UpsertAsync(Link(IdKey, sameRemote, null, TestData.Now.AddHours(1), "older"), Ct);
// Both keys link local copy b to different server sequences: the id's row is newer, so the address's goes.
await store.UpsertAsync(Link(Address, sameLocal, b.Id, TestData.Now), Ct);
await store.UpsertAsync(Link(IdKey, Guid.CreateVersion7(), b.Id, TestData.Now.AddDays(1)), Ct);
// Never compared under the address, compared under the id: the id's stays.
await store.UpsertAsync(Link(Address, stale, c.Id, null), Ct);
await store.UpsertAsync(Link(IdKey, stale, null, TestData.Now), Ct);
var (moved, dropped) = await store.RekeyAsync(Address, IdKey, Ct);
Assert.Equal((2, 3), (moved, dropped));
Assert.Empty(await store.ListAsync(Address, Ct));
var links = (await store.ListAsync(IdKey, Ct)).ToDictionary(l => l.RemoteId);
Assert.Equal(4, links.Count);
Assert.Equal(a.Id, links[plain].LocalId);
Assert.Equal("newer", links[sameRemote].Title);
Assert.False(links.ContainsKey(sameLocal));
Assert.Null(links[stale].LocalId);
Assert.Equal(b.Id, Assert.NotNull(await store.FindByLocalAsync(IdKey, b.Id, Ct)).LocalId);
// Again: nothing left to move; and a key onto itself does nothing.
Assert.Equal((0, 0), await store.RekeyAsync(Address, IdKey, Ct));
Assert.Equal((0, 0), await store.RekeyAsync(IdKey, IdKey, Ct));
}
}
public sealed class SqliteSyncLinkRekeyTests(TestContext context) : SyncLinkRekeyTests(context)
{
protected override Backend Backend => Backend.SqliteMemory;
}
public sealed class PostgresSyncLinkRekeyTests(TestContext context) : SyncLinkRekeyTests(context)
{
protected override Backend Backend => Backend.Postgres;
}
@@ -0,0 +1,172 @@
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);
}
}
}
@@ -0,0 +1,117 @@
using System.Text.Json;
using Chronos.Data;
using Chronos.Data.Repositories;
using Chronos.Protocol;
using Chronos.Server.Hosting;
using Chronos.Testing;
namespace Chronos.Server.Tests.Api;
/// <summary>The server's stable id announced by <c>/server/info</c> (owner review 5, #66).</summary>
[Category("server")]
public sealed class ServerInfoIdTests(TestContext context)
{
private static async Task<ServerInfo> InfoAsync(TestServer server)
{
using var response = await server.Client.GetAsync("/api/v1/server/info");
return await response.ReadAsync(ProtocolJson.Default.ServerInfo);
}
[Test]
public async Task TheIdIsMadeOnceAndKeptAcrossRestarts()
{
var root = context.CreateTempDirectory("kept");
void Same(Configuration.ServerOptions o)
{
o.DataPath = Path.Combine(root, "data");
o.ArchivePath = Path.Combine(root, "archive");
}
Guid first;
await using (var server = await TestServer.StartAsync(context, Same))
{
first = Assert.NotNull((await InfoAsync(server)).ServerId);
Assert.NotEqual(Guid.Empty, first);
var stored = await server.Database.Settings.GetJsonAsync(SettingScope.App, null, ServerHost.ServerIdKey, context.Cancellation);
Assert.Equal("\"" + first.ToString("D") + "\"", stored);
}
await using (var again = await TestServer.StartAsync(context, Same))
Assert.Equal((Guid?)first, (await InfoAsync(again)).ServerId);
// Another server (another database) has another id.
await using (var other = await TestServer.StartAsync(context))
Assert.NotEqual((Guid?)first, (await InfoAsync(other)).ServerId);
}
[Test]
public async Task TheIdSurvivesARestoreOfTheIndex()
{
var folder = context.CreateTempDirectory("restore");
Guid id;
string backup;
await using (var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "chronos.db")), context.Cancellation))
{
id = await ServerHost.ServerIdAsync(db, null, context.Cancellation);
backup = await db.BackupAsync(Path.Combine(folder, "backups"), cancellationToken: context.Cancellation);
}
// A new index (the disk replaced), then the backup put back.
await using var restored = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "new.db")), context.Cancellation);
await restored.RestoreAsync(backup, context.Cancellation);
Assert.Equal(id, await ServerHost.ServerIdAsync(restored, null, context.Cancellation));
}
[Test]
public async Task AStoredIdThatIsNotOneIsMadeAgain()
{
var folder = context.CreateTempDirectory("damaged");
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "chronos.db")), context.Cancellation);
foreach (var damaged in new[] { "\"not a guid\"", "\"" + Guid.Empty.ToString("D") + "\"", "42" })
{
await db.Settings.SetJsonAsync(SettingScope.App, null, ServerHost.ServerIdKey, damaged, context.Cancellation);
var made = await ServerHost.ServerIdAsync(db, null, context.Cancellation);
Assert.NotEqual(Guid.Empty, made);
Assert.Equal(made, await ServerHost.ServerIdAsync(db, null, context.Cancellation));
}
}
[Test]
[RequiresEnvironment("CHRONOS_TEST_PG")]
public async Task TheIdIsKeptOnPostgres()
{
var baseConnection = Environment.GetEnvironmentVariable("CHRONOS_TEST_PG")!;
var admin = DatabaseFactory.Open(DatabaseOptions.Postgres(baseConnection));
var schema = "chronos_s_" + Guid.NewGuid().ToString("N")[..16];
await using (var session = await admin.OpenAsync(context.Cancellation))
await session.ExecuteScriptAsync($"CREATE SCHEMA {schema}", context.Cancellation);
try
{
var options = DatabaseOptions.Postgres(baseConnection.TrimEnd(';', ' ') + $";Search Path={schema}");
Guid id;
await using (var db = await ChronosDatabase.OpenAsync(options, context.Cancellation))
id = await ServerHost.ServerIdAsync(db, null, context.Cancellation);
await using (var db = await ChronosDatabase.OpenAsync(options, context.Cancellation))
Assert.Equal(id, await ServerHost.ServerIdAsync(db, null, context.Cancellation));
}
finally
{
await using (var session = await admin.OpenAsync(CancellationToken.None))
await session.ExecuteScriptAsync($"DROP SCHEMA IF EXISTS {schema} CASCADE", CancellationToken.None);
await admin.DisposeAsync();
}
}
[Test]
public void AnOlderClientAndAnOlderServerReadEachOther()
{
var id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
var info = new ServerInfo("Studio NAS", "1.1.0", 1, ["sync"], null, ["jpeg"], new ServerLimits(1, 1), null, 3, id);
var json = JsonSerializer.Serialize(info, ProtocolJson.Default.ServerInfo);
Assert.Contains("\"serverId\":\"" + id.ToString("D") + "\"", json);
Assert.Equal(info.ServerId, Assert.NotNull(JsonSerializer.Deserialize(json, ProtocolJson.Default.ServerInfo)).ServerId);
// A 1.0 server sends no id: it reads as null, and a 1.0 client ignores the member it does not know.
const string older = """{"name":"NAS","version":"1.0.0","apiVersion":1,"capabilities":["sync"],"formats":["jpeg"],"limits":{"maxUploadBytes":1,"chunkSizeBytes":1}}""";
Assert.Null(Assert.NotNull(JsonSerializer.Deserialize(older, ProtocolJson.Default.ServerInfo)).ServerId);
Assert.False(JsonSerializer.Serialize(info with { ServerId = null }, ProtocolJson.Default.ServerInfo).Contains("serverId", StringComparison.Ordinal));
using var parsed = JsonDocument.Parse(json);
Assert.Equal("Studio NAS", parsed.RootElement.GetProperty("name").GetString());
}
}