Files
summercms/backpack/services_test.go
Jakub Zych 5f2291849d test(01-02): add failing test for optional plugin services
- Cover typed Publish/Lookup, duplicate providers and HasPlugin
- Require missing/cycle errors to name plugin IDs before Boot
- Assert greeter works with and without an optional extra
2026-09-16 13:20:53 +02:00

73 lines
1.6 KiB
Go

package backpack
import (
"testing"
"git.golem15.com/golem15/summercms/compass"
)
type ping interface {
Ping() string
}
type pingImpl struct{ s string }
func (p pingImpl) Ping() string { return p.s }
func TestPublishLookup(t *testing.T) {
app := New(&compass.Config{})
if err := app.Publish[ping](pingImpl{s: "pong"}); err != nil {
t.Fatalf("Publish: %v", err)
}
got, ok := app.Lookup[ping]()
if !ok {
t.Fatal("Lookup returned false")
}
if got.Ping() != "pong" {
t.Fatalf("Ping = %q, want pong", got.Ping())
}
}
func TestLookupAbsentInterface(t *testing.T) {
app := New(&compass.Config{})
got, ok := app.Lookup[ping]()
if ok {
t.Fatalf("Lookup = (%v, true), want false", got)
}
if got != nil {
t.Fatalf("absent lookup value = %v, want nil", got)
}
}
func TestPublishDuplicateRejected(t *testing.T) {
app := New(&compass.Config{})
if err := app.Publish[ping](pingImpl{s: "a"}); err != nil {
t.Fatalf("Publish: %v", err)
}
if err := app.Publish[ping](pingImpl{s: "b"}); err == nil {
t.Fatal("expected duplicate publish error")
}
}
func TestServiceRegistriesAreAppScoped(t *testing.T) {
a := New(&compass.Config{})
b := New(&compass.Config{})
if err := a.Publish[ping](pingImpl{s: "a"}); err != nil {
t.Fatal(err)
}
if _, ok := b.Lookup[ping](); ok {
t.Fatal("app B should not see app A's service")
}
}
func TestHasPluginUsesCompleteSet(t *testing.T) {
app := New(&compass.Config{})
app.SetPlugins([]string{"golem15.hello", "golem15.optional"})
if !app.HasPlugin("golem15.optional") {
t.Fatal("HasPlugin(golem15.optional) = false")
}
if app.HasPlugin("golem15.missing") {
t.Fatal("HasPlugin(golem15.missing) = true")
}
}