|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.ai |
| 18 | + |
| 19 | +import com.google.firebase.ai.type.Candidate |
| 20 | +import com.google.firebase.ai.type.Content |
| 21 | +import com.google.firebase.ai.type.FinishReason |
| 22 | +import com.google.firebase.ai.type.GenerateContentResponse |
| 23 | +import com.google.firebase.ai.type.Part |
| 24 | +import com.google.firebase.ai.type.PublicPreviewAPI |
| 25 | +import com.google.firebase.ai.type.TextPart |
| 26 | +import com.google.firebase.ai.type.content |
| 27 | +import io.kotest.matchers.collections.shouldHaveSize |
| 28 | +import io.kotest.matchers.shouldBe |
| 29 | +import io.kotest.matchers.types.shouldBeInstanceOf |
| 30 | +import io.mockk.coEvery |
| 31 | +import io.mockk.every |
| 32 | +import io.mockk.mockk |
| 33 | +import kotlinx.coroutines.flow.flowOf |
| 34 | +import kotlinx.coroutines.flow.toList |
| 35 | +import kotlinx.coroutines.test.runTest |
| 36 | +import org.junit.Before |
| 37 | +import org.junit.Test |
| 38 | +import org.junit.runner.RunWith |
| 39 | +import org.robolectric.RobolectricTestRunner |
| 40 | + |
| 41 | +@OptIn(PublicPreviewAPI::class) |
| 42 | +@RunWith(RobolectricTestRunner::class) |
| 43 | +class TemplateChatTests { |
| 44 | + private val model = mockk<TemplateGenerativeModel>() |
| 45 | + private val templateId = "test-template" |
| 46 | + private val inputs = mapOf("key" to "value") |
| 47 | + |
| 48 | + private lateinit var chat: TemplateChat |
| 49 | + |
| 50 | + @Before |
| 51 | + fun setup() { |
| 52 | + chat = TemplateChat(model, templateId, inputs) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `sendMessage(Content) adds prompt and response to history`() = runTest { |
| 57 | + val prompt = content("user") { text("hello") } |
| 58 | + val responseContent = content("model") { text("hi") } |
| 59 | + val response = createResponse(responseContent) |
| 60 | + |
| 61 | + coEvery { model.generateContentWithHistory(templateId, inputs, any()) } returns response |
| 62 | + |
| 63 | + chat.sendMessage(prompt) |
| 64 | + |
| 65 | + chat.history shouldHaveSize 2 |
| 66 | + chat.history[0] shouldBeEquivalentTo prompt |
| 67 | + chat.history[1] shouldBeEquivalentTo responseContent |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `sendMessageStream(Content) adds prompt and aggregated responses to history`() = runTest { |
| 72 | + val prompt = content("user") { text("hello") } |
| 73 | + val response1 = createResponse(content("model") { text("hi ") }) |
| 74 | + val response2 = createResponse(content("model") { text("there") }) |
| 75 | + |
| 76 | + every { model.generateContentWithHistoryStream(templateId, inputs, any()) } returns |
| 77 | + flowOf(response1, response2) |
| 78 | + |
| 79 | + val flow = chat.sendMessageStream(prompt) |
| 80 | + flow.toList() |
| 81 | + |
| 82 | + chat.history shouldHaveSize 2 |
| 83 | + chat.history[0] shouldBeEquivalentTo prompt |
| 84 | + chat.history[1].parts shouldHaveSize 2 |
| 85 | + chat.history[1].parts[0].shouldBeInstanceOf<TextPart>().text shouldBe "hi " |
| 86 | + chat.history[1].parts[1].shouldBeInstanceOf<TextPart>().text shouldBe "there" |
| 87 | + } |
| 88 | + |
| 89 | + private fun createResponse(content: Content): GenerateContentResponse { |
| 90 | + return GenerateContentResponse.Internal( |
| 91 | + listOf(Candidate.Internal(content.toInternal(), finishReason = FinishReason.Internal.STOP)) |
| 92 | + ) |
| 93 | + .toPublic() |
| 94 | + } |
| 95 | + |
| 96 | + private infix fun Content.shouldBeEquivalentTo(other: Content) { |
| 97 | + this.role shouldBe other.role |
| 98 | + this.parts shouldHaveSize other.parts.size |
| 99 | + this.parts.zip(other.parts).forEach { (a, b) -> a.shouldBeEquivalentTo(b) } |
| 100 | + } |
| 101 | + |
| 102 | + private fun Part.shouldBeEquivalentTo(other: Part) { |
| 103 | + this::class shouldBe other::class |
| 104 | + if (this is TextPart && other is TextPart) { |
| 105 | + this.text shouldBe other.text |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments