Files
Chronos/tests/Chronos.Data.Tests/Migrations/FoundCopiesMigrationTests.cs
T
Alby96andClaude Opus 5.5 283d04d9cd feat: found copies foundation (contracts, migration 0012, store, wire, trash and job hooks, keys)
Owner review 6 asks Chronos to recognise the same time-lapse held under different ids on the
server and on this PC, compare the copies, and keep them apart or integrate and sync them. This
lays the shared foundation the later parcels build on, without any UI or detection yet.

- Migration 0012 adds found_copies (local-server and local-local pairs with evidence, scores,
  answer, policy and the 404 record) and copy_probes; every existing link with a local sequence
  becomes a Linked pair, so a server deletion and the per-link policy have a place.
- FoundCopyStore keeps the pairs: upserts that never touch the answer, a compare-and-set of the
  state with an Integrating backstop (a failed or cancelled job reads as the previous state),
  link promotion and demotion, the once-per-refresh 404 count, the merge's links stage
  (session keys, links reset, pairs moved, refused when both are linked on one server), the
  local overlap queries and the probes. SyncLinkStore.RekeyAsync moves pairs and probes with
  the links in the same transaction.
- Contracts for detection, review, plans, the Integrate and MergeSequences jobs (kinds 25/26,
  High level, new detail steps) and the Upload all rule that keeps suspected, kept-apart,
  deleted, busy and server-born sequences here.
- Wire: POST photos/lookup, uploads into a target (IntoTarget/Copied, trailing and optional),
  frames/delete on the client, capabilities photo-lookup and upload-into, cached per remote.
- The trash plans and deletes accept the job that holds the sequence (its own hold is not
  Busy) and a frames version checked under the lease; a Background job never interrupts a
  running one.
- Settings for linking, the photo policy and projects; the en and it keys of design section 9.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:39:04 +01:00

104 lines
5.8 KiB
C#

