mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-21 11:09:56 +00:00
routers/ and every test in it is green. That is the first real check on the filter
work, and running it found two defects and one habit worth naming.
THE HEADER CEILING WAS UNSET, and it is the twin of the body one. fasthttp's
per-connection read buffer is 4 KiB by default and that buffer caps the TOTAL
request headers, so a request whose headers do not fit is refused at the socket
before any handler — with a message naming a buffer size. Every authenticated
request here carries a signed IAM bearer and the gateway's identity set beside it,
which clears 4 KiB together. zip states the ceiling and names the seam in its own
comment; ai set BodyLimit and never set this. Found because a test signed a real
token and the request would not parse.
A FILTER IS MIDDLEWARE, so it is tested through an app. It ends in Continue(), and
Continue means "the next handler" — which a detached context does not have, so
calling one on a TestCtx panics inside fiber's Next. zip's own doc says TestCtx is
for calling ONE handler and a chain belongs to an app. probe.through does that, and
it puts each filter in the position it actually occupies, ahead of a handler.
A FIBER CTX IS POOLED. The harness first captured the Ctx the handler was reached
with and read it after the request; fiber releases its Ctx at the end, so that read
returns a recycled object with nothing of the request on it — and the tenant
attribution tests failed reporting an empty project, which reads exactly like the
filter never ran. Capture the VALUE, never the Ctx.
Two more honest corrections to the tests themselves:
- A "*" mapping claims every verb, and the live table lists them one at a time
because the router registers a handler per method. The doc-table check now
asserts the PATH is served for a "*" row, which is what "*" says.
- The probe never returns a silent zero on a failed run. It did, and a status of 0
reads as "the filter answered nothing" — which sent three assertions looking for
a bug in the filter that was in the harness.
And the tests that had nothing left to test are gone rather than adapted: the
auto-signin and static filter tests went with the filters, and the session
nil-safety test is recast onto the property that survived — no credential resolves
to nobody, and bills nobody, because the filters ask that before any controller.
194 lines
6.3 KiB
Go
194 lines
6.3 KiB
Go
// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package routers
|
|
|
|
import (
|
|
"github.com/zap-proto/zip"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A resource that does not say what it holds publishes an untyped bag.
|
|
//
|
|
// This is the property the whole file exists for: `data` used to be one sentence
|
|
// for every operation, so every generated client handed the caller an `any`. A
|
|
// new row that forgets `shape` would silently rejoin that world, and the document
|
|
// would still look full.
|
|
func TestEveryResourceSaysWhatItHolds(t *testing.T) {
|
|
for _, r := range resources {
|
|
if r.shape == nil {
|
|
t.Errorf("%s/%s has no shape: its operations can only publish the bare envelope", r.ns, r.path)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The collection answers a list of the resource, the member answers one.
|
|
//
|
|
// Asserted through Document rather than by calling the builders, because what
|
|
// matters is what a CLIENT reads — a shape threaded correctly into items() and
|
|
// then dropped on the way out would pass a narrower test.
|
|
func TestCollectionListsWhatTheMemberReturns(t *testing.T) {
|
|
doc := Document(built())
|
|
paths, _ := doc["paths"].(map[string]any)
|
|
|
|
for _, r := range resources {
|
|
if r.shape == nil || r.noList {
|
|
continue
|
|
}
|
|
name := component(reflect.TypeOf(r.shape))
|
|
want := "#/components/schemas/" + name
|
|
|
|
data := dataSchema(t, paths, openAPIPath(r.collection()), "get")
|
|
if data == nil {
|
|
t.Errorf("GET %s does not narrow data", r.collection())
|
|
continue
|
|
}
|
|
if data["type"] != "array" {
|
|
t.Errorf("GET %s answers a %v, not a list", r.collection(), data["type"])
|
|
continue
|
|
}
|
|
items, _ := data["items"].(map[string]any)
|
|
if items["$ref"] != want {
|
|
t.Errorf("GET %s lists %v, want %s", r.collection(), items["$ref"], want)
|
|
}
|
|
|
|
if !r.noRead {
|
|
one := dataSchema(t, paths, openAPIPath(r.member()), "get")
|
|
if one == nil || one["$ref"] != want {
|
|
t.Errorf("GET %s answers %v, want %s", r.member(), one, want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Every $ref the document writes must resolve to a schema it carries.
|
|
//
|
|
// A dangling $ref is the exact failure the deleted hand-authored spec left
|
|
// behind: operations pointing at component definitions that no longer existed,
|
|
// which every generator resolves to "unknown" without complaining.
|
|
func TestEveryReferenceResolves(t *testing.T) {
|
|
doc := Document(built())
|
|
comps, _ := doc["components"].(map[string]any)
|
|
schemas, _ := comps["schemas"].(map[string]any)
|
|
|
|
var walk func(any)
|
|
walk = func(v any) {
|
|
switch x := v.(type) {
|
|
case map[string]any:
|
|
if r, ok := x["$ref"].(string); ok {
|
|
const p = "#/components/schemas/"
|
|
name, found := strings.CutPrefix(r, p)
|
|
if !found {
|
|
t.Errorf("$ref %q does not point into this document", r)
|
|
} else if _, ok := schemas[name]; !ok {
|
|
t.Errorf("$ref %q resolves to nothing", r)
|
|
}
|
|
}
|
|
for _, e := range x {
|
|
walk(e)
|
|
}
|
|
case []any:
|
|
for _, e := range x {
|
|
walk(e)
|
|
}
|
|
}
|
|
}
|
|
walk(doc)
|
|
}
|
|
|
|
// dataSchema is the `data` an operation narrows the envelope to, or nil when it
|
|
// has not narrowed it.
|
|
func dataSchema(t *testing.T, paths map[string]any, path, verb string) map[string]any {
|
|
t.Helper()
|
|
item, _ := paths[path].(map[string]any)
|
|
op, _ := item[verb].(map[string]any)
|
|
resp, _ := op["responses"].(map[string]any)
|
|
ok200, _ := resp["200"].(map[string]any)
|
|
content, _ := ok200["content"].(map[string]any)
|
|
js, _ := content["application/json"].(map[string]any)
|
|
schema, _ := js["schema"].(map[string]any)
|
|
all, _ := schema["allOf"].([]any)
|
|
if len(all) != 2 {
|
|
return nil
|
|
}
|
|
narrowed, _ := all[1].(map[string]any)
|
|
props, _ := narrowed["properties"].(map[string]any)
|
|
data, _ := props["data"].(map[string]any)
|
|
return data
|
|
}
|
|
|
|
// The id a caller uses is derived by ONE rule, and this document does not own a
|
|
// copy of it.
|
|
//
|
|
// It used to: an operationID here reproduced zip.ID's encoding "rather than
|
|
// inventing a second one". A copy of a rule is a second one the moment the
|
|
// original moves, and it moved — zip stopped spelling the version every address
|
|
// shares, this copy did not, and 307 operations went out under a name no other
|
|
// app in the fleet would have given the same address. The composition did not
|
|
// refuse them, because two spellings of one name are not a duplicate.
|
|
//
|
|
// So the derivation is called, not reproduced, and this asserts the property
|
|
// that makes that worth doing: what the document publishes is what zip.ID says,
|
|
// for every address in it.
|
|
func TestOperationIDsAreZipsRuleAndNotACopy(t *testing.T) {
|
|
for path, item := range docPaths(t) {
|
|
methods, ok := item.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for verb, raw := range methods {
|
|
op, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
got, _ := op["operationId"].(string)
|
|
if want := zip.ID(verb, path); got != want {
|
|
t.Errorf("%s %s publishes %q, zip.ID says %q", verb, path, got, want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// The version every address here shares is not in the name it publishes, which
|
|
// is the half of zip's rule this document most depends on: /v1 is on all of it,
|
|
// so carrying it would spend a token on every name to say nothing.
|
|
func TestPublishedIDsDropTheSharedVersion(t *testing.T) {
|
|
for path, item := range docPaths(t) {
|
|
methods, ok := item.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, raw := range methods {
|
|
op, _ := raw.(map[string]any)
|
|
id, _ := op["operationId"].(string)
|
|
if strings.Contains(id, "_v1_") && !strings.Contains(path[3:], "/v1/") {
|
|
t.Errorf("%s publishes %q — the leading version is not part of a name", path, id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// docPaths is the published document's path map, which is what a consumer reads.
|
|
func docPaths(t *testing.T) map[string]any {
|
|
t.Helper()
|
|
doc := Document(built())
|
|
paths, ok := doc["paths"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("the document publishes no paths: %T", doc["paths"])
|
|
}
|
|
return paths
|
|
}
|