matrixcloud / api /server_test.go
github-actions[bot]
Deploy from 4dfd54f2
857a91b
Raw
History Blame Contribute Delete
2.16 kB
package api
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/agent-matrix/matrix-runtime/internal/config"
"github.com/agent-matrix/matrix-runtime/internal/jobs"
)
func jsonBody(s string) io.Reader { return strings.NewReader(s) }
func testServer(t *testing.T) *Server {
t.Helper()
cfg := config.Defaults(config.ModeLocalDev)
cfg.DataDir = t.TempDir()
return NewServer(cfg, jobs.NewManager(cfg), nil)
}
func TestHealth(t *testing.T) {
srv := testServer(t)
req := httptest.NewRequest(http.MethodGet, "/v1/health", nil)
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}
var body map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
if body["status"] != "ok" {
t.Errorf("status = %v", body["status"])
}
}
func TestCapabilities(t *testing.T) {
srv := testServer(t)
req := httptest.NewRequest(http.MethodGet, "/v1/capabilities", nil)
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}
var body struct {
Capabilities []string `json:"capabilities"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
want := map[string]bool{"mcp.test": false, "model.inspect": false}
for _, c := range body.Capabilities {
if _, ok := want[c]; ok {
want[c] = true
}
}
for c, seen := range want {
if !seen {
t.Errorf("capability %q missing", c)
}
}
}
func TestCreateJob_BadType(t *testing.T) {
srv := testServer(t)
req := httptest.NewRequest(http.MethodPost, "/v1/jobs", jsonBody(`{"type":"nope"}`))
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status %d, want 400", rec.Code)
}
}
func TestGetJob_NotFound(t *testing.T) {
srv := testServer(t)
req := httptest.NewRequest(http.MethodGet, "/v1/jobs/job_missing", nil)
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("status %d, want 404", rec.Code)
}
}