|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import FirebaseVertexAI |
| 16 | +import XCTest |
| 17 | + |
| 18 | +@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
| 19 | +final class ModalityTokenCountTests: XCTestCase { |
| 20 | + let decoder = JSONDecoder() |
| 21 | + |
| 22 | + // MARK: - Decoding Tests |
| 23 | + |
| 24 | + func testDecodeModalityTokenCount_valid() throws { |
| 25 | + let json = """ |
| 26 | + { |
| 27 | + "modality": "TEXT", |
| 28 | + "tokenCount": 123 |
| 29 | + } |
| 30 | + """ |
| 31 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 32 | + |
| 33 | + let tokenCount = try decoder.decode(ModalityTokenCount.self, from: jsonData) |
| 34 | + |
| 35 | + XCTAssertEqual(tokenCount.modality, .text) |
| 36 | + XCTAssertEqual(tokenCount.modality.rawValue, "TEXT") |
| 37 | + XCTAssertEqual(tokenCount.tokenCount, 123) |
| 38 | + } |
| 39 | + |
| 40 | + func testDecodeModalityTokenCount_missingTokenCount_defaultsToZero() throws { |
| 41 | + let json = """ |
| 42 | + { |
| 43 | + "modality": "AUDIO" |
| 44 | + } |
| 45 | + """ |
| 46 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 47 | + |
| 48 | + let tokenCount = try decoder.decode(ModalityTokenCount.self, from: jsonData) |
| 49 | + |
| 50 | + XCTAssertEqual(tokenCount.modality, .audio) |
| 51 | + XCTAssertEqual(tokenCount.modality.rawValue, "AUDIO") |
| 52 | + XCTAssertEqual(tokenCount.tokenCount, 0) |
| 53 | + } |
| 54 | + |
| 55 | + func testDecodeModalityTokenCount_unrecognizedModalityString_succeeds() throws { |
| 56 | + let newModality = "NEW_MODALITY_NAME" |
| 57 | + let json = """ |
| 58 | + { |
| 59 | + "modality": "\(newModality)", |
| 60 | + "tokenCount": 50 |
| 61 | + } |
| 62 | + """ |
| 63 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 64 | + |
| 65 | + let tokenCount = try decoder.decode(ModalityTokenCount.self, from: jsonData) |
| 66 | + |
| 67 | + XCTAssertEqual(tokenCount.tokenCount, 50) |
| 68 | + XCTAssertEqual(tokenCount.modality.rawValue, newModality) |
| 69 | + } |
| 70 | + |
| 71 | + func testDecodeModalityTokenCount_missingModalityKey_throws() throws { |
| 72 | + let json = """ |
| 73 | + { |
| 74 | + "tokenCount": 50 |
| 75 | + } |
| 76 | + """ |
| 77 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 78 | + |
| 79 | + do { |
| 80 | + _ = try decoder.decode(ModalityTokenCount.self, from: jsonData) |
| 81 | + XCTFail("Expected a DecodingError, but decoding succeeded.") |
| 82 | + } catch let DecodingError.keyNotFound(key, _) { |
| 83 | + XCTAssertEqual(key.stringValue, "modality") |
| 84 | + } catch { |
| 85 | + XCTFail("Expected a DecodingError.keyNotFound, but received \(error)") |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments