The event-capture tables (captures, mappings, bindings, binding_dispatches,
playbook_dispatches) now live in PocketBase-owned collections and their ids
are opaque record ids rather than autoincrement integers. The task lifecycle
stays on SQLite: tasks/transitions/events carry FIFO claim ordering, id-cursor
pagination and the task-grant map, none of which gain anything from being
hand-editable. binding_dispatches.task_id is the single seam and stays an
integer addressing that store.
BREAKING: CapturedEvent.Id, Mapping.Id, Binding.Id and Binding.MappingId are
strings on the wire, as are the ids in the capture/mapping/binding requests.
RecordDispatchRequest.task_id is deliberately unchanged.
What the collections buy, beyond storage: a payload nobody has modelled yet is
inspectable, a mapping is editable at runtime, and a binding's relation to its
mapping is enforced. The old schema declared no FOREIGN KEY on that link, so a
dangling mapping_id was previously possible and is now refused.
Behaviour preserved deliberately, each recovered from the SQLite version
rather than reimplemented from memory:
- draft -> pending_approval -> armed, with approval only from
pending_approval and an edit always returning a binding for re-approval
- one binding per source, guarded on insert, update and approve
- an empty secret on update keeps the stored one, so an edit form that does
not echo it back cannot silently disarm a binding
- ErrMappingNotFound / ErrBindingNotFound on a missing delete target
- sentinels are re-exported from storecontract, never redefined: staterpc
matches on exact canonical message to rehydrate them, so a second
definition would break errors.Is across the wire invisibly
Two implementation notes worth keeping:
- at-most-once dispatch is now a unique index rather than INSERT OR IGNORE.
Only the unique-violation maps to ErrAlreadyDispatched; any other save
error is returned, because treating them all as "already recorded" would
turn the ledger into a silent at-least-once dispatcher.
- captures store received_at themselves at nanosecond precision in a
fixed-width layout. PocketBase autodates are millisecond and record ids
are random, so same-millisecond captures would otherwise order
arbitrarily; the old store got insertion order free from autoincrement.
- ListUndispatchedCaptures is raw SQL: the anti-join is a subquery and
PocketBase's filter DSL is not SQL. A read fires no hooks either way.
Adds tool_calls, which had no persistence at all: tool calls only ever reached
the on-disk task log, readable as text but never queryable or watchable.
Nothing writes it yet.
The binding secret cipher moves with the bindings it protects.
307 lines
11 KiB
Go
307 lines
11 KiB
Go
package webui
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
|
|
"github.com/samcharles93/archie-core/internal/store"
|
|
)
|
|
|
|
// mappingTestServer builds a Server with both Mappings and Captures backed
|
|
// by the same concrete *store.Store, so preview tests can insert a real
|
|
// capture and resolve a mapping against it end to end.
|
|
func mappingTestServer(t *testing.T) *Server {
|
|
t.Helper()
|
|
s, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "test.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = s.Close() })
|
|
eda := edastore.OpenTest(t)
|
|
return &Server{
|
|
Store: s,
|
|
Log: slog.New(slog.DiscardHandler),
|
|
Mappings: eda,
|
|
Captures: eda,
|
|
}
|
|
}
|
|
|
|
// doJSON issues a request carrying the browser mutation headers
|
|
// (Content-Type, X-Archie-CSRF) every write handler requires via
|
|
// authorizeTaskMutation, so GET/POST/PATCH/DELETE calls in this package's
|
|
// tests exercise the handler logic rather than the CSRF gate. Use
|
|
// doJSONNoCSRF to test the gate itself.
|
|
func doJSON(t *testing.T, srv *Server, method, path string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
return doJSONWithHeaders(t, srv, method, path, body, true)
|
|
}
|
|
|
|
// doJSONNoCSRF is doJSON without the X-Archie-CSRF header, for asserting
|
|
// that a mutation handler refuses an unmarked cross-origin-shaped request.
|
|
func doJSONNoCSRF(t *testing.T, srv *Server, method, path string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
return doJSONWithHeaders(t, srv, method, path, body, false)
|
|
}
|
|
|
|
func doJSONWithHeaders(t *testing.T, srv *Server, method, path string, body any, csrf bool) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
var r *http.Request
|
|
if body != nil {
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
t.Fatalf("marshal request body: %v", err)
|
|
}
|
|
r = httptest.NewRequestWithContext(t.Context(), method, path, bytes.NewReader(b))
|
|
} else {
|
|
r = httptest.NewRequestWithContext(t.Context(), method, path, nil)
|
|
}
|
|
r.Header.Set("Content-Type", "application/json")
|
|
if csrf {
|
|
r.Header.Set("X-Archie-CSRF", "1")
|
|
}
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
func TestHandleMappingCreateAndGet(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "sentry issue opened",
|
|
"source_hint": "sentry",
|
|
"fields": []mapping.Field{
|
|
{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true},
|
|
},
|
|
})
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("create status = %d, want %d; body = %s", w.Code, http.StatusCreated, w.Body.String())
|
|
}
|
|
var created mapping.Mapping
|
|
if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("unmarshal created mapping: %v", err)
|
|
}
|
|
if created.ID == "" || created.Name != "sentry issue opened" {
|
|
t.Fatalf("created mapping = %+v", created)
|
|
}
|
|
|
|
w = doJSON(t, srv, http.MethodGet, "/api/mappings/"+created.ID, nil)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("get status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
var got mapping.Mapping
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("unmarshal got mapping: %v", err)
|
|
}
|
|
if got.ID != created.ID || len(got.Fields) != 1 {
|
|
t.Fatalf("got mapping = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingCreateRejectsInvalidMapping(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{"name": "", "fields": []mapping.Field{}})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusBadRequest, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingGetMissingReturns404(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodGet, "/api/mappings/999", nil)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d", w.Code, http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingsList(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
for range 3 {
|
|
doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "m",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
}
|
|
w := doJSON(t, srv, http.MethodGet, "/api/mappings", nil)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
var body struct {
|
|
Mappings []mapping.Mapping `json:"mappings"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if len(body.Mappings) != 3 {
|
|
t.Fatalf("mappings = %d, want 3", len(body.Mappings))
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingUpdate(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "orig",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
var created mapping.Mapping
|
|
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
|
|
|
w = doJSON(t, srv, http.MethodPatch, "/api/mappings/"+created.ID, map[string]any{
|
|
"name": "renamed",
|
|
"fields": []mapping.Field{{Name: "b", Path: "b", Type: mapping.TypeBool}},
|
|
})
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("update status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
|
|
w = doJSON(t, srv, http.MethodGet, "/api/mappings/"+created.ID, nil)
|
|
var got mapping.Mapping
|
|
_ = json.Unmarshal(w.Body.Bytes(), &got)
|
|
if got.Name != "renamed" || len(got.Fields) != 1 || got.Fields[0].Name != "b" {
|
|
t.Fatalf("updated mapping = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingUpdateMissingReturns404(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPatch, "/api/mappings/999", map[string]any{
|
|
"name": "x",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusNotFound, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingDelete(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", map[string]any{
|
|
"name": "m",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
var created mapping.Mapping
|
|
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
|
|
|
w = doJSON(t, srv, http.MethodDelete, "/api/mappings/"+created.ID, nil)
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("delete status = %d, want %d; body = %s", w.Code, http.StatusNoContent, w.Body.String())
|
|
}
|
|
w = doJSON(t, srv, http.MethodGet, "/api/mappings/"+created.ID, nil)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("get after delete status = %d, want %d", w.Code, http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingPreviewResolvesAgainstARealCapture(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
capID, err := srv.Captures.InsertCapture(t.Context(), store.CapturedEvent{
|
|
Source: "sentry",
|
|
Body: `{"issue":{"title":"bug found"},"count":3}`,
|
|
}, 0, 0)
|
|
if err != nil {
|
|
t.Fatalf("InsertCapture: %v", err)
|
|
}
|
|
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings/preview", map[string]any{
|
|
"capture_id": capID,
|
|
"fields": []mapping.Field{
|
|
{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true},
|
|
{Name: "missing", Path: "nope", Type: mapping.TypeString, Required: true},
|
|
},
|
|
})
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("preview status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
var body struct {
|
|
Values map[string]any `json:"values"`
|
|
Failures []mapping.Failure `json:"failures"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if body.Values["title"] != "bug found" {
|
|
t.Fatalf("values = %+v", body.Values)
|
|
}
|
|
if len(body.Failures) != 1 || body.Failures[0].FieldName != "missing" || body.Failures[0].Reason != mapping.ReasonMissing {
|
|
t.Fatalf("failures = %+v", body.Failures)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingPreviewUnknownCaptureReturns404(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings/preview", map[string]any{
|
|
"capture_id": "rabsent00000000",
|
|
"fields": []mapping.Field{{Name: "a", Path: "a", Type: mapping.TypeString}},
|
|
})
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusNotFound, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestMappingCaptureScanWindowUsesConfiguredCaptureMaxEvents(t *testing.T) {
|
|
if got := mappingCaptureScanWindow(20000); got != 20000 {
|
|
t.Errorf("mappingCaptureScanWindow(20000) = %d, want 20000", got)
|
|
}
|
|
}
|
|
|
|
func TestMappingCaptureScanWindowFallsBackWhenUnconfigured(t *testing.T) {
|
|
if got := mappingCaptureScanWindow(0); got != captureScanWindowDefault {
|
|
t.Errorf("mappingCaptureScanWindow(0) = %d, want default %d", got, captureScanWindowDefault)
|
|
}
|
|
if got := mappingCaptureScanWindow(-1); got != captureScanWindowDefault {
|
|
t.Errorf("mappingCaptureScanWindow(-1) = %d, want default %d", got, captureScanWindowDefault)
|
|
}
|
|
}
|
|
|
|
func TestHandleMappingsUnavailableWhenNotConfigured(t *testing.T) {
|
|
srv := &Server{Store: nil, Log: slog.New(slog.DiscardHandler)}
|
|
w := doJSON(t, srv, http.MethodGet, "/api/mappings", nil)
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status = %d, want %d", w.Code, http.StatusServiceUnavailable)
|
|
}
|
|
}
|
|
|
|
// TestHandleMappingMutationsRequireCSRFHeader mirrors
|
|
// TestHandleBindingMutationsRequireCSRFHeader: every mapping write handler
|
|
// must enforce the same browser mutation contract as handleTaskAction
|
|
// (docs/prds/dynamic-curators.md), not just bindings.
|
|
func TestHandleMappingMutationsRequireCSRFHeader(t *testing.T) {
|
|
srv := mappingTestServer(t)
|
|
newMapping := map[string]any{
|
|
"name": "sentry issue opened",
|
|
"source_hint": "sentry",
|
|
"fields": []mapping.Field{
|
|
{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true},
|
|
},
|
|
}
|
|
w := doJSON(t, srv, http.MethodPost, "/api/mappings", newMapping)
|
|
var created mapping.Mapping
|
|
if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("unmarshal created mapping: %v; body = %s", err, w.Body.String())
|
|
}
|
|
id := created.ID
|
|
|
|
tests := []struct {
|
|
name, method, path string
|
|
body any
|
|
}{
|
|
{"create", http.MethodPost, "/api/mappings", newMapping},
|
|
{"update", http.MethodPatch, "/api/mappings/" + id, newMapping},
|
|
{"delete", http.MethodDelete, "/api/mappings/" + id, nil},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
w := doJSONNoCSRF(t, srv, tc.method, tc.path, tc.body)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d without a CSRF header; body = %s", w.Code, http.StatusForbidden, w.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|