Files
zeekay 0d72ccf5a3 lx/dex: gate gRPC transport behind the grpc build tag
Default build pulls zero gRPC code; rebuild with `-tags=grpc` to opt
into gRPC.

Decomplected via a `grpcBackend` interface in `sdk/go/client` and a
`grpcSubClient` interface in `client/go`. The default build wires a
no-op backend (`*_nogrpc.go`); the `grpc` build wires the real
protobuf-backed client (`*_grpc.go`). Public methods stay the same in
both builds — the gRPC fast path is preferred when wired, else
JSON-RPC / WebSocket.

Files gated:
- pkg/grpc/pb/lxdex_grpc.pb.go            (//go:build grpc)
- pkg/proto/engine/proto/lx_engine_grpc.pb.go (//go:build grpc)

Files refactored:
- sdk/go/client.go: //go:build grpc with stub in client_nogrpc.go
- sdk/go/client/client.go: gRPC removed from method bodies; backed by
  the grpcBackend interface (grpc_backend{,_grpc,_nogrpc}.go)
- client/go/main.go: GrpcClient + ConnectGrpc moved to grpc.go
  (//go:build grpc); nogrpc.go stubs ConnectGrpc with a clear error

Verification:
- go build ./... (default) clean across sdk/go, client/go, top-level
- go build -tags=grpc ./... clean across all three modules
- go test ./... and go test -tags=grpc ./... green
- go list -deps shows 0 google.golang.org/grpc packages in default
  builds of sdk/go/client and client/go; 62 packages with -tags=grpc

READMEs in sdk/go and client/go document the build-tag opt-in.
2026-05-16 17:54:11 -07:00

61 lines
1.5 KiB
Go

package client
import (
"testing"
)
func TestNewClient(t *testing.T) {
t.Run("default options", func(t *testing.T) {
client, err := NewClient()
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
if client == nil {
t.Fatal("NewClient() returned nil")
}
if client.jsonRPCURL != "http://localhost:8080" {
t.Errorf("Expected jsonRPCURL http://localhost:8080, got %s", client.jsonRPCURL)
}
if client.wsURL != "ws://localhost:8081" {
t.Errorf("Expected wsURL ws://localhost:8081, got %s", client.wsURL)
}
})
t.Run("with options", func(t *testing.T) {
client, err := NewClient(
WithJSONRPCURL("http://custom:9090"),
WithWebSocketURL("ws://custom:9091"),
WithGRPCURL("custom:50052"),
WithAPIKey("test-api-key"),
)
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
if client.jsonRPCURL != "http://custom:9090" {
t.Errorf("Expected jsonRPCURL http://custom:9090, got %s", client.jsonRPCURL)
}
if client.wsURL != "ws://custom:9091" {
t.Errorf("Expected wsURL ws://custom:9091, got %s", client.wsURL)
}
if client.grpcURL != "custom:50052" {
t.Errorf("Expected grpcURL custom:50052, got %s", client.grpcURL)
}
if client.apiKey != "test-api-key" {
t.Errorf("Expected apiKey test-api-key, got %s", client.apiKey)
}
})
}
func TestClientDisconnect(t *testing.T) {
client, err := NewClient()
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
// Should not panic when not connected
err = client.Disconnect()
if err != nil {
t.Errorf("Disconnect() error = %v", err)
}
}