refactor(pipeline): MP1b review - unify DatCollectionAdapter + TOC/log test gaps

Adversarially-verified review findings 7 and 8:

(7) The DatCollection->IDatReaderWriter adapter existed as THREE
near-identical copies (App-internal original, Bake's copy, Content.
Tests' copy) — a structure where adapter drift is exactly what the
live-vs-pak equivalence suite cannot detect (both sides would only
drift together if they shared one implementation). Now ONE public
AcDream.Content.DatCollectionAdapter next to IDatReaderWriter (GL-free
home established in MP1a), carrying App's FULL behavior including the
[dat-miss] TryGet tripwire log (which now also covers the bake tool
and the equivalence suite) and the caching/locking. All three copies
deleted; WbMeshAdapter (App), BakeRunner (Bake), and
PakEquivalenceTests (Content.Tests) resolve the shared class.
Iteration properties return the REAL dat iterations — the App copy's
hardcoded 0 was a stub nothing read; the unification intentionally
keeps truth (noted in the doc comment). Verified post-move: no
Silk.NET anywhere in Content / Bake / Content.Tests / Bake.Tests
resolved dependency graphs.

(8) Two test gaps closed in PakRoundTripTests: (a) direct on-disk TOC
sortedness — blobs added in DESCENDING key order, then the raw file
bytes parsed (not through the reader) and every TOC entry asserted
strictly ascending; (b) corrupt-blob logging — five repeated reads
through both public paths (TryReadObjectMeshData + ContainsKey) with
stderr captured, asserting exactly ONE [pak-corrupt] line for the
victim key.

Full suite: 4120 tests, 0 failures (Content.Tests 56, Bake.Tests 1,
plus the pre-existing 4 skips).
This commit is contained in:
Erik 2026-07-05 22:18:30 +02:00
parent 86e0dc4655
commit 859cf5ec02
7 changed files with 135 additions and 351 deletions

View file

@ -383,6 +383,79 @@ public class PakRoundTripTests : IDisposable {
Assert.False(File.Exists(path));
}
// ---- on-disk TOC sortedness (review finding 8) ---------------------------
[Fact]
public void OnDiskToc_IsSortedAscendingByKey_RegardlessOfAddOrder() {
var path = NewTempPakPath();
// Add blobs in DESCENDING key order — the on-disk TOC must still come
// out ascending (the reader's binary-search precondition, asserted
// here against the raw bytes, not through the reader).
var blobs = MakeBlobSet(12).OrderByDescending(b => PakKey.Compose(b.Type, b.FileId)).ToArray();
WritePak(path, blobs);
var fileBytes = File.ReadAllBytes(path);
var header = PakHeader.ReadFrom((ReadOnlySpan<byte>)fileBytes);
Assert.Equal((uint)blobs.Length, header.TocCount);
ulong previousKey = 0;
for (uint i = 0; i < header.TocCount; i++) {
int pos = checked((int)((long)header.TocOffset + i * PakTocEntry.Size));
var entry = PakTocEntry.ReadFrom(fileBytes.AsSpan(pos, PakTocEntry.Size));
Assert.True(entry.Key > previousKey || i == 0,
$"TOC entry {i} key 0x{entry.Key:X16} is not strictly greater than its predecessor 0x{previousKey:X16}");
previousKey = entry.Key;
}
}
// ---- corruption logged once (review finding 8) ----------------------------
[Fact]
public void CorruptBlob_RepeatedReads_LogExactlyOnce() {
var path = NewTempPakPath();
var blobs = MakeBlobSet(2);
WritePak(path, blobs);
// Flip a byte in the first blob (plain CRC corruption).
using (var fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite)) {
fs.Position = PakHeader.Size + 4;
int b = fs.ReadByte();
fs.Position = PakHeader.Size + 4;
fs.WriteByte((byte)(b ^ 0xFF));
}
var victimKey = PakKey.Compose(blobs[0].Type, blobs[0].FileId);
var originalError = Console.Error;
var capture = new StringWriter();
try {
Console.SetError(capture);
using var reader = new PakReader(path);
// Hammer the corrupt entry through BOTH public paths, repeatedly.
Assert.False(reader.TryReadObjectMeshData(victimKey, out _));
Assert.False(reader.TryReadObjectMeshData(victimKey, out _));
Assert.False(reader.ContainsKey(victimKey));
Assert.False(reader.ContainsKey(victimKey));
Assert.False(reader.TryReadObjectMeshData(victimKey, out _));
}
finally {
Console.SetError(originalError);
}
string logged = capture.ToString();
int occurrences = CountOccurrences(logged, $"0x{victimKey:X16}");
Assert.True(occurrences == 1,
$"expected exactly ONE [pak-corrupt] line for key 0x{victimKey:X16} across 5 reads, got {occurrences}:\n{logged}");
}
private static int CountOccurrences(string haystack, string needle) {
int count = 0, index = 0;
while ((index = haystack.IndexOf(needle, index, StringComparison.Ordinal)) >= 0) {
count++;
index += needle.Length;
}
return count;
}
// ---- helpers -------------------------------------------------------------
/// <summary>Locates the on-disk file position of the TOC entry for <paramref name="key"/> by raw parsing.</summary>