Files
Chronos/tests/Chronos.Data.Tests/Archiving/ArchiveProgressDetailTests.cs
T
Alby96andClaude Opus 5.5 45e6911082 feat(jobs): every job handler reports a structured progress detail
The job list showed a bar and a free-text line that each handler worded
its own way. Each handler now reports its step, the item it is on, photo
x of y and sequence/folder/output/step n of m through the P2.1 JobContext
detail. Clients can word and lay that out themselves, and the 2/s
throttle keeps the cost low: counts within a step go through
ReportItemAsync, which allocates nothing.

- scan: List, then Index (folder n of m, photo x of y as an adopted or
  reconciled folder is read) or Check, then FollowUps
- verify, thumbs, proxies, deep analysis: photo x of y of sequence n of m;
  light analysis: sequence n of m; a thumbnail/proxy checkpoint carries
  the frame count it saves
- convert: Verify (resume), Convert photo x of y, Replace; reorganize:
  step n of m with the title; import: the file being copied, photo x of y
- maintenance: the step key, step n of 5, then Check; duplicates, orphans
  and backup run with no count; cleanup names step n of m; empty trash:
  entry n of m
- render (local, workspace, render node): Analyse, Encode (frames of the
  last output, output n of m in a batch), Verify; sync, video copy,
  workspace copy/send/move: Upload/Download/CopyProxy/CopyOriginal/
  CopyVideo/Reorganize with their items and counts

The detail carries keys and names only, never localized text. The
existing Message strings stay for clients that still show them.

Refs #65

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:38:05 +01:00

181 lines
8.7 KiB
C#

