archive.json and sequence.json with source-generated JSON, forward-compatible reading (unknown fields kept, unknown enum names tolerated) and atomic writes; lease files with expiry and stale takeover; thumbs.pack with index, crash-safe append and random access; folder scanning, in-place adoption, reconcile, verify, index snapshot, rename planning and thumbnail/proxy rendering. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
166 lines
6.0 KiB
C#
166 lines
6.0 KiB
C#
using Titano.Core.Archiving;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Archiving;
|
|
|
|
[Category("archive")]
|
|
public sealed class ThumbPackTests(TestContext context)
|
|
{
|
|
private static byte[] Payload(int seed, int length = 300)
|
|
{
|
|
var data = new byte[length];
|
|
new Random(seed).NextBytes(data);
|
|
return data;
|
|
}
|
|
|
|
private static byte[] Hash(int seed) => ThumbPack.Fingerprint(null, $"originals/{seed}.jpg", seed);
|
|
|
|
private string NewPack() => Path.Combine(context.CreateTempDirectory(), ".titano", "thumbs.pack");
|
|
|
|
private static void Fill(string path, int count, int seedOffset = 0)
|
|
{
|
|
using var writer = ThumbPackWriter.Open(path);
|
|
for (var i = 0; i < count; i++) writer.Append(i, 320, 213, Hash(i + seedOffset), Payload(i + seedOffset));
|
|
}
|
|
|
|
[Test]
|
|
public void EntriesAreReadBackByFrameIndex()
|
|
{
|
|
var path = NewPack();
|
|
Fill(path, 100);
|
|
using var reader = ThumbPackReader.Open(path);
|
|
Assert.Equal(100, reader.Entries.Count);
|
|
Assert.BytesEqual(Payload(57), reader.Read(57));
|
|
Assert.BytesEqual(Hash(57), reader.Entries[57].SourceHash);
|
|
Assert.Equal(320, reader.Entries[57].Width);
|
|
Assert.Null(reader.Read(100));
|
|
}
|
|
|
|
[Test]
|
|
public void AppendingAgainReplacesAFrameAndKeepsTheOthers()
|
|
{
|
|
var path = NewPack();
|
|
Fill(path, 3);
|
|
using (var writer = ThumbPackWriter.Open(path))
|
|
{
|
|
Assert.Equal(3, writer.Entries.Count);
|
|
writer.Append(1, 200, 100, Hash(99), Payload(99));
|
|
writer.Append(3, 200, 100, Hash(3), Payload(3));
|
|
}
|
|
using var reader = ThumbPackReader.Open(path);
|
|
Assert.Equal(4, reader.Entries.Count);
|
|
Assert.BytesEqual(Payload(99), reader.Read(1));
|
|
Assert.BytesEqual(Payload(0), reader.Read(0));
|
|
Assert.BytesEqual(Payload(3), reader.Read(3));
|
|
}
|
|
|
|
[Test]
|
|
public void AnIncompleteTailIsIgnoredAndTruncatedByTheNextWriter()
|
|
{
|
|
var path = NewPack();
|
|
Fill(path, 2);
|
|
var committed = new FileInfo(path).Length;
|
|
// Simulated crash mid-append: an entry header and part of its payload, no index.
|
|
using (var stream = new FileStream(path, FileMode.Append))
|
|
{
|
|
stream.Write("TENT"u8);
|
|
stream.Write(BitConverter.GetBytes(2));
|
|
stream.Write(new byte[30]);
|
|
}
|
|
using (var reader = ThumbPackReader.Open(path))
|
|
{
|
|
Assert.Equal(2, reader.Entries.Count);
|
|
Assert.BytesEqual(Payload(1), reader.Read(1));
|
|
}
|
|
using (var writer = ThumbPackWriter.Open(path))
|
|
{
|
|
Assert.Equal(committed, new FileInfo(path).Length);
|
|
writer.Append(2, 320, 213, Hash(2), Payload(2));
|
|
}
|
|
using var after = ThumbPackReader.Open(path);
|
|
Assert.Equal(3, after.Entries.Count);
|
|
Assert.BytesEqual(Payload(2), after.Read(2));
|
|
}
|
|
|
|
[Test]
|
|
public void EntriesWrittenAfterTheLastIndexAreFoundWithoutIt()
|
|
{
|
|
var path = NewPack();
|
|
Fill(path, 5);
|
|
// Simulated crash after new entries overwrote the old index but before the new index was written.
|
|
var withoutIndex = File.ReadAllBytes(path);
|
|
using (var reader = ThumbPackReader.Open(path))
|
|
{
|
|
var end = reader.Entries.Values.Max(e => e.Offset + e.Length);
|
|
File.WriteAllBytes(path, withoutIndex[..(int)end]);
|
|
}
|
|
using var scanned = ThumbPackReader.Open(path);
|
|
Assert.Equal(5, scanned.Entries.Count);
|
|
for (var i = 0; i < 5; i++) Assert.BytesEqual(Payload(i), scanned.Read(i));
|
|
}
|
|
|
|
[Test]
|
|
public void ADamagedThumbnailReadsAsMissing()
|
|
{
|
|
var path = NewPack();
|
|
Fill(path, 3);
|
|
long offset;
|
|
using (var reader = ThumbPackReader.Open(path)) offset = reader.Entries[1].Offset;
|
|
using (var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
|
|
{
|
|
stream.Position = offset + 10;
|
|
var b = stream.ReadByte();
|
|
stream.Position = offset + 10;
|
|
stream.WriteByte((byte)(b ^ 0xFF));
|
|
}
|
|
using var damaged = ThumbPackReader.Open(path);
|
|
Assert.Null(damaged.Read(1));
|
|
Assert.BytesEqual(Payload(2), damaged.Read(2));
|
|
}
|
|
|
|
[Test]
|
|
public void AnOpenReaderKeepsReadingWhileAWriterAppends()
|
|
{
|
|
var path = NewPack();
|
|
Fill(path, 3);
|
|
using var reader = ThumbPackReader.Open(path);
|
|
using (var writer = ThumbPackWriter.Open(path))
|
|
{
|
|
for (var i = 3; i < 50; i++) writer.Append(i, 320, 213, Hash(i), Payload(i, 2000));
|
|
}
|
|
Assert.BytesEqual(Payload(0), reader.Read(0));
|
|
Assert.BytesEqual(Payload(2), reader.Read(2));
|
|
using var fresh = ThumbPackReader.Open(path);
|
|
Assert.Equal(50, fresh.Entries.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void CompactionDropsReplacedEntries()
|
|
{
|
|
var path = NewPack();
|
|
for (var round = 0; round < 4; round++) Fill(path, 20, seedOffset: round * 100);
|
|
long garbage;
|
|
using (var reader = ThumbPackReader.Open(path)) garbage = reader.GarbageBytes;
|
|
Assert.Greater(garbage, 0);
|
|
var before = new FileInfo(path).Length;
|
|
ThumbPackWriter.Compact(path);
|
|
Assert.Equal(before - garbage, new FileInfo(path).Length);
|
|
using var compacted = ThumbPackReader.Open(path);
|
|
Assert.Equal(0L, compacted.GarbageBytes);
|
|
for (var i = 0; i < 20; i++) Assert.BytesEqual(Payload(i + 300), compacted.Read(i));
|
|
}
|
|
|
|
[Test]
|
|
public void AMissingOrForeignFileReadsAsEmpty()
|
|
{
|
|
var path = NewPack();
|
|
using (var none = ThumbPackReader.Open(path)) Assert.Equal(0, none.Entries.Count);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
File.WriteAllText(path, "not a pack");
|
|
using (var foreign = ThumbPackReader.Open(path)) Assert.Equal(0, foreign.Entries.Count);
|
|
Fill(path, 1);
|
|
using var rewritten = ThumbPackReader.Open(path);
|
|
Assert.Equal(1, rewritten.Entries.Count);
|
|
}
|
|
}
|