Add an Explain button that has Claude explain a problem
When stuck, the admin can ask Claude for an intuitive explanation of a problem. It is committed as explain.md and shown as folded sections, so the reader opens only as much as they need: the key observation, hints that get stronger, the approach, a walkthrough with an SVG diagram per step, complexity, pitfalls and a pseudocode sketch. - internal/claude: streamed call with a JSON-schema reply, high effort, the server-side refusal fallback, fixed error messages and logged token usage. CLAUDE_API_KEY and CLAUDE_MODEL (default claude-opus-5-5); without a key only this feature is off. - internal/explain: the prompt, the schema, rendering to markdown, and a per-problem job tracker. The request starts a job and the page polls, since a call outlasts nginx's 120s timeout. - internal/statement: fetches statements from LeetCode's GraphQL API and USACO's problem pages. Others (Codeforces is behind Cloudflare) are pasted. - problem.md holds the statement, editable from the meta editor and shown only to the admin. Diagrams are sanitized with DOMPurify. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package statement
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// judge serves the saved responses in testdata: LeetCode's GraphQL answer
|
||||
// for path-with-minimum-effort and USACO's page for cpid=644.
|
||||
func judge(t *testing.T) *Fetcher {
|
||||
t.Helper()
|
||||
lc, _ := os.ReadFile("testdata/leetcode.json")
|
||||
us, _ := os.ReadFile("testdata/usaco.html")
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/graphql":
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
if !strings.Contains(string(b), `"s":"path-with-minimum-effort"`) {
|
||||
w.Write([]byte(`{"data":{"question":null}}`))
|
||||
return
|
||||
}
|
||||
w.Write(lc)
|
||||
case r.URL.Query().Get("cpid") == "644":
|
||||
w.Write(us)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return &Fetcher{LeetCodeAPI: srv.URL + "/graphql", USACOBase: srv.URL}
|
||||
}
|
||||
|
||||
func TestLeetCode(t *testing.T) {
|
||||
md, err := judge(t).Fetch(context.Background(), "https://leetcode.com/problems/path-with-minimum-effort/description/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"# Path With Minimum Effort", "You are a hiker", "`heights`"} {
|
||||
if !strings.Contains(md, want) {
|
||||
t.Errorf("markdown is missing %q:\n%s", want, md)
|
||||
}
|
||||
}
|
||||
if strings.Contains(md, "<p>") {
|
||||
t.Errorf("HTML left in the markdown:\n%s", md)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeetCodeUnknownSlug(t *testing.T) {
|
||||
if _, err := judge(t).Fetch(context.Background(), "https://leetcode.com/problems/no-such-thing/"); err == nil {
|
||||
t.Fatal("want an error for a problem LeetCode does not have")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUSACO(t *testing.T) {
|
||||
md, err := judge(t).Fetch(context.Background(), "https://usaco.org/index.php?page=viewproblem2&cpid=644")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"# Problem 3. Closing the Farm", "FJ wants to temporarily close", "SAMPLE INPUT"} {
|
||||
if !strings.Contains(md, want) {
|
||||
t.Errorf("markdown is missing %q:\n%s", want, md)
|
||||
}
|
||||
}
|
||||
if strings.Contains(md, "navbar") || strings.Contains(md, "Contest has ended") {
|
||||
t.Errorf("page chrome leaked into the statement:\n%s", md)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsupported(t *testing.T) {
|
||||
for _, link := range []string{"https://cses.fi/problemset/task/1068", "not a url", ""} {
|
||||
if _, err := judge(t).Fetch(context.Background(), link); !errors.Is(err, ErrUnsupported) {
|
||||
t.Errorf("Fetch(%q) = %v, want ErrUnsupported", link, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user