Files
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

169 lines
5.9 KiB
Go

package evaluate
import (
"context"
"encoding/json"
"strings"
"testing"
"time"
"adam-french.co.uk/usaco-site/internal/claude"
"adam-french.co.uk/usaco-site/internal/explain"
"adam-french.co.uk/usaco-site/internal/runner"
)
func ptr(s string) *string { return &s }
func TestPrompt(t *testing.T) {
run := &runner.RunResponse{Compile: runner.Compile{OK: true}, Results: []runner.Result{
{Name: "1", Verdict: runner.Accepted, TimeMs: 3},
{Name: "2", Verdict: runner.WrongAnswer, TimeMs: 4, Input: "2\n", Output: "5\n", Expected: ptr("4\n")},
{Name: "big", Verdict: runner.TimeLimit, TimeMs: 2000, Message: "exceeded 2s", Input: strings.Repeat("9 ", 2000)},
}}
p := Prompt(Input{
Name: "moocast", Statement: "# Moocast\n\nCows.", Lang: runner.Cpp,
Source: "int main() {\n return 0;\n}\n",
Examples: []explain.Example{{In: "1\n", Out: "2\n"}},
Note: " overflow? ",
Run: run,
})
for _, want := range []string{
"<problem name=\"moocast\">\n# Moocast\n\nCows.\n</problem>",
"<example n=\"1\">\n<input>\n1\n</input>\n<output>\n2\n</output>",
"<code lang=\"cpp\">\n1| int main() {\n2| return 0;\n3| }\n</code>",
"1/3 passed on the saved tests:\n- test 1: AC in 3 ms\n- test 2: WA in 4 ms\n- test big: TLE in 2000 ms (exceeded 2s)\n",
"<failed_test name=\"2\" verdict=\"WA\">\n<input>\n2\n</input>\n<expected>\n4\n</expected>\n<got>\n5\n</got>\n</failed_test>",
"<failed_test name=\"big\" verdict=\"TLE\">\n(too big to show)\n</failed_test>",
"<context>\noverflow?\n</context>",
} {
if !strings.Contains(p, want) {
t.Errorf("prompt is missing %q:\n%s", want, p)
}
}
p = Prompt(Input{Name: "two-sum", Statement: "x", Lang: runner.Cpp, LeetCode: true, Source: "class Solution {};", RunNote: "The tests were not run: the code runner is not configured."})
for _, want := range []string{
"<code lang=\"cpp\" leetcode=\"true\">\n1| class Solution {};\n</code>",
"<test_run>\nThe tests were not run: the code runner is not configured.\n</test_run>",
} {
if !strings.Contains(p, want) {
t.Errorf("prompt is missing %q:\n%s", want, p)
}
}
if strings.Contains(p, "<context>") {
t.Errorf("a blank note still made a context block:\n%s", p)
}
p = Prompt(Input{Name: "x", Lang: runner.Cpp, Source: "int main( {}", Run: &runner.RunResponse{Compile: runner.Compile{Output: "main.cpp:1: error"}}})
if !strings.Contains(p, "It did not compile:\nmain.cpp:1: error\n") {
t.Errorf("a compile error is not in the prompt:\n%s", p)
}
}
func TestNumberedPadsToTheWidestLine(t *testing.T) {
got := numbered(strings.Repeat("x\n", 10))
if !strings.HasPrefix(got, " 1| x\n") || !strings.HasSuffix(got, "10| x\n") {
t.Errorf("numbered = %q", got)
}
}
func TestOutcome(t *testing.T) {
r := &runner.RunResponse{Compile: runner.Compile{OK: true}, Results: []runner.Result{{Verdict: runner.Unchecked}, {Verdict: runner.Unchecked}}}
if got := Outcome(r); got != "2 ran, unchecked" {
t.Errorf("Outcome = %q", got)
}
if got := Outcome(&runner.RunResponse{}); got != "did not compile" {
t.Errorf("Outcome = %q", got)
}
}
// fakeAsker answers with r.
type fakeAsker struct{ r Result }
func (fakeAsker) Model() string { return "fake-model" }
func (f fakeAsker) JSON(_ context.Context, _ claude.Request, out any) error {
*out.(*Result) = f.r
return nil
}
func TestGenerateCleansTheReply(t *testing.T) {
r := Result{
Verdict: "wrong",
Issues: []Issue{
{Line: 2, Severity: "error", Title: "Overflow"},
{Line: 99, Severity: "fatal", Title: "Past the end"},
{Line: 1, Severity: "info"},
},
Counterexample: Counterexample{Input: "3\n", Why: "no expected output"},
}
now := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC)
run := &runner.RunResponse{Compile: runner.Compile{OK: true}, Results: []runner.Result{{Verdict: runner.Accepted}}}
ev, err := Generate(context.Background(), fakeAsker{r}, Input{Lang: runner.Python, Source: "a\nb\n", Run: run}, now)
if err != nil {
t.Fatal(err)
}
want := []Issue{{Line: 2, Severity: "error", Title: "Overflow"}, {Line: 0, Severity: "info", Title: "Past the end"}}
if len(ev.Issues) != 2 || ev.Issues[0] != want[0] || ev.Issues[1] != want[1] {
t.Errorf("issues = %+v, want %+v", ev.Issues, want)
}
if ev.Counterexample != (Counterexample{}) {
t.Errorf("a counterexample with no expected output was kept: %+v", ev.Counterexample)
}
if ev.Lang != runner.Python || ev.Source != "a\nb\n" || ev.Model != "fake-model" || !ev.At.Equal(now) || ev.Tests != "1/1 passed" {
t.Errorf("evaluation = %+v", ev)
}
// The page reads the reply's fields at the top level, beside lang.
b, _ := json.Marshal(ev)
if !strings.Contains(string(b), `"verdict":"wrong"`) || !strings.Contains(string(b), `"lang":"python"`) {
t.Errorf("JSON = %s", b)
}
}
// Every property must be required and every object closed for structured
// outputs, and the Go struct must read every field the schema asks for.
func TestSchemaMatchesResult(t *testing.T) {
var check func(path string, s map[string]any)
check = func(path string, s map[string]any) {
if s["type"] == "array" {
check(path+"[]", s["items"].(map[string]any))
return
}
if s["type"] != "object" {
return
}
if s["additionalProperties"] != false {
t.Errorf("%s is not closed", path)
}
props := s["properties"].(map[string]any)
req := map[string]bool{}
for _, r := range s["required"].([]string) {
req[r] = true
}
for k, v := range props {
if !req[k] {
t.Errorf("%s.%s is not required", path, k)
}
check(path+"."+k, v.(map[string]any))
}
}
check("", Schema)
b, _ := json.Marshal(Result{Issues: []Issue{{}}})
var got map[string]any
json.Unmarshal(b, &got)
for k := range Schema["properties"].(map[string]any) {
if _, ok := got[k]; !ok {
t.Errorf("Result has no field for %q", k)
}
}
for v := range Verdicts {
found := false
for _, e := range Schema["properties"].(map[string]any)["verdict"].(map[string]any)["enum"].([]string) {
found = found || e == v
}
if !found {
t.Errorf("verdict %q is labelled but not in the schema", v)
}
}
}