Slice 1 of docs/prds/memory-engine-unification.md: persist the participant, so a later curator can address agent-user memory scope. - messages gains sender_id: schema, ALTER migration (idempotent via duplicate-column detection), both inserts, and both history selects. Pre-existing rows default to empty. - curator.SessionSummary gains AgentID and ConversationMessage gains SenderID; the sessioncurator adapter fills them from the session's bot identity and the stored record. The memory contract is untouched and nothing consumes the new fields yet. Deriving a session's participant from its user-role messages, and failing closed on ambiguity, stays with the curator rewrite.
146 lines
4.6 KiB
Go
146 lines
4.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])
|
|
}
|
|
}
|
|
|
|
// TestAdapterRecentSessionsCarriesAgentID pins that a session's agent reaches
|
|
// the curator contract, so an agent-user scope can be addressed without
|
|
// recovering the agent from the session id.
|
|
func TestAdapterRecentSessionsCarriesAgentID(t *testing.T) {
|
|
t.Parallel()
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
sess := gateway.SessionContext{
|
|
SessionID: "s1",
|
|
Source: messaging.SessionSource{Platform: "telegram", BotUser: "winter", ChannelID: "chat-1"},
|
|
LastActiveAt: time.Unix(2000, 0),
|
|
}
|
|
if err := store.Save(ctx, sess); err != nil {
|
|
t.Fatalf("Save() = %v", err)
|
|
}
|
|
|
|
got, err := NewAdapter(store).RecentSessions(ctx, time.Unix(1000, 0))
|
|
if err != nil {
|
|
t.Fatalf("RecentSessions() = %v, want nil", err)
|
|
}
|
|
if len(got) != 1 || got[0].AgentID != "winter" {
|
|
t.Fatalf("RecentSessions() = %+v, want one session with AgentID winter", got)
|
|
}
|
|
}
|
|
|
|
// TestAdapterMessagesCarriesSenderID pins that the channel-native participant
|
|
// identifier survives the store-to-curator hop, and stays empty for a message
|
|
// whose channel offers none.
|
|
func TestAdapterMessagesCarriesSenderID(t *testing.T) {
|
|
t.Parallel()
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
if err := store.Save(ctx, gateway.SessionContext{SessionID: "s1", LastActiveAt: time.Unix(1000, 0)}); err != nil {
|
|
t.Fatalf("Save() = %v", err)
|
|
}
|
|
for _, m := range []messaging.Message{
|
|
{Sender: "alice", SenderID: "u-42", Role: messaging.RoleUser, Text: "hello", At: time.Unix(1, 0)},
|
|
{Sender: "archie", Role: messaging.RoleAssistant, Text: "hi", At: time.Unix(2, 0)},
|
|
} {
|
|
if err := store.SaveMessage(ctx, "s1", m); err != nil {
|
|
t.Fatalf("SaveMessage(%v) = %v", m, err)
|
|
}
|
|
}
|
|
|
|
got, err := NewAdapter(store).Messages(ctx, "s1", 10)
|
|
if err != nil {
|
|
t.Fatalf("Messages() = %v, want nil", err)
|
|
}
|
|
if len(got) != 2 {
|
|
t.Fatalf("Messages() = %+v, want 2", got)
|
|
}
|
|
if got[0].SenderID != "u-42" {
|
|
t.Errorf("Messages()[0].SenderID = %q, want %q", got[0].SenderID, "u-42")
|
|
}
|
|
if got[1].SenderID != "" {
|
|
t.Errorf("Messages()[1].SenderID = %q, want empty", got[1].SenderID)
|
|
}
|
|
}
|