Files
at-container-registry/pkg/appview/holdclient/tier_update_test.go
T
Evan JarrettandClaude Fable 5.1 0080957a21 remove the runtime test_mode switch; the testmode build tag is the only one
server.test_mode survived the build-tag refactor only to feed five
behavioral branches: the registry's fall-back to the default hold when
the user's hold is unreachable, backfill warning suppression for
external holds, the appview listener close on shutdown, the hold's
relay-crawl skip, and the hold's appview-issuer tolerance. Every one of
them is a "this is a local development build" decision, which is what
the tag already says, and local development has to build with the tag
or nothing resolves. So they read atproto.TestModeBuild now, and the
flag, SetTestMode, IsTestMode, the middleware option, the backfill
constructor parameter, the never-read field on RemoteHoldAuthorizer,
the example and template YAML lines, and the docker-compose env vars
are gone. The registry keeps the fallback as a field seeded from the
constant so the production-path tests can pin it off under the tag.

The 24 SetTestMode calls in tests were dead already: stripping them and
running the affected packages tagged changed nothing.

Tests that resolve a loopback did:web used to t.Fatal naming the tag,
which left a bare `go test ./...` permanently red in five packages.
They now live under `//go:build testmode`: whole-file constraints where
every test needs it, and sibling *_testmode_test.go files holding the
moved tests plus their fixtures where a file mixed. The harness carries
the constraint too, with its package doc in an untagged doc.go so the
package still exists without it. An untagged run compiles those tests
out and passes; make test keeps the tag and runs everything.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UwYzaG3Yy7uA8FbZ5qk3tQ
2026-09-11 11:09:44 -05:00

89 lines
2.7 KiB
Go

package holdclient
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"atcr.io/pkg/atproto"
"github.com/bluesky-social/indigo/atproto/atcrypto"
)
func TestUpdateCrewTierWithRetry_SucceedsAfterTransientFailures(t *testing.T) {
priv, err := atcrypto.GeneratePrivateKeyP256()
if err != nil {
t.Fatalf("generate key: %v", err)
}
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Fail the first two attempts, succeed on the third.
if calls.Add(1) < 3 {
http.Error(w, "temporarily unavailable", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"tierName":"bosun"}`))
}))
defer srv.Close()
err = updateCrewTierWithRetry(context.Background(), "did:web:hold", srv.URL, "did:plc:user", 1, priv, "did:web:appview")
if err != nil {
t.Fatalf("expected success after retries, got %v", err)
}
if got := calls.Load(); got != 3 {
t.Errorf("expected 3 attempts, got %d", got)
}
}
func TestUpdateCrewTierWithRetry_FailsAfterMaxAttempts(t *testing.T) {
priv, err := atcrypto.GeneratePrivateKeyP256()
if err != nil {
t.Fatalf("generate key: %v", err)
}
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
http.Error(w, "down", http.StatusServiceUnavailable)
}))
defer srv.Close()
err = updateCrewTierWithRetry(context.Background(), "did:web:hold", srv.URL, "did:plc:user", 1, priv, "did:web:appview")
if err == nil {
t.Fatal("expected error after exhausting retries")
}
if got := calls.Load(); got != int32(tierUpdateMaxAttempts) {
t.Errorf("expected %d attempts, got %d", tierUpdateMaxAttempts, got)
}
}
// Ensure the URL builder matches the expected hold endpoint, guarding against
// accidental path drift (the retry test relies on hitting the test server).
func TestUpdateCrewTierOnHold_PostsToEndpoint(t *testing.T) {
priv, err := atcrypto.GeneratePrivateKeyP256()
if err != nil {
t.Fatalf("generate key: %v", err)
}
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
if err := UpdateCrewTierOnHold(context.Background(), "did:web:hold", srv.URL, "did:plc:user", 0, priv, "did:web:appview"); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotPath != atproto.HoldUpdateCrewTier {
t.Errorf("posted to %q, want %q", gotPath, atproto.HoldUpdateCrewTier)
}
}
// The tests above cover the two helpers. UpdateCrewTierOnAllHolds — the
// function the Stripe webhook actually calls, and the one whose error decides
// whether a paid upgrade is retried or dropped — had no test at all.