package webui import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "google.golang.org/grpc" controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/events" ) func TestWorkRequestRejectsLongTitle(t *testing.T) { for _, title := range []string{strings.Repeat("a", 81), strings.Repeat("界", 81)} { t.Run(title[:3], func(t *testing.T) { body, err := json.Marshal(workRequest{Workflow: "implement", Title: title, Instructions: "Full task description"}) if err != nil { t.Fatal(err) } request := httptest.NewRequestWithContext(access.WithPrincipal(t.Context(), access.Principal{IdentityID: "operator"}), http.MethodPost, "/api/work-requests", strings.NewReader(string(body))) request.Header.Set("Content-Type", "application/json") request.Header.Set("X-Archie-CSRF", "1") response := httptest.NewRecorder() newWorkflowsAPI(&Server{}).handleWorkRequest(response, request) if response.Code != http.StatusBadRequest || !strings.Contains(response.Body.String(), "title must be 80 characters or fewer") { t.Fatalf("response = %d %s", response.Code, response.Body.String()) } }) } } type workRequestTasks struct { storecontract.TaskStore requester string } func (s *workRequestTasks) EnqueueChatTask(_ context.Context, _, _, _, _, _, requester, _, _ string, _ map[string]any) (*task.Task, error) { s.requester = requester return &task.Task{ID: 1}, nil } func (*workRequestTasks) InsertEvent(context.Context, events.Event) (int64, error) { return 1, nil } type workRequestControl struct { controlpb.ControlPlaneServiceClient } func (workRequestControl) Query(_ context.Context, r *controlpb.QueryRequest, _ ...grpc.CallOption) (*controlpb.QueryResponse, error) { value := []byte(`{}`) if r.Kind == workflowDefinitionsKind { value = []byte(`{"definitions":[{"id":"report","yaml":"id: report\nrepository: none\nidentity: worker\n"}]}`) } return &controlpb.QueryResponse{Resource: &controlpb.Resource{ValueJson: value}}, nil } func TestWorkRequestRequester(t *testing.T) { for _, tt := range []struct { name, claimed string principal *access.Principal status int requester string }{ {"authenticated requester", "", &access.Principal{IdentityID: "signed-in-human"}, http.StatusCreated, "signed-in-human"}, {"cannot claim another requester", `,"identity":"another-account"`, &access.Principal{IdentityID: "signed-in-human"}, http.StatusBadRequest, ""}, {"missing principal", "", nil, http.StatusUnauthorized, ""}, {"empty principal", "", &access.Principal{}, http.StatusUnauthorized, ""}, } { t.Run(tt.name, func(t *testing.T) { ctx := t.Context() if tt.principal != nil { ctx = access.WithPrincipal(ctx, *tt.principal) } req := httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/work-requests", strings.NewReader(`{"workflow":"report","title":"Report","instructions":"Summarise results"`+tt.claimed+`}`)) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Archie-CSRF", "1") tasks := &workRequestTasks{} response := httptest.NewRecorder() newWorkflowsAPI(&Server{Store: tasks, ControlPlane: workRequestControl{}}).handleWorkRequest(response, req) if response.Code != tt.status || tasks.requester != tt.requester { t.Fatalf("response = %d %s, requester = %q; want %d, %q", response.Code, response.Body.String(), tasks.requester, tt.status, tt.requester) } }) } }