package ai import ( "context" "encoding/json" "net/http" "net/http/httptest" "reflect" "strings" "testing" ) func TestStrictRankedIDs(t *testing.T) { for _, response := range []string{ `[0,1]`, `text {"ranked_article_ids":["a"]}`, "```json\n{\"ranked_article_ids\":[\"a\"]}\n```", `{"ranked_article_ids":["unknown"]}`, `{"ranked_article_ids":["a","a"]}`, `{"ranked_article_ids":null}`, `{}`, `{"ranked_article_ids":[1]}`, `{"ranked_article_ids":[],"extra":1}`, `{"ranked_article_ids":[],"ranked_article_ids":[]}`, `{"ranked_article_ids":[]} {}`, `{"ranked_article_ids":[]} trailing`, } { if _, err := parseRankedArticleIDs(response, rankingArticles("a", "b")); err == nil { t.Errorf("accepted %s", response) } } for _, response := range []string{`{"ranked_article_ids":[]}`, `{"ranked_article_ids":["b","a"]}`} { if _, err := parseRankedArticleIDs(response, rankingArticles("a", "b")); err != nil { t.Fatal(err) } } } func TestRankingRetryAndFailureDiscardAllResults(t *testing.T) { p := &rankingFakeProvider{responses: []string{`bad`, `{"ranked_article_ids":["b"]}`}} got, err := RankArticles(context.Background(), p, nil, rankingArticles("a", "b"), 2, "title") if err != nil || !reflect.DeepEqual(articleIDs(got), []string{"b"}) || len(p.prompts) != 2 { t.Fatalf("retry result=%v err=%v calls=%d", articleIDs(got), err, len(p.prompts)) } p = &rankingFakeProvider{responses: []string{`{"ranked_article_ids":["a"]}`, `bad`, `bad`}} got, err = RankArticles(context.Background(), p, nil, rankingArticles("a", "b", "c", "d"), 2, "title") if err == nil || len(got) != 0 || len(p.prompts) != 3 { t.Fatalf("unsafe partial result=%v err=%v calls=%d", got, err, len(p.prompts)) } p = &rankingFakeProvider{} got, err = RankArticles(context.Background(), p, nil, rankingArticles("a"), 2, "title") if err == nil || len(got) != 0 || len(p.prompts) != 2 { t.Fatal("provider errors must fail after two attempts") } ctx, cancel := context.WithCancel(context.Background()) cancel() p = &rankingFakeProvider{} if _, err = RankArticles(ctx, p, nil, rankingArticles("a"), 2, "title"); err == nil || len(p.prompts) != 0 { t.Fatal("cancellation ignored") } } func TestRankingContentModes(t *testing.T) { articles := rankingArticles("a") articles[0].Content = "real content" if strings.Contains(buildRankingPrompt(nil, articles, "title"), "real content") { t.Fatal("title mode sent content") } if !strings.Contains(buildRankingPrompt(nil, articles, "title_content"), "real content") { t.Fatal("title_content lost content") } articles[0].Content = " " + articles[0].Title + " " if !strings.Contains(buildRankingPrompt(nil, articles, "title_content"), "contenu indisponible : titre seul") { t.Fatal("duplicate title is not content") } } func TestOllamaRankingJSONMode(t *testing.T) { calls := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var body map[string]any if err := json.NewDecoder(r.Body).Decode(&body); err != nil { t.Error(err) } if body["format"] != "json" || body["think"] != false { t.Errorf("missing JSON mode: %v", body) } calls++ w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"response":"{\"ranked_article_ids\":[\"a\"]}"}`)) })) defer server.Close() result, err := RankArticles(context.Background(), newOllama(server.URL, "test"), nil, rankingArticles("a"), 2, "title") if err != nil || len(result) != 1 || calls != 1 { t.Fatalf("result=%v err=%v calls=%d", result, err, calls) } } func TestArticleCapsPreserveHistoricalGlobalAndDefaultSource(t *testing.T) { ranked := rankingArticles("a", "b", "c") got := (&Pipeline{}).ApplyArticleCaps(ranked, nil, 733) if len(got) != 3 { t.Fatal("historical global limit clamped") } got = (&Pipeline{}).ApplyArticleCaps(ranked, nil, 0) if len(got) != 0 { t.Fatal("invalid global limit must be safe") } }