diff --git a/pluginkit/CHANGELOG.md b/pluginkit/CHANGELOG.md index fdc0f80..e7e95b7 100644 --- a/pluginkit/CHANGELOG.md +++ b/pluginkit/CHANGELOG.md @@ -23,6 +23,7 @@ Releases of this module are tagged `pluginkit/vX.Y.Z`. Releases up to - `settings.Name` names the variable of each setting from the application's prefix, the plugin's id and the key. - `Schema.Resolve` works out each value from the environment values the application found, then from the default. An empty value counts as unset, and a value its type rejects is named by its variable. - A secret's value is a `settings.Hidden`, which prints, logs and encodes as `set` or `not set`. +- `ScreenGate` is an optional interface whose `ShowsScreens` tells an application whether a plugin's admin menu entries and screens show for the caller in the context. - The module needs Go 1.27.4. ## 0.12.0 - 2026-11-07 diff --git a/pluginkit/features/features_test.go b/pluginkit/features/features_test.go index e952d57..a875aa3 100644 --- a/pluginkit/features/features_test.go +++ b/pluginkit/features/features_test.go @@ -65,3 +65,7 @@ func TestSettings(t *testing.T) { func TestSettingsDeclarations(t *testing.T) { runFeature(t, "settings-declarations.feature", initializeSettings()) } + +func TestScreenGate(t *testing.T) { + runFeature(t, "screen-gate.feature", initializeScreenGate(t)) +} diff --git a/pluginkit/features/screen-gate.feature b/pluginkit/features/screen-gate.feature new file mode 100644 index 0000000..ba77d02 --- /dev/null +++ b/pluginkit/features/screen-gate.feature @@ -0,0 +1,40 @@ +Feature: Hiding a plugin's admin screens from a caller + A plugin can answer whether its admin menu entries and screens show for + the caller its context carries, for example not before the caller has + set up what the screens work on. An application asks every plugin that + answers, through one filter hook, and leaves out the screens of the + plugins it lists. A plugin that does not answer always shows its + screens. An answer that fails, panics or comes after the hook's budget + ran out hides nothing, so a broken plugin never takes a working screen + away, and the report names its plugin. + + Scenario: The plugins whose screens hide are listed in registration order + Given the plugins "notes", "feed" and "mail" answer whether their screens show + And the plugin "theme" is registered without answering it + And "notes" hides its screens + And "mail" hides its screens + When the application asks which plugins hide their screens + Then the plugins that hide their screens are "notes" and "mail" + + Scenario: An answer that fails or panics hides nothing + Given the plugins "mail", "archive" and "notes" answer whether their screens show + And "mail" hides its screens + And "mail" fails with "mailbox unreadable" + And "archive" hides its screens + And "notes" hides its screens + And "notes" panics with "boom" + When the application asks which plugins hide their screens + Then the plugins that hide their screens are "archive" + And the report holds "pluginkit: mail admin.screens: mailbox unreadable" + And the report holds "pluginkit: notes admin.screens panicked: boom" with the stack of the panic + + Scenario: An answer that comes after the budget ran out hides nothing + Given the plugins "mail" and "notes" answer whether their screens show + And the hook has a budget of 2 ms + And "mail" takes 5 ms + And "mail" hides its screens + And "notes" hides its screens + When the application asks which plugins hide their screens + Then no plugin hides its screens + And the report marks the failure of "mail" as caused by the ended context + And the report marks the failure of "notes" as caused by the ended context diff --git a/pluginkit/features/steps_screen_gate_test.go b/pluginkit/features/steps_screen_gate_test.go new file mode 100644 index 0000000..764c755 --- /dev/null +++ b/pluginkit/features/steps_screen_gate_test.go @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: Apache-2.0 + +package features_test + +import ( + "context" + "fmt" + "slices" + "testing" + "time" + + "github.com/cucumber/godog" + + "github.com/gopherium/framework/pluginkit" +) + +// screensHook is the name of the hook a scenario asks the plugins through. +const screensHook = "admin.screens" + +// gated is the interface an application asks its plugins through. +type gated interface { + ID() string + pluginkit.ScreenGate +} + +var _ pluginkit.ScreenGate = (*gatePlugin)(nil) + +// gatePlugin is a plugin of a scenario that answers whether its screens show. +type gatePlugin struct { + *scenePlugin + hides bool +} + +// ShowsScreens handles the call, then answers that the screens show unless the scenario hides them. +func (g *gatePlugin) ShowsScreens(ctx context.Context) (bool, error) { + if err := g.Handle(ctx); err != nil { + return false, err + } + return !g.hides, nil +} + +// gateWorld is the part of a scenario's state only the screen gate steps use. +type gateWorld struct { + gates map[string]*gatePlugin + hidden []string +} + +// gateWorldKey carries a scenario's gate state in its context. +type gateWorldKey struct{} + +// gatesOf returns the gate state of the scenario ctx belongs to. +func gatesOf(ctx context.Context) *gateWorld { + return ctx.Value(gateWorldKey{}).(*gateWorld) +} + +// registerGates registers the plugins of list, in order after any registered before, as answering for their screens. +func registerGates(ctx context.Context, list string) { + w, gates := worldOf(ctx), gatesOf(ctx) + w.name = screensHook + for _, id := range names(list) { + p := &scenePlugin{id: id, world: w} + g := &gatePlugin{scenePlugin: p} + w.plugins = append(w.plugins, g) + w.byID[id] = p + gates.gates[id] = g + } +} + +// hideScreens makes the plugin id answer that its screens hide. +func hideScreens(ctx context.Context, id string) error { + g, ok := gatesOf(ctx).gates[id] + if !ok { + return fmt.Errorf("no plugin %q answers for its screens in this scenario", id) + } + g.hides = true + return nil +} + +// hiddenOf lists p after ids when its screens hide, turning an answer after ctx ended into the context's error. +func hiddenOf(ctx context.Context, p gated, ids []string) ([]string, error) { + shows, err := p.ShowsScreens(ctx) + if err == nil { + err = ctx.Err() + } + if err != nil || shows { + return ids, err + } + return append(ids, p.ID()), nil +} + +// askHidden asks every plugin of the scenario whether its screens show, through one filter hook. +func askHidden(ctx context.Context) error { + w := worldOf(ctx) + hook, err := pluginkit.NewHook[gated](w.name, w.plugins, w.settings()) + if err != nil { + return err + } + w.onFakeClock(func() { + gatesOf(ctx).hidden = pluginkit.Filter(context.Background(), hook, []string(nil), hiddenOf) + }) + return nil +} + +// hiddenAre checks that the plugins of list, and no others, were listed as hiding their screens, in that order. +func hiddenAre(ctx context.Context, list string) error { + if hidden, want := gatesOf(ctx).hidden, names(list); !slices.Equal(hidden, want) { + return fmt.Errorf("listed %q as hiding their screens, want %q", hidden, want) + } + return nil +} + +// noneHidden checks that no plugin was listed as hiding its screens. +func noneHidden(ctx context.Context) error { + if hidden := gatesOf(ctx).hidden; len(hidden) != 0 { + return fmt.Errorf("listed %q as hiding their screens, want none", hidden) + } + return nil +} + +// initializeScreenGate returns the steps of the screen gate feature, each scenario with its own world on a fake clock. +func initializeScreenGate(t *testing.T) func(*godog.ScenarioContext) { + return func(sc *godog.ScenarioContext) { + sc.Before(func(ctx context.Context, _ *godog.Scenario) (context.Context, error) { + w := &hookWorld{ + t: t, budget: time.Hour, timedFirings: 20, byID: map[string]*scenePlugin{}, late: map[string]bool{}, + } + ctx = context.WithValue(ctx, hookWorldKey{}, w) + return context.WithValue(ctx, gateWorldKey{}, &gateWorld{gates: map[string]*gatePlugin{}}), nil + }) + sc.After(noPanicLeft) + sc.Step(`^the plugins? (.+) answers? whether (?:its|their) screens show$`, registerGates) + sc.Step(`^the plugin "([^"]*)" is registered without answering it$`, registerBystander) + sc.Step(`^"([^"]*)" hides its screens$`, hideScreens) + sc.Step(`^the hook has a budget of (\S+) ms$`, setBudget) + sc.Step(`^"([^"]*)" takes (\S+) ms$`, setTakes) + sc.Step(`^"([^"]*)" fails with "([^"]*)"$`, failsWith) + sc.Step(`^"([^"]*)" panics with "([^"]*)"$`, panicsWith) + sc.Step(`^the application asks which plugins hide their screens$`, askHidden) + sc.Step(`^the plugins that hide their screens are (.+)$`, hiddenAre) + sc.Step(`^no plugin hides its screens$`, noneHidden) + sc.Step(`^the report holds "([^"]*)"( with the stack of the panic)?$`, reportHolds) + sc.Step(`^the report marks the failure of "([^"]*)" as caused by the ended context$`, reportMarksContextEnded) + } +} diff --git a/pluginkit/pluginkit.go b/pluginkit/pluginkit.go index f97d471..40b2baa 100644 --- a/pluginkit/pluginkit.go +++ b/pluginkit/pluginkit.go @@ -63,3 +63,9 @@ type PublicPathProvider interface { // PublicPaths returns those paths, whether or not Start ran or worked. PublicPaths() []string } + +// ScreenGate is implemented by plugins whose admin menu entries and screens show only for some callers. +type ScreenGate interface { + // ShowsScreens reports whether they show for the caller ctx carries, returning by the time ctx ends. + ShowsScreens(ctx context.Context) (bool, error) +}