package cli import ( "context" "errors" "io" "math" "os" "path/filepath" "testing" appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config" "go.maximumdirect.net/ckconfig" "go.maximumdirect.net/commandkit" "go.maximumdirect.net/configkit" "go.maximumdirect.net/promptkit" ) func TestServerSelectorsPreservePresenceAndEvidence(t *testing.T) { path := filepath.Join(t.TempDir(), "app.yml") if err := os.WriteFile(path, []byte("server:\n addr: :1234\n"), 0600); err != nil { t.Fatal(err) } var result *configkit.Result[appconfig.Document] var loadErr error var captured serveInput app, err := commandkit.New(commandkit.Identity{Name: "selectors"}, commandkit.NewCommand(commandkit.CommandSpec[serveInput]{ Name: "serve", Flags: serveFlags(), Run: func(inv commandkit.Invocation, input serveInput) commandkit.Outcome { captured = input result, loadErr = loadApplication(inv.Context, input.Selection, input.Application, struct{}{}) if loadErr != nil { return commandkit.Failure("load failed").WithCause(loadErr) } return commandkit.Success() }, })) if err != nil { t.Fatal(err) } args := []string{"serve", "--config", path, "--overlay=", "--addr=", "--artifact-root", " ./a/../artifacts ", "--max-request-bytes=0", "--max-artifact-bytes=9223372036854775807", "--max-response-bytes=4294967296"} outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: io.Discard, Err: io.Discard}) if outcome.Status != 0 { t.Fatalf("outcome=%+v err=%v", outcome, loadErr) } if !captured.Selection.Config.Set || !captured.Selection.Overlay.Set || !captured.Application.Addr.Set || !captured.Application.MaxRequestBytes.Set { t.Fatalf("presence lost: %+v", captured) } if result.Config.Server.Addr != "" || result.Config.Server.MaxRequestBytes != 0 || result.Config.Server.MaxArtifactBytes != math.MaxInt64 || result.Config.Server.MaxResponseBytes != 4294967296 || result.Config.Server.ArtifactRoot != "artifacts" { t.Fatal(result.Config) } explanation, ok := result.Explain(configkit.Keys("server", "artifact_root")) var cli, normalized bool for _, entry := range explanation.History { cli = cli || entry.Origin.Layer == configkit.LayerCLI normalized = normalized || entry.Origin.Operation == configkit.ChangeNormalization } if !ok || !cli || !normalized { t.Fatalf("evidence lost: %+v", explanation) } if len(namedApplicationOverrides(applicationInput{})) != 0 { t.Fatal("omitted HTTP flags became overrides") } if err := os.WriteFile(path, []byte("transient_retries: 7\n"), 0600); err != nil { t.Fatal(err) } fresh, err := loadApplication(context.Background(), captured.Selection, captured.Application, struct{}{}) if err != nil || fresh.Config.TransientRetries == nil || *fresh.Config.TransientRetries != 7 || fresh == result { t.Fatalf("fresh load: %+v %v", fresh, err) } } func TestApplicationSelectionErrorsAndCancellation(t *testing.T) { path := filepath.Join(t.TempDir(), "app.yml") if err := os.WriteFile(path, []byte("{}\n"), 0600); err != nil { t.Fatal(err) } selection := ckconfig.Selection{Config: commandkit.Supplied[string]{Set: true, Value: path}} for _, overlay := range []commandkit.Supplied[string]{{}, {Set: true}, {Set: true, Value: "default"}} { selection.Overlay = overlay if _, err := loadApplication(context.Background(), selection, applicationInput{}, struct{}{}); err != nil { t.Fatalf("overlay=%+v: %v", overlay, err) } } selection.Overlay = commandkit.Supplied[string]{Set: true, Value: "missing"} _, err := loadApplication(context.Background(), selection, applicationInput{}, struct{}{}) var native *configkit.LoadError if !errors.As(err, &native) { t.Fatalf("missing overlay lost typed error: %v", err) } selection = ckconfig.Selection{Config: commandkit.Supplied[string]{Set: true}} if _, err := loadApplication(context.Background(), selection, applicationInput{}, struct{}{}); !errors.As(err, &native) { t.Fatalf("empty config lost typed error: %v", err) } ctx, cancel := context.WithCancel(context.Background()) cancel() if _, err := loadApplication(ctx, ckconfig.Selection{}, applicationInput{}, struct{}{}); !errors.Is(err, context.Canceled) { t.Fatalf("cancellation lost: %v", err) } } func TestEngineUsesOptionalMainAndNativeResourceContexts(t *testing.T) { root := t.TempDir() t.Chdir(root) write := func(path, body string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(body), 0600); err != nil { t.Fatal(err) } } settings := appconfig.Settings(appconfig.Document{}) engine, err := newEngine(settings) if err != nil { t.Fatal(err) } if _, err := engine.InspectProfile(context.Background(), "auto"); err != nil { t.Fatalf("bundle missing without external main: %v", err) } settings.PromptKitConfiguration = "missing.yml" if _, err := newEngine(settings); err == nil { t.Fatal("missing selected main accepted") } write("framework/main.yml", "config:\n imports:\n - glob: collections/*/collection.yml\n - glob: collections/*/collection.yaml\n") write("framework/collections/example/collection.yaml", "config:\n imports:\n - path: definitions.yml\ncollections:\n review:\n release_version: '1'\n") write("framework/collections/example/definitions.yml", "collections:\n review:\n prompts:\n - id: summary\n messages:\n - role: user\n content_file: message.txt\n output:\n format: text\n validation_mode: none\n") write("framework/collections/example/message.txt", "source-relative body") settings.PromptKitConfiguration = "framework/main.yml" engine, err = newEngine(settings) if err != nil { t.Fatal(err) } _, err = engine.InspectPrompt(context.Background(), "review/summary") if err != nil { t.Fatal(err) } // Native preparation uses the captured source-relative message resource, // not the process cwd or application configuration's directory. prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "review/summary", ProfileID: "auto"}) if err != nil { t.Fatal(err) } if len(prepared.Messages) != 1 || prepared.Messages[0].Content != "source-relative body" { t.Fatalf("resource context lost: %+v", prepared) } view, err := engine.InspectConfiguration() if err != nil { t.Fatal(err) } if len(view.Sources()) < 3 { t.Fatalf("native source evidence missing: %+v", view.Sources()) } }