Files
Alby96andClaude Opus 5.5 f33bcac32e
CI / Build and test (push) Successful in 13m44s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 16s
Release / Build, test and publish (push) Successful in 26m34s
CI / PostgreSQL 16 integration (push) Failing after 17m36s
CI / PostgreSQL 17 integration (push) Failing after 15m36s
CI / Docker image (push) Successful in 1m12s
Release / Server image (push) Failing after 15s
feat(copies): matches between this PC and the server, Integrate and merge (owner review 6, v1.2.0) (#74)
The same time-lapse on a server and on this PC under different ids, or twice on this PC, is recognised,
compared and settled as the owner asked in owner review 6.

Matches are found after each sync refresh, through the server's new photo lookup, and after the jobs that
change photos. The default is to ask every time. Server › Matches offers:
- Keep apart, which is remembered;
- Link only and Unlink;
- the Compare dialog: photos, details line by line, projects and the plan.

Integrate completes both sides. Photos default to Union, projects are copied across, and a losing version
goes to the trash only after a hash check. Afterwards new photos move by themselves, and deletions wait for
the user.

A merge of two local sequences verifies every photo before the other sequence goes to its archive's trash.
Upload all local skips suspected matches and flags them.

Migration 0012 adds the found_copies and copy_probes tables. Jobs 25 (Integrate) and 26 (MergeSequences) are
new, and so are the server capabilities photo-lookup and upload-into.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-18 15:20:29 +01:00

240 lines
13 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Protocol;
using Chronos.Server.Configuration;
using Chronos.Testing;
namespace Chronos.Server.Tests.Api;
/// <summary><c>POST /api/v1/photos/lookup</c> (owner review 6, #73): counts per probe, scoped to the served archive.</summary>
[Category("server")]
public sealed class PhotoLookupTests(TestContext context)
{
private static readonly DateTime Shot = new DateTime(2026, 9, 28, 18, 2, 11, 123, DateTimeKind.Unspecified).AddTicks(4560);
[Test]
public async Task EachProbeCountsTheSequencesThatHoldItsPhotos()
{
await using var server = await TestServer.StartAsync(context);
await CountsAsync(server, context);
}
[Test]
[RequiresEnvironment("CHRONOS_TEST_PG")]
public async Task EachProbeCountsTheSequencesThatHoldItsPhotosOnPostgres()
{
await using var schema = await PgSchema.CreateAsync(context);
await using var server = await schema.StartAsync(context);
Assert.Equal(Dialect.Postgres, server.Database.Dialect);
await CountsAsync(server, context);
}
[Test]
public async Task OnlyTheServedArchiveIsLookedAt()
{
await using var server = await TestServer.StartAsync(context);
await ScopedAsync(server, context);
}
[Test]
[RequiresEnvironment("CHRONOS_TEST_PG")]
public async Task OnlyTheServedArchiveIsLookedAtOnPostgres()
{
await using var schema = await PgSchema.CreateAsync(context);
await using var server = await schema.StartAsync(context);
await ScopedAsync(server, context);
}
[Test]
public async Task TheLookupNeedsTheReadRight()
{
await using var server = await TestServer.StartAsync(context);
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
using (var refused = await LookupAsync(uploader, new PhotoLookupRequest([new PhotoProbe("a", [Hash('a')])])))
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var anonymous = await LookupAsync(server.Client, new PhotoLookupRequest([new PhotoProbe("a", [Hash('a')])])))
Assert.Equal(HttpStatusCode.Unauthorized, anonymous.StatusCode);
var (viewer, _, _) = await server.UserAsync("guest", "viewer");
using var allowed = await LookupAsync(viewer, new PhotoLookupRequest([new PhotoProbe("a", [Hash('a')])]));
Assert.Empty(Assert.NotNull(await allowed.ReadAsync(ProtocolJson.Default.PhotoLookupResponse)).Results.Single().Hits);
}
[Test]
public async Task MoreThan200ProbesOr20000ItemsAreRefused()
{
await using var server = await TestServer.StartAsync(context);
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var most = Enumerable.Range(0, PhotoLookupRequest.MaxProbes).Select(i => new PhotoProbe("p" + i, [Hash('b')])).ToList();
using (var ok = await LookupAsync(editor, new PhotoLookupRequest(most)))
Assert.Equal(PhotoLookupRequest.MaxProbes, (await ok.ReadAsync(ProtocolJson.Default.PhotoLookupResponse)).Results.Count);
using (var probes = await LookupAsync(editor, new PhotoLookupRequest([.. most, new PhotoProbe("one more", [Hash('b')])])))
await probes.ExpectErrorAsync(HttpStatusCode.RequestEntityTooLarge, "too_many");
// 20,000 items fit in one request (a body larger than the other JSON calls take); one more does not.
var hashes = Enumerable.Range(0, PhotoLookupRequest.MaxItems - 100).Select(i => i.ToString("x64", System.Globalization.CultureInfo.InvariantCulture)).ToList();
var unhashed = Enumerable.Range(0, 99).Select(i => new PhotoKeyDto(1000 + i, Shot.AddSeconds(i))).ToList();
var full = new PhotoProbe("full", hashes, unhashed, "chronos-sync:" + Guid.NewGuid().ToString("N"));
Assert.Equal(PhotoLookupRequest.MaxItems, new PhotoLookupRequest([full]).CountItems());
using (var ok = await LookupAsync(editor, new PhotoLookupRequest([full])))
Assert.Empty((await ok.ReadAsync(ProtocolJson.Default.PhotoLookupResponse)).Results.Single().Hits);
var over = full with { Unhashed = [.. unhashed, new PhotoKeyDto(5, Shot)] };
using (var items = await LookupAsync(editor, new PhotoLookupRequest([over])))
await items.ExpectErrorAsync(HttpStatusCode.RequestEntityTooLarge, "too_many");
}
[Test]
public async Task TheServerAnnouncesTheLookupAndUploadsIntoATarget()
{
await using var server = await TestServer.StartAsync(context);
using var response = await server.Client.GetAsync("/api/v1/server/info");
var info = await response.ReadAsync(ProtocolJson.Default.ServerInfo);
Assert.Contains(ServerCapabilities.PhotoLookup, info.Capabilities);
Assert.Contains(ServerCapabilities.UploadInto, info.Capabilities);
}
/// <summary>
/// Sequence A holds p1, p2 and p3 (p3 captured at <see cref="Shot"/>); B holds p4, tombstoned p5, replaced p6 by p7 and
/// is filed under a session key. One probe counts both, another hits nothing.
/// </summary>
internal static async Task CountsAsync(TestServer server, TestContext context)
{
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var photos = Enumerable.Range(1, 7).Select(i => Uploads.Photo(3000 + i, 700 + i)).ToArray();
var a = (await Uploads.UploadAsync(editor, photos[0], "IMG_0001.JPG", sessionKey: "a")).SequenceId;
await Uploads.UploadAsync(editor, photos[1], "IMG_0002.JPG", sessionKey: "a");
await Uploads.UploadAsync(editor, photos[2], "IMG_0003.JPG", sessionKey: "a", capturedAt: Shot);
var b = (await Uploads.UploadAsync(editor, photos[3], "IMG_0004.JPG", sessionKey: "b")).SequenceId;
var now = DateTimeOffset.UtcNow;
await server.Database.FrameRemovals.SyncSequenceAsync(b,
[
new FrameRemoval(b, Uploads.Sha(photos[4]), null, RemovalReason.Deleted, now),
new FrameRemoval(b, Uploads.Sha(photos[5]), Uploads.Sha(photos[6]), RemovalReason.Replaced, now),
// A stale tombstone: B still holds p4, which counts once, as held.
new FrameRemoval(b, Uploads.Sha(photos[3]), null, RemovalReason.Deleted, now),
], context.Cancellation);
var archive = (await server.Database.Sequences.GetAsync(b, context.Cancellation))!.ArchiveId;
var key = "chronos-sync:" + Guid.NewGuid().ToString("N");
await server.Database.Sequences.MapSessionKeyAsync(archive, key, b, context.Cancellation);
var probe = new PhotoProbe("local-1",
[Uploads.Sha(photos[0]).ToUpperInvariant(), Uploads.Sha(photos[1]), Uploads.Sha(photos[1]), Uploads.Sha(photos[3]), Uploads.Sha(photos[4]),
Uploads.Sha(photos[6]), Hash('c')],
[
// The same millisecond as p3 (the index keeps microseconds); the next millisecond, another size: no match.
new PhotoKeyDto(photos[2].Length, new DateTime(2026, 9, 28, 18, 2, 11, 123).AddTicks(9999)),
new PhotoKeyDto(photos[2].Length, new DateTime(2026, 9, 28, 18, 2, 11, 124)),
new PhotoKeyDto(photos[2].Length + 1, Shot),
],
key);
var nothing = new PhotoProbe("local-2", [Hash('d')], [new PhotoKeyDto(photos[2].Length, Shot.AddDays(1))], "chronos-sync:" + Guid.NewGuid().ToString("N"));
using var response = await LookupAsync(editor, new PhotoLookupRequest([probe, nothing]));
var results = (await response.ReadAsync(ProtocolJson.Default.PhotoLookupResponse)).Results;
Assert.SequenceEqual(["local-1", "local-2"], results.Select(r => r.Key));
var hits = results[0].Hits.ToDictionary(h => h.SequenceId);
Assert.Equal(2, hits.Count);
Assert.Equal(new PhotoLookupHit(a, Hashes: 2, Removed: 0, Unhashed: 1, SessionKey: false), hits[a]);
Assert.Equal(new PhotoLookupHit(b, Hashes: 1, Removed: 2, Unhashed: 0, SessionKey: true), hits[b]);
Assert.Empty(results[1].Hits);
}
/// <summary>A sequence of another archive of the index holding the same photo and session key is never answered.</summary>
internal static async Task ScopedAsync(TestServer server, TestContext context)
{
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var photo = Uploads.Photo(4000, 801);
var mine = (await Uploads.UploadAsync(editor, photo, sessionKey: "mine", capturedAt: Shot)).SequenceId;
var key = "chronos-sync:" + Guid.NewGuid().ToString("N");
var foreign = await ForeignFrameAsync(server, context, photo, Shot, key);
using var response = await LookupAsync(editor, new PhotoLookupRequest([new PhotoProbe("x", [Uploads.Sha(photo)], [new PhotoKeyDto(photo.Length, Shot)], key)]));
var hit = (await response.ReadAsync(ProtocolJson.Default.PhotoLookupResponse)).Results.Single().Hits.Single();
Assert.Equal(new PhotoLookupHit(mine, 1, 0, 1, false), hit);
Assert.NotEqual(foreign.SequenceId, hit.SequenceId);
}
/// <summary>
/// A frame of <paramref name="data"/> in a sequence of a second archive of the index (as a desktop sharing its library
/// keeps other archives' sequences), its file under that archive's root; optionally filed under <paramref name="sessionKey"/>.
/// </summary>
internal static async Task<Frame> ForeignFrameAsync(TestServer server, TestContext context, byte[] data, DateTime? capturedAt = null, string? sessionKey = null)
{
var db = server.Database;
var other = new Archive { Id = Guid.CreateVersion7(), Name = "Another archive", RootPath = context.CreateTempDirectory("other-archive"), Created = DateTimeOffset.UtcNow };
await db.Archives.UpsertAsync(other, context.Cancellation);
var sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = other.Id, Folder = "2026/other", UpdatedAt = DateTimeOffset.UtcNow };
await db.Sequences.UpsertAsync(sequence, context.Cancellation);
const string relative = "2026/other/originals/0000_IMG_0001.CR3";
var path = Path.Combine(other.RootPath, relative);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await File.WriteAllBytesAsync(path, data, context.Cancellation);
var frame = new Frame
{
Id = Guid.CreateVersion7(),
SequenceId = sequence.Id,
Index = 0,
RelativePath = relative,
FileName = Path.GetFileName(relative),
OriginalFileName = "IMG_0001.CR3",
Size = data.Length,
Sha256 = Uploads.Sha(data),
CapturedAt = capturedAt,
};
await db.Frames.InsertAsync(frame, context.Cancellation);
await db.Sequences.RefreshStatsAsync(sequence.Id, context.Cancellation);
if (sessionKey is not null) await db.Sequences.MapSessionKeyAsync(other.Id, sessionKey, sequence.Id, context.Cancellation);
return frame;
}
internal static Task<HttpResponseMessage> LookupAsync(HttpClient client, PhotoLookupRequest request) =>
client.PostAsJsonAsync("/api/v1/photos/lookup", request, ProtocolJson.Default.PhotoLookupRequest);
private static string Hash(char c) => new(c, 64);
}
/// <summary>A throw-away schema on the PostgreSQL test server (<c>CHRONOS_TEST_PG</c>), selected through <c>Search Path</c>.</summary>
internal sealed class PgSchema : IAsyncDisposable
{
private readonly IDatabase _admin;
private readonly string _name;
private PgSchema(IDatabase admin, string name, string connectionString)
{
_admin = admin;
_name = name;
ConnectionString = connectionString;
}
public string ConnectionString { get; }
public static async Task<PgSchema> CreateAsync(TestContext context)
{
var baseConnection = Environment.GetEnvironmentVariable("CHRONOS_TEST_PG")!;
var admin = DatabaseFactory.Open(DatabaseOptions.Postgres(baseConnection));
var name = "chronos_l_" + Guid.NewGuid().ToString("N")[..16];
await using (var session = await admin.OpenAsync(context.Cancellation))
await session.ExecuteScriptAsync($"CREATE SCHEMA {name}", context.Cancellation);
return new PgSchema(admin, name, baseConnection.TrimEnd(';', ' ') + $";Search Path={name}");
}
/// <summary>A test server on this schema.</summary>
public Task<TestServer> StartAsync(TestContext context, Action<ServerOptions>? configure = null,
Action<Microsoft.Extensions.DependencyInjection.IServiceCollection>? services = null) =>
TestServer.StartAsync(context, o =>
{
o.Database = DatabaseKind.Postgres;
o.DbConnectionString = ConnectionString;
configure?.Invoke(o);
}, services);
public async ValueTask DisposeAsync()
{
await using (var session = await _admin.OpenAsync(CancellationToken.None))
await session.ExecuteScriptAsync($"DROP SCHEMA IF EXISTS {_name} CASCADE", CancellationToken.None);
await _admin.DisposeAsync();
}
}