package app import ( "context" "encoding/json" "fmt" "io" "net/http" "net/http/httptest" "strconv" "strings" "sync/atomic" "testing" "time" ) func TestJevBatchesKeepItemsIsolatedAndMapAnswers(t *testing.T) { for _, tc := range []struct { name, text string items, calls int }{ {"empty", "", 0, 0}, {"whole_page", "small", 24, 1}, {"question_limit", "small", 61, 3}, {"byte_limit", strings.Repeat("界<", 2400), 3, 3}, } { t.Run(tc.name, func(t *testing.T) { var calls atomic.Int32 provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls.Add(1) body, _ := io.ReadAll(r.Body) var req struct { State map[string]string `json:"state"` Questions map[string]struct { Instructions struct { Question string `json:"question"` Passage map[string]string `json:"passage"` } `json:"instructions"` } `json:"questions"` } if err := json.Unmarshal(body, &req); err != nil { t.Error(err) } if len(body) > maxJevRequestBytes || len(req.Questions) > 96 || len(req.State) != 1 || req.State["query"] != "limits" { t.Error("batch exceeded budget or leaked items into shared state") } answers := map[string]Decision{} for id, q := range req.Questions { index, _ := strconv.Atoi(strings.SplitN(id, "_", 2)[0]) if q.Instructions.Passage["heading"] != strconv.Itoa(index) || q.Instructions.Passage["text"] != tc.text || q.Instructions.Question == "" { t.Error("question lost its isolated passage", id) } answers[id] = Decision{Type: "noul", Noul: float64(index) / 100} } writeJSON(w, 200, JevResponse{Model: "test", Answers: answers}) })) defer provider.Close() j := &Jev{URL: provider.URL, Key: "key", Model: "test", HTTP: provider.Client(), slots: make(chan struct{}, 2)} items := make([]map[string]string, tc.items) for i := range items { items[i] = map[string]string{"heading": strconv.Itoa(i), "text": tc.text} } answers, err := j.evaluateItems(context.Background(), "passage", "limits", "passage", items, passageQuestions) if err != nil || len(answers) != tc.items || int(calls.Load()) != tc.calls { t.Fatal(err, len(answers), calls.Load()) } for i, a := range answers { for name := range passageQuestions { if a[name].Noul != float64(i)/100 { t.Fatal("misrouted answer", i, name, a[name]) } } } }) } } func TestJevBatchRejectsOversizedItemsBeforeCalling(t *testing.T) { j := &Jev{Model: "test"} items := []map[string]string{{"text": "small"}, {"text": strings.Repeat("x", maxJevRequestBytes)}} if _, err := j.evaluateItems(context.Background(), "passage", "limits", "passage", items, passageQuestions); err == nil { t.Fatal("oversized item accepted") } } func TestJevBatchesRespectGlobalConcurrencyAndCancellation(t *testing.T) { started, release := make(chan struct{}, 3), make(chan struct{}) var active, peak atomic.Int32 provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { n := active.Add(1) defer active.Add(-1) for old := peak.Load(); n > old; old = peak.Load() { if peak.CompareAndSwap(old, n) { break } } started <- struct{}{} select { case <-release: case <-r.Context().Done(): return } var req struct { Questions map[string]Question `json:"questions"` } _ = json.NewDecoder(r.Body).Decode(&req) answers := map[string]Decision{} for id := range req.Questions { answers[id] = Decision{Type: "noul", Noul: .5} } writeJSON(w, 200, JevResponse{Model: "test", Answers: answers}) })) defer provider.Close() j := &Jev{URL: provider.URL, Key: "key", Model: "test", HTTP: provider.Client(), slots: make(chan struct{}, 2)} ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() done := make(chan error, 1) items := make([]map[string]string, 60) go func() { _, err := j.evaluateItems(ctx, "passage", "limits", "passage", items, passageQuestions) done <- err }() for range 2 { select { case <-started: case <-ctx.Done(): t.Fatal("batches did not run concurrently") } } close(release) if err := <-done; err != nil || peak.Load() != 2 { t.Fatal(err, peak.Load()) } canceled, stop := context.WithCancel(context.Background()) stop() if _, err := j.evaluateItems(canceled, "passage", "limits", "passage", items, passageQuestions); err == nil { t.Fatal("canceled batch succeeded") } } func TestDeadlineReturnsCompletedSourcesAndTimeoutStatuses(t *testing.T) { s, page := fixtureService(t) s.cfg.Concurrency = 1 s.cfg.RequestTimeout = 100 * time.Millisecond slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() })) defer slow.Close() status, payload := callHTTP(t, s, "/contents", fmt.Sprintf(`{"urls":[%q,%q,%q]}`, page.URL, slow.URL, slow.URL+"/queued")) var out ResultsResponse _ = json.Unmarshal([]byte(jsonText(payload)), &out) if status != 200 || len(out.Results) != 1 || len(out.Statuses) != 3 { t.Fatal(status, out) } for i, item := range out.Statuses { if item.ID == "" || i == 0 && item.Status != "success" || i > 0 && (item.Error == nil || item.Error.Tag != "REQUEST_TIMEOUT" || item.Error.HTTPStatusCode != 504) { t.Fatal("bad per-url status", item) } } var outcome, detail string if err := s.log.db.QueryRow("select outcome,detail from requests where request_id=? and endpoint='contents'", out.RequestID).Scan(&outcome, &detail); err != nil || outcome != "partial" || !strings.Contains(detail, "REQUEST_TIMEOUT") { t.Fatal("partial timeout lost from logs", err, outcome, detail) } status, _ = callHTTP(t, s, "/contents", fmt.Sprintf(`{"urls":[%q]}`, slow.URL)) if status != 504 { t.Fatal("empty timed-out request must still fail", status) } }