Files
Chronos/tests/Titano.Data.Tests/Sync/SyncRulesTests.cs
T
Claude 17311cc459 fix(sync): send wanted proxies of unverified originals and recount them
Wanted proxies were matched to this PC's originals by SHA-256 only, and
frames adopted from a folder stay unhashed until verified. A desktop that
uploaded an adopted HEIC folder counted none of the proxies the server asked
for, showed the link in sync and sent none of them. The upload now keeps the
hash it computes for each original the index has none for and makes the
proxies from those too; the comparison counts a wanted frame no hash matched
against an unhashed original this PC reads by size (the server names its copy
by its own template, so not by name).

The wanted count was recomputed only when frame or render counters moved, so
proxies a render node sent left the row "Local ahead" with an upload that
sent nothing, and proxies the server started asking for later were never
counted. A link whose copy may make proxies for the server now keeps the
server's update time of the last comparison (sync_links.diff_remote_updated_at,
migration 0006) and is compared again when it moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 11:06:53 +00:00

156 lines
8.0 KiB
C#

using Titano.Core.Imaging;
using Titano.Core.Imaging.Decoders;
using Titano.Core.Model;
using Titano.Data.Sync;
using Titano.Protocol;
using Titano.Testing;
namespace Titano.Data.Tests.Sync;
public sealed class SyncRulesTests
{
private static readonly Sequence Local = new() { Id = Guid.CreateVersion7(), ArchiveId = Guid.CreateVersion7(), Folder = "2026/a", SettingsVersion = 4 };
private static readonly SyncLink Agreed = new()
{
Server = "https://nas",
RemoteId = Guid.CreateVersion7(),
Title = "Harbour",
RemoteSettingsVersion = 7,
BaseLocalSettings = 4,
BaseRemoteSettings = 7,
};
[Test]
public void StateFollowsLinksOriginalsAndSettingsVersions()
{
Assert.Equal(SyncStatus.LocalOnly, SyncRules.Status(Local, null));
Assert.Equal(SyncStatus.RemoteOnly, SyncRules.Status(null, Agreed));
Assert.Equal(SyncStatus.ProxiesOnly, SyncRules.Status(null, Agreed with { ProxiesCached = 3 }));
Assert.Equal(SyncStatus.InSync, SyncRules.Status(Local, Agreed));
Assert.Equal(SyncStatus.LocalAhead, SyncRules.Status(Local, Agreed with { MissingRemote = 2 }));
Assert.Equal(SyncStatus.LocalAhead, SyncRules.Status(Local with { SettingsVersion = 5 }, Agreed));
Assert.Equal(SyncStatus.ServerAhead, SyncRules.Status(Local, Agreed with { MissingLocal = 1 }));
Assert.Equal(SyncStatus.ServerAhead, SyncRules.Status(Local, Agreed with { RemoteSettingsVersion = 8 }));
// Originals never conflict: what this PC lacks is downloaded after what the server lacks is uploaded.
Assert.Equal(SyncStatus.LocalAhead, SyncRules.Status(Local, Agreed with { MissingRemote = 1, MissingLocal = 1 }));
Assert.Equal(SyncStatus.Conflict, SyncRules.Status(Local with { SettingsVersion = 5 }, Agreed with { RemoteSettingsVersion = 8 }));
// A link nothing was agreed on yet: both sides count as changed until the content is compared.
Assert.Equal(SyncStatus.Conflict, SyncRules.Status(Local, Agreed with { BaseLocalSettings = null, BaseRemoteSettings = null }));
}
[Test]
public void TheLibraryStoresOnlyStatesOfLocalCopies()
{
Assert.Equal(SyncState.InSync, SyncRules.ToSequenceState(SyncStatus.InSync));
Assert.Equal(SyncState.Conflict, SyncRules.ToSequenceState(SyncStatus.Conflict));
Assert.Equal(SyncState.LocalAhead, SyncRules.ToSequenceState(SyncStatus.LocalAhead));
Assert.Equal(SyncState.ServerAhead, SyncRules.ToSequenceState(SyncStatus.ServerAhead));
Assert.Equal(SyncState.LocalOnly, SyncRules.ToSequenceState(SyncStatus.LocalOnly));
Assert.Null(SyncRules.ToSequenceState(SyncStatus.Transferring));
Assert.Null(SyncRules.ToSequenceState(SyncStatus.RemoteOnly));
}
[Test]
public void OriginalsAreComparedByContent()
{
Assert.Equal((0, 0), SyncRules.CompareOriginals(["AA", "bb"], ["aa", "BB"]));
Assert.Equal((1, 0), SyncRules.CompareOriginals(["aa", "bb", "cc"], ["aa", "bb"]));
Assert.Equal((0, 2), SyncRules.CompareOriginals(["aa"], ["aa", "bb", "cc"]));
Assert.Equal((1, 1), SyncRules.CompareOriginals(["aa", "dd"], ["aa", "ee"]));
// Frames not hashed yet (adopted, not verified) stand for server frames no hash matched.
Assert.Equal((0, 0), SyncRules.CompareOriginals(["aa", null, null], ["aa", "bb", "cc"]));
Assert.Equal((0, 1), SyncRules.CompareOriginals([null], ["bb", "cc"]));
Assert.Equal((1, 0), SyncRules.CompareOriginals([null, null], ["bb"]));
}
[Test]
public void ProxiesTheServerAsksForAreCountedForOriginalsThisPcReads()
{
var heif = new ImageDecoder([new NativeHeif()]);
List<Frame> local =
[
Original("aa", 100, ImageFileFormat.Heif),
Original(null, 200, ImageFileFormat.Heif),
Original(null, 300, ImageFileFormat.Heif),
Original("dd", 400, ImageFileFormat.Avif),
];
var manifest = new SequenceManifest(Guid.CreateVersion7(), 1, 1, null, [],
[
Wanted("AA", 100, "2026-10-01_0001.heic"),
// Not hashed here: matched by size, whatever name the server gave its copy.
Wanted("bb", 200, "2026-10-01_0002.heic"),
Wanted("cc", 999, "IMG_0003.HEIC"),
// The server has no hash of it yet: nothing tells which original it is.
Wanted(string.Empty, 300, "IMG_0004.HEIC"),
// This PC cannot decode AVIF.
Wanted("dd", 400, "IMG_0005.AVIF"),
Wanted("ee", 200, "IMG_0006.HEIC") with { WantsProxy = false },
], [], new ProxySpec(1920, 90));
Assert.Equal(2, SyncRules.ProxiesToSend(manifest, local, heif));
Assert.Equal(0, SyncRules.ProxiesToSend(manifest, local, DecoderFactory.CreateServer()));
Assert.True(SyncRules.MakesProxiesFor(local, heif), "HEIF the server cannot decode and this PC can");
Assert.False(SyncRules.MakesProxiesFor(local, DecoderFactory.CreateServer()), "a PC that reads no more than the server");
Assert.False(SyncRules.MakesProxiesFor([Original("aa", 100, ImageFileFormat.Jpeg)], heif), "the server decodes JPEG itself");
}
[Test]
public void SettingsAreComparedAsProjectsNotAsText()
{
Assert.True(SyncRules.SameSettings(null, "{}"), "no settings is the default project");
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, "{ \"exposure\": { \"deflickerWindow\": { \"manual\": 41, \"isAuto\": false } } }"));
Assert.False(SyncRules.SameSettings(SyncFixture.Window41, SyncFixture.Window33));
var difference = Assert.Contains(SyncRules.Differences(SyncFixture.Window41, SyncFixture.Window33), d => d.Path.StartsWith("exposure.deflickerWindow", StringComparison.Ordinal));
Assert.Equal("exposure.deflickerWindow.manual", difference.Path);
Assert.Equal("41", difference.Mine);
Assert.Equal("33", difference.Theirs);
Assert.Count(1, SyncRules.Differences("""{"exposure":{"exposureOffsetEv":1},"stack":{"length":3}}""", "{}", max: 1));
Assert.Empty(SyncRules.Differences(SyncFixture.Window33, SyncFixture.Window33));
}
[Test]
public void PayloadDifferencesListTheSequenceFieldsBeforeTheSettings()
{
var mine = new SyncPayload("Harbour", ["sunset", "Harbour"], 3, "mine", SyncFixture.Window41);
var theirs = new SyncPayload("Harbour at dusk", ["harbour", "SUNSET"], 3, " mine ", SyncFixture.Window33);
var differences = SyncRules.Differences(mine, theirs, 10);
Assert.SequenceEqual(["sequence.title", "exposure.deflickerWindow.manual"], differences.Select(d => d.Path));
Assert.Equal(("Harbour", "Harbour at dusk"), (differences[0].Mine, differences[0].Theirs));
Assert.Count(1, SyncRules.Differences(mine, theirs, 1));
Assert.True(mine.SameMetadata(mine with { Tags = ["HARBOUR", "sunset"], Notes = "mine " }), "tags ignore case and order, blanks are trimmed");
Assert.Empty(SyncRules.Differences(mine, mine with { Tags = ["harbour", "SUNSET"] }));
}
private static Frame Original(string? sha, long size, ImageFileFormat format) => new()
{
Id = Guid.CreateVersion7(),
SequenceId = Local.Id,
Index = (int)(size / 100),
RelativePath = $"2026/a/{size}",
FileName = $"{size}",
Size = size,
Sha256 = sha,
Format = format,
};
private static ManifestFrame Wanted(string sha, long size, string name) =>
new(Guid.CreateVersion7(), (int)(size / 100), name, size, sha, HasProxy: false, HasThumb: true, WantsProxy: true);
/// <summary>A provider that decodes HEIF natively (WIC with the HEIF extension on a desktop).</summary>
private sealed class NativeHeif : IImageDecoderProvider
{
public string Name => "native-heif";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Heif ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
}
}