Skip to content

Commit d61b46a

Browse files
authored
[Vertex AI] Add integration test for generateContentStream (#14501)
1 parent 4c45213 commit d61b46a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,45 @@ final class IntegrationTests: XCTestCase {
9090
XCTAssertEqual(candidatesTokensDetails.tokenCount, usageMetadata.candidatesTokenCount)
9191
}
9292

93+
func testGenerateContentStream() async throws {
94+
let expectedText = """
95+
1. Mercury
96+
2. Venus
97+
3. Earth
98+
4. Mars
99+
5. Jupiter
100+
6. Saturn
101+
7. Uranus
102+
8. Neptune
103+
"""
104+
let prompt = """
105+
What are the names of the planets in the solar system, ordered from closest to furthest from
106+
the sun? Answer with a Markdown numbered list of the names and no other text.
107+
"""
108+
let chat = model.startChat()
109+
110+
let stream = try chat.sendMessageStream(prompt)
111+
var textValues = [String]()
112+
for try await value in stream {
113+
try textValues.append(XCTUnwrap(value.text))
114+
}
115+
116+
let userHistory = try XCTUnwrap(chat.history.first)
117+
XCTAssertEqual(userHistory.role, "user")
118+
XCTAssertEqual(userHistory.parts.count, 1)
119+
let promptTextPart = try XCTUnwrap(userHistory.parts.first as? TextPart)
120+
XCTAssertEqual(promptTextPart.text, prompt)
121+
let modelHistory = try XCTUnwrap(chat.history.last)
122+
XCTAssertEqual(modelHistory.role, "model")
123+
XCTAssertEqual(modelHistory.parts.count, 1)
124+
let modelTextPart = try XCTUnwrap(modelHistory.parts.first as? TextPart)
125+
let modelText = modelTextPart.text.trimmingCharacters(in: .whitespacesAndNewlines)
126+
XCTAssertEqual(modelText, expectedText)
127+
XCTAssertGreaterThan(textValues.count, 1)
128+
let text = textValues.joined().trimmingCharacters(in: .whitespacesAndNewlines)
129+
XCTAssertEqual(text, expectedText)
130+
}
131+
93132
func testGenerateContent_appCheckNotConfigured_shouldFail() async throws {
94133
let app = try FirebaseApp.defaultNamedCopy(name: TestAppCheckProviderFactory.notConfiguredName)
95134
addTeardownBlock { await app.delete() }

0 commit comments

Comments
 (0)