- internal/jobs replaces explain.Jobs: one registry for every kind of background job, with records in DATA_DIR/jobs. A job's draft is written before its commit, so a failed commit can be saved again (POST /jobs/<kind>/<key>/save) and a restart mid-commit resumes it, without paying Claude twice. - Graceful shutdown: SIGTERM stops new jobs and waits SHUTDOWN_GRACE for running ones while still serving the pages that poll them. - internal/usage records every Claude call's tokens and estimated cost in DATA_DIR/usage.jsonl; CLAUDE_MONTHLY_BUDGET refuses new jobs (429) once a month reaches it. The stats page shows the admin the spend. - auth.SameOrigin refuses cross-origin writes to admin routes. - The LeetCode importer runs the clone's script without the server's secrets in its environment. - A sync that leaves HEAD where it was skips the re-index. - docs_test.go checks README's route table against the routes, and the CLI page against the root README's commands. - The repo is going private: Gitea links on the site are admin-only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
253 lines
7.7 KiB
Go
253 lines
7.7 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// run is git in dir, failing the test on error.
|
|
func run(t *testing.T, dir string, args ...string) 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")
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("git %v: %v\n%s", args, err, out)
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
|
|
// remote makes a bare repo with one commit on main, and a second clone of it
|
|
// ("the laptop") for pushing from outside.
|
|
func remote(t *testing.T) (bare, laptop string) {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
bare = filepath.Join(root, "remote.git")
|
|
laptop = filepath.Join(root, "laptop")
|
|
run(t, root, "init", "--quiet", "--bare", "--initial-branch=main", bare)
|
|
run(t, root, "clone", "--quiet", bare, laptop)
|
|
os.MkdirAll(filepath.Join(laptop, "solving", "moocast"), 0o755)
|
|
os.WriteFile(filepath.Join(laptop, "solving", "moocast", "main.cpp"), []byte("int main(){}\n"), 0o644)
|
|
run(t, laptop, "add", "-A")
|
|
run(t, laptop, "commit", "--quiet", "-m", "first")
|
|
run(t, laptop, "push", "--quiet", "origin", "HEAD:main")
|
|
return bare, laptop
|
|
}
|
|
|
|
func newRepo(t *testing.T, bare string, push bool) (*Repo, *int) {
|
|
t.Helper()
|
|
changes := 0
|
|
r, err := New(Config{
|
|
Dir: filepath.Join(t.TempDir(), "clone"), Remote: "file://" + bare, Branch: "main",
|
|
AuthorName: "site", AuthorEmail: "[email protected]", Push: push,
|
|
}, func(string) error { changes++; return nil })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return r, &changes
|
|
}
|
|
|
|
func laptopCommit(t *testing.T, laptop, file, msg string) {
|
|
t.Helper()
|
|
run(t, laptop, "pull", "--quiet", "--rebase")
|
|
os.WriteFile(filepath.Join(laptop, file), []byte(msg+"\n"), 0o644)
|
|
run(t, laptop, "add", "-A")
|
|
run(t, laptop, "commit", "--quiet", "-m", msg)
|
|
run(t, laptop, "push", "--quiet")
|
|
}
|
|
|
|
func TestSyncClonesThenFollowsRemote(t *testing.T) {
|
|
bare, laptop := remote(t)
|
|
r, changes := newRepo(t, bare, true)
|
|
ctx := context.Background()
|
|
|
|
if err := r.Sync(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(r.Dir(), "solving", "moocast", "main.cpp")); err != nil {
|
|
t.Fatal("clone is missing the problem:", err)
|
|
}
|
|
|
|
laptopCommit(t, laptop, "README.md", "from laptop")
|
|
// Stray local junk must not survive a sync: the remote always wins.
|
|
os.WriteFile(filepath.Join(r.Dir(), "junk"), []byte("x"), 0o644)
|
|
if err := r.Sync(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(r.Dir(), "README.md")); err != nil {
|
|
t.Fatal("sync did not bring in the laptop's commit")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(r.Dir(), "junk")); err == nil {
|
|
t.Fatal("sync left an untracked file behind")
|
|
}
|
|
if *changes != 2 {
|
|
t.Fatalf("onChange ran %d times, want 2", *changes)
|
|
}
|
|
|
|
// Nothing new: no rebuild.
|
|
if err := r.Sync(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if *changes != 2 {
|
|
t.Fatalf("a sync with nothing new ran onChange (%d times)", *changes)
|
|
}
|
|
}
|
|
|
|
// A rebuild that failed is tried again on the next sync, though HEAD has
|
|
// not moved since.
|
|
func TestFailedRebuildIsRetried(t *testing.T) {
|
|
bare, _ := remote(t)
|
|
fail := true
|
|
calls := 0
|
|
r, err := New(Config{Dir: filepath.Join(t.TempDir(), "clone"), Remote: "file://" + bare, Branch: "main", Push: true},
|
|
func(string) error {
|
|
calls++
|
|
if fail {
|
|
return os.ErrInvalid
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Sync(context.Background()); err == nil {
|
|
t.Fatal("the failed rebuild was not reported")
|
|
}
|
|
fail = false
|
|
if err := r.Sync(context.Background()); err != nil || calls != 2 {
|
|
t.Fatalf("retry: err %v, %d calls, want 2", err, calls)
|
|
}
|
|
}
|
|
|
|
func TestMutateCommitsAndPushes(t *testing.T) {
|
|
bare, _ := remote(t)
|
|
r, _ := newRepo(t, bare, true)
|
|
ctx := context.Background()
|
|
|
|
err := r.Mutate(ctx, "Solve moocast", func(dir string) error {
|
|
os.MkdirAll(filepath.Join(dir, "solved"), 0o755)
|
|
return os.Rename(filepath.Join(dir, "solving", "moocast"), filepath.Join(dir, "solved", "moocast"))
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := run(t, bare, "log", "-1", "--format=%s %an", "main"); got != "Solve moocast site" {
|
|
t.Fatalf("remote head = %q, want the site's commit", got)
|
|
}
|
|
if got := run(t, bare, "ls-tree", "-r", "--name-only", "main"); got != "solved/moocast/main.cpp" {
|
|
t.Fatalf("remote tree = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestMutateNoChangeMakesNoCommit(t *testing.T) {
|
|
bare, _ := remote(t)
|
|
r, _ := newRepo(t, bare, true)
|
|
before := run(t, bare, "rev-parse", "main")
|
|
if err := r.Mutate(context.Background(), "Nothing", func(string) error { return nil }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if after := run(t, bare, "rev-parse", "main"); after != before {
|
|
t.Fatal("an edit that changed nothing was committed")
|
|
}
|
|
}
|
|
|
|
func TestMutateEditErrorDiscards(t *testing.T) {
|
|
bare, _ := remote(t)
|
|
r, _ := newRepo(t, bare, true)
|
|
boom := errors.New("boom")
|
|
err := r.Mutate(context.Background(), "Broken", func(dir string) error {
|
|
os.WriteFile(filepath.Join(dir, "half-done"), []byte("x"), 0o644)
|
|
return boom
|
|
})
|
|
if !errors.Is(err, boom) {
|
|
t.Fatalf("err = %v, want the edit's own error", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(r.Dir(), "half-done")); err == nil {
|
|
t.Fatal("a failed edit left its files behind")
|
|
}
|
|
}
|
|
|
|
// An outside push between the site's sync and its push is the normal race
|
|
// with `usaco sync` on the laptop. The edit is replayed on the new tip.
|
|
func TestMutateReplaysAfterRejectedPush(t *testing.T) {
|
|
bare, laptop := remote(t)
|
|
r, _ := newRepo(t, bare, true)
|
|
ctx := context.Background()
|
|
if err := r.Sync(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
calls := 0
|
|
err := r.Mutate(ctx, "Update moocast", func(dir string) error {
|
|
calls++
|
|
if calls == 1 {
|
|
// Sneak a push in after this attempt's sync.
|
|
laptopCommit(t, laptop, "laptop.txt", "laptop wins the race")
|
|
}
|
|
return os.WriteFile(filepath.Join(dir, "solving", "moocast", "notes.md"), []byte("notes\n"), 0o644)
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 2 {
|
|
t.Fatalf("edit ran %d times, want 2 (once, then a replay)", calls)
|
|
}
|
|
log := run(t, bare, "log", "--format=%s", "main")
|
|
if !strings.HasPrefix(log, "Update moocast\nlaptop wins the race") {
|
|
t.Fatalf("remote log =\n%s\nwant the site's commit on top of the laptop's", log)
|
|
}
|
|
}
|
|
|
|
func TestMutateGivesUpAfterTwoRejections(t *testing.T) {
|
|
bare, laptop := remote(t)
|
|
r, _ := newRepo(t, bare, true)
|
|
ctx := context.Background()
|
|
if err := r.Sync(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := 0
|
|
err := r.Mutate(ctx, "Update moocast", func(dir string) error {
|
|
n++
|
|
laptopCommit(t, laptop, "laptop.txt", "race "+string(rune('0'+n)))
|
|
return os.WriteFile(filepath.Join(dir, "solving", "moocast", "notes.md"), []byte("notes\n"), 0o644)
|
|
})
|
|
if !errors.Is(err, ErrRejected) {
|
|
t.Fatalf("err = %v, want ErrRejected", err)
|
|
}
|
|
// And the clone is back on the remote's tip, without the lost commit.
|
|
if got, want := run(t, r.Dir(), "rev-parse", "HEAD"), run(t, bare, "rev-parse", "main"); got != want {
|
|
t.Fatal("clone was left on an unpushed commit")
|
|
}
|
|
}
|
|
|
|
func TestNoPushKeepsLocalCommitsAcrossSyncs(t *testing.T) {
|
|
bare, laptop := remote(t)
|
|
r, _ := newRepo(t, bare, false)
|
|
ctx := context.Background()
|
|
before := run(t, bare, "rev-parse", "main")
|
|
|
|
err := r.Mutate(ctx, "Update moocast", func(dir string) error {
|
|
return os.WriteFile(filepath.Join(dir, "solving", "moocast", "notes.md"), []byte("notes\n"), 0o644)
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if run(t, bare, "rev-parse", "main") != before {
|
|
t.Fatal("pushed with Push off")
|
|
}
|
|
laptopCommit(t, laptop, "laptop.txt", "laptop")
|
|
if err := r.Sync(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := run(t, r.Dir(), "log", "--format=%s", "-2"); got != "Update moocast\nlaptop" {
|
|
t.Fatalf("local log = %q, want the local commit rebased onto the laptop's", got)
|
|
}
|
|
}
|