93 lines
3.1 KiB
Go
93 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestWriteCreatorMediaPublishesOnlyCompleteFiles(t *testing.T) {
|
|
destination := filepath.Join(t.TempDir(), "source")
|
|
if err := writeCreatorMedia(destination, []byte("media")); err != nil {
|
|
t.Fatalf("write media: %v", err)
|
|
}
|
|
content, err := os.ReadFile(destination)
|
|
if err != nil {
|
|
t.Fatalf("read media: %v", err)
|
|
}
|
|
if string(content) != "media" {
|
|
t.Fatalf("unexpected media: %q", content)
|
|
}
|
|
matches, err := filepath.Glob(filepath.Join(filepath.Dir(destination), ".creator-media-*"))
|
|
if err != nil {
|
|
t.Fatalf("find temporary media: %v", err)
|
|
}
|
|
if len(matches) != 0 {
|
|
t.Fatalf("temporary files remain: %v", matches)
|
|
}
|
|
}
|
|
|
|
func TestWriteCreatorMediaRejectsEmptyAndOversizedFiles(t *testing.T) {
|
|
destination := filepath.Join(t.TempDir(), "source")
|
|
if err := writeCreatorMedia(destination, nil); err == nil {
|
|
t.Fatal("expected empty media to be rejected")
|
|
}
|
|
if err := writeCreatorMedia(destination, make([]byte, maxCreatorMediaBytes+1)); err == nil {
|
|
t.Fatal("expected oversized media to be rejected")
|
|
}
|
|
}
|
|
|
|
func TestExtractCreatorAudioPublishesWavAtomically(t *testing.T) {
|
|
dir := t.TempDir()
|
|
fakeFFmpeg := filepath.Join(dir, "ffmpeg")
|
|
argsFile := filepath.Join(dir, "args")
|
|
script := "#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$ARGS_FILE\"\nfor arg in \"$@\"; do output=\"$arg\"; done\nprintf 'RIFFfake' > \"$output\"\n"
|
|
if err := os.WriteFile(fakeFFmpeg, []byte(script), 0o700); err != nil {
|
|
t.Fatalf("write fake ffmpeg: %v", err)
|
|
}
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
t.Setenv("ARGS_FILE", argsFile)
|
|
videoPath := filepath.Join(dir, "source")
|
|
audioPath := filepath.Join(dir, "audio.wav")
|
|
if err := os.WriteFile(videoPath, []byte("video"), 0o600); err != nil {
|
|
t.Fatalf("write source: %v", err)
|
|
}
|
|
if err := extractCreatorAudio(context.Background(), videoPath, audioPath); err != nil {
|
|
t.Fatalf("extract audio: %v", err)
|
|
}
|
|
if !fileExists(audioPath) {
|
|
t.Fatal("expected published audio")
|
|
}
|
|
if _, err := os.Stat(audioPath + ".tmp"); !os.IsNotExist(err) {
|
|
t.Fatalf("temporary audio remains: %v", err)
|
|
}
|
|
args, err := os.ReadFile(argsFile)
|
|
if err != nil {
|
|
t.Fatalf("read ffmpeg args: %v", err)
|
|
}
|
|
if !strings.Contains(string(args), "-f\n") || !strings.Contains(string(args), "\nwav\n") {
|
|
t.Fatalf("ffmpeg did not request WAV output: %s", args)
|
|
}
|
|
}
|
|
|
|
func TestTranscribeCreatorAudioRequiresExplicitProvider(t *testing.T) {
|
|
t.Setenv("CREATOR_TRANSCRIPTION_BIN", "")
|
|
if _, err := transcribeCreatorAudio(context.Background(), "/tmp/audio.wav", "whisper", "base"); err == nil {
|
|
t.Fatal("expected missing provider error")
|
|
}
|
|
}
|
|
|
|
func TestTranscribeCreatorAudioUsesConfiguredBinaryPath(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "whisper")
|
|
if err := os.WriteFile(path, []byte("#!/bin/sh\nprintf configured-transcript\n"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("CREATOR_TRANSCRIPTION_BIN", path)
|
|
transcript, err := transcribeCreatorAudio(context.Background(), "/tmp/audio.wav", "whisper", "base")
|
|
if err != nil || transcript != "configured-transcript" {
|
|
t.Fatalf("configured transcription: transcript=%q err=%v", transcript, err)
|
|
}
|
|
}
|