package cli import ( "bytes" "context" "os" "path/filepath" "strings" "testing" ) func writeRuntimeErrorConfig(t *testing.T, operatorDocument string) string { t.Helper() dir := t.TempDir() main := filepath.Join(dir, "config.yml") for name, body := range map[string]string{"config.yml": "version: 3\n", "prompts.yml": operatorDocument} { if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0600); err != nil { t.Fatal(err) } } return main } func TestResourceCommandsClassifyOperatorPromptErrorsAsInputErrors(t *testing.T) { t.Setenv("AUDITA_TEST_PROVIDER_KEY", "synthetic-cli-redaction-secret") definitions := map[string]string{ "malformed operator YAML": "profiles: [\n", "invalid operator template": "collections:\n audita:\n prompts:\n - id: modules.grammar.proposal\n messages:\n - role: user\n content: '{{'\n output:\n format: text\n validation_mode: none\n", "missing operator message resource": "collections:\n audita:\n prompts:\n - id: modules.grammar.proposal\n messages:\n - role: user\n content_file: missing-instruction.md\n output:\n format: text\n validation_mode: none\n", } for name, document := range definitions { t.Run(name, func(t *testing.T) { main := writeRuntimeErrorConfig(t, document) var stdout, stderr bytes.Buffer if code := Run(context.Background(), []string{"promptkit", "list", "prompts", "--config", main}, &stdout, &stderr); code == 0 { t.Fatalf("invalid resource accepted: stdout=%q stderr=%q", stdout.String(), stderr.String()) } if strings.Contains(stderr.String(), "synthetic-cli-redaction-secret") { t.Fatal("configured secret leaked") } }) } } func TestResourceCommandKeepsCallerCancellationAsFailure(t *testing.T) { main := writeRuntimeErrorConfig(t, "profiles:\n - id: proposal\n model: local-model\n") ctx, cancel := context.WithCancel(context.Background()) cancel() var stdout, stderr bytes.Buffer if code := Run(ctx, []string{"promptkit", "list", "prompts", "--config", main}, &stdout, &stderr); code == 0 { t.Fatalf("cancellation succeeded: stdout=%q stderr=%q", stdout.String(), stderr.String()) } }