|
| 1 | +package memory |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/priyanshujain/openbotkit/provider" |
| 9 | + "github.com/priyanshujain/openbotkit/provider/anthropic" |
| 10 | + "github.com/priyanshujain/openbotkit/provider/gemini" |
| 11 | + "github.com/priyanshujain/openbotkit/provider/openai" |
| 12 | +) |
| 13 | + |
| 14 | +type providerTestCase struct { |
| 15 | + name string |
| 16 | + provider provider.Provider |
| 17 | + model string |
| 18 | +} |
| 19 | + |
| 20 | +func availableProviders(t *testing.T) []providerTestCase { |
| 21 | + t.Helper() |
| 22 | + var providers []providerTestCase |
| 23 | + |
| 24 | + if key := os.Getenv("ANTHROPIC_API_KEY"); key != "" { |
| 25 | + providers = append(providers, providerTestCase{ |
| 26 | + name: "anthropic", |
| 27 | + provider: anthropic.New(key), |
| 28 | + model: "claude-sonnet-4-6", |
| 29 | + }) |
| 30 | + } |
| 31 | + if key := os.Getenv("OPENAI_API_KEY"); key != "" { |
| 32 | + providers = append(providers, providerTestCase{ |
| 33 | + name: "openai", |
| 34 | + provider: openai.New(key), |
| 35 | + model: "gpt-4o-mini", |
| 36 | + }) |
| 37 | + } |
| 38 | + if key := os.Getenv("GEMINI_API_KEY"); key != "" { |
| 39 | + providers = append(providers, providerTestCase{ |
| 40 | + name: "gemini", |
| 41 | + provider: gemini.New(key), |
| 42 | + model: "gemini-2.0-flash", |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + if len(providers) == 0 { |
| 47 | + t.Skip("no API keys set — skipping integration tests (set ANTHROPIC_API_KEY, OPENAI_API_KEY, or GEMINI_API_KEY)") |
| 48 | + } |
| 49 | + return providers |
| 50 | +} |
| 51 | + |
| 52 | +type providerLLM struct { |
| 53 | + p provider.Provider |
| 54 | + model string |
| 55 | +} |
| 56 | + |
| 57 | +func (pl *providerLLM) Chat(ctx context.Context, req provider.ChatRequest) (*provider.ChatResponse, error) { |
| 58 | + req.Model = pl.model |
| 59 | + return pl.p.Chat(ctx, req) |
| 60 | +} |
| 61 | + |
| 62 | +func TestIntegration_Extract(t *testing.T) { |
| 63 | + for _, tc := range availableProviders(t) { |
| 64 | + t.Run(tc.name, func(t *testing.T) { |
| 65 | + llm := &providerLLM{p: tc.provider, model: tc.model} |
| 66 | + |
| 67 | + messages := []string{ |
| 68 | + "My name is Alice and I'm a software engineer at TechCorp", |
| 69 | + "I really prefer using Go for backend development over Python", |
| 70 | + "I'm currently building a personal assistant called BotKit", |
| 71 | + } |
| 72 | + |
| 73 | + facts, err := Extract(context.Background(), llm, messages) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("Extract: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + if len(facts) == 0 { |
| 79 | + t.Fatal("expected at least 1 fact extracted") |
| 80 | + } |
| 81 | + |
| 82 | + // Verify facts have valid categories. |
| 83 | + validCategories := map[string]bool{ |
| 84 | + "identity": true, "preference": true, |
| 85 | + "relationship": true, "project": true, |
| 86 | + } |
| 87 | + for _, f := range facts { |
| 88 | + if f.Content == "" { |
| 89 | + t.Error("fact has empty content") |
| 90 | + } |
| 91 | + if !validCategories[f.Category] { |
| 92 | + t.Errorf("fact has invalid category %q: %q", f.Category, f.Content) |
| 93 | + } |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestIntegration_ExtractAndReconcile(t *testing.T) { |
| 100 | + for _, tc := range availableProviders(t) { |
| 101 | + t.Run(tc.name, func(t *testing.T) { |
| 102 | + db := testDB(t) |
| 103 | + if err := Migrate(db); err != nil { |
| 104 | + t.Fatalf("migrate: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + llm := &providerLLM{p: tc.provider, model: tc.model} |
| 108 | + |
| 109 | + messages := []string{ |
| 110 | + "My name is Bob and I live in San Francisco", |
| 111 | + "I prefer dark mode in all my code editors", |
| 112 | + "I'm working on an open source project called DataFlow", |
| 113 | + } |
| 114 | + |
| 115 | + // Extract facts. |
| 116 | + facts, err := Extract(context.Background(), llm, messages) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("Extract: %v", err) |
| 119 | + } |
| 120 | + if len(facts) == 0 { |
| 121 | + t.Fatal("expected at least 1 fact") |
| 122 | + } |
| 123 | + |
| 124 | + // Reconcile into empty DB (should all ADD). |
| 125 | + result, err := Reconcile(context.Background(), db, llm, facts) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("Reconcile: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + if result.Added == 0 { |
| 131 | + t.Error("expected at least 1 add") |
| 132 | + } |
| 133 | + |
| 134 | + count, _ := Count(db) |
| 135 | + if count == 0 { |
| 136 | + t.Fatal("expected memories in DB after reconciliation") |
| 137 | + } |
| 138 | + |
| 139 | + // Verify memories are retrievable. |
| 140 | + memories, err := List(db) |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("List: %v", err) |
| 143 | + } |
| 144 | + for _, m := range memories { |
| 145 | + if m.Content == "" { |
| 146 | + t.Error("memory has empty content") |
| 147 | + } |
| 148 | + if m.Source != "history" { |
| 149 | + t.Errorf("memory source = %q, want 'history'", m.Source) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Second extraction with same facts — should mostly NOOP/skip. |
| 154 | + result2, err := Reconcile(context.Background(), db, llm, facts) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("second Reconcile: %v", err) |
| 157 | + } |
| 158 | + |
| 159 | + // Count should not have grown much (some ADD is OK if LLM decides differently). |
| 160 | + count2, _ := Count(db) |
| 161 | + t.Logf("first run: added=%d, count=%d; second run: added=%d, updated=%d, skipped=%d, count=%d", |
| 162 | + result.Added, count, result2.Added, result2.Updated, result2.Skipped, count2) |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments