package ckprompt_test import ( "bytes" "context" commandkit "gitea.maximumdirect.net/eric/CommandKit" "gitea.maximumdirect.net/eric/promptkit" "io" "strings" "testing" ) func TestBridgeTreeRefreshesAfterSuccessfulRegistration(t *testing.T) { app, err := commandkit.New(commandkit.Identity{Name: "example"}) if err != nil { t.Fatal(err) } var output bytes.Buffer streams := commandkit.Streams{Out: &output, Err: io.Discard} renderRootHelp := func() string { t.Helper() output.Reset() outcome := app.Execute(context.Background(), []string{"--help"}, streams) if outcome.Kind != commandkit.OutcomeSuccess { t.Fatalf("root help outcome = %#v", outcome) } return output.String() } initial := renderRootHelp() if strings.Contains(initial, "custom") || strings.Contains(initial, "config") || strings.Contains(initial, "promptkit") { t.Fatalf("initial help unexpectedly lists future registrations: %q", initial) } custom, err := NewCommand(CommandSpec[struct{}]{Name: "custom", Run: func(Invocation, struct{}) Outcome { return commandkit.Success() }}) if err != nil { t.Fatal(err) } if err := app.Register(custom); err != nil { t.Fatal(err) } providerCalls := 0 if err := commandkit.RegisterConfig(app, commandkit.ConfigSuite[struct{}]{Scopes: []commandkit.ConfigScope[struct{}]{{ Name: "app", Operations: []ConfigOperation{commandkit.ConfigShow}, Acquire: func(context.Context, commandkit.ConfigRequest, struct{}) (commandkit.Acquired[commandkit.ConfigView], error) { providerCalls++ return commandkit.Acquired[commandkit.ConfigView]{}, nil }, }}}); err != nil { t.Fatal(err) } if err := commandkit.RegisterPromptKit(app, commandkit.PromptKitSuite[struct{}]{InspectPrompt: func(context.Context, commandkit.Selection, struct{}, string, string) (commandkit.Acquired[*promptkit.PromptInspection], error) { providerCalls++ return commandkit.Acquired[*promptkit.PromptInspection]{}, nil }}); err != nil { t.Fatal(err) } updated := renderRootHelp() for _, name := range []string{"custom", "config", "promptkit"} { if !strings.Contains(updated, name) { t.Errorf("updated help omits %q: %q", name, updated) } } if providerCalls != 0 { t.Fatalf("help invoked inspection or configuration callbacks %d times", providerCalls) } late, err := NewCommand(CommandSpec[struct{}]{Name: "late"}) if err != nil { t.Fatal(err) } if err := app.Register(late, custom); err == nil { t.Fatal("Register accepted a batch with an existing-name collision") } if err := app.Register(); err != nil { t.Fatalf("empty registration failed: %v", err) } final := renderRootHelp() if !strings.Contains(final, "custom") || !strings.Contains(final, "config") || !strings.Contains(final, "promptkit") || strings.Contains(final, "late") { t.Fatalf("help after failed and empty registration = %q", final) } if outcome := app.Execute(context.Background(), []string{"custom"}, streams); outcome.Kind != commandkit.OutcomeSuccess { t.Fatalf("new command did not parse after registration: %#v", outcome) } }