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]>
This commit is contained in:
@@ -8,10 +8,14 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/anthropics/anthropic-sdk-go/option"
|
||||
|
||||
"adam-french.co.uk/usaco-site/internal/usage"
|
||||
)
|
||||
|
||||
// sse is a minimal streamed reply: one text block holding text.
|
||||
@@ -111,3 +115,21 @@ func TestFailures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Every answered call is in the ledger, a refusal included: it is billed.
|
||||
func TestLedger(t *testing.T) {
|
||||
l, err := usage.Open(filepath.Join(t.TempDir(), "usage.jsonl"), 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, stop := range []string{"end_turn", "refusal"} {
|
||||
c := fake(t, 200, sse(`{"a":"hi"}`, stop), nil, nil)
|
||||
c.Ledger = l
|
||||
var out map[string]any
|
||||
c.JSON(context.Background(), Request{Tag: "explain", System: "s", User: "u", Schema: map[string]any{}, MaxTokens: 10}, &out)
|
||||
}
|
||||
s := l.Summarize(time.Now())
|
||||
if len(s.Kinds) != 1 || s.Kinds[0].Name != "explain" || s.Kinds[0].Calls != 2 || s.Month <= 0 {
|
||||
t.Fatalf("summary = %+v", s)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user