Files
adamfandClaude Opus 5.5 ccf4e4dd74
Test and publish the site / test (push) Successful in 1m19s
Test and publish the site / publish (push) Successful in 1m31s
Explain concepts on the site, and reuse their code from the CLI
/usaco/concepts is a library of techniques Claude explained: the admin
asks by topic ("help explain fenwick trees") or pastes code they could
not follow, and gets an intuitive explanation with SVG diagrams plus a
C++ snippet in contest style. Given code, the walkthrough traces that
code and the snippet is a general version of it. Anyone can read the
library; generating, rewriting and deleting are admin-only.

Each concept is committed as concepts/<slug>/: concept.md (tags in a
header comment, then a # title), snippet.cpp (first line
"// concept: <slug>") and request.md (the question, for rewrites). The
slug is Claude's to pick, so a new concept's job is keyed by an id and
returns {slug} as its result; a taken slug gets -2, -3... rather than
overwriting. Rewriting starts from the saved question and version plus
a note on what to change. The index reads concepts into Index.Concepts,
with their date from git.

internal/concept shares explain's math and diagram rules, now split out
as explain.MathRules and explain.DiagramRules; the explain prompt is
byte-for-byte unchanged. Code fences in explanations are highlighted
with the same highlight.js setup as CodeBlock (web/src/highlight.js).

`usaco snippet` lists concepts, prints one's snippet, or pastes it into
a solution above main (or class Solution), keeping a .bak and refusing
a concept the file already has. In interactive mode it pastes into the
selection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 12:58:38 +01:00

191 lines
7.4 KiB
Go

package index
import (
"os"
"os/exec"
"path/filepath"
"strings"
"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")
write("solved/moocast/reviews/2026-09-04-090000.md", "<!-- review verdict=incorrect -->\n_Reviewed by m._\n")
write("solved/moocast/reviews/2026-09-04-100000.md", "<!-- review verdict=correct audio=moocast/2026-09-04-100000.mp3 -->\n_Reviewed by m._\n")
write("solved/moocast/reviews/notes.md", "hand-written\n")
commit(4, "Add a test")
write("concepts/fenwick-tree/concept.md", "<!-- concept tags=range-query,prefix-sums -->\n# Fenwick tree\n\n_Prefix sums with updates in O(log n)._\n\n_Explained by m on 5 September 2026._\n\n## Intuition\n\nLowbits.\n")
write("concepts/fenwick-tree/snippet.cpp", "// concept: fenwick-tree\nstruct BIT {};\n")
write("concepts/fenwick-tree/request.md", "help explain fenwick trees\n")
write("concepts/hand-made/snippet.cpp", "int f();\n")
write("concepts/empty/notes.txt", "nothing to show\n")
commit(5, "Explain concept fenwick-tree")
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)
}
wantReviews := []Review{
{ID: "notes", Content: "hand-written\n"},
{ID: "2026-09-04-100000", Verdict: "correct", Audio: "moocast/2026-09-04-100000.mp3", HasAudio: true, Content: "_Reviewed by m._\n"},
{ID: "2026-09-04-090000", Verdict: "incorrect", Content: "_Reviewed by m._\n"},
}
if m.ReviewCount != 3 || len(m.Reviews) != 3 {
t.Fatalf("moocast reviews = %+v", m.Reviews)
}
for i, r := range wantReviews {
if m.Reviews[i] != r {
t.Errorf("review %d = %+v, want %+v", i, m.Reviews[i], r)
}
}
if w := idx.Problems["watermelon"]; w.HasExplanation || w.Statement != "" || w.Reviews == nil || w.ReviewCount != 0 {
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)
}
if len(m.History) != 3 {
t.Fatalf("a concept's commit touched moocast: %+v", m.History)
}
if len(idx.Concepts) != 2 {
t.Fatalf("concepts = %v, want fenwick-tree and hand-made (empty has nothing to show)", idx.Concepts)
}
f := idx.Concepts["fenwick-tree"]
if f.Title != "Fenwick tree" || f.Summary != "Prefix sums with updates in O(log n)." || len(f.Tags) != 2 || f.Tags[1] != "prefix-sums" {
t.Fatalf("fenwick-tree = %+v", f)
}
if !strings.HasPrefix(f.Content, "_Prefix sums") || strings.Contains(f.Content, "# Fenwick") || strings.Contains(f.Content, "<!--") {
t.Fatalf("fenwick-tree content keeps its header or title: %q", f.Content)
}
if f.Snippet == nil || f.Snippet.Lang != "cpp" || f.Request != "help explain fenwick trees\n" || f.Updated == nil || !f.Updated.Equal(day(5)) {
t.Fatalf("fenwick-tree snippet/request/updated = %+v / %q / %v", f.Snippet, f.Request, f.Updated)
}
if h := idx.Concepts["hand-made"]; h.Title != "hand-made" || h.Content != "" || h.Snippet == nil || len(h.Tags) != 0 {
t.Fatalf("hand-made = %+v, want the slug as title and only a snippet", h)
}
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)
}
}
}