package webui import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/identity" ) type controlPlaneClientStub struct { command *controlpb.CommandRequest query *controlpb.Resource history *controlpb.HistoryRequest revisions []*controlpb.Revision err error } func (f *controlPlaneClientStub) Catalog(context.Context, *controlpb.CatalogRequest, ...grpc.CallOption) (*controlpb.CatalogResponse, error) { return &controlpb.CatalogResponse{}, f.err } func (f *controlPlaneClientStub) Query(context.Context, *controlpb.QueryRequest, ...grpc.CallOption) (*controlpb.QueryResponse, error) { return &controlpb.QueryResponse{Resource: f.query}, f.err } func (f *controlPlaneClientStub) History(_ context.Context, request *controlpb.HistoryRequest, _ ...grpc.CallOption) (*controlpb.HistoryResponse, error) { f.history = request return &controlpb.HistoryResponse{Revisions: f.revisions}, f.err } func (f *controlPlaneClientStub) Command(_ context.Context, request *controlpb.CommandRequest, _ ...grpc.CallOption) (*controlpb.CommandResponse, error) { f.command = request if f.err != nil { return nil, f.err } return &controlpb.CommandResponse{Resource: &controlpb.Resource{Kind: request.Kind, Version: 2, ValueJson: request.ValueJson}}, nil } func (f *controlPlaneClientStub) Watch(context.Context, *controlpb.WatchRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[controlpb.WatchResponse], error) { return nil, f.err } func TestControlPlaneCommandSuppliesTrustedAttribution(t *testing.T) { client := &controlPlaneClientStub{} server := &Server{ControlPlane: client} body := bytes.NewBufferString(`{"value":{"max_model_tool_steps":12},"expected_version":1,"actor":"browser"}`) request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/control-plane/resources/workflow-execution-settings/commands/replace", body) request.Header.Set("Content-Type", "application/json") request.Header.Set("X-Archie-CSRF", "1") response := httptest.NewRecorder() server.Handler().ServeHTTP(response, request) if response.Code != http.StatusOK { t.Fatalf("status = %d, body = %q", response.Code, response.Body.String()) } if client.command.Actor != string(identity.SystemID) || client.command.Source != "archie-ui" { t.Fatalf("attribution = %q/%q, want the System identity from archie-ui", client.command.Actor, client.command.Source) } if !strings.HasPrefix(client.command.RequestId, "ui-") || client.command.ExpectedVersion != 1 { t.Fatalf("request = %+v", client.command) } var got struct { Resource controlPlaneResource `json:"resource"` } if err := json.Unmarshal(response.Body.Bytes(), &got); err != nil { t.Fatal(err) } if got.Resource.Version != 2 || string(got.Resource.Value) != `{"max_model_tool_steps":12}` { t.Fatalf("resource = %+v", got.Resource) } } func TestControlPlaneConflictMapsToHTTPConflict(t *testing.T) { server := &Server{ControlPlane: &controlPlaneClientStub{err: status.Error(codes.Aborted, "version conflict")}} request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/control-plane/resources/settings/commands/replace", strings.NewReader(`{"value":{},"expected_version":1}`)) request.Header.Set("Content-Type", "application/json") request.Header.Set("X-Archie-CSRF", "1") response := httptest.NewRecorder() server.Handler().ServeHTTP(response, request) if response.Code != http.StatusConflict { t.Fatalf("status = %d, want 409", response.Code) } } func TestControlPlaneHistoryRendersRevisionsForRestore(t *testing.T) { client := &controlPlaneClientStub{revisions: []*controlpb.Revision{ {Version: 2, ValueJson: []byte(`{"max_model_tool_steps":40}`), Actor: string(identity.SystemID), Source: "archie-ui", RequestId: "ui-2"}, {Version: 1, ValueJson: []byte(`{"max_model_tool_steps":25}`), Actor: "system:migration", Source: "legacy-config", RequestId: "import"}, }} server := &Server{ControlPlane: client} request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/control-plane/resources/workflow-execution-settings/history", nil) response := httptest.NewRecorder() server.Handler().ServeHTTP(response, request) if response.Code != http.StatusOK { t.Fatalf("status = %d, body = %q", response.Code, response.Body.String()) } if client.history.GetKind() != "workflow-execution-settings" { t.Fatalf("history requested for %q", client.history.GetKind()) } var got struct { Revisions []struct { Version int64 `json:"version"` Value json.RawMessage `json:"value"` Actor string `json:"actor"` Source string `json:"source"` } `json:"revisions"` } if err := json.Unmarshal(response.Body.Bytes(), &got); err != nil { t.Fatal(err) } if len(got.Revisions) != 2 || got.Revisions[0].Version != 2 || got.Revisions[0].Actor != string(identity.SystemID) { t.Fatalf("revisions = %+v", got.Revisions) } // The value is the restore payload: it has to survive as JSON, not base64. if string(got.Revisions[1].Value) != `{"max_model_tool_steps":25}` { t.Fatalf("older value = %s, want the JSON a replace can send back", got.Revisions[1].Value) } } func TestControlPlaneHistoryWithoutAControlPlaneIsUnavailable(t *testing.T) { server := &Server{} request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/control-plane/resources/limits/history", nil) response := httptest.NewRecorder() server.Handler().ServeHTTP(response, request) if response.Code != http.StatusServiceUnavailable { t.Fatalf("status = %d, want 503", response.Code) } }