Stream Opus/derived read path: serve from seekable disk FileStream, never a whole-file byte[]; HasOpusAsync is index-only

This commit is contained in:
daniel-c-harvey
2026-06-26 14:58:11 -04:00
parent 1e17ffc380
commit d72263aea1
5 changed files with 241 additions and 61 deletions
+121 -7
View File
@@ -39,8 +39,8 @@ public class TrackFormatResolverTests
private static TrackFormatResolver CreateResolver(FileDb fileDatabase)
{
// The resolver only calls GetAudioBinaryAsync (a vault read), which never touches the router —
// but TrackContentService requires one, so supply a real router with real processors.
// The resolver only opens vault streams / index checks, which never touch the router — but
// TrackContentService requires one, so supply a real router with real processors.
var router = new AudioProcessorRouter(
new AudioProcessor(), new Mp3AudioProcessor(), new FlacAudioProcessor());
var contentService = new TrackContentService(fileDatabase, router);
@@ -48,6 +48,18 @@ public class TrackFormatResolverTests
fileDatabase, contentService, NullLogger<TrackFormatResolver>.Instance);
}
// Drains a ResolvedAudio's disk-backed stream to a byte[] and disposes it, so the assertions compare the
// bytes actually served (read-path streaming returns an open Stream, not a buffered AudioBinary).
private static async Task<byte[]> ReadAllAsync(Stream stream)
{
await using (stream)
{
using var ms = new MemoryStream();
await stream.CopyToAsync(ms);
return ms.ToArray();
}
}
// Seeds a source artifact in the tracks vault with the given extension, mirroring how the upload path
// stores the original bytes (WAV/MP3/FLAC). Returns the bytes for downstream identity assertions.
private static async Task<byte[]> SeedSourceAsync(FileDb db, string entryKey, string extension)
@@ -63,6 +75,14 @@ public class TrackFormatResolverTests
// Seeds the Opus audio + sidecar in the track-opus vault exactly as OpusTranscodeService does:
// audio under OpusAudioKey (the bare EntryKey) with the .opus extension, sidecar under OpusSidecarKey.
private static async Task<(byte[] opus, byte[] sidecar)> SeedOpusAsync(FileDb db, string entryKey)
{
var opusBytes = await SeedOpusAudioOnlyAsync(db, entryKey);
var sidecarBytes = await SeedOpusSidecarOnlyAsync(db, entryKey);
return (opusBytes, sidecarBytes);
}
// Seeds only the Opus audio half (no sidecar) — a half-derived track, used to prove HasOpusAsync rejects it.
private static async Task<byte[]> SeedOpusAudioOnlyAsync(FileDb db, string entryKey)
{
await db.CreateVaultAsync(VaultConstants.TrackOpus, MediaVaultType.Audio);
@@ -72,6 +92,13 @@ public class TrackFormatResolverTests
var audioOk = await db.RegisterResourceAsync(
VaultConstants.TrackOpus, OpusTranscodeService.OpusAudioKey(entryKey), opusAudio);
Assert.That(audioOk, Is.True, "opus audio seed must succeed");
return opusBytes;
}
// Seeds only the sidecar half (no Opus audio) — the other half-derived case HasOpusAsync must reject.
private static async Task<byte[]> SeedOpusSidecarOnlyAsync(FileDb db, string entryKey)
{
await db.CreateVaultAsync(VaultConstants.TrackOpus, MediaVaultType.Audio);
var sidecarBytes = new byte[] { 7, 7, 7, 7 };
var sidecar = new MediaBinary(new MediaBinaryParams(
@@ -79,8 +106,7 @@ public class TrackFormatResolverTests
var sidecarOk = await db.RegisterResourceAsync(
VaultConstants.TrackOpus, OpusTranscodeService.OpusSidecarKey(entryKey), sidecar);
Assert.That(sidecarOk, Is.True, "sidecar seed must succeed");
return (opusBytes, sidecarBytes);
return sidecarBytes;
}
[Test]
@@ -98,8 +124,8 @@ public class TrackFormatResolverTests
Assert.That(resolved, Is.Not.Null);
Assert.That(resolved!.ResolvedFormat, Is.EqualTo(AudioFormat.Lossless));
Assert.That(resolved.ContentType, Is.EqualTo("audio/flac"));
Assert.That(resolved.Audio.Buffer, Is.EqualTo(bytes));
Assert.That(resolved.DidFallBack(AudioFormat.Lossless), Is.False);
Assert.That(await ReadAllAsync(resolved.Stream), Is.EqualTo(bytes));
}
[Test]
@@ -116,8 +142,8 @@ public class TrackFormatResolverTests
Assert.That(resolved, Is.Not.Null);
Assert.That(resolved!.ResolvedFormat, Is.EqualTo(AudioFormat.Opus));
Assert.That(resolved.ContentType, Is.EqualTo("audio/ogg"));
Assert.That(resolved.Audio.Buffer, Is.EqualTo(opusBytes));
Assert.That(resolved.DidFallBack(AudioFormat.Opus), Is.False);
Assert.That(await ReadAllAsync(resolved.Stream), Is.EqualTo(opusBytes));
}
[Test]
@@ -136,8 +162,8 @@ public class TrackFormatResolverTests
"the resolved format must reflect what was actually returned");
Assert.That(resolved.ContentType, Is.EqualTo("audio/wav"),
"a fallback returns the lossless content-type so the decoder picks the right decoder");
Assert.That(resolved.Audio.Buffer, Is.EqualTo(bytes));
Assert.That(resolved.DidFallBack(AudioFormat.Opus), Is.True);
Assert.That(await ReadAllAsync(resolved.Stream), Is.EqualTo(bytes));
}
[Test]
@@ -177,4 +203,92 @@ public class TrackFormatResolverTests
Assert.That(bytes, Is.Null);
}
// --- HasOpusAsync completeness matrix (both halves required) ---
[Test]
public async Task HasOpusAsync_WhenAudioAndSidecarPresent_ReturnsTrue()
{
var db = (await FileDb.FromAsync(_testDir))!;
const string entryKey = "complete-opus";
await SeedOpusAsync(db, entryKey);
var resolver = CreateResolver(db);
Assert.That(await resolver.HasOpusAsync(entryKey), Is.True);
}
[Test]
public async Task HasOpusAsync_WhenSidecarMissing_ReturnsFalse()
{
var db = (await FileDb.FromAsync(_testDir))!;
const string entryKey = "audio-only-opus";
await SeedOpusAudioOnlyAsync(db, entryKey);
var resolver = CreateResolver(db);
Assert.That(await resolver.HasOpusAsync(entryKey), Is.False,
"audio without the seek/setup sidecar is unseekable — counts as not-having-Opus");
}
[Test]
public async Task HasOpusAsync_WhenAudioMissing_ReturnsFalse()
{
var db = (await FileDb.FromAsync(_testDir))!;
const string entryKey = "sidecar-only-opus";
await SeedOpusSidecarOnlyAsync(db, entryKey);
var resolver = CreateResolver(db);
Assert.That(await resolver.HasOpusAsync(entryKey), Is.False);
}
[Test]
public async Task HasOpusAsync_WhenNeitherPresent_ReturnsFalse()
{
var db = (await FileDb.FromAsync(_testDir))!;
var resolver = CreateResolver(db);
// No track-opus vault at all (and no entries) — must miss to false, not throw.
Assert.That(await resolver.HasOpusAsync("ghost"), Is.False);
}
[Test]
public async Task HasOpusAsync_IsIndexOnly_DoesNotReadFileBodies()
{
var db = (await FileDb.FromAsync(_testDir))!;
const string entryKey = "indexed-but-bodiless";
await SeedOpusAsync(db, entryKey);
var resolver = CreateResolver(db);
// Delete the backing files but leave the index entries intact. An index-only existence check still
// reports true; a body-loading implementation would now miss. This is the load-bearing assertion
// that HasOpusAsync never streams a file body (the opus-status loop runs it over the whole catalogue).
var vaultDir = Path.Combine(_testDir, VaultConstants.TrackOpus);
foreach (var file in Directory.EnumerateFiles(vaultDir))
{
if (Path.GetFileName(file) != "index")
File.Delete(file);
}
Assert.That(await resolver.HasOpusAsync(entryKey), Is.True,
"HasOpusAsync must be index-only: true even when no file bodies are present");
}
[Test]
public async Task ResolveAsync_DisposingResult_DisposesUnderlyingStream()
{
var db = (await FileDb.FromAsync(_testDir))!;
const string entryKey = "dispose-track";
await SeedSourceAsync(db, entryKey, ".wav");
var resolver = CreateResolver(db);
var resolved = await resolver.ResolveAsync(entryKey, AudioFormat.Lossless);
Assert.That(resolved, Is.Not.Null);
var stream = resolved!.Stream;
Assert.That(stream.CanRead, Is.True, "a freshly resolved handle holds an open stream");
await resolved.DisposeAsync();
Assert.That(stream.CanRead, Is.False,
"disposing ResolvedAudio must dispose the underlying FileStream so a handle never leaks on the throw path");
}
}