- internal/jobs replaces explain.Jobs: one registry for every kind of background job, with records in DATA_DIR/jobs. A job's draft is written before its commit, so a failed commit can be saved again (POST /jobs/<kind>/<key>/save) and a restart mid-commit resumes it, without paying Claude twice. - Graceful shutdown: SIGTERM stops new jobs and waits SHUTDOWN_GRACE for running ones while still serving the pages that poll them. - internal/usage records every Claude call's tokens and estimated cost in DATA_DIR/usage.jsonl; CLAUDE_MONTHLY_BUDGET refuses new jobs (429) once a month reaches it. The stats page shows the admin the spend. - auth.SameOrigin refuses cross-origin writes to admin routes. - The LeetCode importer runs the clone's script without the server's secrets in its environment. - A sync that leaves HEAD where it was skips the re-index. - docs_test.go checks README's route table against the routes, and the CLI page against the root README's commands. - The repo is going private: Gitea links on the site are admin-only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
163 lines
5.8 KiB
Go
163 lines
5.8 KiB
Go
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/jobs"
|
|
"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) jobs.Status {
|
|
t.Helper()
|
|
for range 400 {
|
|
rec := do(r, "GET", "/usaco/api/problems/"+name+"/explain", "", true)
|
|
var s jobs.Status
|
|
json.Unmarshal(rec.Body.Bytes(), &s)
|
|
if s.State != jobs.Running {
|
|
return s
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
t.Fatal("job still running")
|
|
return jobs.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 != jobs.Done {
|
|
t.Fatalf("job = %+v, want done", s)
|
|
}
|
|
|
|
for _, want := range []string{"secret statement", "stuck on order", "<my_attempt file=\"main.cpp\">"} {
|
|
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 != jobs.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 != jobs.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)
|
|
}
|
|
}
|