Files
adamfandClaude Opus 5.5 cae5ec8e5a
Test and publish the site / test (push) Successful in 2m15s
Test and publish the site / publish (push) Successful in 9m5s
Keep site jobs across restarts, meter Claude spend, harden admin routes
- 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]>
2026-10-23 22:10:01 +01:00

91 lines
3.1 KiB
Go

package leetcode
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestDaily(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"data":{"activeDailyCodingChallengeQuestion":{"question":{"titleSlug":"two-sum"}}}}`))
}))
defer srv.Close()
slug, err := (&Importer{API: srv.URL}).Daily(context.Background())
if err != nil || slug != "two-sum" {
t.Fatalf("Daily = %q, %v; want two-sum", slug, err)
}
}
func TestDailyMissing(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"data":{"activeDailyCodingChallengeQuestion":null}}`))
}))
defer srv.Close()
if _, err := (&Importer{API: srv.URL}).Daily(context.Background()); err == nil {
t.Fatal("Daily with no question succeeded")
}
}
func TestSlug(t *testing.T) {
for _, link := range []string{
"https://leetcode.com/problems/two-sum/",
"https://leetcode.com/problems/two-sum/description/?envType=daily-question",
" https://www.leetcode.com/problems/two-sum ",
"leetcode.com/problems/two-sum/",
"two-sum",
} {
if got, err := Slug(link); err != nil || got != "two-sum" {
t.Errorf("Slug(%q) = %q, %v; want two-sum", link, got, err)
}
}
for _, link := range []string{
"",
"https://codeforces.com/problems/two-sum/",
"https://leetcode.com/contest/weekly-contest-400/",
"https://leetcode.com/problems/",
"https://leetcode.com/problems/../x/",
"https://evil.com/leetcode.com/problems/two-sum/",
} {
if got, err := Slug(link); err == nil {
t.Errorf("Slug(%q) = %q, want an error", link, got)
}
}
}
// The script's own "Error: ..." line is the message, not the exit status.
func TestImportError(t *testing.T) {
if _, err := exec.LookPath("python3"); err != nil {
t.Skip("no python3")
}
root := t.TempDir()
os.MkdirAll(filepath.Join(root, "lib"), 0o755)
script := "import sys\nprint('Error: LeetCode has no problem ' + repr(sys.argv[2]), file=sys.stderr)\nsys.exit(1)\n"
os.WriteFile(filepath.Join(root, "lib", "leetcode.py"), []byte(script), 0o644)
err := (&Importer{}).Import(context.Background(), root, "nope")
if err == nil || !strings.Contains(err.Error(), "LeetCode has no problem 'nope'") {
t.Fatalf("Import = %v, want the script's message", err)
}
}
// The script is the clone's, so it must not see the server's secrets.
func TestImportHidesSecrets(t *testing.T) {
if _, err := exec.LookPath("python3"); err != nil {
t.Skip("no python3")
}
t.Setenv("CLAUDE_API_KEY", "sk-secret")
t.Setenv("GIT_TOKEN", "gitea-secret")
root := t.TempDir()
os.MkdirAll(filepath.Join(root, "lib"), 0o755)
script := "import os, sys\nleaked = [k for k in ('CLAUDE_API_KEY', 'GIT_TOKEN') if k in os.environ]\nprint('Error: leaked ' + ','.join(leaked) if leaked else 'Error: clean', file=sys.stderr)\nsys.exit(1)\n"
os.WriteFile(filepath.Join(root, "lib", "leetcode.py"), []byte(script), 0o644)
if err := (&Importer{}).Import(context.Background(), root, "x"); err == nil || err.Error() != "clean" {
t.Fatalf("Import = %v, want the script to see no secrets", err)
}
}