mirror of
https://github.com/zap-proto/zip.git
synced 2026-10-19 15:38:57 +00:00
App, Group, Operation. A group is a prefix and what holds under it, an operation
is one typed route declared there, and the verb methods are generic methods —
which Go 1.27 allows on a concrete type and still refuses on an interface. So In
and Out come from the handler and the caller writes no type arguments:
users := app.Group("/v1/users")
users.Post("/", api.Create)
mint := users.Group("/mint").With(middleware.Mint(gate))
mint.Post("/deposit", api.Deposit)
A Group is a CLOSED HANDLE: two unexported fields, and no method that hands back
the app, the parent or the fiber underneath. A subsystem given the group at its
own prefix cannot reach above it, so confinement is a property of the type
instead of a rule something has to police — which is what a host needs in order
to hand out a bounded surface and know what comes back.
That is bought by giving up the router interface, deliberately. Router and
OpTarget are gone, and with them the ability to implement a route table outside
this package: a decorator is no longer a router, a gate is middleware on a group,
and a bound is a path. The five package-level verb functions go the same way —
a registration is a method on the thing it registers into.
Raw carries what is not a typed operation: a stream, an upgrade, a probe, a
foreign subtree. It names its method, and it is the longer spelling because
opting out of the typed model is the exception and should read like one.
Co-authored-by: Hanzo Dev <[email protected]>
73 lines
2.5 KiB
Go
73 lines
2.5 KiB
Go
package zip
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type doorIn struct{}
|
|
type doorOut struct{ OK bool }
|
|
|
|
func doorHandler(context.Context, *doorIn) (*doorOut, error) { return &doorOut{true}, nil }
|
|
|
|
// TestTestSeesTheSameSurfaceServingWould is the invariant [App.Test] claims and
|
|
// did not hold: "it builds the program if nothing is live, exactly as serving
|
|
// would".
|
|
//
|
|
// Serving calls prepare, which installs the deferred projections — /mcp, the
|
|
// OpenAPI document, the op-call plane, the plugin route. Building does not. So
|
|
// Test used to answer 404 on all four while production answered 200: a test could
|
|
// not see the surface it was written to check, and the closer it got to the real
|
|
// program the more confidently it reported the wrong thing.
|
|
//
|
|
// Callers papered over that with an exported Prepare they had to remember to
|
|
// call, which is why unexporting it broke hanzoai/cloud's MCP tests rather than
|
|
// fixing them. The projections belong to "run the program", not to the caller's
|
|
// discipline.
|
|
func TestTestSeesTheSameSurfaceServingWould(t *testing.T) {
|
|
app := New(Config{AppName: "door", DisableStartupMessage: true})
|
|
app.Post("/thing", doorHandler)
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/mcp",
|
|
strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
t.Fatal("/mcp answered 404 under Test — the door serving installs is missing, " +
|
|
"so a test cannot see the surface production exposes")
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("/mcp = %d, want 200", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
// TestTestPreparesIdempotently: prepare runs at most once, so repeated Test calls
|
|
// (the normal shape of a table test) must not reinstall the projections and
|
|
// collide on their own routes.
|
|
func TestTestPreparesIdempotently(t *testing.T) {
|
|
app := New(Config{AppName: "door2", DisableStartupMessage: true})
|
|
app.Post("/thing", doorHandler)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
req, _ := http.NewRequest(http.MethodPost, "/mcp",
|
|
strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test %d: %v", i, err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("Test %d: /mcp = %d, want 200", i, resp.StatusCode)
|
|
}
|
|
}
|
|
}
|