package commandkit_test import ( "bytes" "context" "errors" "fmt" commandkit "gitea.maximumdirect.net/eric/CommandKit" configkit "gitea.maximumdirect.net/eric/ConfigKit" "gitea.maximumdirect.net/eric/promptkit" "strings" "testing" ) func TestStandardConfigurationCancellationBoundaries(t *testing.T) { for _, operation := range []string{"validate", "show", "sources", "explain"} { for _, before := range []bool{true, false} { t.Run(fmt.Sprintf("%s/before=%t", operation, before), func(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() if before { cancel() } loads, contexts := 0, 0 scope, err := commandkit.NewConfigKitScope[struct{}, struct{}]("app", "", func(context.Context, commandkit.Selection, struct{}) (*configkit.Result[struct{}], error) { loads++ cancel() return configkit.Load(configkit.NoSource(), configkit.Options[struct{}]{}) }) if err != nil { t.Fatal(err) } app, _ := commandkit.New(commandkit.Identity{Name: "example"}) if err := commandkit.RegisterConfig(app, commandkit.ConfigSuite[struct{}]{Scopes: []commandkit.ConfigScope[struct{}]{scope}, NewContext: func() struct{} { contexts++; return struct{}{} }}); err != nil { t.Fatal(err) } args := []string{"config", operation} if operation == "explain" { args = append(args, "--path", "") } var out bytes.Buffer result := app.Execute(ctx, args, commandkit.Streams{Out: &out}) want := 1 if before { want = 0 } if result.Kind != commandkit.OutcomeOperational || result.Status != 1 || !errors.Is(result.Cause, context.Canceled) || loads != want || contexts != want || out.Len() != 0 { t.Fatalf("result=%#v loads=%d contexts=%d out=%q", result, loads, contexts, out.String()) } }) } } } func TestAggregateCancellationRetainsCompletedValidationAndDiagnostics(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() app, _ := commandkit.New(commandkit.Identity{Name: "example"}) calls := 0 acquire := func(context.Context, commandkit.ConfigRequest, struct{}) (commandkit.Acquired[commandkit.ConfigView], error) { calls++ if calls == 2 { cancel() } return commandkit.Acquired[commandkit.ConfigView]{Diagnostics: []commandkit.Diagnostic{commandkit.MessageDiagnostic(configkit.SeverityWarning, "acquired warning")}}, nil } if err := commandkit.RegisterConfig(app, commandkit.ConfigSuite[struct{}]{Scopes: []commandkit.ConfigScope[struct{}]{ {Name: "app", Operations: []commandkit.ConfigOperation{commandkit.ConfigValidate}, Acquire: acquire}, {Name: "promptkit", Operations: []commandkit.ConfigOperation{commandkit.ConfigValidate}, Acquire: acquire}, {Name: "zeta", Operations: []commandkit.ConfigOperation{commandkit.ConfigValidate}, Acquire: acquire}, }}); err != nil { t.Fatal(err) } var out, diagnostics bytes.Buffer result := app.Execute(ctx, []string{"config", "validate", "--scope", "all"}, commandkit.Streams{Out: &out, Err: &diagnostics}) if !errors.Is(result.Cause, context.Canceled) || !result.ReportIncomplete || calls != 2 || out.String() != "app: valid\n" || strings.Count(diagnostics.String(), "acquired warning") != 2 { t.Fatalf("result=%#v calls=%d out=%q diagnostics=%q", result, calls, out.String(), diagnostics.String()) } } func TestDefinitionInspectionCancellationBoundaries(t *testing.T) { for _, resource := range []string{"prompt", "profile"} { for _, before := range []bool{true, false} { t.Run(fmt.Sprintf("%s/before=%t", resource, before), func(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() if before { cancel() } calls, contexts := 0, 0 diagnostics := []commandkit.Diagnostic{commandkit.MessageDiagnostic(configkit.SeverityWarning, "acquired warning")} suite := commandkit.PromptKitSuite[struct{}]{NewContext: func() struct{} { contexts++; return struct{}{} }, InspectPrompt: func(context.Context, commandkit.Selection, struct{}, string, string) (commandkit.Acquired[*promptkit.PromptInspection], error) { calls++ cancel() return commandkit.Acquired[*promptkit.PromptInspection]{Value: &promptkit.PromptInspection{}, Diagnostics: diagnostics}, nil }, InspectProfile: func(context.Context, commandkit.Selection, struct{}, string) (commandkit.Acquired[*promptkit.ProfileInspection], error) { calls++ cancel() return commandkit.Acquired[*promptkit.ProfileInspection]{Value: &promptkit.ProfileInspection{}, Diagnostics: diagnostics}, nil }, } app, _ := commandkit.New(commandkit.Identity{Name: "example"}) if err := commandkit.RegisterPromptKit(app, suite); err != nil { t.Fatal(err) } var out, errs bytes.Buffer result := app.Execute(ctx, []string{"promptkit", "show", resource, "example"}, commandkit.Streams{Out: &out, Err: &errs}) want := 1 if before { want = 0 } if result.Kind != commandkit.OutcomeOperational || !errors.Is(result.Cause, context.Canceled) || calls != want || contexts != want || out.Len() != 0 || (!before && !strings.Contains(errs.String(), "acquired warning")) { t.Fatalf("result=%#v calls=%d contexts=%d out=%q diagnostics=%q", result, calls, contexts, out.String(), errs.String()) } }) } } }