mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-08 10:25:28 +08:00
test(docs): assert ollama show id field and document ollama endpoints
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
package ollama
|
||||
|
||||
import (
|
||||
"ds2api/internal/config"
|
||||
"ds2api/internal/util"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"ds2api/internal/config"
|
||||
"ds2api/internal/util"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var WriteJSON = util.WriteJSON
|
||||
@@ -15,11 +16,11 @@ type ConfigReader interface {
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
Store ConfigReader
|
||||
Store ConfigReader
|
||||
}
|
||||
|
||||
type OllamaModelRequest struct {
|
||||
Model string `json:"model"`
|
||||
Model string `json:"model"`
|
||||
}
|
||||
|
||||
func RegisterRoutes(r chi.Router, h *Handler) {
|
||||
@@ -31,18 +32,22 @@ func RegisterRoutes(r chi.Router, h *Handler) {
|
||||
func (h *Handler) GetVersion(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"version":"0.23.1"}`))
|
||||
_, _ = w.Write([]byte(`{"version":"0.23.1"}`))
|
||||
}
|
||||
func (h *Handler) ListOllamaModels(w http.ResponseWriter, r *http.Request) {
|
||||
WriteJSON(w, http.StatusOK, config.OllamaModelsResponse())
|
||||
}
|
||||
func (h *Handler) GetOllamaModel(w http.ResponseWriter, r *http.Request) {
|
||||
var payload OllamaModelRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
http.Error(w, "Invalid JSON body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
http.Error(w, "Invalid JSON body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err := r.Body.Close(); err != nil {
|
||||
slog.Warn("[ollama] failed to close request body", "error", err)
|
||||
}
|
||||
}()
|
||||
modelID := payload.Model
|
||||
model, ok := config.OllamaModelByID(h.Store, modelID)
|
||||
if !ok {
|
||||
|
||||
@@ -1,38 +1,37 @@
|
||||
package ollama
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"strings"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type ollamaTestSurface struct {
|
||||
Store ConfigReader
|
||||
handler *Handler
|
||||
Store ConfigReader
|
||||
handler *Handler
|
||||
}
|
||||
|
||||
func (h *ollamaTestSurface) apiHandler() *Handler {
|
||||
if h.handler == nil {
|
||||
h.handler = &Handler{Store: h.Store}
|
||||
}
|
||||
return h.handler
|
||||
if h.handler == nil {
|
||||
h.handler = &Handler{Store: h.Store}
|
||||
}
|
||||
return h.handler
|
||||
}
|
||||
|
||||
|
||||
func registerOllamaTestRoutes(r chi.Router, h *ollamaTestSurface) {
|
||||
r.Get("/api/version", h.apiHandler().GetVersion)
|
||||
r.Get("/api/tags", h.apiHandler().ListOllamaModels)
|
||||
r.Post("/api/show", h.apiHandler().GetOllamaModel)
|
||||
r.Get("/api/version", h.apiHandler().GetVersion)
|
||||
r.Get("/api/tags", h.apiHandler().ListOllamaModels)
|
||||
r.Post("/api/show", h.apiHandler().GetOllamaModel)
|
||||
}
|
||||
|
||||
|
||||
func TestGetOllamaVersionRoute(t *testing.T) {
|
||||
h := &ollamaTestSurface{}
|
||||
r := chi.NewRouter()
|
||||
registerOllamaTestRoutes(r, h)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/version", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/version", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
@@ -40,12 +39,11 @@ func TestGetOllamaVersionRoute(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestGetOllamaModelsRoute(t *testing.T) {
|
||||
h := &ollamaTestSurface{}
|
||||
r := chi.NewRouter()
|
||||
registerOllamaTestRoutes(r, h)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/tags", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/tags", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
@@ -53,7 +51,6 @@ func TestGetOllamaModelsRoute(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestGetOllamaModelRoute(t *testing.T) {
|
||||
h := &ollamaTestSurface{}
|
||||
r := chi.NewRouter()
|
||||
@@ -61,18 +58,28 @@ func TestGetOllamaModelRoute(t *testing.T) {
|
||||
|
||||
t.Run("direct", func(t *testing.T) {
|
||||
body := `{"model":"deepseek-v4-flash"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("expected valid json body, got err=%v body=%s", err, rec.Body.String())
|
||||
}
|
||||
if _, ok := payload["id"]; !ok {
|
||||
t.Fatalf("expected response has lowercase id field, body=%s", rec.Body.String())
|
||||
}
|
||||
if _, ok := payload["ID"]; ok {
|
||||
t.Fatalf("expected response does not expose uppercase ID field, body=%s", rec.Body.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("direct_nothinking", func(t *testing.T) {
|
||||
body := `{"model":"deepseek-v4-flash-nothinking"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
@@ -83,7 +90,7 @@ func TestGetOllamaModelRoute(t *testing.T) {
|
||||
|
||||
t.Run("direct_expert", func(t *testing.T) {
|
||||
body := `{"model":"deepseek-v4-pro"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
@@ -94,7 +101,7 @@ func TestGetOllamaModelRoute(t *testing.T) {
|
||||
|
||||
t.Run("direct_vision", func(t *testing.T) {
|
||||
body := `{"model":"deepseek-v4-vision"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
@@ -110,7 +117,7 @@ func TestGetOllamaModelRouteNotFound(t *testing.T) {
|
||||
registerOllamaTestRoutes(r, h)
|
||||
|
||||
body := `{"model":"not-exists"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
|
||||
Reference in New Issue
Block a user