Replaced gateway.Message as the currency at the channel-facing boundary with the canonical internal/domain/messaging.Message. Channel adapters now build the record directly (RoleUser, ConversationID, Sender) and the Router hands it back unchanged, so nothing converts between two shapes in process. gateway.Message is deleted; the dashboard route that had no home in the record lives in a new Inbound envelope (Message + transport-only Page), which reaches the system prompt and is never persisted. Role is now set at construction instead of derived at the store boundary, and the assistant reply is a direct messaging.Message (RoleAssistant, Sender=botUser). The gRPC proto is unchanged: pb.Message still carries no role, and both sides keep deriving it from the owning session's bot identity via gateway.RoleForSender (ToStoredMessage/FromStoredMessage are deleted). Adds tests that the old shape could not express: Page reaches the system prompt but not the persisted record, and a channel-built Inbound round-trips with the Role the channel set. Refs: archie-core-9g5e
119 lines
3.5 KiB
Go
119 lines
3.5 KiB
Go
package gatewayrpc
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/gateway"
|
|
)
|
|
|
|
// authChat is a minimal ChatContract for the auth boundary: only Snapshot
|
|
// and Stream are exercised, the rest fall through to the nil embedded
|
|
// contract and are never called.
|
|
type authChat struct {
|
|
gateway.ChatContract
|
|
snapshot gateway.ChatSnapshot
|
|
}
|
|
|
|
func (f *authChat) Snapshot(context.Context) (gateway.ChatSnapshot, error) {
|
|
return f.snapshot, nil
|
|
}
|
|
|
|
func (f *authChat) Stream(context.Context, gateway.Inbound) (<-chan gateway.ChatEvent, error) {
|
|
ch := make(chan gateway.ChatEvent)
|
|
close(ch)
|
|
return ch, nil
|
|
}
|
|
|
|
// serveAuth serves the chat contract on a loopback listener with the token
|
|
// interceptors installed when token is non-empty, mirroring the standalone
|
|
// gateway composition. It returns the dial target.
|
|
func serveAuth(t *testing.T, token string) string {
|
|
t.Helper()
|
|
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("listen: %v", err)
|
|
}
|
|
var opts []grpc.ServerOption
|
|
if token != "" {
|
|
opts = []grpc.ServerOption{
|
|
grpc.ChainUnaryInterceptor(UnaryServerInterceptor(token)),
|
|
grpc.ChainStreamInterceptor(StreamServerInterceptor(token)),
|
|
}
|
|
}
|
|
server := grpc.NewServer(opts...)
|
|
RegisterServer(server, &authChat{})
|
|
go func() { _ = server.Serve(listener) }()
|
|
t.Cleanup(server.Stop)
|
|
return listener.Addr().String()
|
|
}
|
|
|
|
func TestGatewayTokenRoundTrip(t *testing.T) {
|
|
target := serveAuth(t, "correct-token")
|
|
client, cleanup, err := Dial(target, "correct-token")
|
|
if err != nil {
|
|
t.Fatalf("Dial with the correct token: %v", err)
|
|
}
|
|
defer cleanup()
|
|
if _, err := client.Snapshot(t.Context()); err != nil {
|
|
t.Fatalf("Snapshot with the correct token: %v", err)
|
|
}
|
|
events, err := client.Stream(t.Context(), gateway.Inbound{})
|
|
if err != nil {
|
|
t.Fatalf("Stream with the correct token: %v", err)
|
|
}
|
|
for range events {
|
|
}
|
|
}
|
|
|
|
func TestGatewayTokenMissingIsRejected(t *testing.T) {
|
|
target := serveAuth(t, "correct-token")
|
|
// A loopback target dials without a credential, so the dial itself
|
|
// succeeds; the server rejects the unauthenticated call.
|
|
client, cleanup, err := Dial(target, "")
|
|
if err != nil {
|
|
t.Fatalf("Dial to a loopback target without a token: %v", err)
|
|
}
|
|
defer cleanup()
|
|
if _, err := client.Snapshot(t.Context()); status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("Snapshot without a token: code = %v, want Unauthenticated (err = %v)", status.Code(err), err)
|
|
}
|
|
}
|
|
|
|
func TestGatewayTokenWrongIsRejected(t *testing.T) {
|
|
target := serveAuth(t, "correct-token")
|
|
client, cleanup, err := Dial(target, "wrong-token")
|
|
if err != nil {
|
|
t.Fatalf("Dial with a token: %v", err)
|
|
}
|
|
defer cleanup()
|
|
if _, err := client.Snapshot(t.Context()); status.Code(err) != codes.Unauthenticated {
|
|
t.Fatalf("Snapshot with the wrong token: code = %v, want Unauthenticated (err = %v)", status.Code(err), err)
|
|
}
|
|
}
|
|
|
|
func TestGatewayStreamWithoutTokenIsRejected(t *testing.T) {
|
|
target := serveAuth(t, "correct-token")
|
|
client, cleanup, err := Dial(target, "")
|
|
if err != nil {
|
|
t.Fatalf("Dial to a loopback target without a token: %v", err)
|
|
}
|
|
defer cleanup()
|
|
events, err := client.Stream(t.Context(), gateway.Inbound{})
|
|
if err != nil {
|
|
t.Fatalf("Stream without a token: %v", err)
|
|
}
|
|
event, ok := <-events
|
|
if !ok {
|
|
t.Fatal("Stream without a token closed with no error event")
|
|
}
|
|
if event.Kind != "error" {
|
|
t.Fatalf("Stream without a token: event kind = %q, want error", event.Kind)
|
|
}
|
|
}
|