Skip to content

Commit d537c1e

Browse files
committed
Revert MockURLProtocol changes and inline JSON tests
1 parent 7df2377 commit d537c1e

File tree

4 files changed

+40
-77
lines changed

4 files changed

+40
-77
lines changed

FirebaseAI/Tests/Unit/GenerativeModelGoogleAITests.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -384,27 +384,6 @@ final class GenerativeModelGoogleAITests: XCTestCase {
384384
XCTAssertEqual(errorURLMetadata.retrievalStatus, .error)
385385
}
386386

387-
func testGenerateContent_success_urlContext_emptyURLMetadata() async throws {
388-
let json = """
389-
{
390-
"candidates": [
391-
{
392-
"content": { "role": "model", "parts": [ { "text": "Some text." } ] },
393-
"finishReason": "STOP",
394-
"urlContextMetadata": { "urlMetadata": [] }
395-
}
396-
]
397-
}
398-
"""
399-
MockURLProtocol.requestHandler = nil
400-
MockURLProtocol.dataRequestHandler = try GenerativeModelTestUtil.httpRequestHandler(json: json)
401-
402-
let response = try await model.generateContent(testPrompt)
403-
404-
let candidate = try XCTUnwrap(response.candidates.first)
405-
XCTAssertNil(candidate.urlContextMetadata)
406-
}
407-
408387
func testGenerateContent_failure_invalidAPIKey() async throws {
409388
let expectedStatusCode = 400
410389
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(

FirebaseAI/Tests/Unit/GenerativeModelVertexAITests.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -556,27 +556,6 @@ final class GenerativeModelVertexAITests: XCTestCase {
556556
XCTAssertEqual(urlMetadata.retrievalStatus, .error)
557557
}
558558

559-
func testGenerateContent_success_urlContext_emptyURLMetadata() async throws {
560-
let json = """
561-
{
562-
"candidates": [
563-
{
564-
"content": { "role": "model", "parts": [ { "text": "Some text." } ] },
565-
"finishReason": "STOP",
566-
"urlContextMetadata": { "urlMetadata": [] }
567-
}
568-
]
569-
}
570-
"""
571-
MockURLProtocol.requestHandler = nil
572-
MockURLProtocol.dataRequestHandler = try GenerativeModelTestUtil.httpRequestHandler(json: json)
573-
574-
let response = try await model.generateContent(testPrompt)
575-
576-
let candidate = try XCTUnwrap(response.candidates.first)
577-
XCTAssertNil(candidate.urlContextMetadata)
578-
}
579-
580559
func testGenerateContent_success_image_invalidSafetyRatingsIgnored() async throws {
581560
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
582561
forResource: "unary-success-image-invalid-safety-ratings",

FirebaseAI/Tests/Unit/MockURLProtocol.swift

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ class MockURLProtocol: URLProtocol, @unchecked Sendable {
2222
AsyncLineSequence<URL.AsyncBytes>?
2323
))?
2424

25-
nonisolated(unsafe) static var dataRequestHandler: ((URLRequest) throws -> (
26-
URLResponse,
27-
Data?
28-
))?
29-
3025
override class func canInit(with request: URLRequest) -> Bool {
3126
#if os(watchOS)
3227
print("MockURLProtocol cannot be used on watchOS.")
@@ -43,40 +38,31 @@ class MockURLProtocol: URLProtocol, @unchecked Sendable {
4338
fatalError("`client` is nil.")
4439
}
4540

46-
if let requestHandler = MockURLProtocol.requestHandler {
47-
Task {
48-
let (response, stream) = try requestHandler(self.request)
49-
client.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
50-
if let stream = stream {
51-
do {
52-
for try await line in stream {
53-
guard let data = line.data(using: .utf8) else {
54-
fatalError("Failed to convert \"\(line)\" to UTF8 data.")
55-
}
56-
client.urlProtocol(self, didLoad: data)
57-
// Add a newline character since AsyncLineSequence strips them when reading line by
58-
// line;
59-
// without the following, the whole file is delivered as a single line.
60-
client.urlProtocol(self, didLoad: "\n".data(using: .utf8)!)
41+
guard let requestHandler = MockURLProtocol.requestHandler else {
42+
fatalError("No request handler set.")
43+
}
44+
45+
Task {
46+
let (response, stream) = try requestHandler(self.request)
47+
client.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
48+
if let stream = stream {
49+
do {
50+
for try await line in stream {
51+
guard let data = line.data(using: .utf8) else {
52+
fatalError("Failed to convert \"\(line)\" to UTF8 data.")
6153
}
62-
} catch {
63-
client.urlProtocol(self, didFailWithError: error)
64-
XCTFail("Unexpected failure reading lines from stream: \(error.localizedDescription)")
54+
client.urlProtocol(self, didLoad: data)
55+
// Add a newline character since AsyncLineSequence strips them when reading line by
56+
// line;
57+
// without the following, the whole file is delivered as a single line.
58+
client.urlProtocol(self, didLoad: "\n".data(using: .utf8)!)
6559
}
60+
} catch {
61+
client.urlProtocol(self, didFailWithError: error)
62+
XCTFail("Unexpected failure reading lines from stream: \(error.localizedDescription)")
6663
}
67-
client.urlProtocolDidFinishLoading(self)
68-
}
69-
} else if let dataRequestHandler = MockURLProtocol.dataRequestHandler {
70-
Task {
71-
let (response, data) = try dataRequestHandler(self.request)
72-
client.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
73-
if let data = data {
74-
client.urlProtocol(self, didLoad: data)
75-
}
76-
client.urlProtocolDidFinishLoading(self)
7764
}
78-
} else {
79-
fatalError("No request handler set.")
65+
client.urlProtocolDidFinishLoading(self)
8066
}
8167
}
8268

FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,23 @@ final class GenerateContentResponseTests: XCTestCase {
106106
"functionCalls should be empty when there are no candidates."
107107
)
108108
}
109+
110+
func testURLContextMetadata_withEmptyURLMetadata_isNil() throws {
111+
let json = """
112+
{
113+
"candidates": [
114+
{
115+
"content": { "role": "model", "parts": [ { "text": "Some text." } ] },
116+
"finishReason": "STOP",
117+
"urlContextMetadata": { "urlMetadata": [] }
118+
}
119+
]
120+
}
121+
""".data(using: .utf8)!
122+
123+
let response = try JSONDecoder().decode(GenerateContentResponse.self, from: json)
124+
125+
let candidate = try XCTUnwrap(response.candidates.first)
126+
XCTAssertNil(candidate.urlContextMetadata)
127+
}
109128
}

0 commit comments

Comments
 (0)