|
| 1 | +package gogpt_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "mime" |
| 8 | + "mime/multipart" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + |
| 14 | + . "github.com/sashabaranov/go-gpt3" |
| 15 | + "github.com/sashabaranov/go-gpt3/internal/test" |
| 16 | + |
| 17 | + "context" |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +// TestAudio Tests the transcription and translation endpoints of the API using the mocked server. |
| 22 | +func TestAudio(t *testing.T) { |
| 23 | + server := test.NewTestServer() |
| 24 | + server.RegisterHandler("/v1/audio/transcriptions", handleAudioEndpoint) |
| 25 | + server.RegisterHandler("/v1/audio/translations", handleAudioEndpoint) |
| 26 | + // create the test server |
| 27 | + var err error |
| 28 | + ts := server.OpenAITestServer() |
| 29 | + ts.Start() |
| 30 | + defer ts.Close() |
| 31 | + |
| 32 | + config := DefaultConfig(test.GetTestToken()) |
| 33 | + config.BaseURL = ts.URL + "/v1" |
| 34 | + client := NewClientWithConfig(config) |
| 35 | + |
| 36 | + testcases := []struct { |
| 37 | + name string |
| 38 | + createFn func(context.Context, AudioRequest) (AudioResponse, error) |
| 39 | + }{ |
| 40 | + { |
| 41 | + "transcribe", |
| 42 | + client.CreateTranscription, |
| 43 | + }, |
| 44 | + { |
| 45 | + "translate", |
| 46 | + client.CreateTranslation, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + ctx := context.Background() |
| 51 | + |
| 52 | + dir, cleanup := createTestDirectory(t) |
| 53 | + defer cleanup() |
| 54 | + |
| 55 | + for _, tc := range testcases { |
| 56 | + t.Run(tc.name, func(t *testing.T) { |
| 57 | + path := filepath.Join(dir, "fake.mp3") |
| 58 | + createTestFile(t, path) |
| 59 | + |
| 60 | + req := AudioRequest{ |
| 61 | + FilePath: path, |
| 62 | + Model: "whisper-3", |
| 63 | + } |
| 64 | + _, err = tc.createFn(ctx, req) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("audio API error: %v", err) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// createTestFile creates a fake file with "hello" as the content. |
| 73 | +func createTestFile(t *testing.T, path string) { |
| 74 | + file, err := os.Create(path) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("failed to create file %v", err) |
| 77 | + } |
| 78 | + if _, err = file.WriteString("hello"); err != nil { |
| 79 | + t.Fatalf("failed to write to file %v", err) |
| 80 | + } |
| 81 | + file.Close() |
| 82 | +} |
| 83 | + |
| 84 | +// createTestDirectory creates a temporary folder which will be deleted when cleanup is called. |
| 85 | +func createTestDirectory(t *testing.T) (path string, cleanup func()) { |
| 86 | + t.Helper() |
| 87 | + |
| 88 | + path, err := os.MkdirTemp(os.TempDir(), "") |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + return path, func() { os.RemoveAll(path) } |
| 94 | +} |
| 95 | + |
| 96 | +// handleAudioEndpoint Handles the completion endpoint by the test server. |
| 97 | +func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) { |
| 98 | + var err error |
| 99 | + |
| 100 | + // audio endpoints only accept POST requests |
| 101 | + if r.Method != "POST" { |
| 102 | + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 103 | + } |
| 104 | + |
| 105 | + mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| 106 | + if err != nil { |
| 107 | + http.Error(w, "failed to parse media type", http.StatusBadRequest) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + if !strings.HasPrefix(mediaType, "multipart") { |
| 112 | + http.Error(w, "request is not multipart", http.StatusBadRequest) |
| 113 | + } |
| 114 | + |
| 115 | + boundary, ok := params["boundary"] |
| 116 | + if !ok { |
| 117 | + http.Error(w, "no boundary in params", http.StatusBadRequest) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + fileData := &bytes.Buffer{} |
| 122 | + mr := multipart.NewReader(r.Body, boundary) |
| 123 | + part, err := mr.NextPart() |
| 124 | + if err != nil && errors.Is(err, io.EOF) { |
| 125 | + http.Error(w, "error accessing file", http.StatusBadRequest) |
| 126 | + return |
| 127 | + } |
| 128 | + if _, err = io.Copy(fileData, part); err != nil { |
| 129 | + http.Error(w, "failed to copy file", http.StatusInternalServerError) |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + if len(fileData.Bytes()) == 0 { |
| 134 | + w.WriteHeader(http.StatusInternalServerError) |
| 135 | + http.Error(w, "received empty file data", http.StatusBadRequest) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + if _, err = w.Write([]byte(`{"body": "hello"}`)); err != nil { |
| 140 | + http.Error(w, "failed to write body", http.StatusInternalServerError) |
| 141 | + return |
| 142 | + } |
| 143 | +} |
0 commit comments