using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Archiving.Jobs;
using Chronos.Data.Housekeeping;
using Chronos.Data.Jobs;
using Chronos.Data.Tests.Jobs;
using Chronos.Testing;
using static Chronos.Data.Tests.Archiving.ArchiveFixture;
namespace Chronos.Data.Tests.Archiving;
/// <summary>
/// What the archive jobs say they are doing (owner review 5, #65): the step, the item, photo x of y and sequence, folder
/// or step n of m, sent as the job's progress detail; no localized text in it.
/// </summary>
[Category("archive")]
public sealed class ArchiveProgressDetailTests(TestContext context)
{
private static void Populate(ArchiveFixture fx)
{
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 4);
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "b"), Start.AddDays(1), 3, firstSeed: 50);
}
private static async Task<(DetailRecorder Recorder, JobEngine Engine)> StartAsync(ArchiveFixture fx)
{
var recorder = new DetailRecorder();
var engine = recorder.Engine(fx.Db, fx.Service.CreateJobHandlers());
await engine.StartAsync(fx.Ct);
return (recorder, engine);
}
private static async Task<Guid> RunAsync(ArchiveFixture fx, JobEngine engine, ArchiveJobRequest request)
{
var job = await engine.EnqueueAsync(request, cancellationToken: fx.Ct);
await fx.WaitForAsync(job.Id, JobState.Done);
return job.Id;
}
[Test]
public async Task AScanListsThenIndexesEachNewFolderPhotoByPhotoAndChecksThemOnARescan()
{
await using var fx = await CreateAsync(context);
Populate(fx);
var (recorder, engine) = await StartAsync(fx);
await using var _ = engine;
var first = await RunAsync(fx, engine, ArchiveJobs.Scan(fx.Archive));
var details = recorder.Details(first);
Assert.Equal(JobStep.List, details[0].Step);
var index = details.Where(d => d.Step == JobStep.Index).ToList();
Assert.Equal(new JobDetail(JobStep.Index, "a", 0, 0, 1, 2, JobPartUnit.Folder), index[0]);
// Photo after photo inside an adopted folder, then the next folder.
Assert.SequenceEqual([1L, 2L, 3L, 4L], index.Where(d => d.Item == "a" && d.ItemTotal > 0).Select(d => d.ItemDone));
Assert.True(index.Where(d => d.ItemTotal > 0).All(d => d.ItemTotal == (d.Item == "a" ? 4 : 3)));
Assert.SequenceEqual([1, 2], index.Select(d => d.Part).Distinct());
Assert.True(index.All(d => d.Parts == 2 && d.PartUnit == JobPartUnit.Folder));
Assert.Equal(JobStep.Index, recorder.Steps(first)[1]);
// Nothing changed: each folder is checked, folder n of m, and nothing is indexed.
var again = await RunAsync(fx, engine, ArchiveJobs.Scan(fx.Archive));
Assert.SequenceEqual([JobStep.List, JobStep.Check], recorder.Steps(again));
var check = recorder.Details(again).Where(d => d.Step == JobStep.Check).ToList();
Assert.SequenceEqual([("a", 1), ("b", 2)], check.Select(d => (d.Item!, d.Part)));
Assert.True(check.All(d => d.Parts == 2 && d.PartUnit == JobPartUnit.Folder));
}
[Test]
public async Task AVerifyCountsThePhotosOfEachSequence()
{
await using var fx = await CreateAsync(context);
Populate(fx);
await fx.ScanAsync();
var (recorder, engine) = await StartAsync(fx);
await using var _ = engine;
var id = await RunAsync(fx, engine, ArchiveJobs.Verify(fx.Archive));
var details = recorder.Details(id);
Assert.True(details.All(d => d.Step == JobStep.Verify && d.PartUnit == JobPartUnit.Sequence && d.Parts == 2), string.Join("; ", details));
Assert.SequenceEqual([(1, 1L), (1, 2L), (1, 3L), (1, 4L), (2, 1L), (2, 2L), (2, 3L)], details.Select(d => (d.Part, d.ItemDone)));
Assert.SequenceEqual([4L, 3L], details.GroupBy(d => d.Part).Select(g => g.Max(d => d.ItemTotal)));
Assert.True(details.All(d => !string.IsNullOrEmpty(d.Item)));
}
[Test]
public async Task AThumbnailPassReportsEachFrameOfEachSequence()
{
await using var fx = await CreateAsync(context);
Populate(fx);
await fx.ScanAsync();
var ids = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Select(l => l.Id).ToList();
var (recorder, engine) = await StartAsync(fx);
await using var _ = engine;
var id = await RunAsync(fx, engine, ArchiveJobs.Thumbnails(ids));
var details = recorder.Details(id);
Assert.True(details.All(d => d.Step == JobStep.Thumbs && d.PartUnit == JobPartUnit.Sequence && d.Parts == 2), string.Join("; ", details));
Assert.SequenceEqual([1, 2], details.Select(d => d.Part).Distinct());
// Every frame of a sequence goes out, up to its last.
Assert.SequenceEqual([(1, 4L), (2, 3L)], details.GroupBy(d => d.Part).Select(g => (g.Key, g.Max(d => d.ItemDone))));
Assert.True(details.GroupBy(d => d.Part).All(g => g.Count() >= g.Max(d => d.ItemTotal)));
}
[Test]
public async Task MaintenanceNamesItsStepsInOrder()
{
await using var fx = await CreateAsync(context);
Populate(fx);
await fx.ScanAsync();
var (recorder, engine) = await StartAsync(fx);
await using var _ = engine;
var id = await RunAsync(fx, engine, ArchiveJobs.Maintenance(fx.Archive));
var details = recorder.Details(id);
var steps = details.Where(d => d.Step == JobStep.Maintenance).ToList();
Assert.SequenceEqual(["leases", "temp", "packs", "duplicates", "vacuum"], steps.Select(d => d.Item!));
Assert.SequenceEqual([1, 2, 3, 4, 5], steps.Select(d => d.Part));
Assert.True(steps.All(d => d.Parts == 5 && d.PartUnit == JobPartUnit.Step));
Assert.SequenceEqual([JobStep.Maintenance, JobStep.Check], recorder.Steps(id));
}
[Test]
public async Task DuplicatesAndOrphansRunWithoutACount()
{
await using var fx = await CreateAsync(context);
Populate(fx);
await fx.ScanAsync();
var (recorder, engine) = await StartAsync(fx);
await using var _ = engine;
foreach (var request in new[] { ArchiveJobs.Duplicates(fx.Archive), ArchiveJobs.Orphans(fx.Archive) })
{
var id = await RunAsync(fx, engine, request);
// Every report until the one that completes it.
var reports = recorder.Of(id);
var running = reports.TakeWhile(p => p.Total == 0 || p.Done < p.Total).ToList();
Assert.NotEmpty(running);
Assert.True(running.All(p => p.Detail == new JobDetail(JobStep.Check) && p.Total == 0),
$"{request.Kind}: {string.Join("; ", reports.Select(p => $"{p.Done}/{p.Total} {p.Detail}"))}");
}
}
[Test]
public async Task ABackupRunsWithoutACountAndACleanupNamesItsSteps()
{
await using var fx = await CreateAsync(context);
var (recorder, engine) = await StartAsync(fx);
await using var _ = engine;
var backup = await RunAsync(fx, engine, ArchiveJobs.Backup(keep: 1));
var reports = recorder.Of(backup);
var running = reports.TakeWhile(p => p.Total == 0 || p.Done < p.Total).ToList();
Assert.NotEmpty(running);
Assert.True(running.All(p => p.Detail == new JobDetail(JobStep.Backup) && p.Total == 0), string.Join("; ", reports.Select(p => $"{p.Done}/{p.Total} {p.Detail}")));
var cleanup = await RunAsync(fx, engine, ArchiveJobs.Cleanup(fx.Archive, [CleanupSteps.JobHistory, CleanupSteps.Audit], 0));
var steps = recorder.Details(cleanup);
Assert.SequenceEqual([(CleanupSteps.JobHistory, 1), (CleanupSteps.Audit, 2)], steps.Select(d => (d.Item!, d.Part)));
Assert.True(steps.All(d => d.Step == JobStep.Cleanup && d.Parts == 2 && d.PartUnit == JobPartUnit.Step));
}
[Test]
public async Task EmptyingTheTrashCountsItsEntries()
{
await using var fx = await CreateAsync(context);
Populate(fx);
await fx.ScanAsync();
var ids = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Select(l => l.Id).ToList();
await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync(ids, null, fx.Ct), null, null, fx.Ct);
var empty = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
var (recorder, engine) = await StartAsync(fx);
await using var _ = engine;
var id = await RunAsync(fx, engine, ArchiveJobs.EmptyTrash(fx.Archive, empty));
var details = recorder.Details(id);
Assert.True(details.All(d => d.Step == JobStep.Erase && d.PartUnit == JobPartUnit.Entry && d.Parts == 2), string.Join("; ", details));
Assert.SequenceEqual([1, 2], details.Select(d => d.Part));
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
}
}