Stream changed local audio into the workspace

This commit is contained in:
2026-12-01 20:28:58 +00:00
parent 59813b72fa
commit d70bf98871
3 changed files with 77 additions and 8 deletions

View File

@@ -652,14 +652,7 @@ func copyFileIfChanged(store artifacts.Store, src, dst string) (string, error) {
}
}
data, err := os.ReadFile(src)
if err != nil {
return "", err
}
if err := store.WriteFileAtomic(dst, data, fileops.WorkspaceFileMode); err != nil {
return "", err
}
return srcChecksum, nil
return fileops.CopyFileAtomicWithChecksum(src, dst, fileops.WorkspaceFileMode)
}
func writeBytesIfChanged(store artifacts.Store, dst string, data []byte) (string, error) {

View File

@@ -0,0 +1,73 @@
package stage
import (
"os"
"path/filepath"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
)
type disappearingInputStore struct {
artifacts.Store
source string
}
func (s disappearingInputStore) Checksum(path string) (string, error) {
sum, err := s.Store.Checksum(path)
if path == s.source && err == nil {
err = os.Remove(path)
}
return sum, err
}
func TestCopyFileIfChangedStreamsAndPreservesDestination(t *testing.T) {
root := t.TempDir()
src, dst := filepath.Join(root, "source.flac"), filepath.Join(root, "prepared", "source.flac")
if err := os.WriteFile(src, []byte("first recording"), 0600); err != nil {
t.Fatal(err)
}
store := artifacts.NewLocalStore(root)
first, err := copyFileIfChanged(store, src, dst)
if err != nil {
t.Fatal(err)
}
info, err := os.Stat(dst)
if err != nil {
t.Fatal(err)
}
second, err := copyFileIfChanged(store, src, dst)
if err != nil {
t.Fatal(err)
}
same, err := os.Stat(dst)
if err != nil || !os.SameFile(info, same) || first != second {
t.Fatalf("unchanged copy: %v", err)
}
if err := os.WriteFile(src, []byte("changed recording"), 0600); err != nil {
t.Fatal(err)
}
changed, err := copyFileIfChanged(store, src, dst)
if err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(dst)
if err != nil || string(data) != "changed recording" {
t.Fatalf("destination=%q error=%v", data, err)
}
checksum, err := artifacts.SHA256File(dst)
if err != nil || checksum != changed || changed == first {
t.Fatalf("checksum=%q changed=%q error=%v", checksum, changed, err)
}
if err := os.WriteFile(src, []byte("replacement"), 0600); err != nil {
t.Fatal(err)
}
_, err = copyFileIfChanged(disappearingInputStore{Store: store, source: src}, src, dst)
if err == nil {
t.Fatal("expected missing source during replacement")
}
data, err = os.ReadFile(dst)
if err != nil || string(data) != "changed recording" {
t.Fatalf("destination after failure=%q error=%v", data, err)
}
}