93 lines
4.1 KiB
Go
93 lines
4.1 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/tradarr/backend/internal/generation"
|
|
"github.com/tradarr/backend/internal/models"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTicket34NoulQuestionsDistinctAndAnyGroupYesRequiresEveryGroup(t *testing.T) {
|
|
instructions := map[string]string{}
|
|
for _, pass := range []string{generation.MentionDirect, generation.MentionIndirect} {
|
|
r, _ := NewLayaHTTPRanker(LayaHTTPConfig{BaseURL: "http://fake.invalid", BatchSize: 1})
|
|
calls := 0
|
|
r.client = &http.Client{Transport: ticket31Transport(func(req *http.Request) (*http.Response, error) {
|
|
calls++
|
|
var body struct {
|
|
States []any
|
|
Questions map[string]struct{ Type, Instructions string }
|
|
}
|
|
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(body.Questions) != 1 {
|
|
t.Fatal("multiple questions")
|
|
}
|
|
key := ""
|
|
for k, q := range body.Questions {
|
|
key = k
|
|
if q.Type != "noul" {
|
|
t.Fatal("not binary")
|
|
}
|
|
instructions[pass] = q.Instructions
|
|
}
|
|
probability := 0.1
|
|
if calls == 1 {
|
|
probability = 0.9
|
|
}
|
|
raw, _ := json.Marshal(map[string]any{"results": []any{map[string]any{"answers": map[string]any{key: map[string]any{"type": "noul", "noul": probability, "answer_confidence": 0.99}}, "usage": map[string]any{"truncated": false}}}})
|
|
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(string(raw))), Header: make(http.Header)}, nil
|
|
})}
|
|
assets := []models.UserAsset{{Name: strings.Repeat("a", 400), Symbol: "ONE"}, {Name: strings.Repeat("b", 400), Symbol: "TWO"}}
|
|
got, err := r.DecideMentions(context.Background(), "title", pass, assets, []generation.Candidate{{ID: "one"}})
|
|
if err != nil || len(got) != 1 || calls != 2 {
|
|
t.Fatalf("calls=%d err=%v", calls, err)
|
|
}
|
|
}
|
|
if instructions[generation.MentionDirect] == instructions[generation.MentionIndirect] || instructions[generation.MentionDirect] == "" {
|
|
t.Fatal("same direct/indirect question")
|
|
}
|
|
}
|
|
func TestTicket34NoulRejectsMissingInvalidAbstainedOrTruncatedGroup(t *testing.T) {
|
|
for _, answer := range []string{`{"type":"score","score":0.9,"answer_confidence":0.9}`, `{"type":"noul","noul":null,"answer_confidence":0.9}`, `{"type":"noul","noul":2,"answer_confidence":0.9}`, `{"type":"noul","noul":0.9}`, `{"type":"noul","noul":0.9,"answer_confidence":0.9,"low_confidence":true}`} {
|
|
r, _ := NewLayaHTTPRanker(LayaHTTPConfig{BaseURL: "http://fake.invalid", BatchSize: 1})
|
|
calls := 0
|
|
r.client = &http.Client{Transport: ticket31Transport(func(req *http.Request) (*http.Response, error) {
|
|
calls++
|
|
var body struct{ Questions map[string]any }
|
|
_ = json.NewDecoder(req.Body).Decode(&body)
|
|
key := ""
|
|
for k := range body.Questions {
|
|
key = k
|
|
}
|
|
value := `{"type":"noul","noul":0.9,"answer_confidence":0.99}`
|
|
if calls == 2 {
|
|
value = answer
|
|
}
|
|
raw := `{"results":[{"answers":{"` + key + `":` + value + `},"usage":{"truncated":false}}]}`
|
|
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(raw)), Header: make(http.Header)}, nil
|
|
})}
|
|
got, err := r.DecideMentions(context.Background(), "title", generation.MentionDirect, []models.UserAsset{{Name: strings.Repeat("a", 400)}, {Name: strings.Repeat("b", 400)}}, []generation.Candidate{{ID: "one"}})
|
|
if err == nil || got != nil || calls != 2 {
|
|
t.Fatal("accepted partial/invalid group")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTicket34NoulRejectsExplicitErrorEvenWithOtherwiseValidResult(t *testing.T) {
|
|
r, _ := NewLayaHTTPRanker(LayaHTTPConfig{BaseURL: "http://fake.invalid"})
|
|
r.client = &http.Client{Transport: ticket31Transport(func(req *http.Request) (*http.Response, error) {
|
|
body := `{"error":"private failure","results":[{"answers":{"direct_mention":{"type":"noul","noul":0.9,"answer_confidence":0.99}},"usage":{"truncated":false}}]}`
|
|
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil
|
|
})}
|
|
got, err := r.DecideMentions(context.Background(), "title", generation.MentionDirect, nil, []generation.Candidate{{ID: "one"}})
|
|
if err == nil || got != nil {
|
|
t.Fatal("explicit error was accepted")
|
|
}
|
|
}
|