package routing_test import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "testing" "github.com/mathiasbq/supervisor/internal/routing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestLoggerLogDecision(t *testing.T) { var captured map[string]any srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, "/mcp", r.URL.Path) body, _ := io.ReadAll(r.Body) require.NoError(t, json.Unmarshal(body, &captured)) _ = json.NewEncoder(w).Encode(map[string]any{"jsonrpc": "2.0", "id": 1, "result": map[string]any{"content": []map[string]any{{"type": "text", "text": "ok"}}}}) })) defer srv.Close() l := routing.NewLogger(srv.URL) err := l.LogDecision(context.Background(), routing.LogEntry{ SessionID: "sess-1", Skill: "code_review", Decision: "local", Message: "model=qwen35, pass_rate=0.94", ProjectRoot: "/home/x/proj", DurationMs: 1234, Failed: false, }) require.NoError(t, err) params := captured["params"].(map[string]any) assert.Equal(t, "tools/call", captured["method"]) assert.Equal(t, "session_log", params["name"]) args := params["arguments"].(map[string]any) assert.Equal(t, "_routing", args["skill"]) assert.Equal(t, "decide", args["phase"]) assert.Equal(t, "skip", args["final_status"]) assert.Contains(t, args["message"].(string), "code_review: local") assert.Equal(t, "sess-1", args["session_id"]) assert.Equal(t, "/home/x/proj", args["project_root"]) assert.Equal(t, float64(1234), args["duration_ms"]) } func TestLoggerLogFailure(t *testing.T) { var captured map[string]any srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, _ := io.ReadAll(r.Body) _ = json.Unmarshal(body, &captured) _ = json.NewEncoder(w).Encode(map[string]any{"jsonrpc": "2.0", "id": 1, "result": map[string]any{}}) })) defer srv.Close() l := routing.NewLogger(srv.URL) err := l.LogDecision(context.Background(), routing.LogEntry{ SessionID: "s", Skill: "debug", Decision: "local", Message: "litellm down", Failed: true, }) require.NoError(t, err) args := captured["params"].(map[string]any)["arguments"].(map[string]any) assert.Equal(t, "fail", args["final_status"]) } func TestLoggerSurfacesUpstreamError(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { http.Error(w, "down", http.StatusBadGateway) })) defer srv.Close() l := routing.NewLogger(srv.URL) err := l.LogDecision(context.Background(), routing.LogEntry{Skill: "x", SessionID: "y", Decision: "local"}) require.Error(t, err) }