package cli import ( "encoding/json" "fmt" "go.maximumdirect.net/promptkit" "os" "path/filepath" "strings" "testing" ) func TestCollectionCheckoutDiscoveryAcrossInvocations(t *testing.T) { fixture, err := filepath.Abs(filepath.Join("..", "..", "..", "examples", "discovery")) if err != nil { t.Fatal(err) } for _, tc := range []struct { name string first string second string }{ {name: "yml then yaml", first: "support-repo", second: "review-repo"}, {name: "yaml then yml", first: "review-repo", second: "support-repo"}, } { t.Run(tc.name, func(t *testing.T) { root := t.TempDir() framework, err := os.ReadFile(filepath.Join(fixture, "framework.yml")) if err != nil { t.Fatal(err) } frameworkPath := filepath.Join(root, "framework.yml") if err := os.WriteFile(frameworkPath, framework, 0o644); err != nil { t.Fatal(err) } collections := filepath.Join(root, "collections") if err := os.MkdirAll(collections, 0o755); err != nil { t.Fatal(err) } install := func(name string) { t.Helper() if err := os.CopyFS(filepath.Join(collections, name), os.DirFS(filepath.Join(fixture, "collections", name))); err != nil { t.Fatal(err) } } config := filepath.Join(root, "app.yml") writeNativeFile(t, config, fmt.Sprintf("promptkit:\n configuration: %q\n", frameworkPath)) input := filepath.Join(root, "transcript.md") if err := os.WriteFile(input, []byte("synthetic discovery transcript"), 0o644); err != nil { t.Fatal(err) } args := func(name string) []string { if name == "support-repo" { return []string{"support/answer", "--config", config, "--profile", "support/fast", "--input-file", "transcript=" + input} } return []string{"review/answer", "--config", config, "--profile", "review/base"} } check := func(name string) { t.Helper() command := append([]string{"promptkit", "show", "prepared"}, args(name)...) outcome, out, stderr := executeNative(t, append(command, "--format=json")...) if outcome.Status != 0 { t.Fatalf("prepare %s: outcome=%+v stderr=%s", name, outcome, stderr) } var prepared promptkit.PreparedRun if err := json.Unmarshal([]byte(out), &prepared); err != nil { t.Fatal(err) } if name == "support-repo" { for _, want := range []string{"support/answer", "support/fast", "support-example", "Answer the support request", "synthetic discovery transcript"} { if !strings.Contains(out, want) { t.Fatalf("missing %q: %s", want, out) } } if prepared.EffectiveModelParams.MaxTokens != 256 { t.Fatalf("inherited profile setting missing: %+v", prepared) } } else if !strings.Contains(out, "review/answer") || !strings.Contains(out, "review-example") || !strings.Contains(out, "Review this material") { t.Fatalf("unexpected review render: %s", out) } } install(tc.first) check(tc.first) outcome, _, _ := executeNative(t, append([]string{"promptkit", "show", "prepared"}, args(tc.second)...)...) if outcome.Status != 1 { t.Fatal("uninstalled collection unexpectedly selected") } install(tc.second) check(tc.first) check(tc.second) }) } }