105 lines
3.3 KiB
Go
105 lines
3.3 KiB
Go
package blocks
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.dev.alexdunmow.com/block/pluginsdk/document"
|
|
)
|
|
|
|
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 TestPoweredBlockWithOptionsRoundTrip(t *testing.T) {
|
|
metadata := &document.Metadata{
|
|
Title: "Missing article",
|
|
Description: "The requested article was not found.",
|
|
CanonicalURL: "https://example.com/docs/missing",
|
|
ImageURL: "https://example.com/media/missing.png",
|
|
Robots: "noindex,nofollow",
|
|
}
|
|
marker := PoweredBlockWithOptions("<h1>{{ title }}</h1>", map[string]any{"title": "Missing"}, PoweredOptions{
|
|
StatusCode: 404,
|
|
Metadata: metadata,
|
|
})
|
|
got, ok := DecodePoweredBlock(marker)
|
|
if !ok {
|
|
t.Fatal("DecodePoweredBlock did not recognize its own marker")
|
|
}
|
|
if got.StatusCode != 404 {
|
|
t.Errorf("status code = %d, want 404", got.StatusCode)
|
|
}
|
|
if got.Metadata == nil || *got.Metadata != *metadata {
|
|
t.Errorf("metadata = %+v, want %+v", got.Metadata, metadata)
|
|
}
|
|
}
|
|
|
|
func TestPoweredBlockWithOptionsPreservesOptionsWhenDataCannotMarshal(t *testing.T) {
|
|
marker := PoweredBlockWithOptions("<h1>fallback</h1>", map[string]any{"bad": make(chan int)}, PoweredOptions{
|
|
StatusCode: 503,
|
|
Metadata: &document.Metadata{Title: "Unavailable"},
|
|
})
|
|
got, ok := DecodePoweredBlock(marker)
|
|
if !ok {
|
|
t.Fatal("DecodePoweredBlock did not recognize fallback marker")
|
|
}
|
|
if got.Data != nil {
|
|
t.Errorf("data = %#v, want nil fallback", got.Data)
|
|
}
|
|
if got.StatusCode != 503 || got.Metadata == nil || got.Metadata.Title != "Unavailable" {
|
|
t.Errorf("options were not preserved: %+v", got)
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|