Files
usaco/site/internal/index/index_test.go
T
adamfandClaude Opus 5.5 63608f130c
Test and publish the site / test (push) Successful in 2m4s
Test and publish the site / publish (push) Successful in 1m44s
Review recorded explanations of a solution on the site
The admin can upload a recording of themselves explaining a problem's
solution out loud (mp3, m4a, wav, webm, ogg or flac, up to 25 MB) and
get interview-style feedback, committed as reviews/<id>.md so every
attempt is kept.

Claude can't take audio, so the recording is transcribed first by an
OpenAI-compatible endpoint (whisper-1 by default; TRANSCRIBE_API_KEY,
with TRANSCRIBE_URL for a self-hosted server). Pace, pauses, filler
words and when complexity first comes up are measured from the word
timestamps and given to Claude with the timestamped transcript, the
statement and optionally the code. The reply covers the verdict, scores
in five areas, the approach and its complexity, missed edge cases,
delivery and what to work on.

Recordings are kept for playback in an S3 bucket (Garage) when the
S3_* settings are given, and are thrown away otherwise. Reviews are
admin-only. The job runs in the background like Explain, and the
statement fetch and save-error handling are now shared between the two.

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

163 lines
5.8 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")
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")
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)
}
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)
}
}
}