69 lines
3.1 KiB
Go
69 lines
3.1 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/tradarr/backend/internal/generation"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type ticket50Provider struct {
|
|
raw, prompt string
|
|
opts GenOptions
|
|
calls int
|
|
err error
|
|
}
|
|
|
|
func (p *ticket50Provider) Name() string { return "fixture" }
|
|
func (p *ticket50Provider) ListModels(context.Context) ([]string, error) { return nil, nil }
|
|
func (p *ticket50Provider) Summarize(_ context.Context, prompt string, opts GenOptions) (string, error) {
|
|
p.calls++
|
|
p.prompt = prompt
|
|
p.opts = opts
|
|
return p.raw, p.err
|
|
}
|
|
func TestTicket50StructuredScoresAndTitleOnlyPrompt(t *testing.T) {
|
|
req := generation.TitleScoreRequest{Watchlist: []generation.ScoringCompany{{ID: "company", Name: "Acme"}}, Titles: []generation.ScoringTitle{{Index: 0, Title: "Title A"}, {Index: 1, Title: "Title B"}, {Index: 2, Title: "Title C"}}}
|
|
for _, tc := range []struct {
|
|
raw string
|
|
valid bool
|
|
}{{`{"scores":[{"index":0,"score":99.5},{"index":1},{"index":2}]}`, true}, {`{"scores":[{"index":0,"score":99.5},{"index":2}]}`, false}, {`{"scores":[{"index":0}]}`, false}, {`{"scores":[]}`, false}, {`{}`, false}, {`{"scores":null}`, false}, {`{"scores":[{"index":0,"score":null}]}`, false}, {`{"scores":[{"index":3,"score":5}]}`, false}, {`{"scores":[{"index":-1,"score":5}]}`, false}, {`{"scores":[{"index":0,"score":101}]}`, false}, {`{"scores":[{"index":0,"score":-1}]}`, false}, {`{"scores":[{"index":0,"score":"5"}]}`, false}, {`{"scores":[{"score":5}]}`, false}, {`{"scores":[{"index":0},{"index":0}]}`, false}, {`{"scores":[`, false}, {"```json\n{\"scores\":[]}\n```", false}, {`{"scores":[]} trailing`, false}, {`{"scores":[],"scores":[]}`, false}} {
|
|
t.Run(tc.raw, func(t *testing.T) {
|
|
p := &ticket50Provider{raw: tc.raw}
|
|
got, err := (titleScorer{provider: p}).Score(context.Background(), req)
|
|
if (err == nil) != tc.valid {
|
|
t.Fatalf("got=%v err=%v", got, err)
|
|
}
|
|
if tc.valid {
|
|
if len(got) != 3 || got[1] != 0 || got[2] != 0 {
|
|
t.Fatal(got)
|
|
}
|
|
if strings.Contains(tc.raw, "99.5") && got[0] != 99.5 {
|
|
t.Fatal(got)
|
|
}
|
|
}
|
|
if !p.opts.JSONMode || !p.opts.RequireComplete || p.calls != 1 || !strings.Contains(p.prompt, "Acme") || !strings.Contains(p.prompt, "Title C") {
|
|
t.Fatal("invalid request options/prompt")
|
|
}
|
|
})
|
|
}
|
|
p := &ticket50Provider{err: errors.New("SECRET raw provider failure")}
|
|
_, err := (titleScorer{provider: p}).Score(context.Background(), req)
|
|
if err == nil || strings.Contains(err.Error(), "SECRET") || p.calls != 1 {
|
|
t.Fatalf("unsafe failure %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTicket50EveryArticleRequiresAnIndexedResult(t *testing.T) {
|
|
for _, raw := range []string{`{"scores":[]}`, `{"scores":[{"index":0,"score":7}]}`, `{"scores":[{"index":0},{"index":2}]}`} {
|
|
if got, err := parseTitleScores(raw, 3); err == nil {
|
|
t.Fatalf("missing article accepted: %s -> %v", raw, got)
|
|
}
|
|
}
|
|
got, err := parseTitleScores(`{"scores":[{"index":2},{"index":0,"score":7},{"index":1}]}`, 3)
|
|
if err != nil || len(got) != 3 || got[0] != 7 || got[1] != 0 || got[2] != 0 {
|
|
t.Fatalf("indexed omitted score must be zero: %v %v", got, err)
|
|
}
|
|
}
|