using Chronos.Data.Migrations; using Chronos.Data.Tests.Infrastructure; using Chronos.Testing; namespace Chronos.Data.Tests.Migrations; /// Migration 0012 (owner review 6, #73): found copies beside the links, every existing link a linked pair. 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("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(() => session.ExecuteAsync(insert, [Guid.NewGuid(), 0, sequence.Id, DbParam.Text(null), Guid.CreateVersion7(), (Guid?)null, TestData.Now], Ct)); await Assert.ThrowsAsync(() => session.ExecuteAsync(insert, [Guid.NewGuid(), 1, sequence.Id, DbParam.Text(null), (Guid?)null, sequence.Id, TestData.Now], Ct)); await Assert.ThrowsAsync(() => 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(() => 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("SELECT count(*) FROM found_copies", [], Ct)); } private static Task 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; }