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

73 lines
2.1 KiB
Go

package usage
import (
"errors"
"math"
"os"
"path/filepath"
"testing"
"time"
)
func near(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
func TestEstimate(t *testing.T) {
e := Entry{Model: "claude-opus-5-5", In: 1_000_000, CacheRead: 1_000_000, CacheWrite: 1_000_000, Out: 1_000_000}
e.Estimate()
if want := 4 + 0.20 + 5 + 20.0; !near(e.Cost, want) {
t.Fatalf("opus 5.5 = %v, want %v", e.Cost, want)
}
// The longest prefix wins: claude-opus-5-5 is not claude-opus-5.
if p, ok := PriceOf("claude-opus-5"); !ok || p.In != 5 {
t.Fatalf("opus 5 = %+v %v", p, ok)
}
if p, ok := PriceOf("claude-something-new"); ok || p != unknown {
t.Fatalf("unknown = %+v %v", p, ok)
}
}
func TestBudgetAndSummary(t *testing.T) {
path := filepath.Join(t.TempDir(), "usage.jsonl")
l, err := Open(path, 1)
if err != nil {
t.Fatal(err)
}
now := time.Date(2026, 10, 23, 12, 0, 0, 0, time.UTC)
last := time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC)
// $0.40 output each on Opus 5.5.
l.Record(Entry{Time: last, Kind: "explain", Model: "claude-opus-5-5", Out: 20_000})
l.Record(Entry{Time: now, Kind: "explain", Model: "claude-opus-5-5", Out: 20_000})
if err := l.Allow(now); err != nil {
t.Fatalf("$0.40 of $1 refused: %v", err)
}
l.Record(Entry{Time: now, Kind: "review", Model: "claude-opus-5-5", Out: 40_000})
if err := l.Allow(now); !errors.Is(err, ErrOverBudget) {
t.Fatalf("$1.20 of $1 = %v", err)
}
// Reopened from disk, with a torn last line.
f, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0)
f.WriteString(`{"time":"2026-10`)
f.Close()
l, err = Open(path, 0)
if err != nil {
t.Fatal(err)
}
if err := l.Allow(now); err != nil {
t.Fatalf("no budget refused: %v", err)
}
s := l.Summarize(now)
if !s.Tracked || !near(s.Month, 1.2) || len(s.Months) != 2 || s.Months[0].Name != "2026-10" || s.Months[0].Calls != 2 {
t.Fatalf("summary = %+v", s)
}
if len(s.Kinds) != 2 || s.Kinds[0].Name != "review" {
t.Fatalf("kinds = %+v", s.Kinds)
}
var none *Ledger
none.Record(Entry{Model: "claude-opus-5-5"})
if none.Allow(now) != nil || none.Summarize(now).Tracked {
t.Fatal("a nil ledger should allow everything and track nothing")
}
}