Prevent collisions between prepared audio filenames
This commit is contained in:
@@ -327,7 +327,12 @@ prerequisites, and map-insertion independence against the same observable order.
|
|||||||
|
|
||||||
**Priority:** P2
|
**Priority:** P2
|
||||||
|
|
||||||
**Status:** Pending
|
**Status:** Complete
|
||||||
|
|
||||||
|
Destination assignment reserves all original basenames and checks generated
|
||||||
|
names globally in deterministic source order, adding suffixes only on conflict.
|
||||||
|
Verified distinct prepared bytes, original-name preservation, input-order
|
||||||
|
independence, and existing duplicate-basename behavior with stage tests.
|
||||||
|
|
||||||
**Stage:** 17; retained local-audio acquisition behavior.
|
**Stage:** 17; retained local-audio acquisition behavior.
|
||||||
|
|
||||||
|
|||||||
41
internal/stage/audio_destinations_test.go
Normal file
41
internal/stage/audio_destinations_test.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package stage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLocalAudioDestinationsPreserveDistinctRecordings(t *testing.T) {
|
||||||
|
env, m := setupPrepareEnv(t)
|
||||||
|
paths := sessionPathsForEnv(env, m.SessionID)
|
||||||
|
first := filepath.Join(t.TempDir(), "one.flac")
|
||||||
|
second := filepath.Join(t.TempDir(), "one.flac")
|
||||||
|
digest := sha256.Sum256([]byte(filepath.Clean(first)))
|
||||||
|
generated := "one-" + hex.EncodeToString(digest[:8]) + ".flac"
|
||||||
|
third := filepath.Join(t.TempDir(), generated)
|
||||||
|
inputs := []string{first, second, third}
|
||||||
|
destinations := localAudioDestinations(inputs)
|
||||||
|
if reversed := localAudioDestinations([]string{third, second, first}); !reflect.DeepEqual(reversed, destinations) {
|
||||||
|
t.Fatal("destination ownership depends on input order")
|
||||||
|
}
|
||||||
|
if destinations[third] != generated {
|
||||||
|
t.Fatal("noncolliding original filename changed")
|
||||||
|
}
|
||||||
|
owners := make(map[string]bool)
|
||||||
|
for i, source := range inputs {
|
||||||
|
if owners[destinations[source]] {
|
||||||
|
t.Fatal("distinct recordings share a destination")
|
||||||
|
}
|
||||||
|
owners[destinations[source]] = true
|
||||||
|
writeFile(t, source, string(rune('a'+i)))
|
||||||
|
}
|
||||||
|
if err := materializeLocalAudioInputs(env, paths, inputs, func(string, string, string) {}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for i, source := range inputs {
|
||||||
|
mustReadFileEquals(t, filepath.Join(paths.AudioDir, destinations[source]), string(rune('a'+i)))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -380,23 +380,37 @@ func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolve
|
|||||||
|
|
||||||
func localAudioDestinations(audioPaths []string) map[string]string {
|
func localAudioDestinations(audioPaths []string) map[string]string {
|
||||||
byBase := make(map[string][]string, len(audioPaths))
|
byBase := make(map[string][]string, len(audioPaths))
|
||||||
|
occupied := make(map[string]bool, len(audioPaths))
|
||||||
for _, audioPath := range audioPaths {
|
for _, audioPath := range audioPaths {
|
||||||
base := filepath.Base(audioPath)
|
base := filepath.Base(audioPath)
|
||||||
byBase[base] = append(byBase[base], audioPath)
|
byBase[base] = append(byBase[base], audioPath)
|
||||||
|
occupied[base] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
destinations := make(map[string]string, len(audioPaths))
|
destinations := make(map[string]string, len(audioPaths))
|
||||||
for base, sources := range byBase {
|
ordered := append([]string(nil), audioPaths...)
|
||||||
if len(sources) == 1 {
|
sort.Strings(ordered)
|
||||||
destinations[sources[0]] = base
|
nextSuffix := make(map[string]int)
|
||||||
|
for _, source := range ordered {
|
||||||
|
if _, assigned := destinations[source]; assigned {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
base := filepath.Base(source)
|
||||||
|
if len(byBase[base]) == 1 {
|
||||||
|
destinations[source] = base
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
extension := filepath.Ext(base)
|
extension := filepath.Ext(base)
|
||||||
stem := strings.TrimSuffix(base, extension)
|
stem := strings.TrimSuffix(base, extension)
|
||||||
for _, source := range sources {
|
digest := sha256.Sum256([]byte(filepath.Clean(source)))
|
||||||
digest := sha256.Sum256([]byte(filepath.Clean(source)))
|
generatedStem := stem + "-" + hex.EncodeToString(digest[:8])
|
||||||
destinations[source] = fmt.Sprintf("%s-%s%s", stem, hex.EncodeToString(digest[:8]), extension)
|
candidate := generatedStem + extension
|
||||||
|
for occupied[candidate] {
|
||||||
|
nextSuffix[generatedStem+extension]++
|
||||||
|
candidate = fmt.Sprintf("%s-%d%s", generatedStem, nextSuffix[generatedStem+extension], extension)
|
||||||
}
|
}
|
||||||
|
destinations[source] = candidate
|
||||||
|
occupied[candidate] = true
|
||||||
}
|
}
|
||||||
return destinations
|
return destinations
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user