|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +type memoryStoreSpy struct { |
| 9 | + getMemoryCalls int |
| 10 | + saveMemoryCalls int |
| 11 | + getWorkspaceFileExactCall int |
| 12 | + saveWorkspaceFileCalls int |
| 13 | +} |
| 14 | + |
| 15 | +func (s *memoryStoreSpy) GetMemory(context.Context, string, string) (string, error) { |
| 16 | + s.getMemoryCalls++ |
| 17 | + return "persisted", nil |
| 18 | +} |
| 19 | + |
| 20 | +func (s *memoryStoreSpy) SaveMemory(context.Context, string, string, string) error { |
| 21 | + s.saveMemoryCalls++ |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +func (s *memoryStoreSpy) GetWorkspaceFile(context.Context, string, string, string) ([]byte, error) { |
| 26 | + return nil, nil |
| 27 | +} |
| 28 | + |
| 29 | +func (s *memoryStoreSpy) GetWorkspaceFileExact(context.Context, string, string, string) ([]byte, error) { |
| 30 | + s.getWorkspaceFileExactCall++ |
| 31 | + return []byte("user-profile"), nil |
| 32 | +} |
| 33 | + |
| 34 | +func (s *memoryStoreSpy) SaveWorkspaceFile(context.Context, string, string, string, []byte) error { |
| 35 | + s.saveWorkspaceFileCalls++ |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +func TestNewMemoryWithStoreForUserEmptyUserIDFailsClosed(t *testing.T) { |
| 40 | + store := &memoryStoreSpy{} |
| 41 | + mem := NewMemoryWithStoreForUser(t.TempDir(), store, "", "agent-1") |
| 42 | + if mem == nil { |
| 43 | + t.Fatal("expected memory") |
| 44 | + } |
| 45 | + if got := mem.LoadMemory(); got != "" { |
| 46 | + t.Fatalf("LoadMemory() = %q, want empty", got) |
| 47 | + } |
| 48 | + if store.getMemoryCalls != 0 { |
| 49 | + t.Fatalf("GetMemory called %d times, want 0", store.getMemoryCalls) |
| 50 | + } |
| 51 | + if err := mem.SaveMemory("x"); err == nil { |
| 52 | + t.Fatal("SaveMemory() error = nil, want error") |
| 53 | + } |
| 54 | + if store.saveMemoryCalls != 0 { |
| 55 | + t.Fatalf("SaveMemory store calls = %d, want 0", store.saveMemoryCalls) |
| 56 | + } |
| 57 | + if got := mem.LoadUserFile(); got != "" { |
| 58 | + t.Fatalf("LoadUserFile() = %q, want empty", got) |
| 59 | + } |
| 60 | + if store.getWorkspaceFileExactCall != 0 { |
| 61 | + t.Fatalf("GetWorkspaceFileExact called %d times, want 0", store.getWorkspaceFileExactCall) |
| 62 | + } |
| 63 | + if err := mem.SaveUserFile("x"); err == nil { |
| 64 | + t.Fatal("SaveUserFile() error = nil, want error") |
| 65 | + } |
| 66 | + if store.saveWorkspaceFileCalls != 0 { |
| 67 | + t.Fatalf("SaveWorkspaceFile calls = %d, want 0", store.saveWorkspaceFileCalls) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestNewMemoryWithStoreForUserCanBeRebound(t *testing.T) { |
| 72 | + store := &memoryStoreSpy{} |
| 73 | + mem := NewMemoryWithStoreForUser(t.TempDir(), store, "", "agent-1").WithUserID("user-1") |
| 74 | + if got := mem.LoadMemory(); got != "persisted" { |
| 75 | + t.Fatalf("LoadMemory() = %q, want persisted", got) |
| 76 | + } |
| 77 | + if err := mem.SaveMemory("x"); err != nil { |
| 78 | + t.Fatalf("SaveMemory() error = %v", err) |
| 79 | + } |
| 80 | + if got := mem.LoadUserFile(); got != "user-profile" { |
| 81 | + t.Fatalf("LoadUserFile() = %q, want user-profile", got) |
| 82 | + } |
| 83 | + if err := mem.SaveUserFile("x"); err != nil { |
| 84 | + t.Fatalf("SaveUserFile() error = %v", err) |
| 85 | + } |
| 86 | +} |
0 commit comments