package ai import ( "context" "encoding/json" "errors" "net/http" "reflect" "strings" "testing" "github.com/tradarr/backend/internal/summaryqa" ) func TestTicket63ResponsesTransmitsFullChatContextWithOptionalPassage(t *testing.T) { for _, passage := range []string{"", "ATTACHED_PASSAGE"} { t.Run(passage, func(t *testing.T) { input := summaryqa.StatelessRequest{UseWebSearch: true, Summary: "FULL_SYNTHESIS", Language: "fr", History: []summaryqa.Exchange{{Question: "FIRST_QUESTION", Answer: "FIRST_ANSWER"}, {Passage: "OLD_ATTACHMENT", Question: "SECOND_QUESTION", Answer: "SECOND_ANSWER"}}, Passage: passage, Question: "CURRENT_QUESTION"} calls := 0 p := newOpenAIWebQA("fixture", "gpt-6.1-terra") p.client = &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) { calls++ var payload struct { Input string `json:"input"` Store bool `json:"store"` Choice string `json:"tool_choice"` Calls int `json:"max_tool_calls"` Instructions string `json:"instructions"` } if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { t.Fatal(err) } var got summaryqa.StatelessRequest if json.Unmarshal([]byte(payload.Input), &got) != nil || got.Summary != input.Summary || !reflect.DeepEqual(got.History, input.History) || got.Question != input.Question || got.Passage != passage || got.Language != "fr" { t.Fatal("vendor input lost chat context") } if payload.Store || payload.Choice != "auto" || payload.Calls != 2 { t.Fatal("stateless/tool safeguards changed") } for _, field := range []string{"citations", "request_id", "response_id", "previous_response_id", "conversation", "status", "articles"} { if strings.Contains(payload.Input, `"`+field+`"`) { t.Fatal("unexpected prompt metadata", field) } } if !strings.Contains(payload.Instructions, "full synthesis") { t.Fatal("instructions still restrict answer to a passage") } return web60Response(`{"status":"completed","output":[`+web60Call+`,`+web60Message+`]}`, 200), nil })} result, err := p.Answer(context.Background(), input) if err != nil || calls != 1 || !result.WebSearchPerformed || len(result.Citations) != 1 { t.Fatal("web chat failed", err) } }) } } func TestTicket63VendorContextLimitIsExactAndNeverTruncates(t *testing.T) { for _, delta := range []int{-1, 0, 1} { req := summaryqa.StatelessRequest{UseWebSearch: true, Question: "é", Language: "fr", History: []summaryqa.Exchange{{Question: "prior", Answer: "answer"}}} fixed := len(req.Question) + len("prior") + len("answer") req.Summary = strings.Repeat("s", summaryqa.MaxContextBytes-fixed+delta) p := newOpenAIWebQA("fixture", "gpt-6.1-terra") calls := 0 p.client = &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) { calls++ var body struct { Input string `json:"input"` } _ = json.NewDecoder(r.Body).Decode(&body) var got summaryqa.StatelessRequest _ = json.Unmarshal([]byte(body.Input), &got) if got.Summary != req.Summary || !reflect.DeepEqual(got.History, req.History) { t.Fatal("vendor context truncated") } return web60Response(`{"status":"completed","output":[`+web60Message+`]}`, 200), nil })} _, err := p.Answer(context.Background(), req) if delta > 0 { if !errors.Is(err, summaryqa.ErrLimit) || calls != 0 { t.Fatal("over-limit context reached HTTP", err) } } else if err != nil || calls != 1 { t.Fatal("within-limit context rejected", delta, err) } } } func TestTicket63NormalAdapterAcceptsGeneralChatAndRejectsOversizeBeforeCall(t *testing.T) { for _, delta := range []int{0, 1} { fake := &ticket50Provider{raw: "answer"} p := statelessQAProvider{provider: fake} req := summaryqa.StatelessRequest{Summary: strings.Repeat("s", summaryqa.MaxContextBytes-len("q")+delta), Question: "q", Language: "fr"} _, err := p.Answer(context.Background(), req) if delta > 0 { if !errors.Is(err, summaryqa.ErrLimit) || fake.calls != 0 { t.Fatal("oversized normal chat reached provider", err) } } else { if err != nil || fake.calls != 1 || !strings.Contains(fake.prompt, req.Summary) || !strings.Contains(fake.prompt, "empty passage means a general question") { t.Fatal("normal chat rejected or truncated", err) } if fake.opts.MaxOutputTokens != 4096 || !fake.opts.RequireComplete { t.Fatal("normal adapter limits changed") } } } }