Spaces:
Runtime error
Runtime error
File size: 2,164 Bytes
857a91b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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)
}
}
|