Files
ai/controllers/zap_forward_test.go
zeekayandHanzo Dev 8405b1eb0d Declare operations on the group that serves them
zip's registration is a verb method on the group, so a route that is not a typed
operation says which method it answers with. The verb table that bound a mapping
string to a registrar holds the method itself now — every one of these is a raw
registration, since the handler is resolved by reflection from a controller
method name — so the table is a value rather than a set of closures.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 06:08:44 -07:00

138 lines
4.8 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"
"slices"
"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.Raw(http.MethodGet, "/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) {
if slices.Contains(n.Peers(), peer) {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("peer %q not connected within deadline (peers=%v)", peer, n.Peers())
}