Files
usaco/site/internal/runner/handler_test.go
T
adamfandClaude Opus 5.5 c972fa9fd9
Test and publish the site / test (push) Successful in 1m23s
Test and publish the site / publish (push) Failing after 4m22s
Run and lint solutions on the site
A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.

Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.

C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.

CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 11:24:22 +01:00

100 lines
3.2 KiB
Go

package runner
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func post(t *testing.T, h http.Handler, path, body string) *httptest.ResponseRecorder {
t.Helper()
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)))
return w
}
func TestHandlerRejects(t *testing.T) {
h := Handler(1)
tooMany := `{"lang":"cpp","source":"","tests":[` + strings.TrimSuffix(strings.Repeat(`{"name":"x"},`, 101), ",") + `]}`
for name, c := range map[string]struct {
path, body string
status int
}{
"lang": {"/run", `{"lang":"rust","source":""}`, 400},
"io slash": {"/run", `{"lang":"cpp","source":"","io":"../x"}`, 400},
"io dot": {"/run", `{"lang":"cpp","source":"","io":".hidden"}`, 400},
"file slash": {"/run", `{"lang":"cpp","source":"","files":{"a/b.h":""}}`, 400},
"file dotdot": {"/lint", `{"lang":"cpp","source":"","files":{"..":""}}`, 400},
"reserved": {"/run", `{"lang":"cpp","source":"","files":{"main":""}}`, 400},
"tests": {"/run", tooMany, 400},
"json": {"/run", `{`, 400},
"big": {"/run", `{"lang":"cpp","source":"` + strings.Repeat("x", maxBody) + `"}`, 413},
} {
w := post(t, h, c.path, c.body)
var e struct{ Error string }
json.Unmarshal(w.Body.Bytes(), &e)
if w.Code != c.status || e.Error == "" {
t.Errorf("%s: %d %s", name, w.Code, w.Body)
}
}
}
func TestHandlerRun(t *testing.T) {
need(t, "g++")
body := `{"lang":"cpp","source":` + jsonString(sumCpp) + `,"io":"sum","tests":[{"name":"1","input":"2 3","output":"5"}]}`
w := post(t, Handler(1), "/run", body)
var resp RunResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil || w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
if !resp.Compile.OK || len(resp.Results) != 1 || resp.Results[0].Verdict != Accepted {
t.Errorf("%+v", resp)
}
// A compile error is still a 200.
w = post(t, Handler(1), "/run", `{"lang":"cpp","source":"nope","tests":[]}`)
if w.Code != 200 || !strings.Contains(w.Body.String(), `"ok":false`) {
t.Errorf("%d %s", w.Code, w.Body)
}
}
func TestHandlerBusy(t *testing.T) {
s := newServer(1, 50*time.Millisecond)
s.slots <- struct{}{} // someone else's run
w := post(t, s.routes(), "/lint", `{"lang":"cpp","source":""}`)
if w.Code != http.StatusServiceUnavailable {
t.Errorf("%d %s", w.Code, w.Body)
}
}
func TestHandlerHealth(t *testing.T) {
w := httptest.NewRecorder()
Handler(1).ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/health", nil))
if w.Code != 200 || !strings.Contains(w.Body.String(), `"g++"`) {
t.Errorf("%d %s", w.Code, w.Body)
}
}
func TestClientAgainstHandler(t *testing.T) {
need(t, "g++")
ts := httptest.NewServer(Handler(1))
defer ts.Close()
c := NewClient(ts.URL)
resp, err := c.Run(context.Background(), RunRequest{Lang: Cpp, Source: sumCpp, Tests: []Test{{Name: "1", Input: "1 1", Output: str("2")}}})
if err != nil || len(resp.Results) != 1 || resp.Results[0].Verdict != Accepted {
t.Errorf("%v %+v", err, resp)
}
if _, err := c.Lint(context.Background(), LintRequest{Lang: "go"}); err == nil || !strings.Contains(err.Error(), "unknown lang") {
t.Errorf("err %v", err)
}
}
func jsonString(s string) string {
b, _ := json.Marshal(s)
return string(b)
}