Files
archie/internal/infrastructure/sessioncurator/adapter_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

85 lines
2.6 KiB
Go

package sessioncurator
import (
"context"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/gateway"
)
func newTestStore(t *testing.T) gateway.SessionStore {
t.Helper()
s := gateway.NewSessionStoreMemory()
t.Cleanup(func() { _ = s.Close() })
return s
}
func TestAdapterRecentSessionsFiltersByActivity(t *testing.T) {
t.Parallel()
store := newTestStore(t)
ctx := context.Background()
old := gateway.SessionContext{SessionID: "old", LastActiveAt: time.Unix(100, 0)}
recent := gateway.SessionContext{SessionID: "recent", LastActiveAt: time.Unix(2000, 0)}
if err := store.Save(ctx, old); err != nil {
t.Fatalf("Save(old) = %v", err)
}
if err := store.Save(ctx, recent); err != nil {
t.Fatalf("Save(recent) = %v", err)
}
a := NewAdapter(store)
got, err := a.RecentSessions(ctx, time.Unix(1000, 0))
if err != nil {
t.Fatalf("RecentSessions() = %v, want nil", err)
}
if len(got) != 1 || got[0].ID != "recent" {
t.Fatalf("RecentSessions() = %v, want only 'recent'", got)
}
}
func TestAdapterMessagesReadsRoleFromRecords(t *testing.T) {
t.Parallel()
store := newTestStore(t)
ctx := context.Background()
sess := gateway.SessionContext{SessionID: "s1", LastActiveAt: time.Unix(1000, 0)}
if err := store.Save(ctx, sess); err != nil {
t.Fatalf("Save() = %v", err)
}
// Roles are set by whoever produced the message; the adapter reads them
// from the records, needing no bot identity of its own.
for _, m := range []messaging.Message{
{Sender: "user123", Role: messaging.RoleUser, Text: "hello", At: time.Unix(1, 0)},
{Sender: "archie", Role: messaging.RoleAssistant, Text: "hi there", At: time.Unix(2, 0)},
} {
if err := store.SaveMessage(ctx, "s1", m); err != nil {
t.Fatalf("SaveMessage(%v) = %v", m, err)
}
}
// A hand-built record without a role reads as a user message.
if err := store.SaveMessage(ctx, "s1", messaging.Message{Text: "bare", At: time.Unix(3, 0)}); err != nil {
t.Fatalf("SaveMessage(bare) = %v", err)
}
a := NewAdapter(store)
got, err := a.Messages(ctx, "s1", 10)
if err != nil {
t.Fatalf("Messages() = %v, want nil", err)
}
if len(got) != 3 {
t.Fatalf("Messages() = %v, want 3", got)
}
if got[0].Role != "user" || got[0].Content != "hello" {
t.Errorf("Messages()[0] = %+v, want user/hello", got[0])
}
if got[1].Role != "assistant" || got[1].Content != "hi there" {
t.Errorf("Messages()[1] = %+v, want assistant/hi there", got[1])
}
if got[2].Role != "user" || got[2].Content != "bare" {
t.Errorf("Messages()[2] = %+v, want user/bare", got[2])
}
}