Files
wuxu 8997886eef
Frontend / quality (push) Canceled after 0s
feat: migrate account usage to CLIProxyAPI
2026-09-08 09:56:08 +08:00

93 lines
2.9 KiB
Go

package store
import (
"database/sql"
"path/filepath"
"testing"
)
func TestAuthIndexCRUDAndUniqueness(t *testing.T) {
s, err := Open(t.TempDir())
if err != nil {
t.Fatal(err)
}
defer s.DB.Close()
first, err := s.CreateAccount("First", "auth-1", "personal")
if err != nil {
t.Fatal(err)
}
if first.AuthIndex != "auth-1" {
t.Fatalf("first = %#v", first)
}
if _, err = s.CreateAccount("Duplicate", "auth-1"); err == nil {
t.Fatal("duplicate auth_index unexpectedly succeeded")
}
second, err := s.CreateAccountWithVisibility("Second", "auth-2", "team", true)
if err != nil {
t.Fatal(err)
}
newIndex := "auth-3"
visible := false
if err = s.UpdateAccountBinding(second.ID, "Updated", &newIndex, "any", &visible); err != nil {
t.Fatal(err)
}
updated, err := s.Account(second.ID)
if err != nil {
t.Fatal(err)
}
if updated.AuthIndex != "auth-3" || updated.DisplayName != "Updated" || updated.PublicVisible || updated.ExpectedKind != "any" {
t.Fatalf("updated = %#v", updated)
}
conflict := "auth-1"
if err = s.UpdateAccountBinding(second.ID, "Updated", &conflict, "any", nil); err == nil {
t.Fatal("conflicting update unexpectedly succeeded")
}
stillUpdated, err := s.Account(second.ID)
if err != nil || stillUpdated.AuthIndex != "auth-3" {
t.Fatalf("failed update changed binding: %#v err=%v", stillUpdated, err)
}
used, err := s.AuthIndexUsed("auth-1", first.ID)
if err != nil || used {
t.Fatalf("exclude current account: used=%v err=%v", used, err)
}
}
func TestAuthIndexMigrationIsIdempotentAndPreservesAccounts(t *testing.T) {
dir := t.TempDir()
db, err := sql.Open("sqlite", filepath.Join(dir, "codex-helper.db"))
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`CREATE TABLE accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT, display_name TEXT NOT NULL, email TEXT,
plan_type TEXT, expected_kind TEXT NOT NULL DEFAULT 'any', public_visible INTEGER NOT NULL DEFAULT 0,
connected INTEGER NOT NULL DEFAULT 0, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL
); INSERT INTO accounts VALUES(7,'Legacy','legacy@example.com','plus','personal',1,1,1,2);`)
if err != nil {
t.Fatal(err)
}
_ = db.Close()
for i := 0; i < 2; i++ {
s, openErr := Open(dir)
if openErr != nil {
t.Fatal(openErr)
}
account, accountErr := s.Account(7)
if accountErr != nil {
_ = s.DB.Close()
t.Fatal(accountErr)
}
if account.AuthIndex != "" || account.DisplayName != "Legacy" || account.Email == nil || *account.Email != "legacy@example.com" || !account.PublicVisible {
_ = s.DB.Close()
t.Fatalf("migrated account = %#v", account)
}
var indexCount int
if queryErr := s.DB.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type='index' AND name='idx_accounts_auth_index'").Scan(&indexCount); queryErr != nil || indexCount != 1 {
_ = s.DB.Close()
t.Fatalf("index count = %d err=%v", indexCount, queryErr)
}
_ = s.DB.Close()
}
}