Files
Chronos/tests/Titano.Data.Tests/Archiving/ProxyTests.cs
T
Claude b4f1fa32d5 fix(proxies): use only a frame's own proxy and let tiers, reports and built flags follow it
- Renders, analyses, the server's manifests and proxy downloads take a proxy only
  when its Exif capture time is the frame's (ArchiveImages.IsProxyOf); one left at
  the index by a frame renumbered since counts as missing and is asked for again
  (V-24).
- A frame whose original the host reads through its proxy (HEIF on the server) is
  preview-only only while it has none: the light analysis and the proxy pass keep
  the flag, the preview-only count, the light finding and the advisor's output cap
  in step with the proxies, and an arriving proxy queues a light analysis on the
  server (V-1, V-25).
- The deep checkpoint's content key includes whether each frame is read from its
  original or its proxy, and analysis.json lists the frames measured from proxies;
  an unreadable verdict on a frame now read from its proxy makes the report stale
  (V-26).
- Thumbnails drawn from proxies are compared with the archive's thumbnail size
  instead of 320 px (V-27).
- A resumed thumbnail or proxy pass sets ThumbsBuilt/ProxiesBuilt only when the
  frames an earlier run did still have theirs, and the proxy pass logs frames
  waiting for a client's proxy apart from unreadable ones (V-28).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 16:07:31 +00:00

