From 22d4d404eb2844493ac4d75fb34100f7cea94fe5 Mon Sep 17 00:00:00 2001 From: Tanzim Hossain Date: Wed, 10 Jul 2024 16:58:45 -0400 Subject: [PATCH 1/4] add test-only APIController constructor --- common/build.gradle.kts | 1 + .../generativeai/common/APIController.kt | 24 +++++++++++++++++++ .../client/generativeai/common/util/tests.kt | 10 ++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/common/build.gradle.kts b/common/build.gradle.kts index bd02f5fa..f73c37cd 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -95,6 +95,7 @@ dependencies { testImplementation("io.kotest:kotest-assertions-jvm:4.0.7") testImplementation("io.kotest:kotest-assertions-json:4.0.7") testImplementation("io.ktor:ktor-client-mock:$ktorVersion") + compileOnly("io.ktor:ktor-client-mock:$ktorVersion") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") } diff --git a/common/src/main/kotlin/com/google/ai/client/generativeai/common/APIController.kt b/common/src/main/kotlin/com/google/ai/client/generativeai/common/APIController.kt index d815f44a..aaf79634 100644 --- a/common/src/main/kotlin/com/google/ai/client/generativeai/common/APIController.kt +++ b/common/src/main/kotlin/com/google/ai/client/generativeai/common/APIController.kt @@ -17,12 +17,15 @@ package com.google.ai.client.generativeai.common import android.util.Log +import androidx.annotation.VisibleForTesting import com.google.ai.client.generativeai.common.server.FinishReason import com.google.ai.client.generativeai.common.util.decodeToFlow import com.google.ai.client.generativeai.common.util.fullModelName import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.engine.HttpClientEngine +import io.ktor.client.engine.mock.MockEngine +import io.ktor.client.engine.mock.respond import io.ktor.client.engine.okhttp.OkHttp import io.ktor.client.plugins.HttpTimeout import io.ktor.client.plugins.contentnegotiation.ContentNegotiation @@ -35,9 +38,12 @@ import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.bodyAsChannel import io.ktor.client.statement.bodyAsText import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode import io.ktor.http.contentType +import io.ktor.http.headersOf import io.ktor.serialization.kotlinx.json.json +import io.ktor.utils.io.ByteChannel import kotlin.time.Duration import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.TimeoutCancellationException @@ -85,6 +91,24 @@ internal constructor( headerProvider: HeaderProvider? = null, ) : this(key, model, requestOptions, OkHttp.create(), apiClient, headerProvider) + @VisibleForTesting(otherwise = VisibleForTesting.NONE) + constructor( + key: String, + model: String, + requestOptions: RequestOptions, + apiClient: String, + headerProvider: HeaderProvider?, + channel: ByteChannel, + status: HttpStatusCode, + ) : this( + key, + model, + requestOptions, + MockEngine { respond(channel, status, headersOf(HttpHeaders.ContentType, "application/json")) }, + apiClient, + headerProvider, + ) + private val model = fullModelName(model) private val client = diff --git a/common/src/test/java/com/google/ai/client/generativeai/common/util/tests.kt b/common/src/test/java/com/google/ai/client/generativeai/common/util/tests.kt index fa84d258..0726d959 100644 --- a/common/src/test/java/com/google/ai/client/generativeai/common/util/tests.kt +++ b/common/src/test/java/com/google/ai/client/generativeai/common/util/tests.kt @@ -28,11 +28,7 @@ import com.google.ai.client.generativeai.common.shared.Content import com.google.ai.client.generativeai.common.shared.TextPart import io.kotest.matchers.collections.shouldNotBeEmpty import io.kotest.matchers.nulls.shouldNotBeNull -import io.ktor.client.engine.mock.MockEngine -import io.ktor.client.engine.mock.respond -import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode -import io.ktor.http.headersOf import io.ktor.utils.io.ByteChannel import io.ktor.utils.io.close import io.ktor.utils.io.writeFully @@ -105,17 +101,15 @@ internal fun commonTest( block: CommonTest, ) = doBlocking { val channel = ByteChannel(autoFlush = true) - val mockEngine = MockEngine { - respond(channel, status, headersOf(HttpHeaders.ContentType, "application/json")) - } val apiController = APIController( "super_cool_test_key", "gemini-pro", requestOptions, - mockEngine, TEST_CLIENT_ID, null, + channel, + status, ) CommonTestScope(channel, apiController).block() } From 8b5404e14c46692cfc337acc4d7d56edc3fc3856 Mon Sep 17 00:00:00 2001 From: Tanzim Hossain Date: Wed, 10 Jul 2024 17:10:26 -0400 Subject: [PATCH 2/4] add .changes --- .changes/common/crowd-company-bead-authority.json | 1 + .changes/generativeai/cellar-cobweb-bait-angle.json | 1 + 2 files changed, 2 insertions(+) create mode 100644 .changes/common/crowd-company-bead-authority.json create mode 100644 .changes/generativeai/cellar-cobweb-bait-angle.json diff --git a/.changes/common/crowd-company-bead-authority.json b/.changes/common/crowd-company-bead-authority.json new file mode 100644 index 00000000..8868b7e1 --- /dev/null +++ b/.changes/common/crowd-company-bead-authority.json @@ -0,0 +1 @@ +{"type":"MINOR","changes":["Add test-only APIController constructor for Vertex AI unit tests"]} diff --git a/.changes/generativeai/cellar-cobweb-bait-angle.json b/.changes/generativeai/cellar-cobweb-bait-angle.json new file mode 100644 index 00000000..04f4e298 --- /dev/null +++ b/.changes/generativeai/cellar-cobweb-bait-angle.json @@ -0,0 +1 @@ +{"type":"PATCH","changes":["Add test-only APIController constructor for Vertex AI unit tests"]} From 6cf416be2dbed3ce4bd8e3fad777d5f30e9cec15 Mon Sep 17 00:00:00 2001 From: Tanzim Hossain Date: Wed, 10 Jul 2024 19:17:00 -0400 Subject: [PATCH 3/4] move dependency line --- common/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/build.gradle.kts b/common/build.gradle.kts index f73c37cd..842e9301 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -90,12 +90,12 @@ dependencies { implementation("com.google.guava:listenablefuture:1.0") implementation("androidx.concurrent:concurrent-futures:1.2.0-alpha02") implementation("androidx.concurrent:concurrent-futures-ktx:1.2.0-alpha02") + compileOnly("io.ktor:ktor-client-mock:$ktorVersion") testImplementation("junit:junit:4.13.2") testImplementation("io.kotest:kotest-assertions-core:4.0.7") testImplementation("io.kotest:kotest-assertions-jvm:4.0.7") testImplementation("io.kotest:kotest-assertions-json:4.0.7") testImplementation("io.ktor:ktor-client-mock:$ktorVersion") - compileOnly("io.ktor:ktor-client-mock:$ktorVersion") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") } From 21ea57b696fe1f34707e6aae1e3e7f71b115ebaf Mon Sep 17 00:00:00 2001 From: Tanzim Hossain Date: Wed, 10 Jul 2024 19:59:03 -0400 Subject: [PATCH 4/4] remove generativeai change file --- .changes/generativeai/cellar-cobweb-bait-angle.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .changes/generativeai/cellar-cobweb-bait-angle.json diff --git a/.changes/generativeai/cellar-cobweb-bait-angle.json b/.changes/generativeai/cellar-cobweb-bait-angle.json deleted file mode 100644 index 04f4e298..00000000 --- a/.changes/generativeai/cellar-cobweb-bait-angle.json +++ /dev/null @@ -1 +0,0 @@ -{"type":"PATCH","changes":["Add test-only APIController constructor for Vertex AI unit tests"]}