Files
archie/cmd/archied/task_actions_test.go
T
sam b6f0aea930 refactor(daemon,webui): make Daemon.Cfg and webui.Server.Cfg reload-safe
Daemon.Cfg, webui.Server.Cfg, and telegramSetup.Cfg were all held by
value (or as a raw shared pointer in webui's case), so a future reload
path could not swap the running configuration -- the consumers held
frozen snapshots made at construction. This is the prerequisite for
issue #348.

Add config.Holder, a small RWMutex-backed snapshot holder:
  - Get returns a value copy under RLock; the copy shares its
    reference-type fields with the published snapshot, so a doc
    comment pins the immutability invariant (a published Config is
    immutable; reload constructs a fresh one).
  - Set replaces the snapshot under WLock.
  - Get is nil-safe; Set is not.

Convert every consumer to read through the accessor. The convention
'cfg := h.Get() once per logical operation' is documented on the
type so the next reader knows that inline d.Cfg.Get().Foo is a code
smell (a Set could land between two consecutive Gets).

Special cases:
  - webui.Server.Cfg: was *config.Config, a raw shared pointer read
    from handler goroutines. Convert to *config.Holder. The
    pre-existing 'cfg := s.Cfg' aliasing bug at api_config.go:295
    (which was *trying* to copy but only copied the pointer) now
    becomes 'cfg := s.Cfg.Get()', which is the value copy the
    read-only view always wanted.
  - daemon_test.go: one test mutated d.Cfg.DiffCapLines in place,
    which the Holder forbids. Replace with the documented
    read-modify-write pattern (prev := d.Cfg.Get(); prev.X = Y;
    d.Cfg.Set(prev)).
  - telegramSetup.Cfg: matches the daemon's API for consistency
    even though setup runs before the daemon is alive.

TaskContext.Cfg in internal/domain/workflow/workflow.go is
deliberately NOT converted -- a task that starts under one config
should finish under it, and a config changing between TDD and gate
stages is a bug, not a feature. Snapshotted at dispatch, as before.

Verified: go test ./... (61 packages), go vet, -race on
internal/daemon and internal/config.
2026-08-09 23:29:52 +10:00

99 lines
3.3 KiB
Go

package main
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/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: store.StatusClosedWontDo},
{name: "chat cancel", viaChat: true, action: "cancel", want: store.StatusClosedWontDo},
{name: "dashboard approve", action: "approve", want: store.StatusQueued},
{name: "chat approve", viaChat: true, action: "approve", want: store.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, store.StatusQueued, store.StatusWaitingHuman, "await"); err != nil {
t.Fatal(err)
}
if tc.viaChat {
controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{
taskByID: st.TaskByID,
requeue: st.Requeue,
transition: st.Transition,
})
var actErr error
if tc.action == "approve" {
actErr = controller.Approve(ctx, task.ID, "reviewer")
} else {
actErr = controller.Cancel(ctx, task.ID, "reviewer")
}
if actErr != nil {
t.Fatalf("chat %s: %v", tc.action, actErr)
}
} else {
srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{MaxRetries: 3})}
req := httptest.NewRequestWithContext(ctx, http.MethodPost,
"/api/tasks/"+strconv.FormatInt(task.ID, 10)+"/action",
bytes.NewBufferString(`{"action":"`+tc.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", tc.action, w.Code, w.Body)
}
}
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)
}
})
}
}