288 lines
17 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Titano.Core.Archiving;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Imaging;
using Titano.Core.Imaging.Decoders;
using Titano.Core.Metadata;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Testing;
using static Titano.Data.Tests.Archiving.ArchiveFixture;
namespace Titano.Data.Tests.Archiving;
/// <summary>Proxies of originals a host cannot decode (BRIEF §2.3, §8.5): sent by a client or render node, checked, kept, thumbnailed.</summary>
[Category("archive")]
public sealed class ProxyTests(TestContext context)
{
[Test]
public void OnlyFormatsAHostCannotDecodeTakeTheirPixelsFromProxies()
{
var server = DecoderFactory.CreateServer();
Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.Heif), "HEIF previews are thumbnails at best");
Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.Avif));
Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.WebP), "no decoder at all");
Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Cr3), "a RAW's embedded full-size preview");
Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Dng));
Assert.False(ArchiveImages.NeedsProxy(new ImageDecoder([]), ImageFileFormat.Jpeg), "proxies are JPEGs");
Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Unknown));
// A desktop whose decoders read HEIF makes the proxies a server asks for.
Assert.False(ArchiveImages.NeedsProxy(new ImageDecoder([new NativeHeif()]), ImageFileFormat.Heif));
}
[Test]
public async Task AProxySentForAFrameThisHostCannotDecodeIsCheckedKeptAndThumbnailed()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx);
var folder = fx.Paths.Sequence(sequence.Folder);
var images = fx.Service.Images;
Assert.Equal(ProxyStoreResult.Invalid, images.StoreProxy(fx.Archive, sequence, frames[0], "not a photo"u8.ToArray()));
Assert.Equal(ProxyStoreResult.WrongFrame, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[1].CapturedAt!.Value, 1)));
Assert.FileMissing(folder.Proxy(0));
var proxy = Proxy(frames[0].CapturedAt!.Value, 2);
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], proxy));
Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct));
// The proxy pass keeps what was sent and makes none from the originals here, even when forced.
var proxies = await images.BuildProxiesAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((0, 1, 0, 1), (proxies.Built, proxies.Reused, proxies.Undecodable, proxies.Waiting));
Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct));
Assert.FileMissing(folder.Proxy(1));
// The thumbnail comes from the proxy; the frame without one has none yet.
var thumbs = await images.BuildThumbnailsAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((1, 0, 1), (thumbs.Built, thumbs.Reused, thumbs.Undecodable));
using (var pack = ThumbPackReader.Open(folder.ThumbsPackPath))
{
var entry = pack.Entries[0];
Assert.Equal((FrameImageRenderer.ThumbnailLongEdge, 240), (entry.Width, entry.Height));
}
var again = await images.BuildThumbnailsAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((0, 1, 1), (again.Built, again.Reused, again.Undecodable));
}
[Test]
public async Task AProxyLeftByARenumberedFrameIsAskedForAgain()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx);
var folder = fx.Paths.Sequence(sequence.Folder);
Directory.CreateDirectory(folder.Proxies);
await File.WriteAllBytesAsync(folder.Proxy(1), Proxy(frames[0].CapturedAt!.Value, 3), fx.Ct);
var proxies = await fx.Service.Images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((0, 2), (proxies.Undecodable, proxies.Waiting));
Assert.FileMissing(folder.Proxy(1));
Assert.False((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt));
}
[Test]
public async Task AProxyMadeFromAPreviewIsAskedForAgain()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx, 4032, 3024);
var folder = fx.Paths.Sequence(sequence.Folder);
var images = fx.Service.Images;
// An earlier version made the frame's proxy here from the original's embedded preview: the frame's capture time,
// a thumbnail's size.
Directory.CreateDirectory(folder.Proxies);
await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[0].CapturedAt!.Value, 5, 160, 120), fx.Ct);
await fx.Db.Sequences.UpdateStatusAsync(sequence.Id, SequenceStatus.ProxiesBuilt, SequenceStatus.None, fx.Ct);
var proxies = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((0, 0, 0, 2), (proxies.Built, proxies.Reused, proxies.Undecodable, proxies.Waiting));
Assert.FileMissing(folder.Proxy(0));
Assert.False((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt), "asked for again");
// A client or render node sending one that small is refused; one of the policy's size (1920 px) is kept.
var frame = frames[0] with { Width = 4032, Height = 3024 };
Assert.Equal(ProxyStoreResult.TooSmall, images.StoreProxy(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 6, 160, 120)));
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 7, 1920, 1440)));
var again = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((0, 1, 0, 1), (again.Built, again.Reused, again.Undecodable, again.Waiting));
Assert.FileExists(folder.Proxy(0));
}
[Test]
public async Task AnArchiveWithoutProxiesRefusesThem()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx);
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { ProxyPolicy = new ProxyPolicy { LongEdge = ProxyPolicy.Disabled } });
Assert.Equal(ProxyStoreResult.Disabled, fx.Service.Images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 4)));
Assert.FileMissing(fx.Paths.Sequence(sequence.Folder).Proxy(0));
}
[Test]
public async Task OnlyAFramesOwnProxyIsReadForIt()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx);
var folder = fx.Paths.Sequence(sequence.Folder);
// Proxies are named by frame index: after a frame was inserted before both, frame 1's place holds frame 0's.
Directory.CreateDirectory(folder.Proxies);
await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[0].CapturedAt!.Value, 1), fx.Ct);
await File.WriteAllBytesAsync(folder.Proxy(1), Proxy(frames[0].CapturedAt!.Value, 2), fx.Ct);
Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(0), frames[0].CapturedAt));
Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(1), frames[1].CapturedAt), "another photo's");
Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(1), null), "a frame without a capture time cannot tell");
Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(2), frames[1].CapturedAt), "none there");
// Renders and analyses read frame 1 from its original, whether the host takes HEIF from proxies or was asked to.
foreach (var useProxies in new[] { false, true })
{
var loaded = await Titano.Data.Processing.SequenceFrames.LoadAsync(fx.Db, fx.Archive, sequence, useProxies, new ImageDecoder([]), fx.Ct);
Assert.SequenceEqual([true, false], loaded.Proxied!);
Assert.SequenceEqual([folder.Proxy(0), fx.Paths.FromRelative(frames[1].RelativePath)], loaded.Files);
}
}
[Test]
public async Task AResumedProxyPassSetsTheBuiltStatusOnlyWhenEveryFrameHasItsProxy()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx);
var images = fx.Service.Images;
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[1], Proxy(frames[1].CapturedAt!.Value, 1)));
// A pass resumed at frame 1 (paused, retried or restarted) finds that frame done, but frame 0 still waits: the
// status must not tell a server's clients that nothing is wanted.
var resumed = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 1, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.Equal((0, 1, 0, 0), (resumed.Built, resumed.Reused, resumed.Undecodable, resumed.Waiting));
Assert.False(await ProxiesBuiltAsync(fx, sequence), "frame 0 has no proxy");
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 2)));
await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 1, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.True(await ProxiesBuiltAsync(fx, sequence), "every frame has its proxy");
}
[Test]
public async Task AFrameReadFromItsProxyIsNotPreviewOnly()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx);
var folder = fx.Paths.Sequence(sequence.Folder);
var images = fx.Service.Images;
// As a server probes them: its decoders read these HEIF files only through a 160 × 120 Exif thumbnail.
var sidecar = ManifestStore.ReadSequence(folder.ManifestPath);
ManifestStore.WriteSequence(folder.ManifestPath, sidecar with
{
Frames = [.. sidecar.Frames.Select(f => f with { Tier = DecodeTier.EmbeddedPreview, Flags = f.Flags | FrameFlags.PreviewOnly, PreviewW = 160, PreviewH = 120 })],
});
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1)));
// Frame 0 now renders from its proxy, not from the thumbnail: it caps nothing. Its tier stays the original's.
await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
var after = ManifestStore.ReadSequence(folder.ManifestPath).Frames;
Assert.Equal((DecodeTier.EmbeddedPreview, FrameFlags.None, (int?)160), (after[0].Tier, after[0].Flags & FrameFlags.PreviewOnly, after[0].PreviewW));
Assert.Equal(FrameFlags.PreviewOnly, after[1].Flags & FrameFlags.PreviewOnly);
Assert.Equal(FrameFlags.None, (await fx.Db.Frames.GetAsync(frames[0].Id, fx.Ct))!.Flags & FrameFlags.PreviewOnly);
Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
// Another photo's proxy in its place is no proxy of frame 0: the pass removes it, and the frame is preview-only again.
await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[1].CapturedAt!.Value, 2), fx.Ct);
await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
Assert.FileMissing(folder.Proxy(0));
Assert.Equal(FrameFlags.PreviewOnly, ManifestStore.ReadSequence(folder.ManifestPath).Frames[0].Flags & FrameFlags.PreviewOnly);
Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
}
[Test]
public async Task ThumbnailsDrawnFromProxiesFollowTheArchivesThumbnailSize()
{
await using var fx = await CreateAsync(context);
var (sequence, frames) = await HeifSequenceAsync(fx);
var folder = fx.Paths.Sequence(sequence.Folder);
var images = fx.Service.Images;
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1, 640, 480)));
Task<ImagePassResult> ThumbnailsAsync(bool force = false) =>
images.BuildThumbnailsAsync(fx.Archive, sequence, force, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
(int, int) Entry()
{
using var pack = ThumbPackReader.Open(folder.ThumbsPackPath);
return (pack.Entries[0].Width, pack.Entries[0].Height);
}
var first = await ThumbnailsAsync();
Assert.Equal((1, 0, 1), (first.Built, first.Reused, first.Undecodable));
Assert.Equal((320, 240), Entry());
// A larger size in Settings › Archive: the proxy's thumbnail is drawn again at it, once.
SetThumbnailEdge(fx, 480);
var larger = await ThumbnailsAsync();
Assert.Equal((1, 0), (larger.Built, larger.Reused));
Assert.Equal((480, 360), Entry());
var same = await ThumbnailsAsync();
Assert.Equal((0, 1), (same.Built, same.Reused));
// A smaller one: once drawn at it, the thumbnail is reused, not drawn and appended to the pack on every pass.
SetThumbnailEdge(fx, 240);
Assert.Equal(1, (await ThumbnailsAsync(force: true)).Built);
Assert.Equal((240, 180), Entry());
var length = new FileInfo(folder.ThumbsPackPath).Length;
var again = await ThumbnailsAsync();
Assert.Equal((0, 1, 1), (again.Built, again.Reused, again.Undecodable));
Assert.Equal(length, new FileInfo(folder.ThumbsPackPath).Length);
}
private static void SetThumbnailEdge(ArchiveFixture fx, int longEdge)
{
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { ThumbnailPolicy = manifest.ThumbnailPolicy with { LongEdge = longEdge } });
}
private static async Task<bool> ProxiesBuiltAsync(ArchiveFixture fx, Sequence sequence) =>
(await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt);
/// <summary>
/// Two ingested frames whose originals the archive engine takes for HEIF (its decoders read none), their size in the
/// sidecar <paramref name="width"/> × <paramref name="height"/> when given.
/// </summary>
private static async Task<(Sequence Sequence, IReadOnlyList<Frame> Frames)> HeifSequenceAsync(ArchiveFixture fx, int? width = null, int? height = null)
{
Guid id = default;
for (var i = 0; i < 2; i++)
{
var staged = fx.Stage(Start.AddSeconds(10 * i), 40 + i);
id = (await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, null, null, "heif", staged.Path, $"IMG_{i:0000}.HEIC",
staged.Size, staged.Sha256, null, null), fx.Ct)).SequenceId;
}
var sequence = (await fx.Db.Sequences.GetAsync(id, fx.Ct))!;
var folder = fx.Paths.Sequence(sequence.Folder);
var sidecar = ManifestStore.ReadSequence(folder.ManifestPath);
ManifestStore.WriteSequence(folder.ManifestPath, sidecar with
{
Frames = [.. sidecar.Frames.Select(f => f with { Format = ImageFileFormat.Heif, W = width ?? f.W, H = height ?? f.H })],
});
await using (var session = await fx.Db.Database.OpenAsync(fx.Ct))
await session.ExecuteAsync("UPDATE frames SET format = $1 WHERE sequence_id = $2", [(int)ImageFileFormat.Heif, id], fx.Ct);
return (sequence, await fx.Db.Frames.ListBySequenceAsync(id));
}
/// <summary>A JPEG proxy (320 × 240 unless sized) with <paramref name="captured"/> in its Exif, as a client makes it.</summary>
private static byte[] Proxy(DateTime captured, int seed, int width = 320, int height = 240)
{
using var image = new ImageBuffer(width, height);
var level = 0.2f + seed % 5 * 0.1f;
for (var c = 0; c < 3; c++) image.Plane(c).Fill(level + c * 0.05f);
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Titano" });
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
}
/// <summary>A provider that decodes HEIF natively (WIC with the HEIF extension on a desktop).</summary>
private sealed class NativeHeif : IImageDecoderProvider
{
public string Name => "native-heif";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Heif ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
}
}