Files
usaco/site/internal/handlers/evaluate_test.go
T
adamfandClaude Opus 5.5 d4849859df
Test and publish the site / test (push) Successful in 1m25s
Test and publish the site / publish (push) Successful in 1m51s
Autocomplete and evaluate solutions on the solve page
The editor's completion popup was on but empty for C++, since
@codemirror/lang-cpp has no completion source. web/src/completions.js
adds one: keywords, the STL, every name in the file (the template's
macros show their expansion, typedefs what they alias), members after
. and ->, and snippets for the loops and lambdas typed over and over.
Python gains the contest standard library and members after ., on top
of lang-python's builtins and local names. Nothing knows types, so
members are the common ones plus those already used in the file.

Evaluate (POST/GET /problems/<name>/evaluate) has Claude judge the
editor's code as a strong competitive programmer would before
submitting. It runs the saved tests on the runner first, as Submit
does, and gives Claude the verdicts and up to three failing cases. The
reply is a verdict, correctness, complexity against the constraints,
line-anchored issues and a traced counterexample (internal/evaluate).
The page shows it in an Evaluation tab of the results panel, marks the
issues in the editor beside the lint's for as long as their lines are
unchanged, and can load the counterexample as the custom input.

It is a background job the page polls, like explanations, but it
commits nothing: the code is a draft. explain.Jobs.StartWith keeps a
finished job's answer in memory until the next one or a restart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 12:46:54 +01:00

147 lines
4.9 KiB
Go

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/explain"
)
// 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 != explain.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 != explain.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", "<code lang=\"cpp\">\n1| int main() {}\n</code>", "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 != explain.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 != explain.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 != explain.Failed || s.Error != "Claude declined to answer" || s.Result != nil {
t.Fatalf("a failed evaluation = %+v", s)
}
}