Seventh slice of docs/prds/eda-playbook-engine.md (t2db.15), consuming
t2db.14's expr package: the rich trigger+actions playbook document and
the coordinator that dispatches it.
- internal/domain/eda/playbook: Load(dir) reads *.yaml/*.yml playbook
documents (trigger + exactly one workflow-kind action + optional CEL
when), validating the HARD BOUNDARY at load: a playbook with 2+ actions
or a non-workflow action position is a reported load failure --
multi-action and Module/Channel/Forge actions stay blocked on the
unresolved execution-time gaps. Any malformed YAML, invalid trigger,
missing workflow name, or when compile failure fails the WHOLE load
(reject-at-load, same shape as routing collisions); a missing dir is an
empty store. Trigger reuses the existing workintake kind/label
vocabulary (no second label-matching mechanism). Dispatch matches
trigger, evaluates when (CEL, event context with actions: {} -- no
prior actions in single-action playbooks), and returns the named
workflow from the registry via the same lookup Route() uses -- no
duplicated dispatch logic. when eval error follows the resolved doc's
J3: false, skip, caller logs.
- Store.Dispatch(reg, DispatchInput) is the coordinator entrypoint; input
carries Labels/Kind/Event. Production task intake wiring
(daemon.go/pollNATS/publishTask) is explicitly NOT in this ticket -- a
follow-up; proven end to end via direct tests plus the shipped example
(examples/eda-playbooks/bug-tdd.yaml) loaded and dispatched through the
real path.
- Config: EDAPlaybookDir (eda_playbook_dir), documented as DISTINCT from
PlaybookDirs (flat binding files) -- a reader cannot confuse the two.
- Bootstrap: playbook.Load wired in loadWorkflows, log-and-abort on
failure matching every prior slice; nonexistent dir is an empty store.
- workflow.Route(), workflowForLabels, and the t2db.9-.12 binding loaders
are UNTOUCHED (verified: their full test suites pass, zero regressions).
Also carries small lint cleanups on t2db.13/.14 code flagged by the gate
run: expr Eval no longer type-asserts to ref.Val (Eval returns ref.Val
directly in v0.32.0; cel-go's own Eval wraps evaluation in recover --
verified in source); module decodeLogArgs extracted to clear the nestif
finding. golangci-lint: 0 issues.
Tests (10): single-action round trip, when-false skip, 2-action load
failure, non-workflow position load failure, when compile failure drops
playbook, empty trigger load failure, trigger mismatch skip, no-when
match, one-invalid-file fails whole load, missing dir empty store, plus
the shipped example load+dispatch. task check clean (113/113 UI, exit 0).
187 lines
5.7 KiB
Go
187 lines
5.7 KiB
Go
package expr
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"cel.dev/cel-go/cel"
|
|
)
|
|
|
|
// TestCompileEvalValidBooleanAgainstEvent is the happy path: a boolean
|
|
// condition reading an event field compiles and evaluates correctly.
|
|
func TestCompileEvalValidBooleanAgainstEvent(t *testing.T) {
|
|
env := NewEnv()
|
|
prg, err := env.Compile(`event.label == "bugfix"`)
|
|
if err != nil {
|
|
t.Fatalf("Compile: %v", err)
|
|
}
|
|
got, err := env.Eval(prg, Context{
|
|
Event: map[string]any{"label": "bugfix", "priority": 3},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Eval: %v", err)
|
|
}
|
|
if b, ok := got.(bool); !ok || !b {
|
|
t.Fatalf("Eval = %#v, want true", got)
|
|
}
|
|
}
|
|
|
|
// TestCompileSyntaxErrorIsReturned: a syntax error is a Compile-time error,
|
|
// never a panic.
|
|
func TestCompileSyntaxErrorIsReturned(t *testing.T) {
|
|
env := NewEnv()
|
|
_, err := env.Compile(`event.label == `)
|
|
if err == nil {
|
|
t.Fatal("Compile(syntax error) = nil, want error")
|
|
}
|
|
}
|
|
|
|
// TestCompileUnknownRootIsReturned: a reference to a root other than
|
|
// event/actions is rejected at compile time -- the reject-at-load rule.
|
|
func TestCompileUnknownRootIsReturned(t *testing.T) {
|
|
env := NewEnv()
|
|
_, err := env.Compile(`foo.bar == 1`)
|
|
if err == nil {
|
|
t.Fatal("Compile(unknown root) = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "'foo'") {
|
|
t.Errorf("Compile(unknown root) error = %q, want it to name the root", err.Error())
|
|
}
|
|
}
|
|
|
|
// TestEvalMissingEventFieldNoPanic: evaluating against a missing event field
|
|
// returns an error, never panics (CEL's documented behavior).
|
|
func TestEvalMissingEventFieldNoPanic(t *testing.T) {
|
|
env := NewEnv()
|
|
prg, err := env.Compile(`event.missing == "x"`)
|
|
if err != nil {
|
|
t.Fatalf("Compile: %v", err)
|
|
}
|
|
_, err = env.Eval(prg, Context{Event: map[string]any{}})
|
|
if err == nil {
|
|
t.Fatal("Eval(missing field) = nil error, want error (not panic)")
|
|
}
|
|
}
|
|
|
|
// TestEvalHasMacroPresence: has() covers the missing-key case positively.
|
|
func TestEvalHasMacroPresence(t *testing.T) {
|
|
env := NewEnv()
|
|
prg, err := env.Compile(`has(event.label)`)
|
|
if err != nil {
|
|
t.Fatalf("Compile: %v", err)
|
|
}
|
|
got, err := env.Eval(prg, Context{Event: map[string]any{}})
|
|
if err != nil {
|
|
t.Fatalf("Eval: %v", err)
|
|
}
|
|
if b, ok := got.(bool); !ok || b {
|
|
t.Fatalf("has() on missing field = %#v, want false", got)
|
|
}
|
|
}
|
|
|
|
// TestEvalActionsContext: prior-action results read via actions.<id>.result.
|
|
func TestEvalActionsContext(t *testing.T) {
|
|
env := NewEnv()
|
|
prg, err := env.Compile(`actions.a.result.written == true`)
|
|
if err != nil {
|
|
t.Fatalf("Compile: %v", err)
|
|
}
|
|
got, err := env.Eval(prg, Context{
|
|
Actions: map[string]map[string]any{
|
|
"a": {"result": map[string]any{"written": true}},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Eval: %v", err)
|
|
}
|
|
if b, ok := got.(bool); !ok || !b {
|
|
t.Fatalf("Eval = %#v, want true", got)
|
|
}
|
|
}
|
|
|
|
// TestEvalDeepNestedNoPanic: hostile deep nested access errors cleanly.
|
|
func TestEvalDeepNestedNoPanic(t *testing.T) {
|
|
env := NewEnv()
|
|
prg, err := env.Compile(`event.a.b.c.d == 1`)
|
|
if err != nil {
|
|
t.Fatalf("Compile: %v", err)
|
|
}
|
|
_, err = env.Eval(prg, Context{Event: map[string]any{}})
|
|
if err == nil {
|
|
t.Fatal("Eval(deep missing) = nil error, want error")
|
|
}
|
|
}
|
|
|
|
// TestCompileOverCostLimitRejected: an expression that would exceed the cost
|
|
// limit is rejected at evaluation, not silently truncated. Verified CEL
|
|
// semantics: cost limits abort with "actual cost limit exceeded" on
|
|
// macro-heavy (map/filter) expressions -- the cost model accrues on
|
|
// iteration, not on field reads.
|
|
func TestCompileOverCostLimitRejected(t *testing.T) {
|
|
env := NewEnv()
|
|
prg, err := env.Compile(`[1,2,3,4,5].map(x, x * 2).filter(x, x > 1).size() > 3`)
|
|
if err != nil {
|
|
t.Fatalf("Compile: %v", err)
|
|
}
|
|
// With the DefaultCostLimit this succeeds (it is a modest expression).
|
|
got, err := env.Eval(prg, Context{Event: map[string]any{}})
|
|
if err != nil {
|
|
t.Fatalf("Eval(default limit): %v", err)
|
|
}
|
|
if b, ok := got.(bool); !ok || !b {
|
|
t.Fatalf("Eval = %#v, want true", got)
|
|
}
|
|
|
|
// The same expression evaluated under a tiny limit must abort with the
|
|
// cost error -- proving the limit is enforced, never truncated.
|
|
if _, err := evalUnderLimit(prg, 1); err == nil {
|
|
t.Fatal("Eval(tiny limit) = nil error, want cost-limit error")
|
|
}
|
|
}
|
|
|
|
// evalUnderLimit rebuilds the program with a caller-supplied cost limit.
|
|
func evalUnderLimit(prg *Program, limit uint64) (any, error) {
|
|
// Rebuild a fresh env/compile here for the test with an explicit limit.
|
|
env := NewEnv()
|
|
ast, issues := env.celEnv.Compile(`[1,2,3,4,5].map(x, x * 2).filter(x, x > 1).size() > 3`)
|
|
if issues != nil && issues.Err() != nil {
|
|
return nil, issues.Err()
|
|
}
|
|
p, err := env.celEnv.Program(ast, cel.CostLimit(limit))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
v, _, err := p.Eval(map[string]any{"event": map[string]any{}, "actions": map[string]any{}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return v.Value(), nil
|
|
}
|
|
|
|
// TestEvalPanicFreeHostileData: a pathological nested map under evaluation
|
|
// must not panic -- CEL returns errors or values, never panics (verified
|
|
// property; this test guards the wrapper contract, not CEL itself). A
|
|
// comparison against a missing key evaluates to false rather than erroring
|
|
// (verified CEL semantics: equality does not require the field to exist).
|
|
func TestEvalPanicFreeHostileData(t *testing.T) {
|
|
env := NewEnv()
|
|
prg, err := env.Compile(`event.child == "x"`)
|
|
if err != nil {
|
|
t.Fatalf("Compile: %v", err)
|
|
}
|
|
deep := map[string]any{}
|
|
cur := deep
|
|
for range 5000 {
|
|
next := map[string]any{}
|
|
cur["child"] = next
|
|
cur = next
|
|
}
|
|
got, err := env.Eval(prg, Context{Event: deep})
|
|
if err != nil {
|
|
t.Fatalf("Eval(hostile deep) error = %v, want no error (missing key in == -> false)", err)
|
|
}
|
|
if b, ok := got.(bool); !ok || b {
|
|
t.Fatalf("Eval(hostile deep) = %#v, want false", got)
|
|
}
|
|
}
|