using Chronos.Data.Migrations;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Testing;
namespace Chronos.Data.Tests.Migrations;
/// <summary>Migration 0012 (owner review 6, #73): found copies beside the links, every existing link a linked pair.</summary>
public abstract class FoundCopiesMigrationTests(TestContext context) : RepositoryTest(context)
{
[Test]
public async Task EveryLinkWithALocalSequenceBecomesOneLinkedPair()
{
var chronos = await OpenChronosAsync();
var database = chronos.Database;
var all = MigrationRunner.Load(Dialect);
Assert.Equal(12, all.Single(m => m.Name == "found_copies").Version);
var archive = TestData.Archive();
await chronos.Archives.UpsertAsync(archive, Ct);
var first = TestData.Sequence(archive.Id);
var second = TestData.Sequence(archive.Id);
await chronos.Sequences.UpsertAsync(first, Ct);
await chronos.Sequences.UpsertAsync(second, Ct);
var (r1, r2, r3) = (Guid.CreateVersion7(), Guid.CreateVersion7(), Guid.CreateVersion7());
await using (var session = await database.OpenAsync(Ct))
{
// Back to an index of 1.1 (schema 11) holding these links, then migrated again.
await session.ExecuteAsync("DROP TABLE copy_probes", [], Ct);
await session.ExecuteAsync("DROP TABLE found_copies", [], Ct);
await session.ExecuteAsync("DELETE FROM schema_migrations WHERE version = 12", [], Ct);
await Link(session, "https://nas.test:8443", r1, first.Id);
await Link(session, "id:server-b", r2, second.Id);
// An observation-only link (a server sequence this PC does not hold) is no pair.
await Link(session, "https://nas.test:8443", r3, null);
}
Assert.Equal(all[^1].Version, await MigrationRunner.MigrateAsync(database, Ct));
await using var after = await database.OpenAsync(Ct);
var rows = await after.QueryListAsync(
"SELECT id, kind, local_id, server, remote_id, other_id, evidence, state, fingerprint, auto_photos, remote_gone_hits, found_at FROM found_copies ORDER BY server",
[], r => r, Ct);
Assert.Equal(2, rows.Count);
Assert.Equal(2, rows.Select(r => r.GetGuid(0)).Distinct().Count());
foreach (var row in rows)
{
Assert.NotEqual(Guid.Empty, row.GetGuid(0));
Assert.Equal(0, row.GetInt32(1));
Assert.True(row.IsNull(5));
Assert.Equal(0, row.GetInt32(6));
Assert.Equal(2, row.GetInt32(7));
Assert.Equal("link", row.GetString(8));
Assert.False(row.GetBool(9));
Assert.Equal(0, row.GetInt32(10));
Assert.True(row.GetDateTimeOffset(11) > DateTimeOffset.UtcNow.AddMinutes(-5));
}
var pairs = rows.Select(r => (r.GetString(3), r.GetGuid(4), r.GetGuid(2))).ToHashSet();
Assert.Contains(("https://nas.test:8443", r1, first.Id), pairs);
Assert.Contains(("id:server-b", r2, second.Id), pairs);
// The ids are random version 4 UUIDs, in the form the binding writes.
foreach (var row in rows) Assert.Equal(4, row.GetGuid(0).Version);
// No change to the links.
Assert.Equal(3L, await after.ScalarAsync<long>("SELECT count(*) FROM sync_links", [], Ct));
}
[Test]
public async Task TheCheckRefusesAServerPairWithoutServerAndALocalPairOfOneSequence()
{
var chronos = await OpenChronosAsync();
var archive = TestData.Archive();
await chronos.Archives.UpsertAsync(archive, Ct);
var sequence = TestData.Sequence(archive.Id);
await chronos.Sequences.UpsertAsync(sequence, Ct);
await using var session = await chronos.Database.OpenAsync(Ct);
const string insert = "INSERT INTO found_copies (id, kind, local_id, server, remote_id, other_id, evidence, fingerprint, found_at, updated_at) " +
"VALUES ($1, $2, $3, $4, $5, $6, 2, 'f', $7, $7)";
await Assert.ThrowsAsync<DatabaseException>(() => session.ExecuteAsync(insert,
[Guid.NewGuid(), 0, sequence.Id, DbParam.Text(null), Guid.CreateVersion7(), (Guid?)null, TestData.Now], Ct));
await Assert.ThrowsAsync<DatabaseException>(() => session.ExecuteAsync(insert,
[Guid.NewGuid(), 1, sequence.Id, DbParam.Text(null), (Guid?)null, sequence.Id, TestData.Now], Ct));
await Assert.ThrowsAsync<DatabaseException>(() => session.ExecuteAsync(insert,
[Guid.NewGuid(), 1, sequence.Id, "https://nas.test:8443", (Guid?)null, Guid.CreateVersion7(), TestData.Now], Ct));
// A pair names existing sequences: a found copy goes with its sequence.
await Assert.ThrowsAsync<DatabaseException>(() => session.ExecuteAsync(insert,
[Guid.NewGuid(), 0, Guid.CreateVersion7(), "https://nas.test:8443", Guid.CreateVersion7(), (Guid?)null, TestData.Now], Ct));
await session.ExecuteAsync(insert, [Guid.NewGuid(), 0, sequence.Id, "https://nas.test:8443", Guid.CreateVersion7(), (Guid?)null, TestData.Now], Ct);
Assert.Equal(1L, await session.ScalarAsync<long>("SELECT count(*) FROM found_copies", [], Ct));
}
private static Task<int> Link(IDbSession session, string server, Guid remoteId, Guid? localId) =>
session.ExecuteAsync("INSERT INTO sync_links (server, remote_id, local_id, title, remote_updated_at) VALUES ($1, $2, $3, $4, $5)",
[server, remoteId, localId, "Remote", TestData.Now], CancellationToken.None);
}
public sealed class SqliteFoundCopiesMigrationTests(TestContext context) : FoundCopiesMigrationTests(context)
{
protected override Backend Backend => Backend.SqliteMemory;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresFoundCopiesMigrationTests(TestContext context) : FoundCopiesMigrationTests(context)
{
protected override Backend Backend => Backend.Postgres;
}