Fifth slice of docs/prds/eda-playbook-engine.md (t2db.13), implementing docs/prds/module-position.md's recommended first slice: prove schema -> go:generate -> Yaegi load -> ModuleRegistry end to end with a side-effect-free action kind, before any kind with a real external effect (notify, image-gen, forge writes) -- those stay blocked on the parent doc's unresolved execution-time gaps (idempotency, mid-run failure). - internal/domain/eda/module/log: hand-written schema source (Args/Result/ SchemaVersion per module-position.md) + logextract generated symbol table via go:generate yaegi extract (same tooling as wfextract/ gateextract; scoped to ./internal/domain/eda/module/...). - internal/domain/eda/module: ModuleRegistry with Register(kind, dir) / Invoke(ctx, kind, rawArgs). No generic Module interface with an any payload: each kind is its own tiny package with its own generated contract; the registry's internal storage is the only place erasure appears (exactly as inside wfextract). Invoke decodes rawArgs strictly into the kind's Args before calling the interpreted function -- an unknown key or wrong type is a reported failure, not a silent zero-value fill (the 'schema defines the accepted message' rule). Generalizes gateeval's fixed-signature Yaegi pattern via yaegiutil.New/Resolve/Safe -- no new interpreter-construction or panic-recovery mechanism. - Config: ModuleDir mirroring PluginDir's shape and doc comment (operator-trusted directory of Yaegi module implementations, one .go per kind; empty means none loaded). Wired at daemon startup in bootstrap.go alongside PlaybookDirs loading -- log-and-abort on load failure (a broken module is a startup failure, not degrade-and-skip). - examples/modules/log.go: operator-facing example (//go:build ignore, mirroring examples/secret-engines/) proving the shipped contract. Tests: registry (register/invoke, unknown kind, missing file, strict arg-decode shape failures, malformed file, panic recovery), logextract smoke test, and an example-load test that runs the shipped examples/modules/log.go through the real registry (not a test fixture). task check clean (113/113 UI, exit 0). Playbook YAML action dispatch is deliberately out of this slice: a minimal 'run this one module action, no chaining' dispatch still requires naming how args values reach the action (parent doc open question 1 -- data-flow/interpolation syntax) and what the result feeds, both unresolved by design. Dispatch is a follow-up ticket that consumes this registry as-is; recorded in module-position.md.
129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
package module
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
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 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())
|
|
}
|
|
}
|