package archiegateway import ( "context" "testing" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/taskactions" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/logging" ) type emptyTaskLogs struct{ storecontract.TaskLogStore } func (emptyTaskLogs) TaskLog(context.Context, int64, int, logging.Query) (logging.TaskLogPage, error) { return logging.TaskLogPage{}, nil } // TestChatTaskWorkflow pins which workflow chat names for a task. A plan // approval pre-writes the task's workflow column to the workflow that runs // once approved, so the column names a workflow that has not started; the // pinned definition names the one the run was compiled from, which is what // the run's own log names. func TestChatTaskWorkflow(t *testing.T) { const feasibility = "id: feasibility\nsteps:\n - type: workflow.approve\n settings: {plan: p, then: implement}\n" pinned := func(t workflow.Task) workflow.Task { t.WorkflowDefinitionYAML = feasibility t.WorkflowDefinitionDigest = workflow.DigestDefinition(feasibility) return t } reviewWait := `{"findings":[{"key":"f","finding":{}}],"workflow":"pr-review"}` tests := []struct { name string task workflow.Task pin bool want string }{ { name: "a plan wait names the workflow that produced the plan", task: workflow.Task{ID: 22, Status: workflow.StatusWaitingApproval, Workflow: "implement"}, pin: true, want: "feasibility", }, { name: "a review wait names the workflow the gate resumes", task: workflow.Task{ID: 30, Status: workflow.StatusWaitingApproval, Workflow: "pr-review", ReviewGate: reviewWait}, pin: true, want: "pr-review", }, { name: "a queued task names the workflow it will run next", task: workflow.Task{ID: 31, Status: workflow.StatusQueued, Workflow: "implement"}, pin: true, want: "implement", }, { name: "a task with no pin names its workflow column", task: workflow.Task{ID: 32, Status: workflow.StatusWaitingApproval, Workflow: "implement"}, want: "implement", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // The list projection is narrow and carries no pin; the full read // is where the pin lives. full := tt.task if tt.pin { full = pinned(tt.task) } lister := chatTaskListerAdapter{ tasks: func(context.Context, int) ([]workflow.Task, error) { return []workflow.Task{tt.task}, nil }, task: func(context.Context, int64) (*workflow.Task, error) { return &full, nil }, } got, err := lister.ListChatTasks(t.Context(), string(identity.OperatorID()), 10) if err != nil || len(got) != 1 { t.Fatalf("list = %d tasks, %v; want 1", len(got), err) } if got[0].Workflow != tt.want { t.Errorf("workflow = %q, want %q", got[0].Workflow, tt.want) } }) } } // TestChatTaskScope pins the chat grant boundary: the operator profile sees // and approves every task, and any other identity only its own. func TestChatTaskScope(t *testing.T) { operator := string(identity.OperatorID()) tasks := []workflow.Task{{ID: 1, Identity: "alice"}, {ID: 2, Identity: "bob"}} byID := func(_ context.Context, id int64) (*workflow.Task, error) { for i := range tasks { if tasks[i].ID == id { return &tasks[i], nil } } return nil, nil } tests := []struct { name string chat string wantList int wantScope *string otherLogs bool }{ {name: "operator sees every task", chat: operator, wantList: 2, otherLogs: true}, {name: "an identity sees only its own", chat: "alice", wantList: 1, wantScope: new("alice")}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { lister := chatTaskListerAdapter{tasks: func(context.Context, int) ([]workflow.Task, error) { return tasks, nil }} got, err := lister.ListChatTasks(t.Context(), tt.chat, 10) if err != nil || len(got) != tt.wantList { t.Fatalf("list = %d tasks, %v; want %d", len(got), err, tt.wantList) } var scope *string controller := chatTaskControllerAdapter{approve: func(_ context.Context, s *string, _ taskactions.Actor, _ int64, _ taskactions.ActionPayload) error { scope = s return nil }} if err := controller.ApproveChatTask(t.Context(), 2, taskactions.Actor{Identity: identity.IdentityID(tt.chat)}); err != nil { t.Fatalf("approve: %v", err) } if (scope == nil) != (tt.wantScope == nil) || (scope != nil && *scope != *tt.wantScope) { t.Fatalf("approve scope = %v, want %v", scope, tt.wantScope) } reader := chatTaskLogReaderAdapter{tasks: byID, taskLogs: emptyTaskLogs{}} _, err = reader.ReadChatTaskLogs(t.Context(), tt.chat, 2, 0, gateway.ChatTaskLogQuery{}) if refused := err != nil && err.Error() == "task 2 belongs to another identity"; refused == tt.otherLogs { t.Fatalf("reading another identity's logs: err = %v, want allowed=%v", err, tt.otherLogs) } }) } }