Skip to content

Commit 98d14cb

Browse files
committed
Add mock response tests to GenerativeModelVertexAITests
1 parent fcdee03 commit 98d14cb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

FirebaseAI/Tests/Unit/GenerativeModelVertexAITests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,29 @@ final class GenerativeModelVertexAITests: XCTestCase {
434434
XCTAssertEqual(text, "The sum of [1, 2, 3] is")
435435
}
436436

437+
func testGenerateContent_success_thinking_thoughtSummary() async throws {
438+
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
439+
forResource: "unary-success-thinking-reply-thought-summary",
440+
withExtension: "json",
441+
subdirectory: vertexSubdirectory
442+
)
443+
444+
let response = try await model.generateContent(testPrompt)
445+
446+
XCTAssertEqual(response.candidates.count, 1)
447+
let candidate = try XCTUnwrap(response.candidates.first)
448+
XCTAssertEqual(candidate.finishReason, .stop)
449+
XCTAssertEqual(candidate.content.parts.count, 2)
450+
let thoughtPart = try XCTUnwrap(candidate.content.parts.first as? TextPart)
451+
XCTAssertTrue(thoughtPart.isThought)
452+
XCTAssertTrue(thoughtPart.text.hasPrefix("Right, someone needs the city where Google"))
453+
XCTAssertEqual(response.thoughtSummary, thoughtPart.text)
454+
let textPart = try XCTUnwrap(candidate.content.parts.last as? TextPart)
455+
XCTAssertFalse(textPart.isThought)
456+
XCTAssertEqual(textPart.text, "Mountain View")
457+
XCTAssertEqual(response.text, textPart.text)
458+
}
459+
437460
func testGenerateContent_success_image_invalidSafetyRatingsIgnored() async throws {
438461
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
439462
forResource: "unary-success-image-invalid-safety-ratings",
@@ -1330,6 +1353,33 @@ final class GenerativeModelVertexAITests: XCTestCase {
13301353
XCTAssertFalse(citations.contains { $0.license?.isEmpty ?? false })
13311354
}
13321355

1356+
func testGenerateContentStream_successWithThinking_thoughtSummary() async throws {
1357+
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
1358+
forResource: "streaming-success-thinking-reply-thought-summary",
1359+
withExtension: "txt",
1360+
subdirectory: vertexSubdirectory
1361+
)
1362+
1363+
var thoughtSummary = ""
1364+
var text = ""
1365+
let stream = try model.generateContentStream("Hi")
1366+
for try await response in stream {
1367+
let candidate = try XCTUnwrap(response.candidates.first)
1368+
XCTAssertEqual(candidate.content.parts.count, 1)
1369+
let part = try XCTUnwrap(candidate.content.parts.first)
1370+
let textPart = try XCTUnwrap(part as? TextPart)
1371+
if textPart.isThought {
1372+
let newThought = try XCTUnwrap(response.thoughtSummary)
1373+
thoughtSummary.append(newThought)
1374+
} else {
1375+
text.append(textPart.text)
1376+
}
1377+
}
1378+
1379+
XCTAssertTrue(thoughtSummary.hasPrefix("**Understanding the Core Question**"))
1380+
XCTAssertTrue(text.hasPrefix("The sky is blue due to a phenomenon"))
1381+
}
1382+
13331383
func testGenerateContentStream_successWithInvalidSafetyRatingsIgnored() async throws {
13341384
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
13351385
forResource: "streaming-success-image-invalid-safety-ratings",

0 commit comments

Comments
 (0)