| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- //go:build unit
- package service
- import (
- "testing"
- "github.com/stretchr/testify/require"
- )
- func TestGeminiTokenCacheKey(t *testing.T) {
- tests := []struct {
- name string
- account *Account
- expected string
- }{
- {
- name: "with_project_id",
- account: &Account{
- ID: 100,
- Credentials: map[string]any{
- "project_id": "my-project-123",
- },
- },
- expected: "gemini:my-project-123",
- },
- {
- name: "project_id_with_whitespace",
- account: &Account{
- ID: 101,
- Credentials: map[string]any{
- "project_id": " project-with-spaces ",
- },
- },
- expected: "gemini:project-with-spaces",
- },
- {
- name: "empty_project_id_fallback_to_account_id",
- account: &Account{
- ID: 102,
- Credentials: map[string]any{
- "project_id": "",
- },
- },
- expected: "gemini:account:102",
- },
- {
- name: "whitespace_only_project_id_fallback_to_account_id",
- account: &Account{
- ID: 103,
- Credentials: map[string]any{
- "project_id": " ",
- },
- },
- expected: "gemini:account:103",
- },
- {
- name: "no_project_id_key_fallback_to_account_id",
- account: &Account{
- ID: 104,
- Credentials: map[string]any{},
- },
- expected: "gemini:account:104",
- },
- {
- name: "nil_credentials_fallback_to_account_id",
- account: &Account{
- ID: 105,
- Credentials: nil,
- },
- expected: "gemini:account:105",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := GeminiTokenCacheKey(tt.account)
- require.Equal(t, tt.expected, result)
- })
- }
- }
- func TestAntigravityTokenCacheKey(t *testing.T) {
- tests := []struct {
- name string
- account *Account
- expected string
- }{
- {
- name: "with_project_id",
- account: &Account{
- ID: 200,
- Credentials: map[string]any{
- "project_id": "ag-project-456",
- },
- },
- expected: "ag:ag-project-456",
- },
- {
- name: "project_id_with_whitespace",
- account: &Account{
- ID: 201,
- Credentials: map[string]any{
- "project_id": " ag-project-spaces ",
- },
- },
- expected: "ag:ag-project-spaces",
- },
- {
- name: "empty_project_id_fallback_to_account_id",
- account: &Account{
- ID: 202,
- Credentials: map[string]any{
- "project_id": "",
- },
- },
- expected: "ag:account:202",
- },
- {
- name: "whitespace_only_project_id_fallback_to_account_id",
- account: &Account{
- ID: 203,
- Credentials: map[string]any{
- "project_id": " ",
- },
- },
- expected: "ag:account:203",
- },
- {
- name: "no_project_id_key_fallback_to_account_id",
- account: &Account{
- ID: 204,
- Credentials: map[string]any{},
- },
- expected: "ag:account:204",
- },
- {
- name: "nil_credentials_fallback_to_account_id",
- account: &Account{
- ID: 205,
- Credentials: nil,
- },
- expected: "ag:account:205",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := AntigravityTokenCacheKey(tt.account)
- require.Equal(t, tt.expected, result)
- })
- }
- }
- func TestOpenAITokenCacheKey(t *testing.T) {
- tests := []struct {
- name string
- account *Account
- expected string
- }{
- {
- name: "basic_account",
- account: &Account{
- ID: 300,
- },
- expected: "openai:account:300",
- },
- {
- name: "account_with_credentials",
- account: &Account{
- ID: 301,
- Credentials: map[string]any{
- "access_token": "test-token",
- },
- },
- expected: "openai:account:301",
- },
- {
- name: "account_id_zero",
- account: &Account{
- ID: 0,
- },
- expected: "openai:account:0",
- },
- {
- name: "large_account_id",
- account: &Account{
- ID: 9999999999,
- },
- expected: "openai:account:9999999999",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := OpenAITokenCacheKey(tt.account)
- require.Equal(t, tt.expected, result)
- })
- }
- }
- func TestGrokTokenCacheKey(t *testing.T) {
- tests := []struct {
- name string
- account *Account
- expected string
- }{
- {
- name: "basic_account",
- account: &Account{
- ID: 350,
- },
- expected: "grok:account:350",
- },
- {
- name: "account_with_email_uses_account_id",
- account: &Account{
- ID: 351,
- Credentials: map[string]any{
- "email": "same-user@example.com",
- },
- },
- expected: "grok:account:351",
- },
- {
- name: "account_id_zero",
- account: &Account{
- ID: 0,
- },
- expected: "grok:account:0",
- },
- {
- name: "nil_account",
- account: nil,
- expected: "grok:account:0",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := GrokTokenCacheKey(tt.account)
- require.Equal(t, tt.expected, result)
- })
- }
- }
- func TestGrokTokenCacheKeySeparatesAccountsWithSameEmail(t *testing.T) {
- first := &Account{
- ID: 351,
- Credentials: map[string]any{
- "email": "same-user@example.com",
- },
- }
- second := &Account{
- ID: 352,
- Credentials: map[string]any{
- "email": "same-user@example.com",
- },
- }
- require.NotEqual(t, GrokTokenCacheKey(first), GrokTokenCacheKey(second))
- }
- func TestClaudeTokenCacheKey(t *testing.T) {
- tests := []struct {
- name string
- account *Account
- expected string
- }{
- {
- name: "basic_account",
- account: &Account{
- ID: 400,
- },
- expected: "claude:account:400",
- },
- {
- name: "account_with_credentials",
- account: &Account{
- ID: 401,
- Credentials: map[string]any{
- "access_token": "claude-token",
- },
- },
- expected: "claude:account:401",
- },
- {
- name: "account_id_zero",
- account: &Account{
- ID: 0,
- },
- expected: "claude:account:0",
- },
- {
- name: "large_account_id",
- account: &Account{
- ID: 9999999999,
- },
- expected: "claude:account:9999999999",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := ClaudeTokenCacheKey(tt.account)
- require.Equal(t, tt.expected, result)
- })
- }
- }
- func TestCacheKeyUniqueness(t *testing.T) {
- // 确保不同平台的缓存键不会冲突
- account := &Account{ID: 123}
- openaiKey := OpenAITokenCacheKey(account)
- claudeKey := ClaudeTokenCacheKey(account)
- require.NotEqual(t, openaiKey, claudeKey, "OpenAI and Claude cache keys should be different")
- require.Contains(t, openaiKey, "openai:")
- require.Contains(t, claudeKey, "claude:")
- }
|