|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package llminternal |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "google.golang.org/adk/auth" |
| 21 | + contextinternal "google.golang.org/adk/internal/context" |
| 22 | + "google.golang.org/adk/session" |
| 23 | +) |
| 24 | + |
| 25 | +func TestGenerateAuthEvent_Nil(t *testing.T) { |
| 26 | + inv := contextinternal.NewInvocationContext(t.Context(), contextinternal.InvocationContextParams{}) |
| 27 | + |
| 28 | + // Nil event |
| 29 | + result := GenerateAuthEvent(inv, nil) |
| 30 | + if result != nil { |
| 31 | + t.Error("GenerateAuthEvent(nil) should return nil") |
| 32 | + } |
| 33 | + |
| 34 | + // Empty RequestedAuthConfigs |
| 35 | + event := &session.Event{ |
| 36 | + Actions: session.EventActions{ |
| 37 | + RequestedAuthConfigs: make(map[string]*auth.AuthConfig), |
| 38 | + }, |
| 39 | + } |
| 40 | + result = GenerateAuthEvent(inv, event) |
| 41 | + if result != nil { |
| 42 | + t.Error("GenerateAuthEvent with empty RequestedAuthConfigs should return nil") |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Note: TestGenerateAuthEvent_CreatesEvent and TestGenerateAuthEvent_MultipleCalls |
| 47 | +// are skipped as they require full invocation context with agent setup. |
| 48 | +// The GenerateAuthEvent function is tested indirectly through integration tests. |
| 49 | + |
| 50 | +func TestGenerateFunctionCallID(t *testing.T) { |
| 51 | + id1 := generateFunctionCallID() |
| 52 | + id2 := generateFunctionCallID() |
| 53 | + |
| 54 | + if id1 == "" { |
| 55 | + t.Error("generateFunctionCallID() returned empty string") |
| 56 | + } |
| 57 | + if id1 == id2 { |
| 58 | + t.Error("generateFunctionCallID() should return unique IDs") |
| 59 | + } |
| 60 | + if len(id1) < 4 || id1[:4] != "adk-" { |
| 61 | + t.Errorf("generateFunctionCallID() = %q, should start with 'adk-'", id1) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestParseAuthConfigFromMap(t *testing.T) { |
| 66 | + data := map[string]any{ |
| 67 | + "credential_key": "test-key", |
| 68 | + "exchanged_auth_credential": map[string]any{ |
| 69 | + "auth_type": "oauth2", |
| 70 | + "oauth2": map[string]any{ |
| 71 | + "access_token": "token123", |
| 72 | + "refresh_token": "refresh456", |
| 73 | + "expires_at": float64(1234567890), |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + config, err := parseAuthConfigFromMap(data) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("parseAuthConfigFromMap() error = %v", err) |
| 81 | + } |
| 82 | + if config.CredentialKey != "test-key" { |
| 83 | + t.Errorf("CredentialKey = %q, want %q", config.CredentialKey, "test-key") |
| 84 | + } |
| 85 | + if config.ExchangedAuthCredential == nil { |
| 86 | + t.Fatal("ExchangedAuthCredential should not be nil") |
| 87 | + } |
| 88 | + if config.ExchangedAuthCredential.OAuth2 == nil { |
| 89 | + t.Fatal("OAuth2 should not be nil") |
| 90 | + } |
| 91 | + if config.ExchangedAuthCredential.OAuth2.AccessToken != "token123" { |
| 92 | + t.Errorf("AccessToken = %q, want %q", config.ExchangedAuthCredential.OAuth2.AccessToken, "token123") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestParseAuthCredentialFromMap(t *testing.T) { |
| 97 | + data := map[string]any{ |
| 98 | + "auth_type": "oauth2", |
| 99 | + "oauth2": map[string]any{ |
| 100 | + "access_token": "access", |
| 101 | + "refresh_token": "refresh", |
| 102 | + "expires_at": float64(9999999999), |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + cred, err := parseAuthCredentialFromMap(data) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("parseAuthCredentialFromMap() error = %v", err) |
| 109 | + } |
| 110 | + if cred.AuthType != auth.AuthCredentialTypeOAuth2 { |
| 111 | + t.Errorf("AuthType = %v, want %v", cred.AuthType, auth.AuthCredentialTypeOAuth2) |
| 112 | + } |
| 113 | + if cred.OAuth2.AccessToken != "access" { |
| 114 | + t.Errorf("AccessToken = %q, want %q", cred.OAuth2.AccessToken, "access") |
| 115 | + } |
| 116 | + if cred.OAuth2.RefreshToken != "refresh" { |
| 117 | + t.Errorf("RefreshToken = %q, want %q", cred.OAuth2.RefreshToken, "refresh") |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestParseAuthCredentialFromMap_NotAMap(t *testing.T) { |
| 122 | + _, err := parseAuthCredentialFromMap("not a map") |
| 123 | + if err == nil { |
| 124 | + t.Error("parseAuthCredentialFromMap() should error for non-map input") |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestAuthPreprocessorResult_Init(t *testing.T) { |
| 129 | + // Verify CurrentAuthPreprocessorResult starts as nil |
| 130 | + if CurrentAuthPreprocessorResult != nil { |
| 131 | + // Clear it for test isolation |
| 132 | + CurrentAuthPreprocessorResult = nil |
| 133 | + } |
| 134 | + |
| 135 | + result := &AuthPreprocessorResult{ |
| 136 | + ToolIdsToResume: make(map[string]bool), |
| 137 | + CredentialsStored: true, |
| 138 | + OriginalEvent: &session.Event{}, |
| 139 | + } |
| 140 | + |
| 141 | + if result.ToolIdsToResume == nil { |
| 142 | + t.Error("ToolIdsToResume should not be nil") |
| 143 | + } |
| 144 | + if !result.CredentialsStored { |
| 145 | + t.Error("CredentialsStored should be true") |
| 146 | + } |
| 147 | + if result.OriginalEvent == nil { |
| 148 | + t.Error("OriginalEvent should not be nil") |
| 149 | + } |
| 150 | +} |
0 commit comments