Files

90 lines
3.4 KiB
Go

package api
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"git.ipao.vip/rogee/creator-hub/internal/creator"
)
func TestCreatorMaterialAudioAndTranscriptionFailuresAreVisible(t *testing.T) {
binDir := t.TempDir()
fakeFFmpeg := filepath.Join(binDir, "ffmpeg")
if err := os.WriteFile(fakeFFmpeg, []byte("#!/bin/sh\nfor arg in \"$@\"; do last=\"$arg\"; done\nprintf x > \"$last\"\n"), 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
audioPath := filepath.Join(t.TempDir(), "audio.wav")
if err := extractCreatorAudio(context.Background(), "/missing/video.mp4", audioPath); err != nil {
t.Fatalf("extractCreatorAudio returned %v", err)
}
if _, err := os.Stat(audioPath); err != nil {
t.Fatalf("extracted audio was not published: %v", err)
}
if err := os.WriteFile(fakeFFmpeg, []byte("#!/bin/sh\necho ffmpeg failed >&2\nexit 1\n"), 0o700); err != nil {
t.Fatal(err)
}
if err := extractCreatorAudio(context.Background(), "/missing/video.mp4", filepath.Join(t.TempDir(), "failed.wav")); err == nil {
t.Fatal("failed ffmpeg command was reported as successful")
}
if err := validateTranscriptionBinary("missing-transcription-binary"); err == nil {
t.Fatal("missing transcription binary was reported as available")
}
if _, err := transcribeCreatorAudio(context.Background(), "audio.wav", "", "model"); err == nil {
t.Fatal("missing transcription provider was reported as successful")
}
if _, err := transcribeCreatorAudio(context.Background(), "audio.wav", "unknown", "model"); err == nil {
t.Fatal("unsupported transcription provider was reported as successful")
}
t.Setenv("CREATOR_TRANSCRIPTION_BIN", filepath.Join(binDir, "not-whisper"))
if _, err := transcribeCreatorAudio(context.Background(), "audio.wav", "whisper", "model"); err == nil {
t.Fatal("mismatched transcription binary was reported as successful")
}
whisper := filepath.Join(binDir, "whisper")
if err := os.WriteFile(whisper, []byte("#!/bin/sh\nprintf 'transcript'\n"), 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("CREATOR_TRANSCRIPTION_BIN", whisper)
text, err := transcribeCreatorAudio(context.Background(), audioPath, "whisper", "small")
if err != nil || text != "transcript" {
t.Fatalf("transcription = %q, err = %v", text, err)
}
if err := os.WriteFile(whisper, []byte("#!/bin/sh\nexit 1\n"), 0o700); err != nil {
t.Fatal(err)
}
if _, err := transcribeCreatorAudio(context.Background(), audioPath, "whisper", "small"); err == nil {
t.Fatal("failed transcription command was reported as successful")
}
}
func TestMaterialArtifactReferenceAndPathStayWithinWorkRoot(t *testing.T) {
if got := materialArtifactReference("work-1", "execution-1", "cover.jpg"); got != "work-1/.runs/execution-1/cover.jpg" {
t.Fatalf("artifact reference = %q", got)
}
root := t.TempDir()
want := filepath.Join(root, "work-1", ".runs", "execution-1", "cover.jpg")
got, err := materialArtifactPath(root, "work-1", "work-1/.runs/execution-1/cover.jpg")
if err != nil || got != want {
t.Fatalf("artifact path = %q, err = %v, want %q", got, err, want)
}
for _, reference := range []string{
"",
"/tmp/cover.jpg",
"other/.runs/execution-1/cover.jpg",
"work-1/../other/cover.jpg",
"work-1/../../outside.jpg",
"work-1",
} {
if _, err := materialArtifactPath(root, "work-1", reference); !errors.Is(err, creator.ErrInvalid) {
t.Errorf("materialArtifactPath(%q) error = %v, want creator.ErrInvalid", reference, err)
}
}
}