Some checks failed
CodeQL / Analyze (go) (push) Successful in 6m28s
Docker Image / build-docker (push) Failing after 13m26s
Lint and Testing / lint (push) Successful in 11m17s
Lint and Testing / test (push) Successful in 11m17s
Lint and Testing / golangci (push) Successful in 2m40s
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go-whisper-api/config"
|
|
)
|
|
|
|
func TestRegistryResolve(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "ggml-small.bin"), []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "ggml-large-v3-turbo.bin"), []byte("y"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg := NewRegistry(dir)
|
|
|
|
id, err := reg.Resolve("whisper-1", "ggml-large-v3-turbo")
|
|
if err != nil || id != "ggml-large-v3-turbo" {
|
|
t.Fatalf("whisper-1: id=%q err=%v", id, err)
|
|
}
|
|
id, err = reg.Resolve("whisper-large-v3-turbo", "")
|
|
if err != nil || id != "large-v3-turbo" {
|
|
t.Fatalf("whisper-large-v3-turbo: id=%q err=%v", id, err)
|
|
}
|
|
id, err = reg.Resolve("ggml-small", "")
|
|
if err != nil || id != "ggml-small" {
|
|
t.Fatalf("ggml-small: id=%q err=%v", id, err)
|
|
}
|
|
id, err = reg.Resolve("", "ggml-small")
|
|
if err != nil || id != "ggml-small" {
|
|
t.Fatalf("empty with default: id=%q err=%v", id, err)
|
|
}
|
|
}
|
|
|
|
func TestHandleOpenAITranscriptionsMissingFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "ggml-small.bin"), []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := &Server{
|
|
cfg: config.API{ModelsDir: dir, Language: "ru"},
|
|
models: NewRegistry(dir),
|
|
}
|
|
body := &bytes.Buffer{}
|
|
w := multipart.NewWriter(body)
|
|
_ = w.WriteField("model", "ggml-small")
|
|
w.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/v1/audio/transcriptions", body)
|
|
req.Header.Set("Content-Type", w.FormDataContentType())
|
|
rec := httptest.NewRecorder()
|
|
srv.handleOpenAITranscriptions(rec, req)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
if !strings.Contains(rec.Body.String(), "file") {
|
|
t.Fatalf("expected file error, got %s", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleOpenAIModels(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "ggml-small.bin"), []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := &Server{
|
|
cfg: config.API{ModelsDir: dir, Language: "ru"},
|
|
models: NewRegistry(dir),
|
|
}
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
rec := httptest.NewRecorder()
|
|
srv.handleOpenAIModels(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
if !strings.Contains(rec.Body.String(), "ggml-small") {
|
|
t.Fatalf("expected model list, got %s", rec.Body.String())
|
|
}
|
|
}
|