core/blocks/powered_test.go
Alex Dunmow 04991295cf feat(abi,blocks): ninjatpl rendering as a host capability (powered blocks + tag/filter callbacks)
pongo2 stays host-side and is never compiled into a guest; plugins render
templates by handing the host a {template, data} pair, and the host calls
back into the free guest instance for plugin-declared tags/filters.

ABI (additive, buf-breaking clean):
- RenderBlockResponse gains a `powered` PoweredBlock{template, data_json};
  a block returns EITHER html OR powered.
- New hooks HOOK_RENDER_TAG (10) / HOOK_APPLY_FILTER (11) with
  RenderTag{Request,Response} and ApplyFilter{Request,Response}.
- PluginManifest gains repeated declared_tags / declared_filters (31/32).

Guest SDK (core/blocks, core/plugin/wasmguest):
- blocks.PoweredBlock(template, data) / DecodePoweredBlock: NUL-sentinel
  marker so BlockFunc's string signature is unchanged (smallest additive
  change — no ripple to existing blocks or the host guest-side).
- blocks.RegisterTag / RegisterFilter (+ RenderContext = context.Context)
  write a package-level registry; runRegister resets it per registration
  for deterministic DESCRIBE + dispatch.
- DESCRIBE emits declared_tags/filters; dispatch handles RENDER_TAG /
  APPLY_FILTER (fn errors → response.error; panics → AbiError INTERNAL,
  instance stays callable).

Docs: core/docs/wasm-abi.md gains the render-as-a-host-capability model,
the powered-block flow (re-entrancy-free), the plugin API, and the
HOST-SIDE CONTRACT the cms phase implements.

Tests: unit round-trips for powered/RENDER_TAG/APPLY_FILTER (dispatch +
error + unknown + panic + per-guest registry isolation) plus a real
wazero round-trip through the compiled fixture module. Verified no
guest-reachable package imports pongo2 (go list -deps on the wasip1
fixture build is clean).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-04 02:36:39 +08:00

60 lines
1.8 KiB
Go

package blocks
import "testing"
func TestPoweredBlockRoundTrip(t *testing.T) {
marker := PoweredBlock("<p>{{ x }}</p>", map[string]any{"x": "y"})
pr, ok := DecodePoweredBlock(marker)
if !ok {
t.Fatal("DecodePoweredBlock did not recognize its own marker")
}
if pr.Template != "<p>{{ x }}</p>" {
t.Errorf("template = %q", pr.Template)
}
if pr.Data["x"] != "y" {
t.Errorf("data = %v", pr.Data)
}
}
func TestDecodePoweredBlockRejectsPlainHTML(t *testing.T) {
if _, ok := DecodePoweredBlock("<p>hello</p>"); ok {
t.Error("plain HTML must not decode as a powered block")
}
if _, ok := DecodePoweredBlock(""); ok {
t.Error("empty string must not decode as a powered block")
}
}
func TestTagFilterRegistry(t *testing.T) {
ResetRegisteredTagsAndFilters()
t.Cleanup(ResetRegisteredTagsAndFilters)
RegisterTag("b", func(map[string]any, RenderContext) (string, error) { return "b", nil })
RegisterTag("a", func(map[string]any, RenderContext) (string, error) { return "a", nil })
RegisterFilter("f", func(string, map[string]any) (string, error) { return "f", nil })
// Empty name / nil fn are ignored.
RegisterTag("", nil)
RegisterFilter("g", nil)
if names := RegisteredTagNames(); len(names) != 2 || names[0] != "a" || names[1] != "b" {
t.Errorf("tag names = %v, want sorted [a b]", names)
}
if names := RegisteredFilterNames(); len(names) != 1 || names[0] != "f" {
t.Errorf("filter names = %v, want [f]", names)
}
if _, ok := LookupTag("a"); !ok {
t.Error("LookupTag(a) missing")
}
if _, ok := LookupFilter("f"); !ok {
t.Error("LookupFilter(f) missing")
}
if _, ok := LookupTag("missing"); ok {
t.Error("LookupTag(missing) should be absent")
}
ResetRegisteredTagsAndFilters()
if RegisteredTagNames() != nil || RegisteredFilterNames() != nil {
t.Error("reset did not clear registries")
}
}