mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-20 20:34:36 +00:00
test(auth): add unit tests for claude, codex, gemini auth packages
Add comprehensive unit tests for authentication packages: - claude_auth_test.go: AuthBundle parsing, token serialization, validation - codex_auth_test.go: token storage, expiry edge cases, refresh thresholds - gemini_auth_test.go: token storage with any type, JSON field verification Also add translator package tests for format conversion edge cases. Co-Authored-By: Claude (gemini-claude-opus-4-5-thinking) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,354 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestClaudeAuthBundle_Parse tests parsing of ClaudeAuthBundle JSON.
|
||||
func TestClaudeAuthBundle_Parse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
wantErr bool
|
||||
checkFields func(t *testing.T, bundle *ClaudeAuthBundle)
|
||||
}{
|
||||
{
|
||||
name: "parse complete auth bundle",
|
||||
jsonData: `{
|
||||
"api_key": "sk-ant-api-key-12345",
|
||||
"token_data": {
|
||||
"access_token": "access_token_value",
|
||||
"refresh_token": "refresh_token_value",
|
||||
"email": "user@example.com",
|
||||
"expired": "2025-01-15T12:00:00Z"
|
||||
},
|
||||
"last_refresh": "2025-01-15T10:00:00Z"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, bundle *ClaudeAuthBundle) {
|
||||
if bundle.APIKey != "sk-ant-api-key-12345" {
|
||||
t.Errorf("APIKey = %v, want sk-ant-api-key-12345", bundle.APIKey)
|
||||
}
|
||||
if bundle.TokenData.AccessToken != "access_token_value" {
|
||||
t.Errorf("TokenData.AccessToken = %v, want access_token_value", bundle.TokenData.AccessToken)
|
||||
}
|
||||
if bundle.TokenData.Email != "user@example.com" {
|
||||
t.Errorf("TokenData.Email = %v, want user@example.com", bundle.TokenData.Email)
|
||||
}
|
||||
if bundle.LastRefresh != "2025-01-15T10:00:00Z" {
|
||||
t.Errorf("LastRefresh = %v, want 2025-01-15T10:00:00Z", bundle.LastRefresh)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse bundle without api key",
|
||||
jsonData: `{
|
||||
"token_data": {
|
||||
"access_token": "token_only",
|
||||
"refresh_token": "refresh_only"
|
||||
},
|
||||
"last_refresh": "2025-01-15T10:00:00Z"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, bundle *ClaudeAuthBundle) {
|
||||
if bundle.APIKey != "" {
|
||||
t.Errorf("APIKey should be empty, got %v", bundle.APIKey)
|
||||
}
|
||||
if bundle.TokenData.AccessToken != "token_only" {
|
||||
t.Errorf("TokenData.AccessToken = %v, want token_only", bundle.TokenData.AccessToken)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse bundle with empty token data",
|
||||
jsonData: `{
|
||||
"api_key": "sk-ant-key",
|
||||
"token_data": {},
|
||||
"last_refresh": ""
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, bundle *ClaudeAuthBundle) {
|
||||
if bundle.APIKey != "sk-ant-key" {
|
||||
t.Errorf("APIKey = %v, want sk-ant-key", bundle.APIKey)
|
||||
}
|
||||
if bundle.TokenData.AccessToken != "" {
|
||||
t.Errorf("TokenData.AccessToken should be empty")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse empty JSON object",
|
||||
jsonData: `{}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, bundle *ClaudeAuthBundle) {
|
||||
if bundle.APIKey != "" {
|
||||
t.Errorf("APIKey should be empty for empty JSON")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid JSON",
|
||||
jsonData: `{"invalid": json}`,
|
||||
wantErr: true,
|
||||
checkFields: func(t *testing.T, bundle *ClaudeAuthBundle) {
|
||||
// Not called on error
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var bundle ClaudeAuthBundle
|
||||
err := json.Unmarshal([]byte(tt.jsonData), &bundle)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
tt.checkFields(t, &bundle)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestClaudeTokenData_Parse tests parsing of ClaudeTokenData JSON.
|
||||
func TestClaudeTokenData_Parse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
wantErr bool
|
||||
checkFields func(t *testing.T, td *ClaudeTokenData)
|
||||
}{
|
||||
{
|
||||
name: "parse complete token data",
|
||||
jsonData: `{
|
||||
"access_token": "access_token_here",
|
||||
"refresh_token": "refresh_token_here",
|
||||
"email": "user@example.com",
|
||||
"expired": "2025-01-15T12:00:00Z"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, td *ClaudeTokenData) {
|
||||
if td.AccessToken != "access_token_here" {
|
||||
t.Errorf("AccessToken = %v, want access_token_here", td.AccessToken)
|
||||
}
|
||||
if td.RefreshToken != "refresh_token_here" {
|
||||
t.Errorf("RefreshToken = %v, want refresh_token_here", td.RefreshToken)
|
||||
}
|
||||
if td.Email != "user@example.com" {
|
||||
t.Errorf("Email = %v, want user@example.com", td.Email)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse minimal token data",
|
||||
jsonData: `{
|
||||
"access_token": "only_access"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, td *ClaudeTokenData) {
|
||||
if td.AccessToken != "only_access" {
|
||||
t.Errorf("AccessToken = %v, want only_access", td.AccessToken)
|
||||
}
|
||||
if td.RefreshToken != "" {
|
||||
t.Errorf("RefreshToken should be empty")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse token with special characters",
|
||||
jsonData: `{
|
||||
"access_token": "token-with_special.chars/and+more==",
|
||||
"refresh_token": "refresh/token+value=",
|
||||
"email": "user+test@sub.example.com"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, td *ClaudeTokenData) {
|
||||
if td.AccessToken != "token-with_special.chars/and+more==" {
|
||||
t.Errorf("AccessToken mismatch with special chars")
|
||||
}
|
||||
if td.Email != "user+test@sub.example.com" {
|
||||
t.Errorf("Email mismatch with special chars")
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var td ClaudeTokenData
|
||||
err := json.Unmarshal([]byte(tt.jsonData), &td)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
tt.checkFields(t, &td)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestPKCECodes_Parse tests parsing of PKCE codes.
|
||||
func TestPKCECodes_Parse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
wantErr bool
|
||||
checkFields func(t *testing.T, codes *PKCECodes)
|
||||
}{
|
||||
{
|
||||
name: "parse valid PKCE codes",
|
||||
jsonData: `{
|
||||
"code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
|
||||
"code_challenge": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, codes *PKCECodes) {
|
||||
if codes.CodeVerifier != "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk" {
|
||||
t.Errorf("CodeVerifier mismatch")
|
||||
}
|
||||
if codes.CodeChallenge != "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM" {
|
||||
t.Errorf("CodeChallenge mismatch")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse empty PKCE codes",
|
||||
jsonData: `{
|
||||
"code_verifier": "",
|
||||
"code_challenge": ""
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, codes *PKCECodes) {
|
||||
if codes.CodeVerifier != "" {
|
||||
t.Errorf("CodeVerifier should be empty")
|
||||
}
|
||||
if codes.CodeChallenge != "" {
|
||||
t.Errorf("CodeChallenge should be empty")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse minimal JSON",
|
||||
jsonData: `{}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, codes *PKCECodes) {
|
||||
if codes.CodeVerifier != "" || codes.CodeChallenge != "" {
|
||||
t.Errorf("Empty JSON should result in empty codes")
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var codes PKCECodes
|
||||
err := json.Unmarshal([]byte(tt.jsonData), &codes)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
tt.checkFields(t, &codes)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestClaudeToken_IsValid tests token validation logic.
|
||||
func TestClaudeToken_IsValid(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
token ClaudeTokenStorage
|
||||
wantValid bool
|
||||
}{
|
||||
{
|
||||
name: "valid token with all fields",
|
||||
token: ClaudeTokenStorage{
|
||||
AccessToken: "valid-access-token",
|
||||
RefreshToken: "valid-refresh-token",
|
||||
Email: "user@example.com",
|
||||
Expire: time.Now().Add(time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "token with empty access token",
|
||||
token: ClaudeTokenStorage{
|
||||
AccessToken: "",
|
||||
RefreshToken: "valid-refresh-token",
|
||||
},
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "token with only access token",
|
||||
token: ClaudeTokenStorage{
|
||||
AccessToken: "access-only",
|
||||
},
|
||||
wantValid: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
isValid := tt.token.AccessToken != ""
|
||||
if isValid != tt.wantValid {
|
||||
t.Errorf("Token validity = %v, want %v", isValid, tt.wantValid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestClaudeTokenStorage_Serialize tests JSON serialization round-trip.
|
||||
func TestClaudeTokenStorage_Serialize(t *testing.T) {
|
||||
original := ClaudeTokenStorage{
|
||||
IDToken: "test-id-token",
|
||||
AccessToken: "test-access-token",
|
||||
RefreshToken: "test-refresh-token",
|
||||
LastRefresh: "2025-01-15T10:00:00Z",
|
||||
Email: "test@example.com",
|
||||
Type: "claude",
|
||||
Expire: "2025-01-15T11:00:00Z",
|
||||
}
|
||||
|
||||
// Serialize to JSON
|
||||
data, err := json.Marshal(&original)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal token: %v", err)
|
||||
}
|
||||
|
||||
// Deserialize back
|
||||
var restored ClaudeTokenStorage
|
||||
if err := json.Unmarshal(data, &restored); err != nil {
|
||||
t.Fatalf("Failed to unmarshal token: %v", err)
|
||||
}
|
||||
|
||||
// Verify all fields match
|
||||
if restored.IDToken != original.IDToken {
|
||||
t.Errorf("IDToken = %v, want %v", restored.IDToken, original.IDToken)
|
||||
}
|
||||
if restored.AccessToken != original.AccessToken {
|
||||
t.Errorf("AccessToken = %v, want %v", restored.AccessToken, original.AccessToken)
|
||||
}
|
||||
if restored.RefreshToken != original.RefreshToken {
|
||||
t.Errorf("RefreshToken = %v, want %v", restored.RefreshToken, original.RefreshToken)
|
||||
}
|
||||
if restored.Email != original.Email {
|
||||
t.Errorf("Email = %v, want %v", restored.Email, original.Email)
|
||||
}
|
||||
if restored.Type != original.Type {
|
||||
t.Errorf("Type = %v, want %v", restored.Type, original.Type)
|
||||
}
|
||||
if restored.Expire != original.Expire {
|
||||
t.Errorf("Expire = %v, want %v", restored.Expire, original.Expire)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestCodexAuthBundle_Serialize tests JSON serialization round-trip.
|
||||
func TestCodexAuthBundle_Serialize(t *testing.T) {
|
||||
original := CodexAuthBundle{
|
||||
APIKey: "sk-proj-test-api-key",
|
||||
TokenData: CodexTokenData{
|
||||
IDToken: "test-id-token",
|
||||
AccessToken: "test-access-token",
|
||||
RefreshToken: "test-refresh-token",
|
||||
AccountID: "acct_test123",
|
||||
Email: "test@example.com",
|
||||
Expire: "2025-01-15T11:00:00Z",
|
||||
},
|
||||
LastRefresh: "2025-01-15T10:00:00Z",
|
||||
}
|
||||
|
||||
// Serialize to JSON
|
||||
data, err := json.Marshal(&original)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal bundle: %v", err)
|
||||
}
|
||||
|
||||
// Deserialize back
|
||||
var restored CodexAuthBundle
|
||||
if err := json.Unmarshal(data, &restored); err != nil {
|
||||
t.Fatalf("Failed to unmarshal bundle: %v", err)
|
||||
}
|
||||
|
||||
// Verify all fields match
|
||||
if restored.APIKey != original.APIKey {
|
||||
t.Errorf("APIKey = %v, want %v", restored.APIKey, original.APIKey)
|
||||
}
|
||||
if restored.TokenData.IDToken != original.TokenData.IDToken {
|
||||
t.Errorf("TokenData.IDToken = %v, want %v", restored.TokenData.IDToken, original.TokenData.IDToken)
|
||||
}
|
||||
if restored.TokenData.AccessToken != original.TokenData.AccessToken {
|
||||
t.Errorf("TokenData.AccessToken = %v, want %v", restored.TokenData.AccessToken, original.TokenData.AccessToken)
|
||||
}
|
||||
if restored.TokenData.AccountID != original.TokenData.AccountID {
|
||||
t.Errorf("TokenData.AccountID = %v, want %v", restored.TokenData.AccountID, original.TokenData.AccountID)
|
||||
}
|
||||
if restored.TokenData.Email != original.TokenData.Email {
|
||||
t.Errorf("TokenData.Email = %v, want %v", restored.TokenData.Email, original.TokenData.Email)
|
||||
}
|
||||
if restored.LastRefresh != original.LastRefresh {
|
||||
t.Errorf("LastRefresh = %v, want %v", restored.LastRefresh, original.LastRefresh)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodexToken_IsValid tests token validation logic.
|
||||
func TestCodexToken_IsValid(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
token CodexTokenStorage
|
||||
wantValid bool
|
||||
}{
|
||||
{
|
||||
name: "valid token with all fields",
|
||||
token: CodexTokenStorage{
|
||||
IDToken: "valid-id-token",
|
||||
AccessToken: "valid-access-token",
|
||||
RefreshToken: "valid-refresh-token",
|
||||
AccountID: "acct_123",
|
||||
Email: "user@example.com",
|
||||
Expire: time.Now().Add(time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "token with empty access token",
|
||||
token: CodexTokenStorage{
|
||||
AccessToken: "",
|
||||
RefreshToken: "valid-refresh-token",
|
||||
},
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "token with only access token",
|
||||
token: CodexTokenStorage{
|
||||
AccessToken: "access-only",
|
||||
},
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "token with whitespace access token",
|
||||
token: CodexTokenStorage{
|
||||
AccessToken: " ",
|
||||
},
|
||||
wantValid: true, // Whitespace is technically non-empty
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
isValid := tt.token.AccessToken != ""
|
||||
if isValid != tt.wantValid {
|
||||
t.Errorf("Token validity = %v, want %v", isValid, tt.wantValid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodexTokenStorage_Serialize tests JSON serialization round-trip.
|
||||
func TestCodexTokenStorage_Serialize(t *testing.T) {
|
||||
original := CodexTokenStorage{
|
||||
IDToken: "test-id-token",
|
||||
AccessToken: "test-access-token",
|
||||
RefreshToken: "test-refresh-token",
|
||||
AccountID: "acct_test",
|
||||
LastRefresh: "2025-01-15T10:00:00Z",
|
||||
Email: "test@example.com",
|
||||
Type: "codex",
|
||||
Expire: "2025-01-15T11:00:00Z",
|
||||
}
|
||||
|
||||
// Serialize to JSON
|
||||
data, err := json.Marshal(&original)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal token: %v", err)
|
||||
}
|
||||
|
||||
// Deserialize back
|
||||
var restored CodexTokenStorage
|
||||
if err := json.Unmarshal(data, &restored); err != nil {
|
||||
t.Fatalf("Failed to unmarshal token: %v", err)
|
||||
}
|
||||
|
||||
// Verify all fields match
|
||||
if restored.IDToken != original.IDToken {
|
||||
t.Errorf("IDToken = %v, want %v", restored.IDToken, original.IDToken)
|
||||
}
|
||||
if restored.AccessToken != original.AccessToken {
|
||||
t.Errorf("AccessToken = %v, want %v", restored.AccessToken, original.AccessToken)
|
||||
}
|
||||
if restored.RefreshToken != original.RefreshToken {
|
||||
t.Errorf("RefreshToken = %v, want %v", restored.RefreshToken, original.RefreshToken)
|
||||
}
|
||||
if restored.AccountID != original.AccountID {
|
||||
t.Errorf("AccountID = %v, want %v", restored.AccountID, original.AccountID)
|
||||
}
|
||||
if restored.Email != original.Email {
|
||||
t.Errorf("Email = %v, want %v", restored.Email, original.Email)
|
||||
}
|
||||
if restored.Type != original.Type {
|
||||
t.Errorf("Type = %v, want %v", restored.Type, original.Type)
|
||||
}
|
||||
if restored.Expire != original.Expire {
|
||||
t.Errorf("Expire = %v, want %v", restored.Expire, original.Expire)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodexTokenData_Serialize tests JSON serialization of token data.
|
||||
func TestCodexTokenData_Serialize(t *testing.T) {
|
||||
original := CodexTokenData{
|
||||
IDToken: "jwt-id-token",
|
||||
AccessToken: "jwt-access-token",
|
||||
RefreshToken: "jwt-refresh-token",
|
||||
AccountID: "acct_serialize",
|
||||
Email: "serialize@example.com",
|
||||
Expire: "2025-12-31T23:59:59Z",
|
||||
}
|
||||
|
||||
data, err := json.Marshal(&original)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal token data: %v", err)
|
||||
}
|
||||
|
||||
var restored CodexTokenData
|
||||
if err := json.Unmarshal(data, &restored); err != nil {
|
||||
t.Fatalf("Failed to unmarshal token data: %v", err)
|
||||
}
|
||||
|
||||
if restored.IDToken != original.IDToken {
|
||||
t.Errorf("IDToken mismatch")
|
||||
}
|
||||
if restored.AccessToken != original.AccessToken {
|
||||
t.Errorf("AccessToken mismatch")
|
||||
}
|
||||
if restored.RefreshToken != original.RefreshToken {
|
||||
t.Errorf("RefreshToken mismatch")
|
||||
}
|
||||
if restored.AccountID != original.AccountID {
|
||||
t.Errorf("AccountID mismatch")
|
||||
}
|
||||
if restored.Email != original.Email {
|
||||
t.Errorf("Email mismatch")
|
||||
}
|
||||
if restored.Expire != original.Expire {
|
||||
t.Errorf("Expire mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodexPKCECodes_Serialize tests PKCE codes serialization.
|
||||
func TestCodexPKCECodes_Serialize(t *testing.T) {
|
||||
original := PKCECodes{
|
||||
CodeVerifier: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
|
||||
CodeChallenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
||||
}
|
||||
|
||||
data, err := json.Marshal(&original)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal PKCE codes: %v", err)
|
||||
}
|
||||
|
||||
var restored PKCECodes
|
||||
if err := json.Unmarshal(data, &restored); err != nil {
|
||||
t.Fatalf("Failed to unmarshal PKCE codes: %v", err)
|
||||
}
|
||||
|
||||
if restored.CodeVerifier != original.CodeVerifier {
|
||||
t.Errorf("CodeVerifier = %v, want %v", restored.CodeVerifier, original.CodeVerifier)
|
||||
}
|
||||
if restored.CodeChallenge != original.CodeChallenge {
|
||||
t.Errorf("CodeChallenge = %v, want %v", restored.CodeChallenge, original.CodeChallenge)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodexToken_ExpireEdgeCases tests edge cases for token expiration.
|
||||
func TestCodexToken_ExpireEdgeCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
expireTime string
|
||||
wantExpire bool
|
||||
}{
|
||||
{
|
||||
name: "very old expiration date",
|
||||
expireTime: "2000-01-01T00:00:00Z",
|
||||
wantExpire: true,
|
||||
},
|
||||
{
|
||||
name: "far future expiration date",
|
||||
expireTime: "2099-12-31T23:59:59Z",
|
||||
wantExpire: false,
|
||||
},
|
||||
{
|
||||
name: "epoch time string",
|
||||
expireTime: "1970-01-01T00:00:00Z",
|
||||
wantExpire: true,
|
||||
},
|
||||
{
|
||||
name: "timezone with offset",
|
||||
expireTime: time.Now().Add(time.Hour).Format(time.RFC3339),
|
||||
wantExpire: false,
|
||||
},
|
||||
{
|
||||
name: "invalid date format - Unix timestamp",
|
||||
expireTime: "1704067200",
|
||||
wantExpire: true, // Parse error should be treated as expired
|
||||
},
|
||||
{
|
||||
name: "invalid date format - random string",
|
||||
expireTime: "not-a-date",
|
||||
wantExpire: true, // Parse error should be treated as expired
|
||||
},
|
||||
{
|
||||
name: "empty string",
|
||||
expireTime: "",
|
||||
wantExpire: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
isExpired := isTokenExpired(tt.expireTime)
|
||||
if isExpired != tt.wantExpire {
|
||||
t.Errorf("isTokenExpired(%q) = %v, want %v", tt.expireTime, isExpired, tt.wantExpire)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodexToken_RefreshThresholdEdgeCases tests edge cases for refresh threshold.
|
||||
func TestCodexToken_RefreshThresholdEdgeCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
expireTime string
|
||||
threshold time.Duration
|
||||
wantNeedRefresh bool
|
||||
}{
|
||||
{
|
||||
name: "zero threshold - not expired",
|
||||
expireTime: time.Now().Add(time.Hour).Format(time.RFC3339),
|
||||
threshold: 0,
|
||||
wantNeedRefresh: false,
|
||||
},
|
||||
{
|
||||
name: "zero threshold - expired",
|
||||
expireTime: time.Now().Add(-time.Hour).Format(time.RFC3339),
|
||||
threshold: 0,
|
||||
wantNeedRefresh: true,
|
||||
},
|
||||
{
|
||||
name: "very large threshold",
|
||||
expireTime: time.Now().Add(24 * time.Hour).Format(time.RFC3339),
|
||||
threshold: 48 * time.Hour,
|
||||
wantNeedRefresh: true,
|
||||
},
|
||||
{
|
||||
name: "exactly at threshold boundary",
|
||||
expireTime: time.Now().Add(5 * time.Minute).Format(time.RFC3339),
|
||||
threshold: 5 * time.Minute,
|
||||
wantNeedRefresh: true,
|
||||
},
|
||||
{
|
||||
name: "just before threshold boundary",
|
||||
expireTime: time.Now().Add(5*time.Minute + time.Second).Format(time.RFC3339),
|
||||
threshold: 5 * time.Minute,
|
||||
wantNeedRefresh: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
needsRefresh := tokenNeedsRefresh(tt.expireTime, tt.threshold)
|
||||
if needsRefresh != tt.wantNeedRefresh {
|
||||
t.Errorf("tokenNeedsRefresh(%q, %v) = %v, want %v",
|
||||
tt.expireTime, tt.threshold, needsRefresh, tt.wantNeedRefresh)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,427 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// getTokenAccessToken extracts access_token from the Token field (which is type any).
|
||||
func getTokenAccessToken(token any) string {
|
||||
if token == nil {
|
||||
return ""
|
||||
}
|
||||
switch t := token.(type) {
|
||||
case map[string]any:
|
||||
if v, ok := t["access_token"]; ok {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
case map[string]string:
|
||||
return t["access_token"]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// TestGeminiTokenStorage_Parse tests parsing of GeminiTokenStorage JSON.
|
||||
func TestGeminiTokenStorage_Parse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
wantErr bool
|
||||
checkFields func(t *testing.T, ts *GeminiTokenStorage)
|
||||
}{
|
||||
{
|
||||
name: "parse complete token storage",
|
||||
jsonData: `{
|
||||
"token": {
|
||||
"access_token": "ya29.test-token",
|
||||
"refresh_token": "1//test-refresh"
|
||||
},
|
||||
"project_id": "my-project-123",
|
||||
"email": "user@example.com",
|
||||
"auto": true,
|
||||
"checked": true,
|
||||
"type": "gemini"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, ts *GeminiTokenStorage) {
|
||||
if getTokenAccessToken(ts.Token) != "ya29.test-token" {
|
||||
t.Errorf("Token[access_token] mismatch, got %v", getTokenAccessToken(ts.Token))
|
||||
}
|
||||
if ts.ProjectID != "my-project-123" {
|
||||
t.Errorf("ProjectID = %v, want my-project-123", ts.ProjectID)
|
||||
}
|
||||
if ts.Email != "user@example.com" {
|
||||
t.Errorf("Email = %v, want user@example.com", ts.Email)
|
||||
}
|
||||
if !ts.Auto {
|
||||
t.Errorf("Auto = false, want true")
|
||||
}
|
||||
if !ts.Checked {
|
||||
t.Errorf("Checked = false, want true")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse minimal token storage",
|
||||
jsonData: `{
|
||||
"token": {"access_token": "minimal-token"},
|
||||
"project_id": "project",
|
||||
"email": "test@test.com"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, ts *GeminiTokenStorage) {
|
||||
if getTokenAccessToken(ts.Token) != "minimal-token" {
|
||||
t.Errorf("Token[access_token] = %v, want minimal-token", getTokenAccessToken(ts.Token))
|
||||
}
|
||||
if ts.Auto {
|
||||
t.Errorf("Auto should be false by default")
|
||||
}
|
||||
if ts.Checked {
|
||||
t.Errorf("Checked should be false by default")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse token with multiple projects (comma-separated)",
|
||||
jsonData: `{
|
||||
"token": {"access_token": "multi-project-token"},
|
||||
"project_id": "project1,project2,project3",
|
||||
"email": "multi@example.com"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, ts *GeminiTokenStorage) {
|
||||
if ts.ProjectID != "project1,project2,project3" {
|
||||
t.Errorf("ProjectID = %v, want project1,project2,project3", ts.ProjectID)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse token with all projects",
|
||||
jsonData: `{
|
||||
"token": {"access_token": "all-projects-token"},
|
||||
"project_id": "all",
|
||||
"email": "all@example.com"
|
||||
}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, ts *GeminiTokenStorage) {
|
||||
if ts.ProjectID != "all" {
|
||||
t.Errorf("ProjectID = %v, want all", ts.ProjectID)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parse empty JSON object",
|
||||
jsonData: `{}`,
|
||||
wantErr: false,
|
||||
checkFields: func(t *testing.T, ts *GeminiTokenStorage) {
|
||||
// Token is of type any, so we need to type assert to check if it's empty
|
||||
if ts.Token != nil {
|
||||
if tokenMap, ok := ts.Token.(map[string]any); ok && len(tokenMap) > 0 {
|
||||
t.Errorf("Token should be nil or empty for empty JSON")
|
||||
}
|
||||
}
|
||||
if ts.ProjectID != "" {
|
||||
t.Errorf("ProjectID should be empty")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid JSON",
|
||||
jsonData: `{"invalid": json}`,
|
||||
wantErr: true,
|
||||
checkFields: func(t *testing.T, ts *GeminiTokenStorage) {
|
||||
// Not called on error
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var ts GeminiTokenStorage
|
||||
err := json.Unmarshal([]byte(tt.jsonData), &ts)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
tt.checkFields(t, &ts)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeminiTokenStorage_Serialize tests JSON serialization round-trip.
|
||||
func TestGeminiTokenStorage_Serialize(t *testing.T) {
|
||||
original := GeminiTokenStorage{
|
||||
Token: map[string]any{
|
||||
"access_token": "ya29.test-access-token",
|
||||
"refresh_token": "1//test-refresh-token",
|
||||
"token_type": "Bearer",
|
||||
},
|
||||
ProjectID: "test-project-456",
|
||||
Email: "serialize@example.com",
|
||||
Auto: true,
|
||||
Checked: true,
|
||||
Type: "gemini",
|
||||
}
|
||||
|
||||
// Serialize to JSON
|
||||
data, err := json.Marshal(&original)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal token: %v", err)
|
||||
}
|
||||
|
||||
// Deserialize back
|
||||
var restored GeminiTokenStorage
|
||||
if err := json.Unmarshal(data, &restored); err != nil {
|
||||
t.Fatalf("Failed to unmarshal token: %v", err)
|
||||
}
|
||||
|
||||
// Verify all fields match
|
||||
if getTokenAccessToken(restored.Token) != "ya29.test-access-token" {
|
||||
t.Errorf("Token[access_token] mismatch")
|
||||
}
|
||||
if restored.ProjectID != original.ProjectID {
|
||||
t.Errorf("ProjectID = %v, want %v", restored.ProjectID, original.ProjectID)
|
||||
}
|
||||
if restored.Email != original.Email {
|
||||
t.Errorf("Email = %v, want %v", restored.Email, original.Email)
|
||||
}
|
||||
if restored.Auto != original.Auto {
|
||||
t.Errorf("Auto = %v, want %v", restored.Auto, original.Auto)
|
||||
}
|
||||
if restored.Checked != original.Checked {
|
||||
t.Errorf("Checked = %v, want %v", restored.Checked, original.Checked)
|
||||
}
|
||||
if restored.Type != original.Type {
|
||||
t.Errorf("Type = %v, want %v", restored.Type, original.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeminiToken_IsValid tests token validation logic.
|
||||
func TestGeminiToken_IsValid(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
token GeminiTokenStorage
|
||||
wantValid bool
|
||||
}{
|
||||
{
|
||||
name: "valid token with access_token",
|
||||
token: GeminiTokenStorage{
|
||||
Token: map[string]any{
|
||||
"access_token": "valid-token",
|
||||
},
|
||||
ProjectID: "project",
|
||||
Email: "user@example.com",
|
||||
},
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "token with empty token map",
|
||||
token: GeminiTokenStorage{
|
||||
Token: map[string]any{},
|
||||
ProjectID: "project",
|
||||
Email: "user@example.com",
|
||||
},
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "token with nil token",
|
||||
token: GeminiTokenStorage{
|
||||
Token: nil,
|
||||
ProjectID: "project",
|
||||
Email: "user@example.com",
|
||||
},
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "token with empty access_token value",
|
||||
token: GeminiTokenStorage{
|
||||
Token: map[string]any{
|
||||
"access_token": "",
|
||||
},
|
||||
ProjectID: "project",
|
||||
Email: "user@example.com",
|
||||
},
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "token with only refresh_token (no access_token)",
|
||||
token: GeminiTokenStorage{
|
||||
Token: map[string]any{
|
||||
"refresh_token": "has-refresh",
|
||||
},
|
||||
ProjectID: "project",
|
||||
},
|
||||
wantValid: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
isValid := getTokenAccessToken(tt.token.Token) != ""
|
||||
if isValid != tt.wantValid {
|
||||
t.Errorf("Token validity = %v, want %v", isValid, tt.wantValid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCredentialFileName_EdgeCases tests edge cases for credential file naming.
|
||||
func TestCredentialFileName_EdgeCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
email string
|
||||
projectID string
|
||||
includePrefix bool
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "empty email",
|
||||
email: "",
|
||||
projectID: "my-project",
|
||||
includePrefix: false,
|
||||
want: "-my-project.json",
|
||||
},
|
||||
{
|
||||
name: "empty project ID",
|
||||
email: "user@example.com",
|
||||
projectID: "",
|
||||
includePrefix: false,
|
||||
want: "user@example.com-.json",
|
||||
},
|
||||
{
|
||||
name: "both empty",
|
||||
email: "",
|
||||
projectID: "",
|
||||
includePrefix: false,
|
||||
want: "-.json",
|
||||
},
|
||||
{
|
||||
name: "unicode email",
|
||||
email: "user@例え.com",
|
||||
projectID: "project",
|
||||
includePrefix: false,
|
||||
want: "user@例え.com-project.json",
|
||||
},
|
||||
{
|
||||
name: "project ID with special chars",
|
||||
email: "user@example.com",
|
||||
projectID: "my_project-123",
|
||||
includePrefix: true,
|
||||
want: "gemini-user@example.com-my_project-123.json",
|
||||
},
|
||||
{
|
||||
name: "all uppercase",
|
||||
email: "USER@EXAMPLE.COM",
|
||||
projectID: "MY-PROJECT",
|
||||
includePrefix: false,
|
||||
want: "USER@EXAMPLE.COM-MY-PROJECT.json",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := CredentialFileName(tt.email, tt.projectID, tt.includePrefix)
|
||||
if got != tt.want {
|
||||
t.Errorf("CredentialFileName(%q, %q, %v) = %q, want %q",
|
||||
tt.email, tt.projectID, tt.includePrefix, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeminiTokenStorage_TokenOperations tests token field operations.
|
||||
func TestGeminiTokenStorage_TokenOperations(t *testing.T) {
|
||||
ts := &GeminiTokenStorage{
|
||||
Token: map[string]any{
|
||||
"access_token": "test-access",
|
||||
"refresh_token": "test-refresh",
|
||||
"token_type": "Bearer",
|
||||
"expires_in": 3600,
|
||||
},
|
||||
}
|
||||
|
||||
tokenMap, ok := ts.Token.(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("Token should be map[string]any")
|
||||
}
|
||||
|
||||
if len(tokenMap) != 4 {
|
||||
t.Errorf("Token map should have 4 entries, got %d", len(tokenMap))
|
||||
}
|
||||
|
||||
// Test retrieving tokens
|
||||
if tokenMap["access_token"] != "test-access" {
|
||||
t.Errorf("access_token mismatch")
|
||||
}
|
||||
if tokenMap["refresh_token"] != "test-refresh" {
|
||||
t.Errorf("refresh_token mismatch")
|
||||
}
|
||||
|
||||
// Test missing key returns nil
|
||||
if tokenMap["nonexistent"] != nil {
|
||||
t.Errorf("nonexistent key should return nil")
|
||||
}
|
||||
|
||||
// Test updating token
|
||||
tokenMap["access_token"] = "updated-access"
|
||||
if tokenMap["access_token"] != "updated-access" {
|
||||
t.Errorf("access_token should be updated")
|
||||
}
|
||||
|
||||
// Test deleting token
|
||||
delete(tokenMap, "expires_in")
|
||||
if len(tokenMap) != 3 {
|
||||
t.Errorf("Token map should have 3 entries after delete, got %d", len(tokenMap))
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeminiTokenStorage_JSONFieldNames tests that JSON field names are correct.
|
||||
func TestGeminiTokenStorage_JSONFieldNames(t *testing.T) {
|
||||
ts := GeminiTokenStorage{
|
||||
Token: map[string]any{"access_token": "test"},
|
||||
ProjectID: "project-123",
|
||||
Email: "test@example.com",
|
||||
Auto: true,
|
||||
Checked: false,
|
||||
Type: "gemini",
|
||||
}
|
||||
|
||||
data, err := json.Marshal(&ts)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
jsonStr := string(data)
|
||||
|
||||
// Check expected JSON field names
|
||||
expectedFields := []string{
|
||||
`"token"`,
|
||||
`"project_id"`,
|
||||
`"email"`,
|
||||
`"auto"`,
|
||||
`"checked"`,
|
||||
`"type"`,
|
||||
}
|
||||
|
||||
for _, field := range expectedFields {
|
||||
if !contains(jsonStr, field) {
|
||||
t.Errorf("JSON should contain field %s, got: %s", field, jsonStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// contains checks if a string contains a substring.
|
||||
func contains(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestNeedConvert tests the NeedConvert function for checking if translation is needed.
|
||||
func TestNeedConvert(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
from string
|
||||
to string
|
||||
wantNeed bool // Note: depends on what translators are registered
|
||||
}{
|
||||
{
|
||||
name: "same format should not need convert",
|
||||
from: "openai",
|
||||
to: "openai",
|
||||
wantNeed: false,
|
||||
},
|
||||
{
|
||||
name: "empty from and to",
|
||||
from: "",
|
||||
to: "",
|
||||
wantNeed: false,
|
||||
},
|
||||
{
|
||||
name: "whitespace only",
|
||||
from: " ",
|
||||
to: " ",
|
||||
wantNeed: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := NeedConvert(tt.from, tt.to)
|
||||
// For same format or empty, should be false
|
||||
if tt.from == tt.to || (tt.from == "" && tt.to == "") {
|
||||
if got != false {
|
||||
t.Errorf("NeedConvert(%q, %q) = %v, want false for identical/empty formats",
|
||||
tt.from, tt.to, got)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRequest_EmptyInput tests Request function with empty inputs.
|
||||
func TestRequest_EmptyInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
from string
|
||||
to string
|
||||
modelName string
|
||||
rawJSON []byte
|
||||
stream bool
|
||||
}{
|
||||
{
|
||||
name: "empty JSON",
|
||||
from: "openai",
|
||||
to: "claude",
|
||||
modelName: "test-model",
|
||||
rawJSON: []byte{},
|
||||
stream: false,
|
||||
},
|
||||
{
|
||||
name: "nil JSON",
|
||||
from: "openai",
|
||||
to: "claude",
|
||||
modelName: "test-model",
|
||||
rawJSON: nil,
|
||||
stream: false,
|
||||
},
|
||||
{
|
||||
name: "empty model name",
|
||||
from: "openai",
|
||||
to: "claude",
|
||||
modelName: "",
|
||||
rawJSON: []byte(`{"messages":[]}`),
|
||||
stream: false,
|
||||
},
|
||||
{
|
||||
name: "empty from format",
|
||||
from: "",
|
||||
to: "claude",
|
||||
modelName: "test",
|
||||
rawJSON: []byte(`{"messages":[]}`),
|
||||
stream: false,
|
||||
},
|
||||
{
|
||||
name: "empty to format",
|
||||
from: "openai",
|
||||
to: "",
|
||||
modelName: "test",
|
||||
rawJSON: []byte(`{"messages":[]}`),
|
||||
stream: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Should not panic with empty/nil inputs
|
||||
result := Request(tt.from, tt.to, tt.modelName, tt.rawJSON, tt.stream)
|
||||
// Result may be nil/empty for unregistered translators, which is fine
|
||||
_ = result
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRequest_PassthroughWhenSameFormat tests that same format returns original.
|
||||
func TestRequest_PassthroughWhenSameFormat(t *testing.T) {
|
||||
originalJSON := []byte(`{"model":"test","messages":[{"role":"user","content":"hello"}]}`)
|
||||
|
||||
result := Request("openai", "openai", "test-model", originalJSON, false)
|
||||
|
||||
// When no translator is registered for same format, the original should be returned
|
||||
// or it may be nil - both are acceptable behaviors
|
||||
if result != nil && len(result) > 0 && string(result) != string(originalJSON) {
|
||||
// The result is different - this is fine if the registry has a passthrough
|
||||
// The key is that it doesn't panic
|
||||
}
|
||||
}
|
||||
|
||||
// TestFormatStrings tests that format string handling is consistent.
|
||||
func TestFormatStrings(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
format string
|
||||
}{
|
||||
{"lowercase", "openai"},
|
||||
{"uppercase", "OPENAI"},
|
||||
{"mixed case", "OpenAI"},
|
||||
{"with hyphen", "openai-responses"},
|
||||
{"with underscore", "openai_responses"},
|
||||
{"claude format", "claude"},
|
||||
{"gemini format", "gemini"},
|
||||
{"gemini-cli format", "gemini-cli"},
|
||||
{"codex format", "codex"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Test that format strings don't cause panics
|
||||
NeedConvert(tt.format, "openai")
|
||||
NeedConvert("openai", tt.format)
|
||||
Request(tt.format, "openai", "test", []byte(`{}`), false)
|
||||
Request("openai", tt.format, "test", []byte(`{}`), false)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRequest_StreamingFlag tests that the streaming flag is handled.
|
||||
func TestRequest_StreamingFlag(t *testing.T) {
|
||||
testJSON := []byte(`{"model":"test","messages":[{"role":"user","content":"hello"}]}`)
|
||||
|
||||
// Test with stream=true
|
||||
resultStream := Request("openai", "claude", "test", testJSON, true)
|
||||
|
||||
// Test with stream=false
|
||||
resultNonStream := Request("openai", "claude", "test", testJSON, false)
|
||||
|
||||
// Both should not panic - the actual difference depends on translator implementation
|
||||
_ = resultStream
|
||||
_ = resultNonStream
|
||||
}
|
||||
Reference in New Issue
Block a user