package signpost_test import ( "strings" "testing" "git.golem15.com/golem15/summercms/modules/pact" "git.golem15.com/golem15/summercms/modules/signpost" ) // TestRegistryDuplicateKey pins D-01: two plugins registering the same menu // item type key fail Activate, the error names the key and both plugin IDs in // activation order, and nothing is published on the app. func TestRegistryDuplicateKey(t *testing.T) { app := smokeApp(t, "") err := signpost.Activate(app, []typesPlugin{ {id: "acme.blog", types: []pact.MenuItemType{ {Key: "acme-page", DefaultPattern: "/:slug", Resolve: echo}, {Key: "acme-post", DefaultPattern: "/blog/:slug", Resolve: echo}, }}, {id: "acme.news", types: []pact.MenuItemType{{Key: "acme-post", DefaultPattern: "/news/:slug", Resolve: echo}}}, }) if err == nil { t.Fatal("Activate accepted a duplicate menu item type key") } msg := err.Error() if !strings.HasPrefix(msg, "signpost: ") || !strings.Contains(msg, `"acme-post"`) { t.Fatalf("error = %q, want the signpost prefix and the key", msg) } first, second := strings.Index(msg, "acme.blog"), strings.Index(msg, "acme.news") if first < 0 || second < 0 || first > second { t.Fatalf("error = %q, want both plugin IDs, first registrant first", msg) } if _, ok := app.Lookup[*signpost.Registry](); ok { t.Fatal("a registry was published after a failed Activate") } // The same key twice inside one plugin is a duplicate too. err = signpost.Activate(smokeApp(t, ""), []typesPlugin{{id: "acme.blog", types: []pact.MenuItemType{ {Key: "acme-post", DefaultPattern: "/a", Resolve: echo}, {Key: "acme-post", DefaultPattern: "/b", Resolve: echo}, }}}) if err == nil || !strings.Contains(err.Error(), "acme-post") { t.Fatalf("duplicate inside one plugin: error = %v", err) } }