Merge branch 'main' into pg/foundation
This commit is contained in:
12 files changed
+344
-75
No files matched your search
@@ -3,17 +3,53 @@ package webui
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// handleTaskMeta serves the lifecycle presentation catalog: the status
|
||||
// vocabulary (with label, pill severity, "needs you" grouping) and the operator
|
||||
// action controls (with label, button variant, confirm prompt). Everything the
|
||||
// dashboard can present is derived here, so the frontend never has to keep a
|
||||
// hand-synced copy of the vocabulary or the control set.
|
||||
// changeStatusMeta describes how to present one changes_captured status value.
|
||||
// The IDs are the producer's own constants (internal/domain/workflow/task), so
|
||||
// the dashboard never spells them out and a rename reaches the browser instead
|
||||
// of turning every row into a raw status code.
|
||||
type changeStatusMeta struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
}
|
||||
|
||||
// taskMetaView is the whole GET /api/task-meta payload. It is served as one
|
||||
// struct rather than an inline map so the wire shape is declared in one place
|
||||
// and pinned against the dashboard's freeze-dried snapshot by a fixture test.
|
||||
type taskMetaView struct {
|
||||
Statuses []taskstate.StatusMeta `json:"statuses"`
|
||||
Actions []taskstate.ActionMeta `json:"actions"`
|
||||
ChangeStatuses []changeStatusMeta `json:"change_statuses"`
|
||||
ConfigSchema string `json:"config_schema"`
|
||||
}
|
||||
|
||||
// buildTaskMeta derives the lifecycle presentation catalog: the status
|
||||
// vocabulary (with label, pill severity, "needs you" grouping), the operator
|
||||
// action controls (with label, button variant, confirm prompt), the
|
||||
// changes_captured status labels and the schema stamp a config_captured
|
||||
// payload carries. Everything the dashboard can present that also exists on
|
||||
// the server is derived here, so the frontend never has to keep a hand-synced
|
||||
// copy of the vocabulary or the control set.
|
||||
func buildTaskMeta() taskMetaView {
|
||||
return taskMetaView{
|
||||
Statuses: taskstate.Statuses(),
|
||||
Actions: taskstate.ActionCatalog(),
|
||||
ChangeStatuses: []changeStatusMeta{
|
||||
{ID: task.ChangeAdded, Label: "Added"},
|
||||
{ID: task.ChangeModified, Label: "Modified"},
|
||||
{ID: task.ChangeDeleted, Label: "Deleted"},
|
||||
{ID: task.ChangeRenamed, Label: "Renamed"},
|
||||
{ID: task.ChangeTypeChanged, Label: "Type changed"},
|
||||
},
|
||||
ConfigSchema: events.ConfigCapturedSchema,
|
||||
}
|
||||
}
|
||||
|
||||
// handleTaskMeta serves the catalog. See buildTaskMeta for what it carries.
|
||||
func (s *Server) handleTaskMeta(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, map[string]any{
|
||||
"statuses": taskstate.Statuses(),
|
||||
"actions": taskstate.ActionCatalog(),
|
||||
})
|
||||
writeJSON(w, buildTaskMeta())
|
||||
}
|
||||
@@ -1,12 +1,24 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
)
|
||||
|
||||
// updateTaskMetaFixture rewrites the committed payload fixture from the served
|
||||
// handler. It is the same generated-artifact discipline the repo already uses
|
||||
// for docs/data/generated/contracts.json (task docs:generate): the fixture is
|
||||
// committed, the gate checks rather than regenerates, and the -update run is
|
||||
// what blesses a deliberate vocabulary change.
|
||||
var updateTaskMetaFixture = flag.Bool("update", false, "rewrite testdata/task_meta.json from the served /api/task-meta payload")
|
||||
|
||||
// The dashboard derives everything it can present from /api/task-meta. If the
|
||||
// catalog is empty or malformed the frontend falls back to its own defaults and
|
||||
// the whole point of the endpoint -- one server-held vocabulary -- is lost.
|
||||
@@ -33,6 +45,11 @@ func TestTaskMetaCatalog(t *testing.T) {
|
||||
Kind string `json:"kind"`
|
||||
Confirm string `json:"confirm"`
|
||||
} `json:"actions"`
|
||||
ChangeStatuses []struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
} `json:"change_statuses"`
|
||||
ConfigSchema string `json:"config_schema"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
@@ -44,6 +61,12 @@ func TestTaskMetaCatalog(t *testing.T) {
|
||||
if len(got.Actions) == 0 {
|
||||
t.Error("actions empty")
|
||||
}
|
||||
if len(got.ChangeStatuses) == 0 {
|
||||
t.Error("change_statuses empty; the dashboard renders raw status codes without it")
|
||||
}
|
||||
if got.ConfigSchema == "" {
|
||||
t.Error("config_schema empty; the config panel would call every capture an unknown schema")
|
||||
}
|
||||
for _, s := range got.Statuses {
|
||||
if s.ID == "" || s.Label == "" {
|
||||
t.Errorf("status missing id/label: %+v", s)
|
||||
@@ -54,4 +77,86 @@ func TestTaskMetaCatalog(t *testing.T) {
|
||||
t.Errorf("action missing id/label: %+v", a)
|
||||
}
|
||||
}
|
||||
for _, cs := range got.ChangeStatuses {
|
||||
if cs.ID == "" || cs.Label == "" {
|
||||
t.Errorf("change status missing id/label: %+v", cs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskMetaChangeStatusesAreDeliberate pins the catalog's change-status IDs
|
||||
// to the producer's constants, in order, each with a distinct label. It is the
|
||||
// same guard config_schema_test.go applies to the config catalog: a sixth
|
||||
// Change* constant added without a label fails here rather than reaching the
|
||||
// dashboard as a raw status code.
|
||||
func TestTaskMetaChangeStatusesAreDeliberate(t *testing.T) {
|
||||
want := []string{
|
||||
task.ChangeAdded,
|
||||
task.ChangeModified,
|
||||
task.ChangeDeleted,
|
||||
task.ChangeRenamed,
|
||||
task.ChangeTypeChanged,
|
||||
}
|
||||
got := buildTaskMeta().ChangeStatuses
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("change_statuses has %d entries, want %d (one per Change* constant)", len(got), len(want))
|
||||
}
|
||||
labels := make(map[string]string, len(got))
|
||||
for i, cs := range got {
|
||||
if cs.ID != want[i] {
|
||||
t.Errorf("change_statuses[%d].id = %q, want %q", i, cs.ID, want[i])
|
||||
}
|
||||
if cs.Label == "" {
|
||||
t.Errorf("change status %q has no label", cs.ID)
|
||||
continue
|
||||
}
|
||||
if prev, dup := labels[cs.Label]; dup {
|
||||
t.Errorf("change statuses %q and %q share the label %q", prev, cs.ID, cs.Label)
|
||||
}
|
||||
labels[cs.Label] = cs.ID
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskMetaPayloadMatchesFixture pins the served payload byte-for-byte
|
||||
// against the committed fixture, and the fixture against the dashboard's
|
||||
// freeze-dried snapshot (ui/src/base/task-meta.jsx) via
|
||||
// ui/test/task-meta-catalogue.test.js. Between the two, a one-sided rename of a
|
||||
// status label, an action, a change status or the config schema fails a test on
|
||||
// whichever side was not updated -- the property neither per-side literal test
|
||||
// could provide.
|
||||
//
|
||||
// Regenerate with:
|
||||
//
|
||||
// go test ./internal/webui -run TestTaskMetaPayloadMatchesFixture -update
|
||||
func TestTaskMetaPayloadMatchesFixture(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/task-meta", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body %s", w.Code, w.Body)
|
||||
}
|
||||
served := w.Body.Bytes()
|
||||
|
||||
const fixture = "testdata/task_meta.json"
|
||||
if *updateTaskMetaFixture {
|
||||
if err := os.MkdirAll("testdata", 0o755); err != nil {
|
||||
t.Fatalf("mkdir testdata: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(fixture, served, 0o644); err != nil {
|
||||
t.Fatalf("write fixture: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
committed, err := os.ReadFile(fixture)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v\nrun: go test ./internal/webui -run TestTaskMetaPayloadMatchesFixture -update", fixture, err)
|
||||
}
|
||||
if !bytes.Equal(served, committed) {
|
||||
t.Errorf("GET /api/task-meta no longer matches %s.\n"+
|
||||
"The fixture pins the dashboard's freeze-dried snapshot (ui/src/base/task-meta.jsx);\n"+
|
||||
"ui/test/task-meta-catalogue.test.js fails until both sides agree.\n"+
|
||||
"regenerate with: go test ./internal/webui -run TestTaskMetaPayloadMatchesFixture -update", fixture)
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"statuses":[{"id":"queued","label":"Queued","kind":"idle","needs_you":false},{"id":"running","label":"Working","kind":"info","needs_you":false},{"id":"waiting_human","label":"Waiting for you","kind":"warn","needs_you":true},{"id":"pr_open","label":"In review","kind":"ok","needs_you":false},{"id":"merged","label":"Merged","kind":"ok","needs_you":false},{"id":"completed","label":"Done","kind":"ok","needs_you":false},{"id":"parked","label":"Parked","kind":"warn","needs_you":true},{"id":"dead","label":"Stopped (too many retries)","kind":"danger","needs_you":false},{"id":"rejected","label":"Rejected","kind":"danger","needs_you":false},{"id":"closed_wont_do","label":"Won't do","kind":"idle","needs_you":false}],"actions":[{"id":"cancel","label":"Cancel","kind":"quiet","confirm":"Cancel \"{title}\"? This closes the forge issue."},{"id":"stop","label":"Stop","kind":"primary","confirm":"Stop \"{title}\"? Recoverable work will remain parked."},{"id":"approve","label":"Approve","kind":"primary","confirm":""},{"id":"reject","label":"Reject","kind":"quiet","confirm":"Reject \"{title}\"? This closes the forge issue."},{"id":"retry","label":"Retry","kind":"primary","confirm":""},{"id":"abandon","label":"Abandon","kind":"quiet","confirm":"Abandon \"{title}\"? This closes the forge issue."},{"id":"archive","label":"Archive","kind":"quiet","confirm":"Archive the local record for \"{title}\"?"},{"id":"open_pr","label":"Open PR","kind":"link","confirm":""},{"id":"open_issue","label":"Open issue","kind":"link","confirm":""}],"change_statuses":[{"id":"added","label":"Added"},{"id":"modified","label":"Modified"},{"id":"deleted","label":"Deleted"},{"id":"renamed","label":"Renamed"},{"id":"typechange","label":"Type changed"}],"config_schema":"archie/task-config@1"}
|
||||
Reference in new issue
Block a user