A seed that sets master page content, a theme preset, mailing lists, posts, email templates or page SEO must declare a plugin_sdk constraint that refuses hosts before v0.3.11, so an older CMS rejects the artifact instead of applying half a site. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
297 lines
12 KiB
Go
297 lines
12 KiB
Go
package bnp
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1"
|
|
"github.com/klauspost/compress/zstd"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// writeFixture lays out a minimal codeless plugin repo.
|
|
func writeFixture(t *testing.T, dir string, files map[string]string) {
|
|
t.Helper()
|
|
for rel, content := range files {
|
|
path := filepath.Join(dir, rel)
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
var codelessFixtureFiles = map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"fixture-theme\"\nversion = \"0.1.0\"\nkind = \"theme\"\n",
|
|
"manifest.yaml": "theme_presets: presets.json\nrequired_icon_packs: [lucide]\n" +
|
|
"master_pages: master_pages.json\n" +
|
|
"system_templates:\n - key: fixture\n title: Fixture Theme\n" +
|
|
"page_templates:\n - system: fixture\n key: landing\n title: Landing\n slots: [main]\n" +
|
|
"template_overrides:\n - template: fixture\n block: heading\n" +
|
|
"email_wrappers: [fixture]\n",
|
|
"templates/fixture/landing.ninjatpl": `<main>{{ content }}</main>`,
|
|
"templates/overrides/fixture/heading.ninjatpl": `<h2 class="deco">{{ text }}</h2>`,
|
|
"templates/email/fixture.ninjatpl": `<table><tr><td>{{ body|safe }}</td></tr></table>`,
|
|
"presets.json": `{"presets":[{"key":"default"}]}`,
|
|
"master_pages.json": `[{"key":"landing","title":"Landing","blocks":[{"block_key":"html","title":"Hero","content":{"x":1},"slot":"main","sort_order":1}]}]`,
|
|
"blocks/blocks.yaml": "blocks:\n - key: hero\n title: Hero\n category: content\n" +
|
|
" schema: hero.schema.json\n template: hero.ninjatpl\n providers: [site]\n",
|
|
"blocks/hero.schema.json": `{"type":"object"}`,
|
|
"blocks/hero.ninjatpl": `<h1>{{ title }}</h1>`,
|
|
"seed/seed.json": `{"settings":{"ensure":{"welcome":true}},` +
|
|
`"pages":[{"slug":"/","title":"Home","template_key":"landing"}],` +
|
|
`"menu_items":[{"menu":"main","label":"Home","page_slug":"/","sort_order":1}]}`,
|
|
"assets/logo.svg": `<svg/>`,
|
|
}
|
|
|
|
func codelessFixture(t *testing.T) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
writeFixture(t, dir, codelessFixtureFiles)
|
|
return dir
|
|
}
|
|
|
|
// readArtifact unpacks a .bnp into member name → contents (directories map
|
|
// to nil).
|
|
func readArtifact(t *testing.T, artifact []byte) map[string][]byte {
|
|
t.Helper()
|
|
dec, err := zstd.NewReader(bytes.NewReader(artifact))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer dec.Close()
|
|
members := map[string][]byte{}
|
|
tr := tar.NewReader(dec)
|
|
for {
|
|
h, err := tr.Next()
|
|
if err == io.EOF {
|
|
return members
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if h.Typeflag == tar.TypeDir {
|
|
members[h.Name] = nil
|
|
continue
|
|
}
|
|
b, err := io.ReadAll(tr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
members[h.Name] = b
|
|
}
|
|
}
|
|
|
|
func artifactManifest(t *testing.T, members map[string][]byte) *abiv1.PluginManifest {
|
|
t.Helper()
|
|
m := &abiv1.PluginManifest{}
|
|
if err := proto.Unmarshal(members[FileManifest], m); err != nil {
|
|
t.Fatalf("decode manifest.pb: %v", err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestCodelessBuildRoundTrip(t *testing.T) {
|
|
dir := codelessFixture(t)
|
|
|
|
out := filepath.Join(t.TempDir(), "fixture-theme-0.1.0.bnp")
|
|
res, err := BuildCodeless(BuildOptions{Dir: dir, Output: out})
|
|
if err != nil {
|
|
t.Fatalf("BuildCodeless: %v", err)
|
|
}
|
|
if !res.Codeless || res.BlockCount != 1 || res.OutputPath != out {
|
|
t.Errorf("result = %+v; want codeless with 1 block at %s", res, out)
|
|
}
|
|
|
|
artifact, err := os.ReadFile(out)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.ArtifactBytes != int64(len(artifact)) {
|
|
t.Errorf("ArtifactBytes = %d, file is %d bytes", res.ArtifactBytes, len(artifact))
|
|
}
|
|
members := readArtifact(t, artifact)
|
|
for _, want := range []string{"plugin.mod", "manifest.pb", "blocks/", "blocks/blocks.yaml", "seed/seed.json", "assets/logo.svg", "templates/email/fixture.ninjatpl"} {
|
|
if _, ok := members[want]; !ok {
|
|
t.Errorf("artifact missing %s", want)
|
|
}
|
|
}
|
|
if _, ok := members["plugin.wasm"]; ok {
|
|
t.Error("codeless artifact carries plugin.wasm")
|
|
}
|
|
|
|
m := artifactManifest(t, members)
|
|
if m.GetName() != "fixture-theme" || m.GetAbiVersion() != 1 {
|
|
t.Errorf("manifest name/abi = %q/%d", m.GetName(), m.GetAbiVersion())
|
|
}
|
|
if len(m.GetSystemTemplates()) != 1 || len(m.GetPageTemplates()) != 1 || m.GetPageTemplates()[0].GetSystemKey() != "fixture" {
|
|
t.Errorf("templates = %v / %v", m.GetSystemTemplates(), m.GetPageTemplates())
|
|
}
|
|
if !m.GetCodeless() || len(m.GetMasterPages()) != 1 || len(m.GetThemePresets()) == 0 {
|
|
t.Errorf("manifest = codeless:%v masters:%d presets:%dB",
|
|
m.GetCodeless(), len(m.GetMasterPages()), len(m.GetThemePresets()))
|
|
}
|
|
if len(m.GetRequiredIconPacks()) != 1 || m.GetRequiredIconPacks()[0] != "lucide" {
|
|
t.Errorf("icon packs = %v", m.GetRequiredIconPacks())
|
|
}
|
|
if len(m.GetBlockTemplateOverrides()) != 1 ||
|
|
m.GetBlockTemplateOverrides()[0].GetTemplateKey() != "fixture" ||
|
|
m.GetBlockTemplateOverrides()[0].GetBlockKey() != "heading" {
|
|
t.Errorf("template overrides = %v", m.GetBlockTemplateOverrides())
|
|
}
|
|
if len(m.GetEmailWrapperSystemKeys()) != 1 || m.GetEmailWrapperSystemKeys()[0] != "fixture" {
|
|
t.Errorf("email wrappers = %v", m.GetEmailWrapperSystemKeys())
|
|
}
|
|
}
|
|
|
|
func TestBuildCodelessFSMatchesDirectoryBuild(t *testing.T) {
|
|
out := filepath.Join(t.TempDir(), "x.bnp")
|
|
if _, err := BuildCodeless(BuildOptions{Dir: codelessFixture(t), Output: out}); err != nil {
|
|
t.Fatalf("BuildCodeless: %v", err)
|
|
}
|
|
fromDir, err := os.ReadFile(out)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
mapFS := fstest.MapFS{}
|
|
for name, content := range codelessFixtureFiles {
|
|
mapFS[name] = &fstest.MapFile{Data: []byte(content), Mode: 0o644}
|
|
}
|
|
fromMem, res, err := BuildCodelessFS(mapFS)
|
|
if err != nil {
|
|
t.Fatalf("BuildCodelessFS: %v", err)
|
|
}
|
|
if !bytes.Equal(fromDir, fromMem) {
|
|
t.Errorf("in-memory artifact (%d bytes) differs from directory artifact (%d bytes)", len(fromMem), len(fromDir))
|
|
}
|
|
if res.OutputPath != "" || res.ArtifactBytes != int64(len(fromMem)) {
|
|
t.Errorf("result = %+v", res)
|
|
}
|
|
}
|
|
|
|
func TestCodelessBuildRejectsBadDeclarations(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
files map[string]string
|
|
}{
|
|
{"bad blocks yaml", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n",
|
|
"blocks/blocks.yaml": "blocks:\n - key: hero\n", // missing title/schema/template
|
|
}},
|
|
{"missing template file", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n",
|
|
"blocks/blocks.yaml": "blocks:\n - key: hero\n title: Hero\n" +
|
|
" schema: hero.schema.json\n template: missing.ninjatpl\n",
|
|
"blocks/hero.schema.json": `{}`,
|
|
}},
|
|
{"block file escapes", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n",
|
|
"blocks/blocks.yaml": "blocks:\n - key: hero\n title: Hero\n" +
|
|
" schema: hero.schema.json\n template: ../plugin.mod\n",
|
|
"blocks/hero.schema.json": `{}`,
|
|
}},
|
|
{"seed media without id", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n",
|
|
"seed/seed.json": `{"media":[{"file":"a.jpg"}]}`,
|
|
"seed/a.jpg": "x",
|
|
}},
|
|
{"data_dir grant", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\ndata_dir = true\n",
|
|
}},
|
|
{"public_routes claim", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n" +
|
|
"public_routes = [{ path = \"/area\", prefix = true }]\n",
|
|
}},
|
|
{"sitemap flag", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\nsitemap = true\n",
|
|
}},
|
|
{"override missing template file", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n",
|
|
"manifest.yaml": "template_overrides:\n - template: x\n block: heading\n",
|
|
}},
|
|
{"override missing block key", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n",
|
|
"manifest.yaml": "template_overrides:\n - template: x\n",
|
|
"templates/overrides/x/heading.ninjatpl": `<h1>{{ text }}</h1>`,
|
|
}},
|
|
{"email wrapper missing template file", map[string]string{
|
|
"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n",
|
|
"manifest.yaml": "email_wrappers: [x]\n",
|
|
}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeFixture(t, dir, tc.files)
|
|
_, err := BuildCodeless(BuildOptions{
|
|
Dir: dir, Output: filepath.Join(t.TempDir(), "x.bnp"),
|
|
})
|
|
if err == nil {
|
|
t.Fatal("BuildCodeless succeeded; want error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
const contentSeed = `{"data_tables":[{"key":"contact"}],
|
|
"pages":[{"slug":"/","title":"Home","replace_existing":true,"seo":{"meta_title":"Home","meta_description":"Hi"}}],
|
|
"master_pages":[{"template":"coffee","blocks":[{"block_key":"footer","content":{"companyName":"Dune","tagline":null}}]}],
|
|
"theme_preset":{"template":"coffee","preset":"dark-roast"},
|
|
"mailing_lists":[{"slug":"main","name":"Newsletter"}],
|
|
"posts":[{"key":"p1","slug":"hello","title":"Hello","post_date":"2026-11-20","body":[{"type":"paragraph"}]}],
|
|
"email_templates":[{"name":"Thanks","subject":"Thanks","body":"Hi","table":"contact"}]}`
|
|
|
|
func contentMod(constraint string) string {
|
|
return "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n\n[compatibility]\nplugin_sdk = \"" + constraint + "\"\n"
|
|
}
|
|
|
|
func TestValidateSeedContentSections(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeFixture(t, dir, map[string]string{"plugin.mod": contentMod(">=0.3.11 <0.4.0"), "seed/seed.json": contentSeed})
|
|
if err := ValidateSeedDir(os.DirFS(dir)); err != nil {
|
|
t.Fatalf("valid content seed rejected: %v", err)
|
|
}
|
|
|
|
cases := map[string]map[string]string{
|
|
"older host admitted": {"plugin.mod": contentMod(">=0.3.8 <0.4.0"), "seed/seed.json": contentSeed},
|
|
"no constraint": {"plugin.mod": "[plugin]\nname = \"x\"\nversion = \"0.1.0\"\n", "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A","body":[{}]}]}`},
|
|
"page seo needs gate": {"plugin.mod": contentMod(">=0.3.8"), "seed/seed.json": `{"pages":[{"slug":"/","title":"Home","seo":{"meta_title":"x"}}]}`},
|
|
"post without body": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A"}]}`},
|
|
"post nested slug": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"blog/a","title":"A","body":[{}]}]}`},
|
|
"post bad date": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A","body":[{}],"post_date":"20 Nov"}]}`},
|
|
"duplicate post key": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"posts":[{"key":"a","slug":"a","title":"A","body":[{}]},{"key":"a","slug":"b","title":"B","body":[{}]}]}`},
|
|
"master without key": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"master_pages":[{"template":"coffee","blocks":[{"content":{"a":1}}]}]}`},
|
|
"preset without id": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"theme_preset":{"template":"coffee"}}`},
|
|
"list without slug": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"mailing_lists":[{"name":"News"}]}`},
|
|
"template bad table": {"plugin.mod": contentMod(">=0.3.11"), "seed/seed.json": `{"email_templates":[{"name":"T","subject":"S","body":"B","table":"nope"}]}`},
|
|
}
|
|
for name, files := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeFixture(t, dir, files)
|
|
if err := ValidateSeedDir(os.DirFS(dir)); err == nil {
|
|
t.Fatal("accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConstraintExcludesBelow(t *testing.T) {
|
|
for c, want := range map[string]bool{
|
|
">=0.3.11 <0.4.0": true, ">0.3.11": true, "=0.3.12": true, ">=v0.4.0": true,
|
|
">=0.3.10 <0.4.0": false, "<0.4.0": false, "": false,
|
|
} {
|
|
if got := constraintExcludesBelow(c, SeedContentMinSDK); got != want {
|
|
t.Errorf("%q: got %v want %v", c, got, want)
|
|
}
|
|
}
|
|
}
|