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{
"\n# Moocast\n\nCows.\n",
"\n\n1\n\n",
"\n1| int main() {\n2| return 0;\n3| }\n",
"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",
"\n\n2\n\n\n4\n\n\n5\n\n",
"\n(too big to show)\n",
"\noverflow?\n",
} {
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{
"\n1| class Solution {};\n",
"\nThe tests were not run: the code runner is not configured.\n",
} {
if !strings.Contains(p, want) {
t.Errorf("prompt is missing %q:\n%s", want, p)
}
}
if strings.Contains(p, "") {
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)
}
}
}