Files
archie/internal/gateway/gateway_stream_test.go
T
sam 7c8f625f0e feat(gateway): flip channel boundary onto messaging.Message
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
2026-09-09 01:05:36 +10:00

89 lines
2.5 KiB
Go

package gateway
import (
"context"
"strings"
"testing"
)
// RouteStream must degrade to Route when no streaming responder is set, so
// adapters can always call it without first checking for support.
func TestRouteStreamFallsBackWhenNoStreamResponder(t *testing.T) {
r := NewRouter(nil, func(context.Context, Inbound) (string, error) {
return "blocking reply", nil
}, "telegram")
var deltas []string
got, err := r.RouteStream(context.Background(), inbound("", "hello"), DeltaFunc(func(d string) {
deltas = append(deltas, d)
}))
if err != nil {
t.Fatalf("RouteStream: %v", err)
}
if got != "blocking reply" {
t.Errorf("reply = %q, want %q", got, "blocking reply")
}
if len(deltas) != 0 {
t.Errorf("expected no deltas without a stream responder, got %v", deltas)
}
}
func TestRouteStreamStreamsFreeText(t *testing.T) {
r := NewRouter(nil, nil, "telegram")
r.LLMStream = func(_ context.Context, _ Inbound, stream TurnStream) (string, error) {
for _, d := range []string{"a", "b", "c"} {
stream.Delta(d)
}
return "abc", nil
}
var sb strings.Builder
got, err := r.RouteStream(context.Background(), inbound("", "hello"), DeltaFunc(func(d string) {
sb.WriteString(d)
}))
if err != nil {
t.Fatalf("RouteStream: %v", err)
}
if got != "abc" || sb.String() != "abc" {
t.Errorf("reply=%q deltas=%q, want both %q", got, sb.String(), "abc")
}
}
// Local commands answer from local state in one step, so they must not be
// sent to the streaming responder.
func TestRouteStreamDoesNotStreamLocalCommands(t *testing.T) {
r := NewRouter(nil, nil, "telegram")
streamed := false
r.LLMStream = func(context.Context, Inbound, TurnStream) (string, error) {
streamed = true
return "", nil
}
if _, err := r.RouteStream(context.Background(), inbound("", "/status"), DeltaFunc(func(string) {})); err != nil {
t.Fatalf("RouteStream: %v", err)
}
if streamed {
t.Error("/status must not be routed to the streaming responder")
}
}
func TestRouteStreamRejectsUnknownCommandLocally(t *testing.T) {
r := NewRouter(nil, nil, "telegram")
streamed := false
r.LLMStream = func(context.Context, Inbound, TurnStream) (string, error) {
streamed = true
return "ok", nil
}
reply, err := r.RouteStream(context.Background(), inbound("", "/commands"), DeltaFunc(func(string) {}))
if err != nil {
t.Fatalf("RouteStream: %v", err)
}
if streamed {
t.Error("unknown /commands must not reach the streaming responder")
}
if !strings.Contains(reply, "/help") {
t.Fatalf("reply = %q, want /help guidance", reply)
}
}