mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-21 02:05:26 +00:00
net/http sets RequestURI on the server side only, and the adaptor that hands a request to the router reads exactly that field — so a request CONSTRUCTED in process arrives as "/" and is answered 404 whatever it asked for. Every request crossing ZAP is built that way, by the forward terminal and by the gateway, so the whole plane answered 404 to everything. One wrap, at both installers, and the bridge test says the requirement at its own call site. And StreamSent now means delivered. Bytes leave an AnthropicWriter at the flush, not at the write: with a buffered destination — which is what the stream callback hands in — a Write returning n > 0 has reached the buffer and nothing else. Read as delivery it tells the failover loop an answer reached a client it never reached, so the loop stops routing around a first-byte failure it could have survived. The flush error was dropped as well, which reported a connection that broke as a success. A flush that moves part of the buffer still counts as delivery, because what stays buffered afterwards is what did not go out. controllers, routers and the root package are all green.
139 lines
4.7 KiB
Go
139 lines
4.7 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 controllers
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
zaplib "github.com/luxfi/zap"
|
|
"github.com/luxfi/zap/forward"
|
|
"github.com/zap-proto/fiber/v3/middleware/adaptor"
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// healthHandler builds the same kind of surface ai bridges in
|
|
// production — a zip app behind fiber's net/http adaptor, exactly what
|
|
// Handler() hands the bridge — but
|
|
// scoped to the one always-present, auth-exempt route used here: GET
|
|
// /v1/health → ApiController.Health → 200 + {"status":"ok",...}. Using the
|
|
// real ControllerRegister + real controller method proves the bridge drives
|
|
// the router routing and controller dispatch over ZAP, without booting ai's full
|
|
// DB/adapter/filter stack (the production handle is the same type).
|
|
func healthHandler() http.Handler {
|
|
app := zip.New(zip.Config{DisableStartupMessage: true})
|
|
app.Get("/v1/health", func(c *zip.Ctx) error {
|
|
(&ApiController{Ctx: c}).Health()
|
|
return nil
|
|
})
|
|
|
|
// Through target, because the terminal builds its request in this process and the
|
|
// adaptor reads the request line — the same wrap InitForwardBridge applies.
|
|
return target(http.HandlerFunc(adaptor.FiberApp(app.Fiber())))
|
|
}
|
|
|
|
// TestForwardBridgeServesControllerHandler stands up two live ZAP nodes, registers
|
|
// the canonical HTTP-over-ZAP terminal (forward.Serve) on the backend with a
|
|
// real controller handler, and asserts GET /v1/health round-trips through the
|
|
// bridge with status 200 and a JSON body. This exercises exactly what
|
|
// InitForwardBridge wires in cmd/aid/main.go.
|
|
func TestForwardBridgeServesControllerHandler(t *testing.T) {
|
|
gwPort := freePort(t)
|
|
bePort := freePort(t)
|
|
|
|
gw := zaplib.NewNode(zaplib.NodeConfig{NodeID: "gateway-test", Port: gwPort, NoDiscovery: true})
|
|
be := zaplib.NewNode(zaplib.NodeConfig{NodeID: "ai-test", Port: bePort, NoDiscovery: true})
|
|
if err := gw.Start(); err != nil {
|
|
t.Fatalf("gateway node start: %v", err)
|
|
}
|
|
defer gw.Stop()
|
|
if err := be.Start(); err != nil {
|
|
t.Fatalf("backend node start: %v", err)
|
|
}
|
|
defer be.Stop()
|
|
|
|
// Register the Forward terminal on the backend node — the same call
|
|
// InitForwardBridge makes, with the same kind of the router http.Handler.
|
|
forward.Serve(be, healthHandler())
|
|
|
|
// Gateway dials the backend, then waits until the peer is visible.
|
|
if err := gw.ConnectDirect("127.0.0.1:" + strconv.Itoa(bePort)); err != nil {
|
|
t.Fatalf("gateway dial backend: %v", err)
|
|
}
|
|
waitForPeer(t, gw, "ai-test")
|
|
|
|
fwd := &forward.Forwarder{Node: gw, Peer: "ai-test"}
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, "http://ai-test/v1/health", nil)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
resp, err := fwd.RoundTrip(req.WithContext(ctx))
|
|
if err != nil {
|
|
t.Fatalf("RoundTrip /v1/health: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status: got %d want %d", resp.StatusCode, http.StatusOK)
|
|
}
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
// ApiController.Health -> ResponseOk -> {"status":"ok","msg":"",...}.
|
|
if !strings.Contains(string(body), `"status":"ok"`) {
|
|
t.Fatalf("body did not carry the health payload through the bridge: %q", body)
|
|
}
|
|
}
|
|
|
|
// TestZapWiringNoNode asserts both node-registration entry points are safe no-ops
|
|
// when ZAP is off (object.GetZapNode() == nil) — neither may panic and ai's HTTP
|
|
// behavior is unchanged. ZAP_ENABLED is unset in the test environment, so the node
|
|
// is never created.
|
|
func TestZapWiringNoNode(t *testing.T) {
|
|
// healthHandler() is non-nil; both guards must short-circuit on the nil node.
|
|
InitForwardBridge(healthHandler())
|
|
InitZapHandlers(healthHandler())
|
|
}
|
|
|
|
func freePort(t *testing.T) int {
|
|
t.Helper()
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("reserve free port: %v", err)
|
|
}
|
|
defer l.Close()
|
|
return l.Addr().(*net.TCPAddr).Port
|
|
}
|
|
|
|
func waitForPeer(t *testing.T, n *zaplib.Node, peer string) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
for _, p := range n.Peers() {
|
|
if p == peer {
|
|
return
|
|
}
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
t.Fatalf("peer %q not connected within deadline (peers=%v)", peer, n.Peers())
|
|
}
|