From 09a7c2463f7c2a5e0a534b4581d4604e728eec6b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 19 Nov 2025 22:51:12 +0000 Subject: [PATCH 1/2] Add tests for HarmBlockMethod compatibility with GoogleAI and VertexAI Added unit tests to `GenerativeModelTesting.kt` to verify that: 1. Using `HarmBlockMethod` with `GoogleAI` backend throws `InvalidStateException`. 2. Using `HarmBlockMethod` with `VertexAI` backend does not throw `InvalidStateException`. This covers an edge case where `HarmBlockMethod` is only supported by VertexAI. --- .../firebase/ai/GenerativeModelTesting.kt | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt b/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt index e12e133efc8..8de1f644438 100644 --- a/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt +++ b/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt @@ -23,8 +23,14 @@ import com.google.firebase.ai.common.util.doBlocking import com.google.firebase.ai.type.Candidate import com.google.firebase.ai.type.Content import com.google.firebase.ai.type.GenerateContentResponse +import com.google.firebase.ai.type.GenerativeBackend +import com.google.firebase.ai.type.HarmBlockMethod +import com.google.firebase.ai.type.HarmBlockThreshold +import com.google.firebase.ai.type.HarmCategory +import com.google.firebase.ai.type.InvalidStateException import com.google.firebase.ai.type.PublicPreviewAPI import com.google.firebase.ai.type.RequestOptions +import com.google.firebase.ai.type.SafetySetting import com.google.firebase.ai.type.ServerException import com.google.firebase.ai.type.TextPart import com.google.firebase.ai.type.content @@ -146,6 +152,97 @@ internal class GenerativeModelTesting { exception.message shouldContain "location" } + @Test + fun `exception thrown when using HarmBlockMethod with GoogleAI`() = doBlocking { + val mockEngine = MockEngine { + respond( + generateContentResponseAsJsonString("text response"), + HttpStatusCode.OK, + headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + val apiController = + APIController( + "super_cool_test_key", + "gemini-2.5-flash", + RequestOptions(), + mockEngine, + TEST_CLIENT_ID, + mockFirebaseApp, + TEST_VERSION, + TEST_APP_ID, + null, + ) + + val safetySettings = + listOf( + SafetySetting( + HarmCategory.HARASSMENT, + HarmBlockThreshold.MEDIUM_AND_ABOVE, + HarmBlockMethod.SEVERITY + ) + ) + + val generativeModel = + GenerativeModel( + "gemini-2.5-flash", + safetySettings = safetySettings, + generativeBackend = GenerativeBackend.googleAI(), + controller = apiController + ) + + val exception = + shouldThrow { + generativeModel.generateContent("my test prompt") + } + + exception.message shouldContain "HarmBlockMethod is unsupported by the Google Developer API" + } + + @Test + fun `exception NOT thrown when using HarmBlockMethod with VertexAI`() = doBlocking { + val mockEngine = MockEngine { + respond( + generateContentResponseAsJsonString("text response"), + HttpStatusCode.OK, + headersOf(HttpHeaders.ContentType, "application/json") + ) + } + + val apiController = + APIController( + "super_cool_test_key", + "gemini-2.5-flash", + RequestOptions(), + mockEngine, + TEST_CLIENT_ID, + mockFirebaseApp, + TEST_VERSION, + TEST_APP_ID, + null, + ) + + val safetySettings = + listOf( + SafetySetting( + HarmCategory.HARASSMENT, + HarmBlockThreshold.MEDIUM_AND_ABOVE, + HarmBlockMethod.SEVERITY + ) + ) + + val generativeModel = + GenerativeModel( + "gemini-2.5-flash", + safetySettings = safetySettings, + generativeBackend = GenerativeBackend.vertexAI("us-central1"), + controller = apiController + ) + + withTimeout(5.seconds) { generativeModel.generateContent("my test prompt") } + } + @OptIn(PublicPreviewAPI::class) private fun generateContentResponseAsJsonString(text: String): String { return JSON.encodeToString( From c59fa501aabfa51aae2cd3f0d8b96a16e6e52c61 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 20 Nov 2025 16:37:08 +0000 Subject: [PATCH 2/2] Add tests for HarmBlockMethod compatibility with GoogleAI and VertexAI Added unit tests to `GenerativeModelTesting.kt` to verify that: 1. Using `HarmBlockMethod` with `GoogleAI` backend throws `InvalidStateException`. 2. Using `HarmBlockMethod` with `VertexAI` backend does not throw `InvalidStateException`. This covers an edge case where `HarmBlockMethod` is only supported by VertexAI. --- .../java/com/google/firebase/ai/GenerativeModelTesting.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt b/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt index 8de1f644438..582b722376a 100644 --- a/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt +++ b/firebase-ai/src/test/java/com/google/firebase/ai/GenerativeModelTesting.kt @@ -193,9 +193,7 @@ internal class GenerativeModelTesting { ) val exception = - shouldThrow { - generativeModel.generateContent("my test prompt") - } + shouldThrow { generativeModel.generateContent("my test prompt") } exception.message shouldContain "HarmBlockMethod is unsupported by the Google Developer API" }