package handlers import ( "context" "encoding/json" "errors" "strings" "testing" "time" "github.com/gin-gonic/gin" "adam-french.co.uk/usaco-site/internal/claude" "adam-french.co.uk/usaco-site/internal/evaluate" "adam-french.co.uk/usaco-site/internal/jobs" ) // fakeEvaluator flags line 1 of whatever it is shown, or fails. type fakeEvaluator struct { user string fail bool } func (f *fakeEvaluator) Model() string { return "fake-model" } func (f *fakeEvaluator) JSON(_ context.Context, req claude.Request, out any) error { f.user = req.User if f.fail { return &claude.Failure{Msg: "Claude declined to answer"} } *out.(*evaluate.Result) = evaluate.Result{ Verdict: "edge_cases", Summary: "Prints zero.", Issues: []evaluate.Issue{{Line: 1, Severity: "error", Title: "Never reads n"}}, } return nil } type evalStatus struct { State string `json:"state"` Error string `json:"error"` Result *evaluate.Evaluation `json:"result"` } func waitEvaluate(t *testing.T, r *gin.Engine, name string) evalStatus { t.Helper() for range 400 { var s evalStatus json.Unmarshal(do(r, "GET", "/usaco/api/problems/"+name+"/evaluate", "", true).Body.Bytes(), &s) if s.State != jobs.Running { return s } time.Sleep(5 * time.Millisecond) } t.Fatal("evaluation still running") return evalStatus{} } const evalBody = `{"lang":"cpp","source":"int main() {}\n","note":"is it fast enough?"}` func TestEvaluateRefusals(t *testing.T) { r, repo, _ := setupRun(t) if rec := do(r, "POST", "/usaco/api/problems/moocast/evaluate", evalBody, true); rec.Code != 503 { t.Fatalf("no Claude = %d, want 503", rec.Code) } repo.srv.Claude = &fakeEvaluator{} if rec := do(r, "POST", "/usaco/api/problems/moocast/evaluate", evalBody, false); rec.Code != 401 { t.Fatalf("public evaluate = %d, want 401", rec.Code) } if rec := do(r, "POST", "/usaco/api/problems/moocast/evaluate", evalBody, true); rec.Code != 400 { t.Fatalf("no statement or link = %d, want 400: %s", rec.Code, rec.Body) } withStatement(t, r) if rec := do(r, "POST", "/usaco/api/problems/moocast/evaluate", `{"lang":"cpp","source":" \n"}`, true); rec.Code != 400 { t.Fatalf("empty source = %d, want 400", rec.Code) } if rec := do(r, "POST", "/usaco/api/problems/moocast/evaluate", `{"lang":"rust","source":"fn main() {}"}`, true); rec.Code != 400 { t.Fatalf("rust = %d, want 400", rec.Code) } if rec := do(r, "POST", "/usaco/api/problems/two-sum/evaluate", `{"lang":"python","source":"x"}`, true); rec.Code != 400 { t.Fatalf("python on LeetCode = %d, want 400", rec.Code) } } func TestEvaluateRunsTheTestsAndCommitsNothing(t *testing.T) { r, repo, fr := setupRun(t) fc := &fakeEvaluator{} repo.srv.Claude = fc withStatement(t, r) commits := len(repo.msg) if rec := do(r, "POST", "/usaco/api/problems/moocast/evaluate", evalBody, true); rec.Code != 202 { t.Fatalf("evaluate = %d %s", rec.Code, rec.Body) } s := waitEvaluate(t, r, "moocast") if s.State != jobs.Done || s.Result == nil { t.Fatalf("job = %+v", s) } ev := s.Result if ev.Verdict != "edge_cases" || ev.Lang != "cpp" || ev.Source != "int main() {}\n" || ev.Model != "fake-model" || ev.Tests != "1/1 passed" { t.Errorf("evaluation = %+v", ev) } if len(ev.Issues) != 1 || ev.Issues[0].Line != 1 { t.Errorf("issues = %+v", ev.Issues) } // Submit's run: the saved tests, with the headers. if fr.run == nil || len(fr.run.Tests) != 3 || fr.run.IO != "moocast" || fr.run.Files["debugging.h"] == "" { t.Errorf("run = %+v", fr.run) } for _, want := range []string{"walkie-talkies", "\n1| int main() {}\n", "1/1 passed on the saved tests", "is it fast enough?"} { if !strings.Contains(fc.user, want) { t.Errorf("prompt is missing %q:\n%s", want, fc.user) } } if len(repo.msg) != commits { t.Errorf("an evaluation committed: %v", repo.msg[commits:]) } } func TestEvaluateWithoutARunner(t *testing.T) { r, repo, fr := setupRun(t) fc := &fakeEvaluator{} repo.srv.Claude = fc withStatement(t, r) // A runner that fails costs the verdicts, not the evaluation. fr.err = errors.New("runner down") do(r, "POST", "/usaco/api/problems/moocast/evaluate", evalBody, true) if s := waitEvaluate(t, r, "moocast"); s.State != jobs.Done || s.Result.Tests != "The tests were not run: the code runner failed." { t.Fatalf("with a failing runner = %+v", s) } repo.srv.Runner = nil if c := getCodeReply(t, r, "moocast"); c.Runner { t.Fatal("runner still reported") } do(r, "POST", "/usaco/api/problems/moocast/evaluate", evalBody, true) s := waitEvaluate(t, r, "moocast") if s.State != jobs.Done || !strings.Contains(fc.user, "the code runner is not configured") { t.Fatalf("with no runner = %+v\n%s", s, fc.user) } fc.fail = true do(r, "POST", "/usaco/api/problems/moocast/evaluate", evalBody, true) if s := waitEvaluate(t, r, "moocast"); s.State != jobs.Failed || s.Error != "Claude declined to answer" || s.Result != nil { t.Fatalf("a failed evaluation = %+v", s) } }