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>
118 lines
5.9 KiB
C#
118 lines
5.9 KiB
C#
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());
|
|
}
|
|
}
|