Files
commerce/mount_equivalence_test.go
zeekay 73b7290e7f square: one webhook route and one verifier; secrets come from the host's KMS
Square keeps an event's created_at across every retry, and the receiver
refused anything older than five minutes, so every retry of an event
first sent while cloud was down (Recreate, ten rollouts a day) was refused
until Square gave up. The verifier now lives in payment/providers/square
and needs only the key and the URL; thirdparty/square is the API client
and no longer registers itself or verifies anything, so every registry
holds one Square type.

Replay is stopped by the event id instead: each verified event is
recorded once for the whole deployment, keyed on the verifying processor
and the event id with an insert-if-absent swap, so a repeat is a
duplicate under any path or org. The path names the one processor that
verifies, and only one that holds its webhook secret may: an unknown name,
or a verifier that needs no secret, is refused rather than tried.
The books an event lands in are the verifying account's, else the
deployment's brand — never a header or session, which on a replay are the
replayer's. An effect that did not land (a store or the ledger did not
answer) leaves the delivery open with a 503, so the provider redelivers;
it used to be acknowledged and never retried. A settlement reads the
payment index before the org's books, because an in-session charge's
receipt is in the payer's org.

POST /v1/commerce/webhooks/:provider is declared once, in setupRoutes, so
standalone and embedded commerce serve the same address; the
/v1/billing/webhooks spelling is gone.

thirdparty/kms read /v1/kms/orgs/<org>/secrets/..., which cloud no longer
serves, and was off in production anyway, so per-org payment credentials
never loaded. It is deleted. secrets.Org reads orgs/<org>/<name> through
the host's reader, the caller re-pointed at that org with zip.ActingAs so
the KMS tenancy check admits it, and payment.Hydrate loads an org's
credentials where the org receives the money: storefront checkout,
storefront subscriptions and wire, and the brand's bank for the billing
wire rail. Hanzo's own billing no longer reads the paying org's
credentials; it charges the deployment's account, as it did. The
standalone binary reads through hanzoai/kms/sdk/go.
2026-10-19 09:12:56 -07:00

144 lines
5.7 KiB
Go

// Copyright (c) 2014-present Hanzo AI, Inc.
// Licensed under MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.
package commerce
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"
luxlog "github.com/luxfi/log"
"github.com/zap-proto/zip"
)
// TestMount_EquivalentToStandalone proves the cloud mount and the standalone
// boot serve the SAME response on /v1/commerce/*.
//
// It used to compare a gin engine against that same engine fronted by
// zip.AdaptNetHTTP — a test that the adapter was transparent. Both halves of
// that premise are gone: v1.48.0 removed gin from the serving path, and the
// mount now registers commerce's handlers directly on the host's zip.App. So
// the question is no longer "does the adapter distort the response" but the one
// that still matters and always did: does a request get a different answer
// depending on whether commerce is hosted or standalone.
//
// Both sides are native zip. The standalone side builds its own app; the cloud
// side registers onto a host app. Same handlers, same middleware chain, so any
// divergence is a real routing or resolution difference — which is exactly what
// this is here to catch.
//
// The /healthz asymmetry is by design (HIP-0106): cloud surfaces are scoped to
// /v1/commerce so one binary can host iam, base, kms and gateway side by side
// without root-path collisions. Probes use /v1/commerce/health.
//
// It asks that of REGISTERED routes. It used to ask it of /v1/commerce/tenant
// and /v1/commerce/health, neither of which has been a route since org-as-tenant
// became GET /v1/commerce/org — so both modes 404'd and the test was comparing
// which catch-all rendered the 404, not what commerce answers. Standalone has a
// SPA fallback and the mount deliberately has none (the host owns unmatched
// paths, or commerce would swallow every other subsystem's misses), so those two
// bodies can never match and the comparison could only ever fail. Status parity
// on a miss is the invariant that survives; body parity belongs to routes that
// exist.
func TestMount_EquivalentToStandalone(t *testing.T) {
tmp := t.TempDir()
// Standalone boot — commerce builds and owns its own app.
standalone, err := Embed(context.Background(), EmbedConfig{
DataDir: filepath.Join(tmp, "standalone"),
HTTPAddr: "127.0.0.1:0",
Dev: true,
})
if err != nil {
t.Fatalf("standalone Embed: %v", err)
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = standalone.Stop(ctx)
})
// Cloud boot — a host zip.App with commerce mounted onto it.
zapp := zip.New(zip.Config{Logger: luxlog.New("test")})
if err := Mount(zapp, filepath.Join(tmp, "cloud"), luxlog.New("test"), nil); err != nil {
t.Fatalf("Mount: %v", err)
}
cases := []struct {
name string
path string
host string
// body says whether the two modes must agree on the bytes as well as
// the status. False for a path neither mode registers: the answer then
// comes from whoever owns unmatched paths, which is commerce standalone
// and the HOST when mounted — different renderers by design.
body bool
}{
// The org itself, resolved through the same host->brand->org resolver.
{name: "org_resolved", path: "/v1/commerce/org", host: "pay.example.test", body: true},
// The public catalog projection, same handler both modes.
{name: "catalog", path: "/v1/commerce/catalog", host: "pay.example.test", body: true},
// Nothing registers this. Both must still MISS — a path that resolves in
// one mode and not the other is the drift worth catching. It used to be
// /v1/commerce/health, which Mount now registers as the scoped probe, so
// the case was measuring the probe rather than a miss.
{name: "unregistered", path: "/v1/commerce/nonesuch", host: "pay.example.test"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reqStandalone := httptest.NewRequest(http.MethodGet, tc.path, nil)
reqStandalone.Host = tc.host
respStandalone, err := standalone.Zip().Test(reqStandalone)
if err != nil {
t.Fatalf("standalone Fiber Test: %v", err)
}
standaloneBody, _ := io.ReadAll(respStandalone.Body)
reqCloud := httptest.NewRequest(http.MethodGet, tc.path, nil)
reqCloud.Host = tc.host
respCloud, err := zapp.Test(reqCloud)
if err != nil {
t.Fatalf("cloud Fiber Test: %v", err)
}
cloudBody, _ := io.ReadAll(respCloud.Body)
if respStandalone.StatusCode != respCloud.StatusCode {
t.Fatalf("status drift on %s: standalone=%d cloud=%d standaloneBody=%q cloudBody=%q",
tc.path, respStandalone.StatusCode, respCloud.StatusCode, string(standaloneBody), string(cloudBody))
}
if tc.body && !bytes.Equal(standaloneBody, cloudBody) {
t.Fatalf("body drift on %s:\n standalone: %q\n cloud: %q", tc.path, string(standaloneBody), string(cloudBody))
}
})
}
}
// The mount serves its own health probe natively, on the commerce-scoped path.
// A probe that targeted root /healthz would answer for the whole binary, not
// for this subsystem.
func TestMount_ServesScopedHealthz(t *testing.T) {
zapp := zip.New(zip.Config{Logger: luxlog.New("test")})
if err := Mount(zapp, t.TempDir(), luxlog.New("test"), nil); err != nil {
t.Fatalf("Mount: %v", err)
}
resp, err := zapp.Test(httptest.NewRequest(http.MethodGet, "/v1/commerce/health", nil))
if err != nil {
t.Fatalf("Fiber Test: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("/v1/commerce/health = %d, want 200", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
if !bytes.Contains(body, []byte(`"service":"commerce"`)) {
t.Fatalf("healthz body = %q, want it to name the subsystem", string(body))
}
}