|
| 1 | +package openai_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "mime" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/sashabaranov/go-openai" |
| 15 | + "github.com/sashabaranov/go-openai/internal/test" |
| 16 | + "github.com/sashabaranov/go-openai/internal/test/checks" |
| 17 | +) |
| 18 | + |
| 19 | +func TestSpeechIntegration(t *testing.T) { |
| 20 | + client, server, teardown := setupOpenAITestServer() |
| 21 | + defer teardown() |
| 22 | + |
| 23 | + server.RegisterHandler("/v1/audio/speech", func(w http.ResponseWriter, r *http.Request) { |
| 24 | + dir, cleanup := test.CreateTestDirectory(t) |
| 25 | + path := filepath.Join(dir, "fake.mp3") |
| 26 | + test.CreateTestFile(t, path) |
| 27 | + defer cleanup() |
| 28 | + |
| 29 | + // audio endpoints only accept POST requests |
| 30 | + if r.Method != "POST" { |
| 31 | + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| 36 | + if err != nil { |
| 37 | + http.Error(w, "failed to parse media type", http.StatusBadRequest) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if mediaType != "application/json" { |
| 42 | + http.Error(w, "request is not json", http.StatusBadRequest) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + // Parse the JSON body of the request |
| 47 | + var params map[string]interface{} |
| 48 | + err = json.NewDecoder(r.Body).Decode(¶ms) |
| 49 | + if err != nil { |
| 50 | + http.Error(w, "failed to parse request body", http.StatusBadRequest) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + // Check if each required field is present in the parsed JSON object |
| 55 | + reqParams := []string{"model", "input", "voice"} |
| 56 | + for _, param := range reqParams { |
| 57 | + _, ok := params[param] |
| 58 | + if !ok { |
| 59 | + http.Error(w, fmt.Sprintf("no %s in params", param), http.StatusBadRequest) |
| 60 | + return |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // read audio file content |
| 65 | + audioFile, err := os.ReadFile(path) |
| 66 | + if err != nil { |
| 67 | + http.Error(w, "failed to read audio file", http.StatusInternalServerError) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // write audio file content to response |
| 72 | + w.Header().Set("Content-Type", "audio/mpeg") |
| 73 | + w.Header().Set("Transfer-Encoding", "chunked") |
| 74 | + w.Header().Set("Connection", "keep-alive") |
| 75 | + _, err = w.Write(audioFile) |
| 76 | + if err != nil { |
| 77 | + http.Error(w, "failed to write body", http.StatusInternalServerError) |
| 78 | + return |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("happy path", func(t *testing.T) { |
| 83 | + res, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{ |
| 84 | + Model: openai.TTSModel1, |
| 85 | + Input: "Hello!", |
| 86 | + Voice: openai.VoiceAlloy, |
| 87 | + }) |
| 88 | + checks.NoError(t, err, "CreateSpeech error") |
| 89 | + defer res.Close() |
| 90 | + |
| 91 | + buf, err := io.ReadAll(res) |
| 92 | + checks.NoError(t, err, "ReadAll error") |
| 93 | + |
| 94 | + // save buf to file as mp3 |
| 95 | + err = os.WriteFile("test.mp3", buf, 0644) |
| 96 | + checks.NoError(t, err, "Create error") |
| 97 | + }) |
| 98 | + t.Run("invalid model", func(t *testing.T) { |
| 99 | + _, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{ |
| 100 | + Model: "invalid_model", |
| 101 | + Input: "Hello!", |
| 102 | + Voice: openai.VoiceAlloy, |
| 103 | + }) |
| 104 | + checks.ErrorIs(t, err, openai.ErrInvalidSpeechModel, "CreateSpeech error") |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("invalid voice", func(t *testing.T) { |
| 108 | + _, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{ |
| 109 | + Model: openai.TTSModel1, |
| 110 | + Input: "Hello!", |
| 111 | + Voice: "invalid_voice", |
| 112 | + }) |
| 113 | + checks.ErrorIs(t, err, openai.ErrInvalidVoice, "CreateSpeech error") |
| 114 | + }) |
| 115 | +} |
0 commit comments