Files
archie/internal/app/archied/task_actions_test.go
T
sam 5c0efaceaf refactor(workflow): relocate Task/Status/Source and define workflow.Store (#754)
* refactor(workflow,store): relocate Task/Status/Source and define workflow.Store

Move store.Task, store.Status*/store.Source*, and the 3-method workflow
store interface out of internal/store into internal/domain/workflow as
workflow.Task/workflow.Status/workflow.Source plus a consumer-owned
workflow.Store, superseding store.WorkflowStore. internal/domain/workflow
now imports zero internal/store symbols; internal/store persists the
domain type instead (infrastructure depends on domain), and its Store
satisfies workflow.Store.

Updates every production and test caller of the relocated symbols across
daemon, webui, gateway, agentworker, agenttransport/nats, storerpc,
taskrun, and worktreerpc, and removes TestRecordDispatchViaExplicitTx per
docs/prds/state-store-contract.md (rev. 2c) sections 4 and 12 step 2.

Two workflow-package integration tests that exercise the real *store.Store
move to an external workflow_test package (store_integration_test.go) to
avoid a workflow(test)->store->workflow(prod) import cycle now that store
depends on workflow.

* no-mistakes(document): docs(state-store): mark §4.8 relocation done in state-store-contract
2026-09-07 00:39:26 +10:00

115 lines
3.9 KiB
Go

package archied
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"strconv"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webui"
)
// The dashboard and chat both let an operator approve or decline a task, and
// they had drifted: declining from the dashboard left the task
// closed_wont_do, cancelling the same task from chat left it rejected. Two
// operators looking at one queue saw different states for the same decision,
// and "rejected" -- which reconcilePRs uses for a pull request closed without
// merging -- no longer meant one thing.
//
// This drives both real surfaces against one store and asserts they agree.
// It lives in cmd/archied because that is the only place both are wired.
func TestDashboardAndChatAgreeOnTerminalStates(t *testing.T) {
tests := []struct {
name string
// viaChat runs the action through the chat router; otherwise it
// goes through the dashboard's HTTP handler.
viaChat bool
action string
want string
}{
{name: "dashboard reject", action: "reject", want: workflow.StatusClosedWontDo},
{name: "chat cancel", viaChat: true, action: "cancel", want: workflow.StatusClosedWontDo},
{name: "dashboard approve", action: "approve", want: workflow.StatusQueued},
{name: "chat approve", viaChat: true, action: "approve", want: workflow.StatusQueued},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "decide on me", "", "", "reviewer")
if err != nil {
t.Fatal(err)
}
if err := st.Transition(ctx, task.ID, workflow.StatusQueued, workflow.StatusWaitingHuman, "await"); err != nil {
t.Fatal(err)
}
if tc.viaChat {
runChatAction(t, ctx, st, tc.action, task.ID)
} else {
runDashboardAction(t, ctx, st, tc.action, task.ID)
}
got, err := st.TaskByID(ctx, task.ID)
if err != nil || got == nil {
t.Fatalf("TaskByID = (%+v, %v)", got, err)
}
if got.Status != tc.want {
t.Errorf("status = %q, want %q: the two surfaces disagree about "+
"what this action means", got.Status, tc.want)
}
})
}
}
// runChatAction performs the approve/cancel action through the chat
// controller path and fails the test on error.
func runChatAction(t *testing.T, ctx context.Context, st store.TaskStore, action string, taskID int64) {
t.Helper()
controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{
taskByID: st.TaskByID,
requeue: st.Requeue,
transition: st.Transition,
})
var err error
if action == "approve" {
err = controller.Approve(ctx, taskID, "reviewer")
} else {
err = controller.Cancel(ctx, taskID, "reviewer")
}
if err != nil {
t.Fatalf("chat %s: %v", action, err)
}
}
// runDashboardAction performs the approve/cancel action through the
// dashboard's HTTP handler and fails the test when the request is
// rejected.
func runDashboardAction(t *testing.T, ctx context.Context, st store.TaskStore, action string, taskID int64) {
t.Helper()
srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{MaxRetries: 3})}
req := httptest.NewRequestWithContext(ctx, http.MethodPost,
"/api/tasks/"+strconv.FormatInt(taskID, 10)+"/action",
bytes.NewBufferString(`{"action":"`+action+`"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("dashboard %s: status %d, body %s", action, w.Code, w.Body)
}
}