Share prepared audio names and speaker identities

This commit is contained in:
2026-12-02 03:13:58 +00:00
parent 9b59fa16b2
commit f902e50def
9 changed files with 159 additions and 55 deletions

View File

@@ -2,6 +2,8 @@ package audio
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"os"
"path/filepath"
@@ -37,7 +39,7 @@ func TestDiscoverLocalSelection(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want := []string{filepath.Join(root, "one.MP3"), filepath.Join(root, "two.flac")}
want := []Source{{filepath.Join(root, "one.MP3"), "one.MP3"}, {filepath.Join(root, "two.flac"), "two.flac"}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
@@ -45,7 +47,7 @@ func TestDiscoverLocalSelection(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want = []string{filepath.Join(root, "extensionless"), filepath.Join(root, "unfamiliar.xyz")}
want = []Source{{filepath.Join(root, "extensionless"), "extensionless"}, {filepath.Join(root, "unfamiliar.xyz"), "unfamiliar.xyz"}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
@@ -104,3 +106,47 @@ func TestDiscoverS3Selection(t *testing.T) {
t.Fatalf("listing error: %v", err)
}
}
func TestDiscoveryPreparedNamesAndSpeakerCollisions(t *testing.T) {
root := t.TempDir()
first := filepath.Join(root, "first", "speaker.flac")
second := filepath.Join(root, "second", "speaker.flac")
for _, file := range []string{first, second} {
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(file, []byte("audio"), 0600); err != nil {
t.Fatal(err)
}
}
digest := sha256.Sum256([]byte(filepath.Clean(first)))
occupiedName := "speaker-" + hex.EncodeToString(digest[:8]) + ".flac"
occupied := filepath.Join(root, occupiedName)
if err := os.WriteFile(occupied, []byte("audio"), 0600); err != nil {
t.Fatal(err)
}
sources, err := DiscoverLocal(root, []string{first, second, occupied}, "")
if err != nil {
t.Fatal(err)
}
names := map[string]string{}
for _, source := range sources {
names[source.Path] = source.PreparedName
}
if names[first] != strings.TrimSuffix(occupiedName, ".flac")+"-1.flac" || names[occupied] != occupiedName || names[first] == names[second] {
t.Fatalf("unexpected names: %v", names)
}
mp3, wav := filepath.Join(root, "speaker.mp3"), filepath.Join(root, "speaker.wav")
for _, file := range []string{mp3, wav} {
if err := os.WriteFile(file, nil, 0600); err != nil {
t.Fatal(err)
}
}
if _, err := DiscoverLocal(root, []string{mp3, wav}, ""); err == nil || !strings.Contains(err.Error(), mp3) || !strings.Contains(err.Error(), wav) {
t.Fatalf("collision error: %v", err)
}
store := &discoveryLister{objects: []storage.ObjectInfo{{Key: "x/speaker.mp3"}, {Key: "x/speaker.wav"}}}
if _, err := DiscoverS3(context.Background(), store, "x/"); err == nil || !strings.Contains(err.Error(), "x/speaker.mp3") || !strings.Contains(err.Error(), "x/speaker.wav") {
t.Fatalf("remote collision: %v", err)
}
}