Files
usaco/site/internal/index/index_test.go
T
adamfandClaude Opus 5.5 6d956f1c76 Add the website: Go server + Vue SPA in site/, with CI
A standalone container for web_server, shaped like job-tracker: serves
/usaco/ with solved problems public and solving/skipped plus edits
(status, link, notes) for the site admin. Git is the only store: the
server clones adamf/usaco from Gitea, pulls on the push webhook, and
commits edits back. CI builds the image only when site/ changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 21:51:34 +01:00

139 lines
4.5 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")
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 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)
}
}
}