|
| 1 | +/* |
| 2 | + * Copyright 2025 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.type |
| 18 | + |
| 19 | +import io.kotest.assertions.json.shouldEqualJson |
| 20 | +import kotlinx.serialization.encodeToString |
| 21 | +import kotlinx.serialization.json.Json |
| 22 | +import org.junit.Test |
| 23 | + |
| 24 | +internal class FunctionDeclarationTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `Basic FunctionDeclaration with name, description and parameters`() { |
| 28 | + val functionDeclaration = |
| 29 | + FunctionDeclaration( |
| 30 | + name = "isUserAGoat", |
| 31 | + description = "Determine if the user is subject to teleportations.", |
| 32 | + parameters = mapOf("userID" to Schema.string("ID of the User making the call")) |
| 33 | + ) |
| 34 | + |
| 35 | + val expectedJson = |
| 36 | + """ |
| 37 | + { |
| 38 | + "name": "isUserAGoat", |
| 39 | + "description": "Determine if the user is subject to teleportations.", |
| 40 | + "parameters": { |
| 41 | + "type": "OBJECT", |
| 42 | + "properties": { |
| 43 | + "userID": { |
| 44 | + "type": "STRING", |
| 45 | + "description": "ID of the User making the call" |
| 46 | + } |
| 47 | + }, |
| 48 | + "required": [ |
| 49 | + "userID" |
| 50 | + ] |
| 51 | + } |
| 52 | + } |
| 53 | + """ |
| 54 | + .trimIndent() |
| 55 | + |
| 56 | + Json.encodeToString(functionDeclaration.toInternal()).shouldEqualJson(expectedJson) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `FunctionDeclaration with optional parameters`() { |
| 61 | + val functionDeclaration = |
| 62 | + FunctionDeclaration( |
| 63 | + name = "isUserAGoat", |
| 64 | + description = "Determine if the user is subject to teleportations.", |
| 65 | + parameters = |
| 66 | + mapOf( |
| 67 | + "userID" to Schema.string("ID of the user making the call"), |
| 68 | + "userName" to Schema.string("Name of the user making the call") |
| 69 | + ), |
| 70 | + optionalParameters = listOf("userName") |
| 71 | + ) |
| 72 | + |
| 73 | + val expectedJson = |
| 74 | + """ |
| 75 | + { |
| 76 | + "name": "isUserAGoat", |
| 77 | + "description": "Determine if the user is subject to teleportations.", |
| 78 | + "parameters": { |
| 79 | + "type": "OBJECT", |
| 80 | + "properties": { |
| 81 | + "userID": { |
| 82 | + "type": "STRING", |
| 83 | + "description": "ID of the user making the call" |
| 84 | + }, |
| 85 | + "userName": { |
| 86 | + "type": "STRING", |
| 87 | + "description": "Name of the user making the call" |
| 88 | + } |
| 89 | + }, |
| 90 | + "required": [ |
| 91 | + "userID" |
| 92 | + ] |
| 93 | + } |
| 94 | + } |
| 95 | + """ |
| 96 | + .trimIndent() |
| 97 | + |
| 98 | + Json.encodeToString(functionDeclaration.toInternal()).shouldEqualJson(expectedJson) |
| 99 | + } |
| 100 | +} |
0 commit comments