Files
managed-portal/internal/server/managed_handlers_test.go
2026-04-27 10:04:36 +08:00

272 lines
9.3 KiB
Go

package server
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"managed-portal/internal/managed"
)
type fakeDockerRuntime struct {
statusByContainer map[string]string
restarted []string
}
type roundTripFunc func(req *http.Request) (*http.Response, error)
func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return fn(req)
}
func (f *fakeDockerRuntime) GetContainerStatus(containerName string) (string, error) {
if status, ok := f.statusByContainer[containerName]; ok {
return status, nil
}
return "unknown", nil
}
func (f *fakeDockerRuntime) RestartContainer(containerName string) error {
f.restarted = append(f.restarted, containerName)
return nil
}
func TestManagedServicesHandlers(t *testing.T) {
t.Parallel()
storeConfigPath := "/srv/store/config/local.yaml"
storeRTSP := "rtsp://store-old/stream"
srv := New(nil)
registry := &managed.Registry{
Services: []managed.Service{{
ID: "store_dwell_alert",
DisplayName: "Store Dwell Alert",
ProjectType: "store_dwell_alert",
ProjectRoot: "/srv/store",
ContainerName: "store-dwell-alert",
ServiceName: "store-dwell-alert",
APIBaseURL: "http://managed.invalid/store",
ResultType: "store_dwell_alert",
}, {
ID: "people_flow_project",
DisplayName: "People Flow Project",
ProjectType: "people_flow_project",
ProjectRoot: "/srv/people",
ContainerName: "people-flow-project",
ServiceName: "people-flow-project",
APIBaseURL: "http://managed.invalid/people",
ResultType: "people_flow_project",
}},
}
client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
response := func(status int, body any) (*http.Response, error) {
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: status,
Header: make(http.Header),
Body: io.NopCloser(bytes.NewReader(data)),
}, nil
}
switch {
case r.Method == http.MethodGet && r.URL.Path == "/store/api/manage/config":
return response(http.StatusOK, map[string]any{
"config_path": storeConfigPath,
"stream": map[string]any{"rtsp_url": storeRTSP},
})
case r.Method == http.MethodPut && r.URL.Path == "/store/api/manage/config":
var payload map[string]string
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
t.Fatalf("decode store config update: %v", err)
}
storeRTSP = payload["rtsp_url"]
return response(http.StatusOK, map[string]any{
"config_path": storeConfigPath,
"stream": map[string]any{"rtsp_url": storeRTSP},
})
case r.Method == http.MethodGet && r.URL.Path == "/store/api/manage/summary":
return response(http.StatusOK, managed.ResultSummary{
ResultType: "store_dwell_alert",
Headline: "Latest report shows 2 active customers, longest dwell 850s",
LastResultTime: "2026-04-16T09:30:00+08:00",
Metrics: map[string]any{
"longest_dwell_seconds": 850,
},
})
case r.Method == http.MethodGet && r.URL.Path == "/store/api/manage/files":
return response(http.StatusOK, map[string]any{
"files": []managed.ResultFile{{
Path: "logs/events.jsonl",
Name: "events.jsonl",
}},
})
case r.Method == http.MethodGet && r.URL.Path == "/store/api/manage/files/preview":
return response(http.StatusOK, managed.FilePreview{
Path: "logs/events.jsonl",
Lines: []string{"preview"},
Count: 1,
})
case r.Method == http.MethodGet && r.URL.Path == "/store/api/manage/files/download":
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Disposition": []string{`attachment; filename="events.jsonl"`},
},
Body: io.NopCloser(strings.NewReader("store-download")),
}, nil
case r.Method == http.MethodGet && r.URL.Path == "/people/api/manage/config":
return response(http.StatusOK, map[string]any{
"config_path": "/srv/people/config/local.yaml",
"runtime": map[string]any{"rtsp_url": "rtsp://people-old/stream"},
})
case r.Method == http.MethodGet && r.URL.Path == "/people/api/manage/summary":
return response(http.StatusOK, managed.ResultSummary{
ResultType: "people_flow_project",
Headline: "Latest window counted 5 people",
LastResultTime: "2026-04-16T09:00:00+08:00",
Metrics: map[string]any{
"recent_window_stats": []map[string]any{{"total_people": 5}},
},
})
case r.Method == http.MethodGet && r.URL.Path == "/people/api/manage/files":
return response(http.StatusOK, map[string]any{"files": []managed.ResultFile{}})
default:
t.Fatalf("unexpected child request: %s %s", r.Method, r.URL.String())
return nil, nil
}
})}
docker := &fakeDockerRuntime{
statusByContainer: map[string]string{
"store-dwell-alert": "running",
"people-flow-project": "stopped",
},
}
srv.managedManager = managed.NewManager(registry, docker, managed.NewRemoteClient(client))
t.Run("GET /api/managed-services", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/managed-services", nil)
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
var payload struct {
Services []managed.ServiceState `json:"services"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if len(payload.Services) != 2 {
t.Fatalf("len(services) = %d", len(payload.Services))
}
if payload.Services[0].RTSP == "" {
t.Fatalf("expected RTSP in list response")
}
})
t.Run("GET /api/managed-services/:id", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/managed-services/store_dwell_alert", nil)
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
if !strings.Contains(recorder.Body.String(), "longest_dwell_seconds") {
t.Fatalf("detail response missing summary metrics: %s", recorder.Body.String())
}
})
t.Run("PUT /api/managed-services/:id/config", func(t *testing.T) {
recorder := httptest.NewRecorder()
body := bytes.NewBufferString(`{"rtsp_url":"rtsp://store-new/stream"}`)
req := httptest.NewRequest(http.MethodPut, "/api/managed-services/store_dwell_alert/config", body)
req.Header.Set("Content-Type", "application/json")
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
if storeRTSP != "rtsp://store-new/stream" {
t.Fatalf("storeRTSP = %q", storeRTSP)
}
})
t.Run("POST /api/managed-services/:id/restart", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/managed-services/people_flow_project/restart", nil)
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
if len(docker.restarted) != 1 || docker.restarted[0] != "people-flow-project" {
t.Fatalf("restarted = %#v", docker.restarted)
}
})
t.Run("GET /api/managed-services/:id/results/summary", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/managed-services/store_dwell_alert/results/summary", nil)
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
if !strings.Contains(recorder.Body.String(), "active customers") {
t.Fatalf("summary response = %s", recorder.Body.String())
}
})
t.Run("GET /api/managed-services/:id/results/files", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/managed-services/store_dwell_alert/results/files", nil)
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
if !strings.Contains(recorder.Body.String(), "events.jsonl") {
t.Fatalf("files response missing expected file: %s", recorder.Body.String())
}
})
t.Run("GET /api/managed-services/:id/results/preview", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/managed-services/store_dwell_alert/results/preview?path=logs/events.jsonl&lines=1", nil)
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
if !strings.Contains(recorder.Body.String(), "preview") {
t.Fatalf("preview response = %s", recorder.Body.String())
}
})
t.Run("GET /api/managed-services/:id/results/download", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/managed-services/store_dwell_alert/results/download?path=logs/events.jsonl", nil)
srv.engine.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", recorder.Code, recorder.Body.String())
}
if got := recorder.Body.String(); got != "store-download" {
t.Fatalf("download body = %q", got)
}
})
}