Importazione e dati: «Tieni il più grande» per gruppi, righe di _febe.jsonl mai perse, ricostruzione robusta (import-1, import-2, library-1, journal-1, rebuild-1, rebuild-2, rebuild-3, backup-1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-14 00:53:10 +00:00
1 parent 78c92eb2a4
commit 628fe5db2b
13 files changed
+1145 -95

No files matched your search

+79 -2
View File
@@ -52,9 +52,14 @@ public sealed class DailyBackupTests
}
await File.WriteAllTextAsync(Path.Combine(folder, "appunti.txt"), "non toccare");
await File.WriteAllTextAsync(Path.Combine(folder, "febe-2026-10-13.sqlite.abcd1234.part"), "copia interrotta");
var clock = new ManualClock(LibraryTimes.Noon(today));
var interrupted = Path.Combine(folder, "febe-2026-10-12.sqlite.abcd1234.part");
await File.WriteAllTextAsync(interrupted, "copia interrotta");
File.SetLastWriteTimeUtc(interrupted, clock.Now.UtcDateTime.AddDays(-1));
var writing = Path.Combine(folder, "febe-2026-10-13.sqlite.ef567890.part");
await File.WriteAllTextAsync(writing, "copia in corso");
File.SetLastWriteTimeUtc(writing, clock.Now.UtcDateTime.AddMinutes(-2));
var backup = new DailyBackup(db.Maintenance, clock, folder);
var result = await backup.RunAsync();
@@ -64,9 +69,81 @@ public sealed class DailyBackupTests
Assert.AreEqual(today, kept[0]);
Assert.AreEqual(today.AddDays(-6), kept[^1]);
Assert.HasCount(5, result.Removed); // four old copies and the interrupted one
Assert.IsFalse(File.Exists(interrupted));
Assert.IsTrue(File.Exists(writing), "A temporary copy written minutes ago may belong to a copy still running.");
Assert.IsTrue(File.Exists(Path.Combine(folder, "appunti.txt")));
}
/// <summary>
/// «Fai una copia adesso» clicked twice, or clicked while the 3:20 copy runs: the copies take turns, and neither
/// one removes the temporary file of the other.
/// </summary>
[TestMethod]
public async Task TwoCopiesAtTheSameTimeBothSucceed()
{
using var temp = new TempFolder();
var folder = temp.Combine("dati", "copie");
var clock = new ManualClock(LibraryTimes.Noon(new DateOnly(2026, 10, 13)));
var database = new SlowBackups();
var first = new DailyBackup(database, clock, folder).RunNowAsync();
await database.FirstStarted.Task.WaitAsync(TimeSpan.FromSeconds(10));
var second = new DailyBackup(database, clock, folder).RunNowAsync();
await Task.WhenAny(second, Task.Delay(500)); // without turns, the second copy finishes here and its retention runs
database.ReleaseFirst.SetResult();
var results = await Task.WhenAll(first, second).WaitAsync(TimeSpan.FromSeconds(10));
Assert.IsTrue(results.All(r => r.Created is not null));
Assert.AreEqual(1, database.MaxConcurrent, "One copy of a folder at a time.");
Assert.HasCount(1, Directory.GetFiles(folder, "*.sqlite"));
Assert.IsEmpty(Directory.GetFiles(folder, "*.part"));
}
/// <summary>VACUUM INTO stand-in: the first copy waits until the test lets it finish.</summary>
private sealed class SlowBackups : Core.Storage.IDatabaseMaintenance
{
private int _calls;
private int _running;
private int _max;
public TaskCompletionSource FirstStarted { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public TaskCompletionSource ReleaseFirst { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public int MaxConcurrent => Volatile.Read(ref _max);
public string DatabasePath => "febe.sqlite";
public int SchemaVersion => 1;
public async Task BackupToAsync(string destinationFile, CancellationToken ct = default)
{
var running = Interlocked.Increment(ref _running);
int seen;
while ((seen = Volatile.Read(ref _max)) < running && Interlocked.CompareExchange(ref _max, running, seen) != seen)
{
}
try
{
await File.WriteAllTextAsync(destinationFile, "copia", ct);
if (Interlocked.Increment(ref _calls) == 1)
{
FirstStarted.SetResult();
await ReleaseFirst.Task.WaitAsync(ct);
}
}
finally
{
Interlocked.Decrement(ref _running);
}
}
public Task<bool> CheckIntegrityAsync(CancellationToken ct = default) => Task.FromResult(true);
public Task OptimizeAsync(CancellationToken ct = default) => Task.CompletedTask;
}
[TestMethod]
public void RecognizesOnlyItsOwnFileNames()
{