Files
archie/internal/domain/eda/module/module_test.go
T
sam d26ba54e77 feat(eda): wire action playbooks through loader and module schemas
Pass 2 of archie-core-nx30. Pass 1 typed each prior action's result in
the CEL env; this pass wires that typing through the loader, module
schemas, and daemon, and removes the now-redundant id classifier.

Loader shape (D2): one playbook is exactly one workflow action
(unchanged) OR one or more module actions; the shapes never mix. Mixed,
unsupported, or zero actions fail the load naming the playbook.
playbook.Load now takes a KindSchemas source (a small interface this
package defines, satisfied by *module.ModuleRegistry) so playbook grows
no module import. Callers: bootstrap.loadEDAPlaybooks passes b.modules;
daemon and playbook tests pass module.New().

Per-playbook env (D3): each action's when and args values compile against
an env built from the PRIOR actions' ids typed by their kinds' Result
structs (expr.NewEnv(DeclaredResult{ID,Type})). event stays dyn. An
undeclared id, a forward reference, or a field the kind's Result lacks is
a compile-time undefined-field error.

Args schema (D4): an args key the kind's Args struct does not define
fails the load. An args VALUE whose compiled type mismatches the field
type is left as a named gap (not cheap without leaking cel-go type kinds
into playbook and handling dyn reads); a wrong-typed value still surfaces
at module invoke, not load.

D1: Store.Dispatch routes workflow playbooks only, so an action playbook
cannot hijack the definition pin. bootstrap.loadEDAPlaybooks logs a
visible Warn (not debug) for each loaded action playbook naming it and
stating it does not execute yet.

D5: ModuleRegistry.Invoke is deliberately schema-agnostic (the stored
invoker is type-erased and returns map[string]any). The map-to-Result
marshalling belongs in the coordinator named by the doc, which is the
t2db.31 run path and out of scope here.

Classifier enumeration (E) and verdict, all proven for a typed action
playbook via CEL (expr.NewEnv with a declared log.Result id):

  unknown id                        CEL compile (undefined field)       -> delete unknownActionReference
  literal map index actions["x"]    CEL compile (no _[_] overload)      -> covered
  computed index actions[key]       CEL compile (undeclared 'key')      -> covered
  dynamic index actions[event.name] CEL compile (no _[_] overload)      -> covered
  "x" in actions                    CEL compile (no @in overload)       -> covered
  size(actions)                     CEL compile (no size overload)      -> covered
  actions.all(...)                  CEL compile (not a range)           -> covered
  actions == {}                     CEL compile (no _==_ overload)      -> covered
  bare actions                      compiles; KEPT classifier           -> retained resolvable check

unknownActionReference is deleted; actionReferences/ActionReferences are
kept solely for the bare-actions spelling CEL does not reject.
2026-09-22 09:02:33 +10:00

149 lines
3.8 KiB
Go

package module
import (
"context"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/domain/eda/module/log"
)
func writeModule(t *testing.T, dir, content string) string {
t.Helper()
path := filepath.Join(dir, "log.go")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write %s: %v", path, err)
}
return path
}
func TestRegisterAndInvokeLogKind(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, `package main
import "github.com/samcharles93/archie-core/internal/domain/eda/module/log"
func Run(a log.Args) log.Result {
return log.Result{Written: a.Message != "", Level: a.Level}
}
`)
r := New()
if err := r.Register("log", dir); err != nil {
t.Fatalf("Register: %v", err)
}
res, err := r.Invoke(context.Background(), "log", map[string]any{"message": "hello", "level": "info"})
if err != nil {
t.Fatalf("Invoke: %v", err)
}
if res["written"] != true {
t.Errorf("result written = %v, want true", res["written"])
}
if res["level"] != "info" {
t.Errorf("result level = %q, want info", res["level"])
}
}
func TestInvokeUnknownKindIsError(t *testing.T) {
r := New()
if _, err := r.Invoke(context.Background(), "notify", nil); err == nil {
t.Fatal("Invoke(unknown kind) = nil, want error")
}
}
func TestRegisterUnknownKindIsError(t *testing.T) {
r := New()
if err := r.Register("notify", t.TempDir()); err == nil {
t.Fatal("Register(unknown kind) = nil, want error")
}
}
func TestKindSchema(t *testing.T) {
r := New()
argsType, resultType, ok := r.KindSchema("log")
if !ok {
t.Fatal("KindSchema(log) = not ok, want the built-in log contract")
}
if argsType != reflect.TypeFor[log.Args]() {
t.Errorf("KindSchema(log) args = %v, want log.Args", argsType)
}
if resultType != reflect.TypeFor[log.Result]() {
t.Errorf("KindSchema(log) result = %v, want log.Result", resultType)
}
if _, _, ok := r.KindSchema("notify"); ok {
t.Fatal("KindSchema(notify) = ok, want false for an unregistered kind")
}
}
func TestRegisterMissingFileIsError(t *testing.T) {
r := New()
if err := r.Register("log", t.TempDir()); err == nil {
t.Fatal("Register(missing file) = nil, want error")
}
}
func TestInvokeWrongShapeIsReportedError(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, `package main
import "github.com/samcharles93/archie-core/internal/domain/eda/module/log"
func Run(a log.Args) log.Result {
return log.Result{Written: true}
}
`)
r := New()
if err := r.Register("log", dir); err != nil {
t.Fatalf("Register: %v", err)
}
// Unknown arg key: shape mismatch, reported error, not silent ignore.
if _, err := r.Invoke(context.Background(), "log", map[string]any{"message": "hi", "bogus": 1}); err == nil {
t.Fatal("Invoke(unknown arg) = nil, want error")
}
// Wrong type for a known arg: reported error.
if _, err := r.Invoke(context.Background(), "log", map[string]any{"message": 42}); err == nil {
t.Fatal("Invoke(wrong type) = nil, want error")
}
}
func TestInvokeMalformedFileIsReported(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, `package main
func Run( {{{`)
r := New()
if err := r.Register("log", dir); err == nil {
t.Fatal("Register(malformed) = nil, want error")
}
}
func TestInvokePanicRecovered(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, `package main
import "github.com/samcharles93/archie-core/internal/domain/eda/module/log"
func Run(a log.Args) log.Result {
var s []string
return log.Result{Level: s[5]}
}
`)
r := New()
if err := r.Register("log", dir); err != nil {
t.Fatalf("Register: %v", err)
}
_, err := r.Invoke(context.Background(), "log", map[string]any{"message": "boom"})
if err == nil {
t.Fatal("Invoke(panicking module) = nil, want recovered error")
}
if !strings.Contains(err.Error(), "panicked") {
t.Errorf("Invoke panic error = %q, want recovered-panic text", err.Error())
}
}