|
| 1 | +package store |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + _ "github.com/mattn/go-sqlite3" |
| 8 | + |
| 9 | + "github.com/DouDOU-start/airgate-core/ent" |
| 10 | + "github.com/DouDOU-start/airgate-core/ent/enttest" |
| 11 | + "github.com/DouDOU-start/airgate-core/ent/migrate" |
| 12 | + "github.com/DouDOU-start/airgate-core/internal/app/account" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAccountStoreKeywordSearchMatchesOAuthEmail(t *testing.T) { |
| 16 | + db := enttestOpen(t) |
| 17 | + defer func() { |
| 18 | + if err := db.Close(); err != nil { |
| 19 | + t.Fatalf("close db: %v", err) |
| 20 | + } |
| 21 | + }() |
| 22 | + |
| 23 | + ctx := context.Background() |
| 24 | + if _, err := db.Account.Create(). |
| 25 | + SetName("Claude Key"). |
| 26 | + SetPlatform("openai"). |
| 27 | + SetType("oauth"). |
| 28 | + SetCredentials(map[string]string{"email": "claude@example.com", "access_token": "token"}). |
| 29 | + Save(ctx); err != nil { |
| 30 | + t.Fatalf("create oauth account: %v", err) |
| 31 | + } |
| 32 | + if _, err := db.Account.Create(). |
| 33 | + SetName("Other Key"). |
| 34 | + SetPlatform("openai"). |
| 35 | + SetType("apikey"). |
| 36 | + SetCredentials(map[string]string{"api_key": "sk-test"}). |
| 37 | + Save(ctx); err != nil { |
| 38 | + t.Fatalf("create api key account: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + store := NewAccountStore(db) |
| 42 | + items, total, err := store.List(ctx, account.ListFilter{Page: 1, PageSize: 20, Keyword: "claude@"}) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("List returned error: %v", err) |
| 45 | + } |
| 46 | + if total != 1 { |
| 47 | + t.Fatalf("total = %d, want 1", total) |
| 48 | + } |
| 49 | + if len(items) != 1 || items[0].Name != "Claude Key" { |
| 50 | + t.Fatalf("items = %+v", items) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func enttestOpen(t *testing.T) *ent.Client { |
| 55 | + t.Helper() |
| 56 | + return enttest.Open(t, "sqlite3", "file:account_store?mode=memory&cache=shared&_fk=1", enttest.WithMigrateOptions(migrate.WithGlobalUniqueID(false))) |
| 57 | +} |
0 commit comments