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]>
147 lines
4.9 KiB
Go
147 lines
4.9 KiB
Go
package index
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fixture builds a repo the way the CLI would have, with fixed commit dates:
|
|
//
|
|
// day 1 new moocast (solving), new watermelon (solving)
|
|
// day 2 done moocast -> solved
|
|
// day 3 skip watermelon -> skipped
|
|
// day 4 add a test to moocast (must not move its solve date)
|
|
func fixture(t *testing.T) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
day := func(n int) string { return time.Date(2026, 9, n, 12, 0, 0, 0, time.UTC).Format(time.RFC3339) }
|
|
git := func(date string, args ...string) {
|
|
t.Helper()
|
|
cmd := exec.Command("git", args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(),
|
|
"GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t",
|
|
"GIT_AUTHOR_DATE="+date, "GIT_COMMITTER_DATE="+date)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
write := func(path, content string) {
|
|
t.Helper()
|
|
p := filepath.Join(dir, path)
|
|
os.MkdirAll(filepath.Dir(p), 0o755)
|
|
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
commit := func(n int, msg string) {
|
|
git(day(n), "add", "-A")
|
|
git(day(n), "commit", "--quiet", "-m", msg)
|
|
}
|
|
move := func(from, to string) {
|
|
os.MkdirAll(filepath.Join(dir, filepath.Dir(to)), 0o755)
|
|
if err := os.Rename(filepath.Join(dir, from), filepath.Join(dir, to)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
git(day(1), "init", "--quiet", "--initial-branch=main")
|
|
write("template.cpp", "// template\n")
|
|
write("solving/moocast/main.cpp", "int main() {}\n")
|
|
write("solving/moocast/moocast.in", "4\n1 2 3\n")
|
|
write("solving/moocast/sol.py", "print(1)\n")
|
|
write("solving/moocast/.url", "https://usaco.org/index.php?page=viewproblem2&cpid=668\n")
|
|
write("solving/moocast/tests/1.in", "a\n")
|
|
write("solving/moocast/tests/1.out", "b\n")
|
|
write("solving/moocast/tests/10.in", "c\n")
|
|
write("solving/moocast/tests/2.in", "d\n")
|
|
write("solving/watermelon/main.cpp", "int main() {}\n")
|
|
write("solving/watermelon/watermelon.in", "")
|
|
commit(1, "new problems")
|
|
|
|
move("solving/moocast", "solved/moocast")
|
|
commit(2, "Solve moocast")
|
|
|
|
move("solving/watermelon", "skipped/watermelon")
|
|
commit(3, "Skip watermelon")
|
|
|
|
write("solved/moocast/tests/3.in", "e\n")
|
|
write("solved/moocast/notes.md", "union find\n")
|
|
write("solved/moocast/problem.md", "# Moocast\n")
|
|
write("solved/moocast/explain.md", "## Key observation\n")
|
|
commit(4, "Add a test")
|
|
return dir
|
|
}
|
|
|
|
func TestBuild(t *testing.T) {
|
|
idx, err := Build(fixture(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(idx.Problems) != 2 {
|
|
t.Fatalf("got %d problems, want 2", len(idx.Problems))
|
|
}
|
|
|
|
m := idx.Problems["moocast"]
|
|
if m.Status != Solved || m.IO != "moocast" || m.Source != "USACO" || !m.HasNotes {
|
|
t.Fatalf("moocast summary wrong: %+v", m.Summary)
|
|
}
|
|
if m.Statement != "# Moocast\n" || m.Explanation != "## Key observation\n" || !m.HasExplanation {
|
|
t.Fatalf("moocast statement/explanation = %q / %q", m.Statement, m.Explanation)
|
|
}
|
|
if w := idx.Problems["watermelon"]; w.HasExplanation || w.Statement != "" {
|
|
t.Fatal("watermelon has no problem.md or explain.md")
|
|
}
|
|
if len(m.Files) != 2 || m.Files[0].Name != "main.cpp" || m.Files[1].Lang != "python" {
|
|
t.Fatalf("moocast files = %+v, want main.cpp then sol.py", m.Files)
|
|
}
|
|
if m.Sample == nil || m.Sample.Content != "4\n1 2 3\n" {
|
|
t.Fatalf("moocast sample = %+v", m.Sample)
|
|
}
|
|
var names []string
|
|
for _, tc := range m.TestSet {
|
|
names = append(names, tc.Name)
|
|
}
|
|
if got := len(names); got != 4 || names[0] != "1" || names[1] != "2" || names[2] != "3" || names[3] != "10" {
|
|
t.Fatalf("test order = %v, want numeric 1 2 3 10", names)
|
|
}
|
|
if m.TestSet[0].Output == nil || m.TestSet[1].Output != nil {
|
|
t.Fatal("test outputs not paired: 1 has one, 2 does not")
|
|
}
|
|
|
|
day := func(n int) time.Time { return time.Date(2026, 9, n, 12, 0, 0, 0, time.UTC) }
|
|
if m.SolvedAt == nil || !m.SolvedAt.Equal(day(2)) {
|
|
t.Fatalf("moocast solvedAt = %v, want day 2 (adding a test later does not count)", m.SolvedAt)
|
|
}
|
|
if !m.FirstSeen.Equal(day(1)) || !m.LastTouched.Equal(day(4)) {
|
|
t.Fatalf("moocast first/last = %v / %v", m.FirstSeen, m.LastTouched)
|
|
}
|
|
if len(m.History) != 3 || m.History[0].Subject != "Add a test" {
|
|
t.Fatalf("moocast history = %+v, want 3 commits newest first", m.History)
|
|
}
|
|
|
|
w := idx.Problems["watermelon"]
|
|
if w.Status != Skipped || w.SolvedAt != nil || w.Sample != nil {
|
|
t.Fatalf("watermelon = %+v, want skipped, unsolved, no sample (empty .in)", w.Summary)
|
|
}
|
|
}
|
|
|
|
func TestSource(t *testing.T) {
|
|
cases := map[string]string{
|
|
"": "",
|
|
"not a url": "",
|
|
"https://cses.fi/problemset/task/1": "CSES",
|
|
"https://www.usaco.org/index.php": "USACO",
|
|
"https://leetcode.com/problems/x/": "LeetCode",
|
|
"https://example.com/problem/1": "example.com",
|
|
}
|
|
for in, want := range cases {
|
|
if got := Source(in); got != want {
|
|
t.Errorf("Source(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|