package cli import ( "bytes" "context" "fmt" commandkit "go.maximumdirect.net/commandkit" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "sync/atomic" "testing" ) func TestProcessingProtectsOperatorSchemas(t *testing.T) { for _, mode := range []string{"transcript", "report", "separate"} { t.Run(mode, func(t *testing.T) { dir := t.TempDir() var calls atomic.Int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls.Add(1) fmt.Fprint(w, `{"choices":[{"message":{"content":"{\"corrections\":[]}"}}]}`) })) defer server.Close() schemaName := "contract.json" if mode == "report" { schemaName = "errors.yml" } schema := filepath.Join(dir, schemaName) original := `{"type":"object","required":["corrections"],"properties":{"corrections":{"type":"array"}}}` files := map[string]string{ "config.yml": "version: 3\nllm: {proposal_profile: fixture}\npipeline:\n stages: [{module: grammar}]\n", "input.json": `[{"speaker":"Alice","start":0,"end":1,"text":"Hello."}]`, schemaName: original, "prompts.yml": fmt.Sprintf("profiles:\n - id: fixture\n endpoint: %s/v1\n model: test\n api_key_required: false\ncollections:\n audita:\n prompts:\n - id: modules.grammar.proposal\n messages: [{role: user, content: Return an empty correction set.}]\n output:\n format: json\n validation_mode: json_schema\n schema_file: %s\n", server.URL, schemaName), } for name, data := range files { if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil { t.Fatal(err) } } output := filepath.Join(dir, "output.json") if mode == "transcript" { output = schema } app, err := buildApplication() if err != nil { t.Fatal(err) } var out, stderr bytes.Buffer result := app.Execute(context.Background(), []string{"process", "--config", filepath.Join(dir, "config.yml"), "--output", output, filepath.Join(dir, "input.json")}, commandkit.Streams{Out: &out, Err: &stderr}) if mode == "separate" { if result.Status != 0 || calls.Load() != 1 { t.Fatalf("status=%d calls=%d err=%s", result.Status, calls.Load(), stderr.String()) } } else { if result.Status == 0 || calls.Load() != 0 || !strings.Contains(stderr.String(), "conflicts with input") { t.Fatalf("status=%d calls=%d err=%s", result.Status, calls.Load(), stderr.String()) } if _, err := os.Stat(filepath.Join(dir, "recoveries.yml")); !os.IsNotExist(err) { t.Fatalf("report created before collision check: %v", err) } } raw, err := os.ReadFile(schema) if err != nil || string(raw) != original { t.Fatalf("schema changed: %s %v", raw, err) } }) } }