240 lines
6.0 KiB
Go
240 lines
6.0 KiB
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewStore_NilDB_异常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
|
|
if _, err := NewStore(ctx, nil, testLogger()); !errors.Is(err, ErrNilDB) {
|
|
t.Fatalf("NewStore(nil db) err = %v, want ErrNilDB", err)
|
|
}
|
|
}
|
|
|
|
func TestStore_AddGet_正常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
item := baseItem()
|
|
// Add returns the stored item with id and timestamps filled in.
|
|
stored, err := s.Add(ctx, item)
|
|
if err != nil {
|
|
t.Fatalf("Add: %v", err)
|
|
}
|
|
if stored.ID == "" {
|
|
t.Fatal("Add returned empty id")
|
|
}
|
|
if stored.CreatedAt.IsZero() || stored.UpdatedAt.IsZero() {
|
|
t.Fatal("Add did not assign timestamps")
|
|
}
|
|
|
|
got, err := s.Get(ctx, stored.ID)
|
|
if err != nil {
|
|
t.Fatalf("Get: %v", err)
|
|
}
|
|
if got.ID != stored.ID {
|
|
t.Errorf("ID = %q, want %q", got.ID, stored.ID)
|
|
}
|
|
if got.PersonaID != item.PersonaID {
|
|
t.Errorf("PersonaID = %q, want %q", got.PersonaID, item.PersonaID)
|
|
}
|
|
if got.RoleID != item.RoleID {
|
|
t.Errorf("RoleID = %q, want %q", got.RoleID, item.RoleID)
|
|
}
|
|
if got.Scope != item.Scope {
|
|
t.Errorf("Scope = %q, want %q", got.Scope, item.Scope)
|
|
}
|
|
if got.Kind != item.Kind {
|
|
t.Errorf("Kind = %q, want %q", got.Kind, item.Kind)
|
|
}
|
|
if got.Content != item.Content {
|
|
t.Errorf("Content = %q, want %q", got.Content, item.Content)
|
|
}
|
|
if got.Confidence != item.Confidence {
|
|
t.Errorf("Confidence = %v, want %v", got.Confidence, item.Confidence)
|
|
}
|
|
if len(got.Tags) != 2 || got.Tags[0] != "habit" || got.Tags[1] != "coffee" {
|
|
t.Errorf("Tags = %v, want [habit coffee]", got.Tags)
|
|
}
|
|
}
|
|
|
|
func TestStore_Add_Defaults_正常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
// Empty Scope/Kind/Confidence/DecayWeight normalize to their defaults.
|
|
stored, err := s.Add(ctx, MemoryItem{
|
|
PersonaID: "persona.a",
|
|
Content: "我们约定每周日晚上看电影",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Add: %v", err)
|
|
}
|
|
if stored.Scope != ScopePersona {
|
|
t.Errorf("Scope = %q, want persona default", stored.Scope)
|
|
}
|
|
if stored.Kind != KindRelationship {
|
|
t.Errorf("Kind = %q, want relationship default", stored.Kind)
|
|
}
|
|
if stored.Confidence != 1.0 {
|
|
t.Errorf("Confidence = %v, want 1.0 default", stored.Confidence)
|
|
}
|
|
if stored.DecayWeight != 1.0 {
|
|
t.Errorf("DecayWeight = %v, want 1.0 default", stored.DecayWeight)
|
|
}
|
|
}
|
|
|
|
func TestStore_Add_Invalid_异常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
tests := []struct {
|
|
name string
|
|
item MemoryItem
|
|
wantErr error
|
|
}{
|
|
{name: "missing persona", item: MemoryItem{Content: "x"}, wantErr: ErrMissingPersona},
|
|
{name: "missing content", item: MemoryItem{PersonaID: "p"}, wantErr: ErrMissingContent},
|
|
{name: "invalid scope", item: MemoryItem{PersonaID: "p", Content: "x", Scope: "bogus"}, wantErr: ErrInvalidScope},
|
|
{name: "invalid kind", item: MemoryItem{PersonaID: "p", Content: "x", Kind: "bogus"}, wantErr: ErrInvalidKind},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
if _, err := s.Add(ctx, tt.item); !errors.Is(err, tt.wantErr) {
|
|
t.Fatalf("Add err = %v, want wrap of %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStore_Get_NotFound_异常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
for _, id := range []string{"", "does-not-exist"} {
|
|
if _, err := s.Get(ctx, id); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("Get(%q) err = %v, want ErrNotFound", id, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStore_ListByPersona_Isolation(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
for _, p := range []string{"persona.a", "persona.b"} {
|
|
stored, err := s.Add(ctx, MemoryItem{
|
|
PersonaID: p,
|
|
Content: "内容 " + p,
|
|
Scope: ScopePersona,
|
|
Kind: KindWorldEvent,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Add for %s: %v", p, err)
|
|
}
|
|
if stored.PersonaID != p {
|
|
t.Fatalf("stored persona = %q, want %q", stored.PersonaID, p)
|
|
}
|
|
}
|
|
|
|
items, err := s.ListByPersona(ctx, "persona.a")
|
|
if err != nil {
|
|
t.Fatalf("ListByPersona: %v", err)
|
|
}
|
|
if len(items) != 1 {
|
|
t.Fatalf("ListByPersona(a) = %d items, want 1", len(items))
|
|
}
|
|
if items[0].PersonaID != "persona.a" {
|
|
t.Errorf("item persona = %q, want persona.a", items[0].PersonaID)
|
|
}
|
|
}
|
|
|
|
func TestStore_ListByPersona_MissingPersona_异常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
if _, err := s.ListByPersona(ctx, ""); !errors.Is(err, ErrMissingPersona) {
|
|
t.Fatalf("ListByPersona(\"\") err = %v, want ErrMissingPersona", err)
|
|
}
|
|
}
|
|
|
|
func TestStore_Delete_正常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
stored, err := s.Add(ctx, baseItem())
|
|
if err != nil {
|
|
t.Fatalf("Add: %v", err)
|
|
}
|
|
if err := s.Delete(ctx, stored.ID); err != nil {
|
|
t.Fatalf("Delete: %v", err)
|
|
}
|
|
if _, err := s.Get(ctx, stored.ID); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("Get after delete err = %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestStore_Delete_NotFound_异常(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
for _, id := range []string{"", "missing"} {
|
|
if err := s.Delete(ctx, id); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("Delete(%q) err = %v, want ErrNotFound", id, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStore_Concurrent_并发(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
const n = 40
|
|
var wg sync.WaitGroup
|
|
errs := make(chan error, n)
|
|
for i := 0; i < n; i++ {
|
|
wg.Add(1)
|
|
go func(i int) {
|
|
defer wg.Done()
|
|
_, err := s.Add(ctx, MemoryItem{
|
|
PersonaID: "persona.a",
|
|
Content: "并发内容",
|
|
Scope: ScopePersona,
|
|
Kind: KindRelationship,
|
|
})
|
|
errs <- err
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
close(errs)
|
|
|
|
for err := range errs {
|
|
if err != nil {
|
|
t.Fatalf("concurrent Add error: %v", err)
|
|
}
|
|
}
|
|
items, err := s.ListByPersona(ctx, "persona.a")
|
|
if err != nil {
|
|
t.Fatalf("ListByPersona: %v", err)
|
|
}
|
|
if len(items) != n {
|
|
t.Fatalf("ListByPersona(a) = %d items, want %d", len(items), n)
|
|
}
|
|
}
|