- 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]>
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
// Package docs_test checks the hand-written documentation against what it
|
|
// describes, so the two cannot drift apart unnoticed.
|
|
package docs_test
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"adam-french.co.uk/usaco-site/internal/handlers"
|
|
)
|
|
|
|
// README.md's route table lists every API route, and nothing else.
|
|
func TestReadmeRoutes(t *testing.T) {
|
|
b, err := os.ReadFile("README.md")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
row := regexp.MustCompile("(?m)^\\| `(GET|POST|PUT|DELETE) /usaco/api([^`?]*)")
|
|
param := regexp.MustCompile(`<([a-z]+)>`)
|
|
documented := map[string]bool{}
|
|
for _, m := range row.FindAllStringSubmatch(string(b), -1) {
|
|
documented[m[1]+" "+param.ReplaceAllString(m[2], ":$1")] = true
|
|
}
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
(&handlers.Server{}).Routes(r.Group("/usaco/api"))
|
|
served := map[string]bool{}
|
|
for _, rt := range r.Routes() {
|
|
served[rt.Method+" "+strings.TrimPrefix(rt.Path, "/usaco/api")] = true
|
|
}
|
|
|
|
for _, missing := range diff(served, documented) {
|
|
t.Errorf("README.md's route table is missing %s", missing)
|
|
}
|
|
for _, stale := range diff(documented, served) {
|
|
t.Errorf("README.md documents %s, which is not served", stale)
|
|
}
|
|
}
|
|
|
|
// The CLI page (web/src/views/CliPage.vue) is a condensed copy of the root
|
|
// README's commands, since the image's build context cannot reach the
|
|
// README. Every command there must be on the page. CI exports only site/,
|
|
// so there this is skipped; `make check` in a full checkout runs it.
|
|
func TestCliPageCoversTheCommands(t *testing.T) {
|
|
readme, err := os.ReadFile("../README.md")
|
|
if os.IsNotExist(err) {
|
|
t.Skip("no root README (CI builds site/ alone)")
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
page, err := os.ReadFile("web/src/views/CliPage.vue")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Commands the page gives as another one's alias.
|
|
aliases := map[string]string{"vim": "(also vim, edit)"}
|
|
for _, m := range regexp.MustCompile(`(?m)^\./usaco ([a-z]+)`).FindAllStringSubmatch(string(readme), -1) {
|
|
cmd := m[1]
|
|
want := "usaco " + cmd
|
|
if a, ok := aliases[cmd]; ok {
|
|
want = a
|
|
}
|
|
if !strings.Contains(string(page), want) {
|
|
t.Errorf("CliPage.vue does not mention %q (README.md has ./usaco %s)", want, cmd)
|
|
}
|
|
}
|
|
}
|
|
|
|
// diff is the keys of a that are not in b, sorted.
|
|
func diff(a, b map[string]bool) []string {
|
|
var out []string
|
|
for k := range a {
|
|
if !b[k] {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|