package party import ( "os" "path/filepath" "strings" "sync" "testing" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/compass" ) // withRegistry swaps the package registry for an empty one for the length // of the test and restores it afterwards. func withRegistry(t *testing.T) { t.Helper() mu.Lock() saved := plugins plugins = nil mu.Unlock() t.Cleanup(func() { mu.Lock() plugins = saved mu.Unlock() }) } // scopedPlugin keeps per-application state on its struct, as plugins are // told to. type scopedPlugin struct { id string label string app *backpack.App register int boot int } func (p *scopedPlugin) ID() string { return p.id } func (p *scopedPlugin) Requires() []string { return nil } func (p *scopedPlugin) Register(app *backpack.App) error { p.app = app p.register++ return nil } func (p *scopedPlugin) Boot(app *backpack.App) error { p.boot++ return nil } // Name is a capability-style method that reads the plugin's own app. func (p *scopedPlugin) Name() string { if p.app == nil || p.app.Config == nil { return "" } return p.app.Config.String("app.name") } type valuePlugin struct{ id string } func (p valuePlugin) ID() string { return p.id } func (valuePlugin) Requires() []string { return nil } func (valuePlugin) Register(*backpack.App) error { return nil } func (valuePlugin) Boot(*backpack.App) error { return nil } type countPlugin int func (*countPlugin) ID() string { return "acme.count" } func (*countPlugin) Requires() []string { return nil } func (*countPlugin) Register(*backpack.App) error { return nil } func (*countPlugin) Boot(*backpack.App) error { return nil } func namedApp(t *testing.T, name string) *backpack.App { t.Helper() dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, "app.yaml"), []byte("name: "+name+"\n"), 0o644); err != nil { t.Fatal(err) } cfg, err := compass.Load(dir) if err != nil { t.Fatalf("Load: %v", err) } return backpack.New(cfg) } func activatedScoped(t *testing.T, app *backpack.App, id string) *scopedPlugin { t.Helper() list, ok := app.Lookup[Activated]() if !ok { t.Fatal("Activated not published") } for _, p := range list { if p.ID() == id { sp, ok := p.(*scopedPlugin) if !ok { t.Fatalf("plugin %s is %T, want *scopedPlugin", id, p) } return sp } } t.Fatalf("plugin %s not activated", id) return nil } func TestActivatePerAppInstances(t *testing.T) { withRegistry(t) orig := &scopedPlugin{id: "acme.scoped", label: "fixed"} Register(orig) Register(valuePlugin{id: "acme.value"}) count := new(countPlugin) Register(count) ids := []string{"acme.scoped", "acme.value", "acme.count"} one := namedApp(t, "one") two := namedApp(t, "two") if _, err := Activate(one, ids); err != nil { t.Fatalf("Activate one: %v", err) } if _, err := Activate(two, ids); err != nil { t.Fatalf("Activate two: %v", err) } p1 := activatedScoped(t, one, "acme.scoped") p2 := activatedScoped(t, two, "acme.scoped") if p1 == p2 { t.Fatal("both applications got the same *scopedPlugin") } if p1 == orig || p2 == orig { t.Fatal("the value passed to Register was handed to an application") } if p1.app != one || p2.app != two { t.Fatal("a plugin stored another application's app") } if got := p1.Name(); got != "one" { t.Fatalf("app one plugin reads app.name %q, want one", got) } if got := p2.Name(); got != "two" { t.Fatalf("app two plugin reads app.name %q, want two", got) } for _, p := range []*scopedPlugin{p1, p2} { if p.label != "fixed" { t.Fatalf("label = %q, want the registered literal's fixed", p.label) } if p.register != 1 || p.boot != 1 { t.Fatalf("register=%d boot=%d, want 1 and 1 per instance", p.register, p.boot) } } if orig.app != nil || orig.register != 0 || orig.boot != 0 { t.Fatalf("original was touched: app=%v register=%d boot=%d", orig.app, orig.register, orig.boot) } for _, app := range []*backpack.App{one, two} { list, _ := app.Lookup[Activated]() var sawValue, sawCount bool for _, p := range list { switch v := p.(type) { case valuePlugin: sawValue = v == (valuePlugin{id: "acme.value"}) case *countPlugin: sawCount = v == count } } if !sawValue { t.Fatal("value plugin not used as is") } if !sawCount { t.Fatal("pointer to a non-struct type not used as is") } } t.Run("parallel", func(t *testing.T) { apps := []*backpack.App{namedApp(t, "left"), namedApp(t, "right")} var wg sync.WaitGroup errs := make([]error, len(apps)) for i, app := range apps { wg.Add(1) go func() { defer wg.Done() _, errs[i] = Activate(app, ids) }() } wg.Wait() for i, err := range errs { if err != nil { t.Fatalf("Activate %d: %v", i, err) } } l := activatedScoped(t, apps[0], "acme.scoped") r := activatedScoped(t, apps[1], "acme.scoped") if l == r || l.Name() != "left" || r.Name() != "right" { t.Fatalf("parallel activation shared instances: %q %q", l.Name(), r.Name()) } }) } func TestRegisterFactory(t *testing.T) { t.Run("called per Activate", func(t *testing.T) { withRegistry(t) calls := 0 RegisterFactory("acme.made", func() Plugin { calls++ return &scopedPlugin{id: "acme.made"} }) one := namedApp(t, "one") two := namedApp(t, "two") if _, err := Activate(one, []string{"acme.made"}); err != nil { t.Fatalf("Activate one: %v", err) } if _, err := Activate(two, []string{"acme.made"}); err != nil { t.Fatalf("Activate two: %v", err) } if calls != 2 { t.Fatalf("factory ran %d times, want 2", calls) } if activatedScoped(t, one, "acme.made") == activatedScoped(t, two, "acme.made") { t.Fatal("factory instances are shared") } }) t.Run("nil fn", func(t *testing.T) { withRegistry(t) RegisterFactory("acme.none", nil) mu.Lock() n := len(plugins) mu.Unlock() if n != 0 { t.Fatalf("registry length = %d after RegisterFactory(nil), want 0", n) } }) t.Run("nil result", func(t *testing.T) { withRegistry(t) RegisterFactory("acme.nil", func() Plugin { return nil }) _, err := Activate(namedApp(t, "one"), nil) if err == nil || !strings.Contains(err.Error(), `factory for "acme.nil" returned nil`) { t.Fatalf("err = %v, want factory nil error", err) } }) t.Run("id mismatch", func(t *testing.T) { withRegistry(t) RegisterFactory("acme.wrong", func() Plugin { return &scopedPlugin{id: "acme.other"} }) _, err := Activate(namedApp(t, "one"), nil) if err == nil || !strings.Contains(err.Error(), "acme.wrong") || !strings.Contains(err.Error(), "acme.other") { t.Fatalf("err = %v, want an error naming acme.wrong and acme.other", err) } }) }