Skip to content

Commit 0a7329f

Browse files
committed
Add mock response tests to GenerativeModelGoogleAITests
1 parent 98d14cb commit 0a7329f

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

FirebaseAI/Tests/Unit/GenerativeModelGoogleAITests.swift

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,52 @@ final class GenerativeModelGoogleAITests: XCTestCase {
262262
)
263263
}
264264

265+
func testGenerateContent_success_thinking_thoughtSummary() async throws {
266+
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
267+
forResource: "unary-success-thinking-reply-thought-summary",
268+
withExtension: "json",
269+
subdirectory: googleAISubdirectory
270+
)
271+
272+
let response = try await model.generateContent(testPrompt)
273+
274+
XCTAssertEqual(response.candidates.count, 1)
275+
let candidate = try XCTUnwrap(response.candidates.first)
276+
XCTAssertEqual(candidate.content.parts.count, 2)
277+
let thoughtPart = try XCTUnwrap(candidate.content.parts.first as? TextPart)
278+
XCTAssertTrue(thoughtPart.isThought)
279+
XCTAssertTrue(thoughtPart.text.hasPrefix("**Thinking About Google's Headquarters**"))
280+
XCTAssertEqual(thoughtPart.text, response.thoughtSummary)
281+
let textPart = try XCTUnwrap(candidate.content.parts.last as? TextPart)
282+
XCTAssertFalse(textPart.isThought)
283+
XCTAssertEqual(textPart.text, "Mountain View")
284+
XCTAssertEqual(textPart.text, response.text)
285+
}
286+
287+
func testGenerateContent_success_thinking_functionCall_thoughtSummaryAndSignature() async throws {
288+
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
289+
forResource: "unary-success-thinking-function-call-thought-summary-signature",
290+
withExtension: "json",
291+
subdirectory: googleAISubdirectory
292+
)
293+
294+
let response = try await model.generateContent(testPrompt)
295+
296+
XCTAssertEqual(response.candidates.count, 1)
297+
let candidate = try XCTUnwrap(response.candidates.first)
298+
XCTAssertEqual(candidate.finishReason, .stop)
299+
XCTAssertEqual(candidate.content.parts.count, 2)
300+
let thoughtPart = try XCTUnwrap(candidate.content.parts.first as? TextPart)
301+
XCTAssertTrue(thoughtPart.isThought)
302+
XCTAssertTrue(thoughtPart.text.hasPrefix("**Thinking Through the New Year's Eve Calculation**"))
303+
let functionCallPart = try XCTUnwrap(candidate.content.parts.last as? FunctionCallPart)
304+
XCTAssertFalse(functionCallPart.isThought)
305+
XCTAssertEqual(functionCallPart.name, "now")
306+
XCTAssertTrue(functionCallPart.args.isEmpty)
307+
let thoughtSignature = try XCTUnwrap(functionCallPart.thoughtSignature)
308+
XCTAssertTrue(thoughtSignature.hasPrefix("CtQOAVSoXO74PmYr9AFu"))
309+
}
310+
265311
func testGenerateContent_failure_invalidAPIKey() async throws {
266312
let expectedStatusCode = 400
267313
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
@@ -397,6 +443,72 @@ final class GenerativeModelGoogleAITests: XCTestCase {
397443
XCTAssertNil(citation.publicationDate)
398444
}
399445

446+
func testGenerateContentStream_successWithThoughtSummary() async throws {
447+
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
448+
forResource: "streaming-success-thinking-reply-thought-summary",
449+
withExtension: "txt",
450+
subdirectory: googleAISubdirectory
451+
)
452+
453+
var thoughtSummary = ""
454+
var text = ""
455+
let stream = try model.generateContentStream("Hi")
456+
for try await response in stream {
457+
let candidate = try XCTUnwrap(response.candidates.first)
458+
XCTAssertEqual(candidate.content.parts.count, 1)
459+
let textPart = try XCTUnwrap(candidate.content.parts.first as? TextPart)
460+
if textPart.isThought {
461+
let newThought = try XCTUnwrap(response.thoughtSummary)
462+
XCTAssertEqual(textPart.text, newThought)
463+
thoughtSummary.append(newThought)
464+
} else {
465+
let newText = try XCTUnwrap(response.text)
466+
XCTAssertEqual(textPart.text, newText)
467+
text.append(newText)
468+
}
469+
}
470+
471+
XCTAssertTrue(thoughtSummary.hasPrefix("**Exploring Sky Color**"))
472+
XCTAssertTrue(text.hasPrefix("The sky is blue because"))
473+
}
474+
475+
func testGenerateContentStream_success_thinking_functionCall_thoughtSummary_signature() async throws {
476+
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
477+
forResource: "streaming-success-thinking-function-call-thought-summary-signature",
478+
withExtension: "txt",
479+
subdirectory: googleAISubdirectory
480+
)
481+
482+
var thoughtSummary = ""
483+
var functionCalls: [FunctionCallPart] = []
484+
let stream = try model.generateContentStream("Hi")
485+
for try await response in stream {
486+
let candidate = try XCTUnwrap(response.candidates.first)
487+
XCTAssertEqual(candidate.content.parts.count, 1)
488+
let part = try XCTUnwrap(candidate.content.parts.first)
489+
if part.isThought {
490+
let textPart = try XCTUnwrap(part as? TextPart)
491+
let newThought = try XCTUnwrap(response.thoughtSummary)
492+
XCTAssertEqual(textPart.text, newThought)
493+
thoughtSummary.append(newThought)
494+
} else {
495+
let functionCallPart = try XCTUnwrap(part as? FunctionCallPart)
496+
XCTAssertEqual(response.functionCalls.count, 1)
497+
let newFunctionCall = try XCTUnwrap(response.functionCalls.first)
498+
XCTAssertEqual(functionCallPart, newFunctionCall)
499+
functionCalls.append(newFunctionCall)
500+
}
501+
}
502+
503+
XCTAssertTrue(thoughtSummary.hasPrefix("**Calculating the Days**"))
504+
XCTAssertEqual(functionCalls.count, 1)
505+
let functionCall = try XCTUnwrap(functionCalls.first)
506+
XCTAssertEqual(functionCall.name, "now")
507+
XCTAssertTrue(functionCall.args.isEmpty)
508+
let thoughtSignature = try XCTUnwrap(functionCall.thoughtSignature)
509+
XCTAssertTrue(thoughtSignature.hasPrefix("CiIBVKhc7vB+vaaq6rA"))
510+
}
511+
400512
func testGenerateContentStream_failureInvalidAPIKey() async throws {
401513
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
402514
forResource: "unary-failure-api-key",

0 commit comments

Comments
 (0)