package handlers import ( "context" "encoding/json" "os" "path/filepath" "strings" "testing" "time" "github.com/gin-gonic/gin" "adam-french.co.uk/usaco-site/internal/claude" "adam-french.co.uk/usaco-site/internal/explain" "adam-french.co.uk/usaco-site/internal/statement" ) // fakeClaude answers every request with one observation. If gate is set, // it waits for it to close first, so a test can catch a job mid-run. type fakeClaude struct { gate chan struct{} user string } func (f *fakeClaude) Model() string { return "fake-model" } func (f *fakeClaude) JSON(_ context.Context, req claude.Request, out any) error { if f.gate != nil { <-f.gate } f.user = req.User *out.(*explain.Result) = explain.Result{Observation: "Think in reverse."} return nil } // fakeJudge knows one link. type fakeJudge struct{} func (fakeJudge) Fetch(_ context.Context, link string) (string, error) { if link == "https://usaco.org/moocast" { return "# Moocast\n\nCows with walkie-talkies.", nil } return "", statement.ErrUnsupported } func waitJob(t *testing.T, r *gin.Engine, name string) explain.Status { t.Helper() for range 400 { rec := do(r, "GET", "/usaco/api/problems/"+name+"/explain", "", true) var s explain.Status json.Unmarshal(rec.Body.Bytes(), &s) if s.State != explain.Running { return s } time.Sleep(5 * time.Millisecond) } t.Fatal("job still running") return explain.Status{} } func TestExplainNeedsClaudeAndAStatement(t *testing.T) { r, repo, _ := setup(t) if rec := do(r, "POST", "/usaco/api/problems/moocast/explain", `{}`, true); rec.Code != 503 { t.Fatalf("no Claude = %d, want 503", rec.Code) } repo.srv.Claude = &fakeClaude{} if rec := do(r, "POST", "/usaco/api/problems/moocast/explain", `{}`, true); rec.Code != 400 { t.Fatalf("no statement or link = %d, want 400: %s", rec.Code, rec.Body) } if rec := do(r, "POST", "/usaco/api/problems/moocast/explain", `{}`, false); rec.Code != 401 { t.Fatalf("public explain = %d, want 401", rec.Code) } } func TestExplainWritesAndCommits(t *testing.T) { r, repo, _ := setup(t) fc := &fakeClaude{gate: make(chan struct{})} repo.srv.Claude = fc if rec := do(r, "PUT", "/usaco/api/problems/moocast/meta", `{"statement":"# Moocast\nsecret statement"}`, true); rec.Code != 200 { t.Fatalf("saving a statement = %d: %s", rec.Code, rec.Body) } if rec := do(r, "POST", "/usaco/api/problems/moocast/explain", `{"note":"stuck on order","includeCode":true}`, true); rec.Code != 202 { t.Fatalf("start = %d: %s", rec.Code, rec.Body) } if rec := do(r, "POST", "/usaco/api/problems/moocast/explain", `{}`, true); rec.Code != 409 { t.Fatalf("second start while running = %d, want 409", rec.Code) } close(fc.gate) if s := waitJob(t, r, "moocast"); s.State != explain.Done { t.Fatalf("job = %+v, want done", s) } for _, want := range []string{"secret statement", "stuck on order", ""} { if !strings.Contains(fc.user, want) { t.Errorf("prompt is missing %q:\n%s", want, fc.user) } } b, err := os.ReadFile(filepath.Join(repo.dir, "solved/moocast/explain.md")) if err != nil || !strings.Contains(string(b), "## Key observation\n\nThink in reverse.") { t.Fatalf("explain.md = %q, %v", b, err) } if last := repo.msg[len(repo.msg)-1]; last != "Explain moocast" { t.Errorf("commit subject = %q", last) } // The public sees the explanation of a solved problem, not the statement. var pub struct{ Statement, Explanation string } json.Unmarshal(do(r, "GET", "/usaco/api/problems/moocast", "", false).Body.Bytes(), &pub) if pub.Statement != "" || !strings.Contains(pub.Explanation, "Think in reverse.") { t.Errorf("public view = %+v", pub) } var adm struct{ Statement string } json.Unmarshal(do(r, "GET", "/usaco/api/problems/moocast", "", true).Body.Bytes(), &adm) if !strings.Contains(adm.Statement, "secret statement") { t.Errorf("admin should see the statement, got %q", adm.Statement) } } func TestExplainFetchesAMissingStatement(t *testing.T) { r, repo, _ := setup(t) repo.srv.Claude = &fakeClaude{} repo.srv.Statements = fakeJudge{} do(r, "PUT", "/usaco/api/problems/watermelon/meta", `{"url":"https://cses.fi/x"}`, true) do(r, "POST", "/usaco/api/problems/watermelon/explain", `{}`, true) if s := waitJob(t, r, "watermelon"); s.State != explain.Failed || !strings.Contains(s.Error, "paste it") { t.Fatalf("unfetchable statement: job = %+v, want an error asking to paste it", s) } do(r, "PUT", "/usaco/api/problems/watermelon/meta", `{"url":"https://usaco.org/moocast"}`, true) do(r, "POST", "/usaco/api/problems/watermelon/explain", `{}`, true) if s := waitJob(t, r, "watermelon"); s.State != explain.Done { t.Fatalf("job = %+v, want done", s) } if b, _ := os.ReadFile(filepath.Join(repo.dir, "solving/watermelon/problem.md")); !strings.Contains(string(b), "walkie-talkies") { t.Errorf("fetched statement not saved: %q", b) } } func TestFetchStatement(t *testing.T) { r, repo, _ := setup(t) repo.srv.Statements = fakeJudge{} if rec := do(r, "GET", "/usaco/api/problems/moocast/statement/fetch", "", true); rec.Code != 400 { t.Fatalf("no link = %d, want 400", rec.Code) } if rec := do(r, "GET", "/usaco/api/problems/moocast/statement/fetch?url=https://cses.fi/x", "", true); rec.Code != 422 { t.Fatalf("unsupported judge = %d, want 422", rec.Code) } rec := do(r, "GET", "/usaco/api/problems/moocast/statement/fetch?url=https://usaco.org/moocast", "", true) if rec.Code != 200 || !strings.Contains(rec.Body.String(), "walkie-talkies") { t.Fatalf("fetch = %d %s", rec.Code, rec.Body) } if _, err := os.Stat(filepath.Join(repo.dir, "solved/moocast/problem.md")); !os.IsNotExist(err) { t.Error("fetching must not save anything") } if rec := do(r, "GET", "/usaco/api/problems/moocast/statement/fetch?url=https://usaco.org/moocast", "", false); rec.Code != 401 { t.Fatalf("public fetch = %d, want 401", rec.Code) } }