From 5b68c7bb860fd692a294ac64b7bd1fe1f8839ff3 Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Wed, 9 Apr 2025 16:51:08 -0700 Subject: [PATCH 1/2] Minimize signins in VertexAI integration tests --- .../GenerateContentIntegrationTests.swift | 15 +++++++++------ .../Integration/ImagenIntegrationTests.swift | 14 +++++++++----- .../Tests/Integration/IntegrationTests.swift | 14 +++++++++----- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift index 715be6e3e32..1a39615e10e 100644 --- a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift +++ b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift @@ -43,12 +43,15 @@ struct GenerateContentIntegrationTests { let userID1: String init() async throws { - let authResult = try await Auth.auth().signIn( - withEmail: Credentials.emailAddress1, - password: Credentials.emailPassword1 - ) - userID1 = authResult.user.uid - + if let user = Auth.auth().currentUser { + userID1 = user.uid + } else { + let authResult = try await Auth.auth().signIn( + withEmail: Credentials.emailAddress1, + password: Credentials.emailPassword1 + ) + userID1 = authResult.user.uid + } storage = Storage.storage() } diff --git a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift index 5b2587743d4..5fb5a254417 100644 --- a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift +++ b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift @@ -39,11 +39,15 @@ struct ImagenIntegrationTests { var userID1: String init() async throws { - let authResult = try await Auth.auth().signIn( - withEmail: Credentials.emailAddress1, - password: Credentials.emailPassword1 - ) - userID1 = authResult.user.uid + if let user = Auth.auth().currentUser { + userID1 = user.uid + } else { + let authResult = try await Auth.auth().signIn( + withEmail: Credentials.emailAddress1, + password: Credentials.emailPassword1 + ) + userID1 = authResult.user.uid + } vertex = VertexAI.vertexAI() storage = Storage.storage() diff --git a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift index 05cc71cde5b..27f6136dc15 100644 --- a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift +++ b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift @@ -48,11 +48,15 @@ final class IntegrationTests: XCTestCase { var userID1 = "" override func setUp() async throws { - let authResult = try await Auth.auth().signIn( - withEmail: Credentials.emailAddress1, - password: Credentials.emailPassword1 - ) - userID1 = authResult.user.uid + if let user = Auth.auth().currentUser { + userID1 = user.uid + } else { + let authResult = try await Auth.auth().signIn( + withEmail: Credentials.emailAddress1, + password: Credentials.emailPassword1 + ) + userID1 = authResult.user.uid + } vertex = VertexAI.vertexAI() model = vertex.generativeModel( From 167f3850ca24fa1e7fee6ad41845f158e19835bc Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Wed, 9 Apr 2025 18:13:58 -0700 Subject: [PATCH 2/2] review --- .../GenerateContentIntegrationTests.swift | 10 +------ .../Integration/ImagenIntegrationTests.swift | 11 +------ .../Tests/Integration/IntegrationTests.swift | 11 +------ .../Tests/Integration/TestHelpers.swift | 30 +++++++++++++++++++ .../VertexAITestApp.xcodeproj/project.pbxproj | 4 +++ 5 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 FirebaseVertexAI/Tests/TestApp/Tests/Integration/TestHelpers.swift diff --git a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift index 1a39615e10e..f1650f26471 100644 --- a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift +++ b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift @@ -43,15 +43,7 @@ struct GenerateContentIntegrationTests { let userID1: String init() async throws { - if let user = Auth.auth().currentUser { - userID1 = user.uid - } else { - let authResult = try await Auth.auth().signIn( - withEmail: Credentials.emailAddress1, - password: Credentials.emailPassword1 - ) - userID1 = authResult.user.uid - } + userID1 = try await TestHelpers.getUserID() storage = Storage.storage() } diff --git a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift index 5fb5a254417..05317411704 100644 --- a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift +++ b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.swift @@ -39,16 +39,7 @@ struct ImagenIntegrationTests { var userID1: String init() async throws { - if let user = Auth.auth().currentUser { - userID1 = user.uid - } else { - let authResult = try await Auth.auth().signIn( - withEmail: Credentials.emailAddress1, - password: Credentials.emailPassword1 - ) - userID1 = authResult.user.uid - } - + userID1 = try await TestHelpers.getUserID() vertex = VertexAI.vertexAI() storage = Storage.storage() } diff --git a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift index 27f6136dc15..af2c7c97d98 100644 --- a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift +++ b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift @@ -48,16 +48,7 @@ final class IntegrationTests: XCTestCase { var userID1 = "" override func setUp() async throws { - if let user = Auth.auth().currentUser { - userID1 = user.uid - } else { - let authResult = try await Auth.auth().signIn( - withEmail: Credentials.emailAddress1, - password: Credentials.emailPassword1 - ) - userID1 = authResult.user.uid - } - + userID1 = try await TestHelpers.getUserID() vertex = VertexAI.vertexAI() model = vertex.generativeModel( modelName: "gemini-2.0-flash", diff --git a/FirebaseVertexAI/Tests/TestApp/Tests/Integration/TestHelpers.swift b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/TestHelpers.swift new file mode 100644 index 00000000000..4dd4c11842d --- /dev/null +++ b/FirebaseVertexAI/Tests/TestApp/Tests/Integration/TestHelpers.swift @@ -0,0 +1,30 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import FirebaseAuth +import FirebaseCore + +enum TestHelpers { + static func getUserID() async throws -> String { + if let user = Auth.auth().currentUser { + return user.uid + } else { + let authResult = try await Auth.auth().signIn( + withEmail: Credentials.emailAddress1, + password: Credentials.emailPassword1 + ) + return authResult.user.uid + } + } +} diff --git a/FirebaseVertexAI/Tests/TestApp/VertexAITestApp.xcodeproj/project.pbxproj b/FirebaseVertexAI/Tests/TestApp/VertexAITestApp.xcodeproj/project.pbxproj index 0012ad35261..a7d2bf023b2 100644 --- a/FirebaseVertexAI/Tests/TestApp/VertexAITestApp.xcodeproj/project.pbxproj +++ b/FirebaseVertexAI/Tests/TestApp/VertexAITestApp.xcodeproj/project.pbxproj @@ -27,6 +27,7 @@ 86D77DFE2D7B5C86003D155D /* GoogleService-Info-Spark.plist in Resources */ = {isa = PBXBuildFile; fileRef = 86D77DFD2D7B5C86003D155D /* GoogleService-Info-Spark.plist */; }; 86D77E022D7B63AF003D155D /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86D77E012D7B63AC003D155D /* Constants.swift */; }; 86D77E042D7B6C9D003D155D /* InstanceConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86D77E032D7B6C95003D155D /* InstanceConfig.swift */; }; + DEF0BB4F2DA74F680093E9F4 /* TestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF0BB4E2DA74F460093E9F4 /* TestHelpers.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -59,6 +60,7 @@ 86D77DFD2D7B5C86003D155D /* GoogleService-Info-Spark.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GoogleService-Info-Spark.plist"; sourceTree = ""; }; 86D77E012D7B63AC003D155D /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; 86D77E032D7B6C95003D155D /* InstanceConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceConfig.swift; sourceTree = ""; }; + DEF0BB4E2DA74F460093E9F4 /* TestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHelpers.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -137,6 +139,7 @@ 868A7C572CCC27AF00E449DD /* Integration */ = { isa = PBXGroup; children = ( + DEF0BB4E2DA74F460093E9F4 /* TestHelpers.swift */, 8689CDCB2D7F8BCF00BF426B /* CountTokensIntegrationTests.swift */, 868A7C4D2CCC1F4700E449DD /* Credentials.swift */, 8661386D2CC943DE00F4B78E /* IntegrationTests.swift */, @@ -289,6 +292,7 @@ files = ( 8689CDCC2D7F8BD700BF426B /* CountTokensIntegrationTests.swift in Sources */, 86D77E042D7B6C9D003D155D /* InstanceConfig.swift in Sources */, + DEF0BB4F2DA74F680093E9F4 /* TestHelpers.swift in Sources */, 868A7C4F2CCC229F00E449DD /* Credentials.swift in Sources */, 864F8F712D4980DD0002EA7E /* ImagenIntegrationTests.swift in Sources */, 862218812D04E098007ED2D4 /* IntegrationTestUtils.swift in Sources */,