Files
archie/internal/store/mappings_test.go
T
sam 3ade200ca7 feat(webui): payload field mapping -- bind JSON paths to named fields, preview before saving
Adds t2db.3: schema-by-example field mapping for the no-code playbook epic.
An operator picks a captured event, clicks through its payload to bind named
fields to JSON paths, and previews the resolution against a real event
before saving -- a missing, null, or type-changed path fails loudly (named
by field and event) rather than handing an agent a blank value.

- internal/domain/mapping: Mapping/Field types, path parsing/walking,
  Resolve (per-field failure reporting), Validate (incl. duplicate names)
- internal/store/mappings.go: SQLite persistence, mirrors captures.go
- internal/webui/api_mapping.go: CRUD + preview HTTP API
- ui/src/mappings/: dashboard editor (clickable payload tree, live preview)

Design decisions recorded in docs/prds/payload-field-mapping.md. t2db.4
(binding a mapping + matcher to a workflow) is out of scope here.
2026-08-22 19:18:15 +10:00

146 lines
4.4 KiB
Go

package store
import (
"errors"
"testing"
"github.com/samcharles93/archie-core/internal/domain/mapping"
)
func testFields() []mapping.Field {
return []mapping.Field{
{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true},
{Name: "count", Path: "count", Type: mapping.TypeNumber},
}
}
func TestInsertMappingRoundTrips(t *testing.T) {
s := openTest(t)
id, err := s.InsertMapping(t.Context(), mapping.Mapping{
Name: "sentry issue opened",
SourceHint: "sentry",
Fields: testFields(),
})
if err != nil {
t.Fatalf("InsertMapping: %v", err)
}
if id == 0 {
t.Fatalf("InsertMapping returned zero id")
}
got, err := s.GetMapping(t.Context(), id)
if err != nil {
t.Fatalf("GetMapping: %v", err)
}
if got == nil {
t.Fatalf("GetMapping returned nil for a just-inserted row")
}
if got.ID != id || got.Name != "sentry issue opened" || got.SourceHint != "sentry" {
t.Fatalf("round-tripped mapping = %+v", got)
}
if len(got.Fields) != 2 || got.Fields[0].Name != "title" || got.Fields[0].Path != "issue.title" ||
got.Fields[0].Type != mapping.TypeString || !got.Fields[0].Required {
t.Fatalf("round-tripped fields = %+v", got.Fields)
}
if got.CreatedAt.IsZero() || got.UpdatedAt.IsZero() {
t.Fatalf("CreatedAt/UpdatedAt must be stamped, got %+v", got)
}
}
func TestGetMappingMissingReturnsNilNoError(t *testing.T) {
s := openTest(t)
got, err := s.GetMapping(t.Context(), 999)
if err != nil {
t.Fatalf("GetMapping: %v", err)
}
if got != nil {
t.Fatalf("GetMapping for a missing id = %+v, want nil", got)
}
}
func TestListMappingsOrdersNewestFirst(t *testing.T) {
s := openTest(t)
for i := range 3 {
if _, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "m", Fields: testFields()}); err != nil {
t.Fatalf("InsertMapping[%d]: %v", i, err)
}
}
got, err := s.ListMappings(t.Context())
if err != nil {
t.Fatalf("ListMappings: %v", err)
}
if len(got) != 3 {
t.Fatalf("ListMappings = %d rows, want 3", len(got))
}
if got[0].ID <= got[1].ID || got[1].ID <= got[2].ID {
t.Fatalf("ListMappings not newest-first: ids = %d, %d, %d", got[0].ID, got[1].ID, got[2].ID)
}
}
func TestUpdateMappingChangesFieldsAndBumpsUpdatedAt(t *testing.T) {
s := openTest(t)
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "orig", Fields: testFields()})
if err != nil {
t.Fatalf("InsertMapping: %v", err)
}
before, err := s.GetMapping(t.Context(), id)
if err != nil || before == nil {
t.Fatalf("GetMapping before update: %+v, %v", before, err)
}
newFields := []mapping.Field{{Name: "renamed", Path: "a.b", Type: mapping.TypeBool}}
if err := s.UpdateMapping(t.Context(), mapping.Mapping{
ID: id, Name: "renamed mapping", SourceHint: "github", Fields: newFields,
}); err != nil {
t.Fatalf("UpdateMapping: %v", err)
}
after, err := s.GetMapping(t.Context(), id)
if err != nil || after == nil {
t.Fatalf("GetMapping after update: %+v, %v", after, err)
}
if after.Name != "renamed mapping" || after.SourceHint != "github" {
t.Fatalf("updated mapping = %+v", after)
}
if len(after.Fields) != 1 || after.Fields[0].Name != "renamed" {
t.Fatalf("updated fields = %+v", after.Fields)
}
if !after.UpdatedAt.After(before.UpdatedAt) && !after.UpdatedAt.Equal(before.UpdatedAt) {
t.Fatalf("UpdatedAt did not advance: before=%v after=%v", before.UpdatedAt, after.UpdatedAt)
}
}
func TestUpdateMappingMissingReturnsErrMappingNotFound(t *testing.T) {
s := openTest(t)
err := s.UpdateMapping(t.Context(), mapping.Mapping{ID: 999, Name: "x", Fields: testFields()})
if !errors.Is(err, ErrMappingNotFound) {
t.Fatalf("UpdateMapping on missing id = %v, want ErrMappingNotFound", err)
}
}
func TestDeleteMappingRemovesRow(t *testing.T) {
s := openTest(t)
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "m", Fields: testFields()})
if err != nil {
t.Fatalf("InsertMapping: %v", err)
}
if err := s.DeleteMapping(t.Context(), id); err != nil {
t.Fatalf("DeleteMapping: %v", err)
}
got, err := s.GetMapping(t.Context(), id)
if err != nil {
t.Fatalf("GetMapping after delete: %v", err)
}
if got != nil {
t.Fatalf("GetMapping after delete = %+v, want nil", got)
}
}
func TestDeleteMappingMissingReturnsErrMappingNotFound(t *testing.T) {
s := openTest(t)
err := s.DeleteMapping(t.Context(), 999)
if !errors.Is(err, ErrMappingNotFound) {
t.Fatalf("DeleteMapping on missing id = %v, want ErrMappingNotFound", err)
}
}