@@ -28,6 +28,7 @@ import com.google.firebase.vertexai.type.ResponseStoppedException
28
28
import com.google.firebase.vertexai.type.SerializationException
29
29
import com.google.firebase.vertexai.type.ServerException
30
30
import com.google.firebase.vertexai.type.ServiceDisabledException
31
+ import com.google.firebase.vertexai.type.TextPart
31
32
import com.google.firebase.vertexai.type.UnsupportedUserLocationException
32
33
import com.google.firebase.vertexai.util.goldenUnaryFile
33
34
import com.google.firebase.vertexai.util.shouldNotBeNullOrEmpty
@@ -37,10 +38,12 @@ import io.kotest.matchers.nulls.shouldNotBeNull
37
38
import io.kotest.matchers.should
38
39
import io.kotest.matchers.shouldBe
39
40
import io.kotest.matchers.shouldNotBe
41
+ import io.kotest.matchers.string.shouldNotBeEmpty
40
42
import io.kotest.matchers.types.shouldBeInstanceOf
41
43
import io.ktor.http.HttpStatusCode
42
44
import kotlin.time.Duration.Companion.seconds
43
45
import kotlinx.coroutines.withTimeout
46
+ import org.json.JSONArray
44
47
import org.junit.Test
45
48
46
49
internal class UnarySnapshotTests {
@@ -198,6 +201,26 @@ internal class UnarySnapshotTests {
198
201
}
199
202
}
200
203
204
+ @Test
205
+ fun `properly translates json text` () =
206
+ goldenUnaryFile(" success-constraint-decoding-json.json" ) {
207
+ val response = model.generateContent(" prompt" )
208
+
209
+ response.candidates.isEmpty() shouldBe false
210
+ with (response.candidates.first().content.parts.first().shouldBeInstanceOf<TextPart >()) {
211
+ shouldNotBeNull()
212
+ val jsonArr = JSONArray (text)
213
+ jsonArr.length() shouldBe 3
214
+ for (i in 0 until jsonArr.length()) {
215
+ with (jsonArr.getJSONObject(i)) {
216
+ shouldNotBeNull()
217
+ getString(" name" ).shouldNotBeEmpty()
218
+ getJSONArray(" colors" ).length() shouldBe 5
219
+ }
220
+ }
221
+ }
222
+ }
223
+
201
224
@Test
202
225
fun `invalid response` () =
203
226
goldenUnaryFile(" failure-invalid-response.json" ) {
0 commit comments