|
| 1 | +package state_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stablekernel/crucible/state" |
| 10 | +) |
| 11 | + |
| 12 | +// TestPaletteCategoryAndExamplesSurface registers a behavior with a category, a |
| 13 | +// per-parameter example set, and behavior-level example usages, then asserts they |
| 14 | +// all reach the Palette descriptor and serialize under the expected JSON keys. |
| 15 | +func TestPaletteCategoryAndExamplesSurface(t *testing.T) { |
| 16 | + reg := state.NewRegistry[order]() |
| 17 | + reg.Guard("minAmount", func(c state.GuardCtx[order]) bool { return c.Entity.amount >= 1 }, |
| 18 | + state.Describe("Passes when the amount is at least min."). |
| 19 | + Category("guards"). |
| 20 | + Examples(`guard("minAmount", {"min": 5})`). |
| 21 | + ParamSpec(state.ParamSpec{ |
| 22 | + Name: "min", |
| 23 | + Type: state.IntParam, |
| 24 | + Required: true, |
| 25 | + Examples: []any{1, 5, 100}, |
| 26 | + })) |
| 27 | + |
| 28 | + d := findDescriptor(t, reg.Palette(), state.KindGuard, "minAmount") |
| 29 | + if d.Category != "guards" { |
| 30 | + t.Errorf("category = %q, want %q", d.Category, "guards") |
| 31 | + } |
| 32 | + if !reflect.DeepEqual(d.Examples, []string{`guard("minAmount", {"min": 5})`}) { |
| 33 | + t.Errorf("descriptor examples = %v", d.Examples) |
| 34 | + } |
| 35 | + if len(d.Params) != 1 || !reflect.DeepEqual(d.Params[0].Examples, []any{1, 5, 100}) { |
| 36 | + t.Errorf("param examples = %+v", d.Params) |
| 37 | + } |
| 38 | + |
| 39 | + b, err := json.Marshal(d) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("marshal: %v", err) |
| 42 | + } |
| 43 | + for _, key := range []string{`"category":"guards"`, `"examples":["guard`, `"examples":[1,5,100]`} { |
| 44 | + if !strings.Contains(string(b), key) { |
| 45 | + t.Errorf("descriptor JSON %s missing %s", b, key) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// TestPaletteWithoutCompletenessFieldsOmitsKeys is the additive guarantee: a |
| 51 | +// descriptor registered without a category or examples must serialize to exactly |
| 52 | +// the same bytes as before — no spurious "category" or "examples" keys. |
| 53 | +func TestPaletteWithoutCompletenessFieldsOmitsKeys(t *testing.T) { |
| 54 | + reg := state.NewRegistry[order]() |
| 55 | + reg.Action("charge", func(state.ActionCtx[order]) (state.Effect, error) { return nil, nil }, |
| 56 | + state.Describe("Charges the order."). |
| 57 | + Param("gateway", state.StringParam). |
| 58 | + Writes("Order")) |
| 59 | + |
| 60 | + d := findDescriptor(t, reg.Palette(), state.KindAction, "charge") |
| 61 | + if d.Category != "" || d.Examples != nil { |
| 62 | + t.Fatalf("expected empty completeness fields, got category=%q examples=%v", d.Category, d.Examples) |
| 63 | + } |
| 64 | + |
| 65 | + b, err := json.Marshal(d) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("marshal: %v", err) |
| 68 | + } |
| 69 | + want := `{"kind":"action","name":"charge","description":"Charges the order.","params":[{"name":"gateway","type":"string","required":true}],"writes":["Order"]}` |
| 70 | + if string(b) != want { |
| 71 | + t.Fatalf("descriptor JSON changed:\n got %s\nwant %s", b, want) |
| 72 | + } |
| 73 | + |
| 74 | + // A param without examples likewise omits the key. |
| 75 | + pb, err := json.Marshal(d.Params[0]) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("marshal param: %v", err) |
| 78 | + } |
| 79 | + if strings.Contains(string(pb), "examples") { |
| 80 | + t.Fatalf("param JSON unexpectedly carries examples: %s", pb) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TestPaletteCompletenessJSONRoundTrip confirms the new fields survive a marshal |
| 85 | +// and unmarshal so a loaded palette preserves them. |
| 86 | +func TestPaletteCompletenessJSONRoundTrip(t *testing.T) { |
| 87 | + reg := state.NewRegistry[order]() |
| 88 | + reg.Service("notify", nil, |
| 89 | + state.Describe("Notifies the customer."). |
| 90 | + Category("side-effects"). |
| 91 | + Examples("notify email"). |
| 92 | + ParamSpec(state.ParamSpec{ |
| 93 | + Name: "channel", |
| 94 | + Type: state.EnumParam, |
| 95 | + Required: true, |
| 96 | + Enum: []string{"email", "sms"}, |
| 97 | + Examples: []any{"email"}, |
| 98 | + })) |
| 99 | + |
| 100 | + in := findDescriptor(t, reg.Palette(), state.KindService, "notify") |
| 101 | + b, err := json.Marshal(in) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("marshal: %v", err) |
| 104 | + } |
| 105 | + var out state.Descriptor |
| 106 | + if err := json.Unmarshal(b, &out); err != nil { |
| 107 | + t.Fatalf("unmarshal: %v", err) |
| 108 | + } |
| 109 | + if !reflect.DeepEqual(in, out) { |
| 110 | + t.Fatalf("round trip mismatch:\n in %+v\nout %+v", in, out) |
| 111 | + } |
| 112 | +} |
0 commit comments