Use shared discovery when preparing audio
This commit is contained in:
@@ -33,7 +33,11 @@ func TestLocalAudioDestinationsPreserveDistinctRecordings(t *testing.T) {
|
||||
owners[destinations[source]] = true
|
||||
writeFile(t, source, string(rune('a'+i)))
|
||||
}
|
||||
if err := materializeLocalAudioInputs(env, paths, inputs, func(string, string, string) {}); err != nil {
|
||||
sources, err := audio.DiscoverLocal("", inputs, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := materializeLocalAudioInputs(env, paths, sources, func(string, string, string) {}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i, source := range inputs {
|
||||
|
||||
@@ -292,7 +292,7 @@ func resolveConfigRelativePath(input config.ResolvedInputFile) (string, error) {
|
||||
return resolvePath(filepath.Dir(basePath), input.Path)
|
||||
}
|
||||
|
||||
func resolveAudioInputs(sessionDir string, inputs config.SessionInputsConfig) ([]string, bool, error) {
|
||||
func resolveAudioInputs(sessionDir string, inputs config.SessionInputsConfig) ([]audio.Source, bool, error) {
|
||||
hasLocal := strings.TrimSpace(inputs.AudioDir) != "" || len(inputs.AudioFiles) > 0
|
||||
if inputs.AudioS3 != nil {
|
||||
if hasLocal {
|
||||
@@ -301,77 +301,19 @@ func resolveAudioInputs(sessionDir string, inputs config.SessionInputsConfig) ([
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
local, err := resolveLocalAudioFiles(sessionDir, inputs)
|
||||
local, err := audio.DiscoverLocal(sessionDir, inputs.AudioFiles, inputs.AudioDir)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return local, false, nil
|
||||
}
|
||||
|
||||
func resolveLocalAudioFiles(sessionDir string, inputs config.SessionInputsConfig) ([]string, error) {
|
||||
if len(inputs.AudioFiles) > 0 {
|
||||
out := make([]string, 0, len(inputs.AudioFiles))
|
||||
seen := make(map[string]struct{}, len(inputs.AudioFiles))
|
||||
for _, p := range inputs.AudioFiles {
|
||||
resolved, err := resolvePath(sessionDir, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !isFlac(resolved) {
|
||||
return nil, fmt.Errorf("audio file %q must have .flac extension", resolved)
|
||||
}
|
||||
if err := requireFile(resolved, "audio file"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, exists := seen[resolved]; exists {
|
||||
return nil, fmt.Errorf("duplicate audio source %q", resolved)
|
||||
}
|
||||
seen[resolved] = struct{}{}
|
||||
out = append(out, resolved)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
audioDir, err := resolvePath(sessionDir, inputs.AudioDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := os.ReadDir(audioDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read audio directory %q: %w", audioDir, err)
|
||||
}
|
||||
|
||||
out := make([]string, 0)
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := entry.Name()
|
||||
full := filepath.Join(audioDir, name)
|
||||
if !isFlac(full) {
|
||||
continue
|
||||
}
|
||||
if err := requireFile(full, "audio file"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, full)
|
||||
}
|
||||
|
||||
if len(out) == 0 {
|
||||
return nil, fmt.Errorf("no .flac files found in audio directory %q", audioDir)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolvedAudio []string, registerInput func(kind, path, checksum string)) error {
|
||||
destinations := audio.PreparedNames(resolvedAudio)
|
||||
func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolvedAudio []audio.Source, registerInput func(kind, path, checksum string)) error {
|
||||
for _, src := range resolvedAudio {
|
||||
dst := filepath.Join(paths.AudioDir, destinations[src])
|
||||
checksum, err := copyFileIfChanged(env.ArtifactStore, src, dst)
|
||||
dst := filepath.Join(paths.AudioDir, src.PreparedName)
|
||||
checksum, err := copyFileIfChanged(env.ArtifactStore, src.Path, dst)
|
||||
if err != nil {
|
||||
return fmt.Errorf("materialize audio %q: %w", src, err)
|
||||
return fmt.Errorf("materialize audio %q: %w", src.Path, err)
|
||||
}
|
||||
registerInput("audio", dst, checksum)
|
||||
}
|
||||
@@ -454,33 +396,7 @@ func listS3AudioObjects(ctx context.Context, env *Env, campaign, sessionID strin
|
||||
}
|
||||
sessionPrefix := artifacts.S3SessionPrefix(env.Config.Pipeline.Storage.S3.RootPrefix, campaign, sessionID)
|
||||
audioPrefix := artifacts.S3AudioPrefix(sessionPrefix, env.Config.Session.Inputs.AudioS3.Prefix)
|
||||
objects, err := env.ObjectStore.List(ctx, audioPrefix)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list s3 audio objects under %q: %w", audioPrefix, err)
|
||||
}
|
||||
|
||||
audioObjects := make([]storage.ObjectInfo, 0, len(objects))
|
||||
seenBase := map[string]string{}
|
||||
for _, obj := range objects {
|
||||
key := strings.TrimSpace(obj.Key)
|
||||
if key == "" || strings.HasSuffix(key, "/") || !isFlac(key) {
|
||||
continue
|
||||
}
|
||||
base := path.Base(key)
|
||||
if previous, exists := seenBase[base]; exists && previous != key {
|
||||
return nil, fmt.Errorf("duplicate s3 audio basename %q from %q and %q", base, previous, key)
|
||||
}
|
||||
seenBase[base] = key
|
||||
obj.Key = key
|
||||
audioObjects = append(audioObjects, obj)
|
||||
}
|
||||
sort.Slice(audioObjects, func(i, j int) bool {
|
||||
return audioObjects[i].Key < audioObjects[j].Key
|
||||
})
|
||||
if len(audioObjects) == 0 {
|
||||
return nil, fmt.Errorf("no .flac files found under s3 audio prefix %q", audioPrefix)
|
||||
}
|
||||
return audioObjects, nil
|
||||
return audio.DiscoverS3(ctx, env.ObjectStore, audioPrefix)
|
||||
}
|
||||
|
||||
func countAudioInputs(inputs []manifest.InputRecord) int {
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/audio"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -214,15 +213,14 @@ func currentPrepareAudioSourceInputs(ctx context.Context, env *Env, m *manifest.
|
||||
return expected, nil
|
||||
}
|
||||
|
||||
destinations := audio.PreparedNames(local)
|
||||
expected := make([]prepareExpectedInput, 0, len(local))
|
||||
for _, source := range local {
|
||||
checksum, err := env.ArtifactStore.Checksum(source)
|
||||
checksum, err := env.ArtifactStore.Checksum(source.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expected = append(expected, prepareExpectedInput{
|
||||
kind: "audio", destinationName: destinations[source], checksum: checksum,
|
||||
kind: "audio", destinationName: source.PreparedName, checksum: checksum,
|
||||
})
|
||||
}
|
||||
return expected, nil
|
||||
|
||||
@@ -324,6 +324,7 @@ func TestPrepareResumeValidatesCurrentS3ObjectIdentity(t *testing.T) {
|
||||
key := "dnd/campaigns/forsaken/sessions/2026-05-03/audio/alice.flac"
|
||||
fake := &storage.FakeBackend{}
|
||||
fake.SeedObject(storage.FakeObject{Key: key, Data: []byte("alice")})
|
||||
fake.SeedObject(storage.FakeObject{Key: "dnd/campaigns/forsaken/sessions/2026-05-03/audio/bob.mp3", Data: []byte("bob")})
|
||||
env.ObjectStore = fake
|
||||
|
||||
if _, err := (prepareStage{}).Run(context.Background(), env, m); err != nil {
|
||||
@@ -426,3 +427,16 @@ func TestPrepareResumeChecksSelectedCatalogContent(t *testing.T) {
|
||||
env.Config.Session.Inputs.Setting = new("two")
|
||||
assertPrepareResume(t, env, m, false)
|
||||
}
|
||||
|
||||
func TestPrepareResumeTracksMixedAudioInventory(t *testing.T) {
|
||||
env, m := setupPrepareEnv(t)
|
||||
root := filepath.Dir(env.Config.SessionPath)
|
||||
writeFile(t, filepath.Join(root, "audio", "first.flac"), "first")
|
||||
writeFile(t, filepath.Join(root, "audio", "second.mp3"), "second")
|
||||
if _, err := (prepareStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertPrepareResume(t, env, m, true)
|
||||
writeFile(t, filepath.Join(root, "audio", "third.WAV"), "third")
|
||||
assertPrepareResume(t, env, m, false)
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@ func TestPrepareStageExplicitAudioFiles(t *testing.T) {
|
||||
|
||||
root := filepath.Dir(env.Config.SessionPath)
|
||||
audioA := filepath.Join(root, "audio", "alice.flac")
|
||||
audioB := filepath.Join(root, "audio", "bob.flac")
|
||||
audioB := filepath.Join(root, "audio", "bob.mp3")
|
||||
writeFile(t, audioA, "a")
|
||||
writeFile(t, audioB, "b")
|
||||
env.Config.Session.Inputs.AudioFiles = []string{"./audio/alice.flac", "./audio/bob.flac"}
|
||||
env.Config.Session.Inputs.AudioFiles = []string{"./audio/alice.flac", "./audio/bob.mp3"}
|
||||
|
||||
s := prepareStage{}
|
||||
result, err := s.Run(context.Background(), env, m)
|
||||
@@ -49,7 +49,7 @@ func TestPrepareStageExplicitAudioFiles(t *testing.T) {
|
||||
filepath.Join(paths.InputsDir, "players.yml"),
|
||||
filepath.Join(paths.InputsDir, "party.yml"),
|
||||
filepath.Join(paths.AudioDir, "alice.flac"),
|
||||
filepath.Join(paths.AudioDir, "bob.flac"),
|
||||
filepath.Join(paths.AudioDir, "bob.mp3"),
|
||||
} {
|
||||
if _, err := os.Stat(p); err != nil {
|
||||
t.Fatalf("expected materialized file %q: %v", p, err)
|
||||
@@ -71,6 +71,7 @@ func TestPrepareStageAudioDirEnumeration(t *testing.T) {
|
||||
|
||||
root := filepath.Dir(env.Config.SessionPath)
|
||||
writeFile(t, filepath.Join(root, "audio", "a.flac"), "a")
|
||||
writeFile(t, filepath.Join(root, "audio", "b.WAV"), "b")
|
||||
writeFile(t, filepath.Join(root, "audio", "ignore.txt"), "x")
|
||||
env.Config.Session.Inputs.AudioFiles = nil
|
||||
env.Config.Session.Inputs.AudioDir = "./audio"
|
||||
@@ -80,8 +81,8 @@ func TestPrepareStageAudioDirEnumeration(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("prepare.Run() error = %v", err)
|
||||
}
|
||||
if got := result.Metadata["audio_files_resolved"]; got != 1 {
|
||||
t.Fatalf("audio_files_resolved = %#v, want 1", got)
|
||||
if got := result.Metadata["audio_files_resolved"]; got != 2 {
|
||||
t.Fatalf("audio_files_resolved = %#v, want 2", got)
|
||||
}
|
||||
|
||||
paths := sessionPathsForEnv(env, m.SessionID)
|
||||
@@ -89,7 +90,7 @@ func TestPrepareStageAudioDirEnumeration(t *testing.T) {
|
||||
t.Fatalf("expected copied flac: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(paths.AudioDir, "ignore.txt")); err == nil {
|
||||
t.Fatalf("unexpected non-flac copied")
|
||||
t.Fatalf("unexpected sidecar copied")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,17 +117,6 @@ func TestPrepareStageValidationFailures(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid audio extension", func(t *testing.T) {
|
||||
env, m := setupPrepareEnv(t)
|
||||
root := filepath.Dir(env.Config.SessionPath)
|
||||
writeFile(t, filepath.Join(root, "audio", "a.mp3"), "x")
|
||||
env.Config.Session.Inputs.AudioFiles = []string{"./audio/a.mp3"}
|
||||
|
||||
_, err := (prepareStage{}).Run(context.Background(), env, m)
|
||||
if err == nil || !strings.Contains(strings.ToLower(err.Error()), ".flac") {
|
||||
t.Fatalf("error = %v, want .flac error", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrepareStageIdempotent(t *testing.T) {
|
||||
@@ -296,7 +286,7 @@ func TestPrepareStageS3AudioDownloadAndMaterialization(t *testing.T) {
|
||||
|
||||
fake := &storage.FakeBackend{}
|
||||
fake.SeedObject(storage.FakeObject{Key: "dnd/campaigns/forsaken/sessions/2026-05-03/audio/alice.flac", Data: []byte("alice")})
|
||||
fake.SeedObject(storage.FakeObject{Key: "dnd/campaigns/forsaken/sessions/2026-05-03/audio/bob.FLAC", Data: []byte("bob")})
|
||||
fake.SeedObject(storage.FakeObject{Key: "dnd/campaigns/forsaken/sessions/2026-05-03/audio/bob.MP3", Data: []byte("bob")})
|
||||
fake.SeedObject(storage.FakeObject{Key: "dnd/campaigns/forsaken/sessions/2026-05-03/audio/ignore.txt", Data: []byte("x")})
|
||||
env.ObjectStore = fake
|
||||
|
||||
@@ -309,7 +299,7 @@ func TestPrepareStageS3AudioDownloadAndMaterialization(t *testing.T) {
|
||||
}
|
||||
|
||||
aliceWork := filepath.Join(sessionPathsForEnv(env, m.SessionID).AudioDir, "alice.flac")
|
||||
bobWork := filepath.Join(sessionPathsForEnv(env, m.SessionID).AudioDir, "bob.FLAC")
|
||||
bobWork := filepath.Join(sessionPathsForEnv(env, m.SessionID).AudioDir, "bob.MP3")
|
||||
for _, p := range []string{aliceWork, bobWork} {
|
||||
if _, err := os.Stat(p); err != nil {
|
||||
t.Fatalf("expected file %q: %v", p, err)
|
||||
@@ -390,11 +380,11 @@ func TestPrepareStageS3AudioFailures(t *testing.T) {
|
||||
wantError string
|
||||
}{
|
||||
{
|
||||
name: "no flac files",
|
||||
name: "no audio candidates",
|
||||
setup: func(_ *Env, _ *manifest.Manifest, fake *storage.FakeBackend) {
|
||||
fake.SeedObject(storage.FakeObject{Key: "dnd/campaigns/forsaken/sessions/2026-05-03/audio/readme.txt", Data: []byte("x")})
|
||||
},
|
||||
wantError: "no .flac files found",
|
||||
wantError: "no audio candidates",
|
||||
},
|
||||
{
|
||||
name: "list error",
|
||||
|
||||
Reference in New Issue
Block a user