Skip to content

Conversation

paulb777
Copy link
Member

@paulb777 paulb777 commented Sep 19, 2025

Summary of Changes

This pull request introduces a robust suite of unit tests for the urlContext APIs within both the GoogleAI and VertexAI generative models. The tests cover various response scenarios, ensuring the correct parsing and handling of URL context metadata by the SDK. To support these new tests, the underlying MockURLProtocol was enhanced to handle direct Data responses, complemented by a new utility function for generating mock JSON HTTP handlers.

Highlights

  • New Unit Tests for URL Context APIs (GoogleAI): Added comprehensive unit tests to GenerativeModelGoogleAITests.swift to validate the handling of urlContext metadata. These tests cover successful retrieval, mixed validity scenarios (success and error), and cases where URL metadata is empty. A new streaming test for urlContext was also introduced.
  • New Unit Tests for URL Context APIs (VertexAI): Similar to the GoogleAI tests, new unit tests were added to GenerativeModelVertexAITests.swift for urlContext APIs. These tests verify successful retrieval, mixed validity, handling of nonexistent retrieved URLs, and empty URL metadata. A streaming test for urlContext was also included.
  • From [Firebase AI] Add Candidate decoding tests for urlMetadata #15348
    • Added a similar decoding test in GenerateContentResponseTests to verify the same parsing logic when urlMetadata is [] in the Candidate.
    • Added another decoding test in GenerateContentResponseTests to verify that the same behaviour occurs when urlMetadata is omitted (rather than an empty array).
      This behaviour is more likely to be observed in practice since [] is typically omitted during JSON serialization in the backend (since it is a default value).
Changelog
  • FirebaseAI/Tests/Unit/GenerativeModelGoogleAITests.swift
    • Added testGenerateContent_success_urlContext to verify successful URL context retrieval.
    • Added testGenerateContent_success_urlContext_mixedValidity to test scenarios with both successful and erroneous URL metadata.
    • Added testGenerateContent_success_urlContext_emptyURLMetadata to ensure correct handling when URL metadata is empty.
    • Added testGenerateContentStream_success_urlContext to test streaming responses with URL context.
  • FirebaseAI/Tests/Unit/GenerativeModelVertexAITests.swift
    • Added testGenerateContent_success_urlContext to verify successful URL context retrieval.
    • Added testGenerateContent_success_urlContext_mixedValidity to test scenarios with both successful and erroneous URL metadata.
    • Added testGenerateContent_success_urlContext_nonexistentRetrievedURL to test cases where retrievedURL is missing.
    • Added testGenerateContent_success_urlContext_emptyURLMetadata to ensure correct handling when URL metadata is empty.
    • Added testGenerateContentStream_success_urlContext to test streaming responses with URL context.
  • FirebaseAI/Tests/Unit/MockURLProtocol.swift
    • Introduced dataRequestHandler static variable to allow mocking HTTP responses with raw Data.
    • Modified startLoading to conditionally use requestHandler (for streaming) or dataRequestHandler (for static data).
  • FirebaseAI/Tests/Unit/TestUtilities/GenerativeModelTestUtil.swift
    • Added httpRequestHandler(json:statusCode:) utility function to create URLRequest handlers that return a specified JSON string as Data.

Copy link
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

@paulb777
Copy link
Member Author

/gemini review

@google-oss-bot
Copy link

1 Warning
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)

Generated by 🚫 Danger

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds unit tests for the urlContext APIs, covering both unary and streaming responses for Google AI and Vertex AI backends. The tests are well-structured and cover important scenarios like success cases and mixed validity responses. I've provided a few suggestions to improve code clarity and correctness. Specifically, I've pointed out some unnecessary uses of XCTUnwrap that can be simplified. I also found a test case where the name and assertion seem to contradict each other, and I've suggested a correction to align them. Overall, these are solid additions to the test suite.

let candidate = try XCTUnwrap(response.candidates.first)
let urlContextMetadata = try XCTUnwrap(candidate.urlContextMetadata)
let urlMetadata = try XCTUnwrap(urlContextMetadata.urlMetadata.first)
XCTAssertEqual(urlMetadata.retrievedURL?.absoluteString, "https://example.com/8")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There seems to be a contradiction in this test. The test name testGenerateContent_success_urlContext_missingRetrievedURL and the mock resource name unary-success-url-context-missing-retrievedurl suggest that the retrievedURL should be missing or nil. However, the assertion checks if retrievedURL?.absoluteString is equal to a specific string, which implies retrievedURL is not nil.

If the intention is to test for a missing URL, the assertion should probably be XCTAssertNil(urlMetadata.retrievedURL). If the current assertion is correct, consider renaming the test to avoid confusion.

Suggested change
XCTAssertEqual(urlMetadata.retrievedURL?.absoluteString, "https://example.com/8")
XCTAssertNil(urlMetadata.retrievedURL)

Copy link
Member Author

@paulb777 paulb777 Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed function to testGenerateContent_success_urlContext_retrievedURLPresentOnErrorStatus cc: @dlarocque @andrewheard

paulb777 and others added 3 commits September 19, 2025 14:53
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@paulb777
Copy link
Member Author

/gemini summarize
/gemini review

Copy link
Contributor

I can only process one command at a time. Please let me know which command you'd like me to execute first: /gemini summarize or /gemini review.

@paulb777
Copy link
Member Author

/gemini summarize

@paulb777
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a robust suite of unit tests for the urlContext APIs, significantly improving test coverage. The changes also include enhancements to the MockURLProtocol to better support testing with static JSON data. The new tests are well-written and cover various scenarios. I have a few minor suggestions to improve the clarity and consistency of some tests.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@paulb777
Copy link
Member Author

/gemini summary

@firebase firebase deleted a comment from gemini-code-assist bot Sep 22, 2025
@paulb777
Copy link
Member Author

/gemini summary

@paulb777
Copy link
Member Author

/gemini review

@firebase firebase deleted a comment from gemini-code-assist bot Sep 22, 2025
@firebase firebase deleted a comment from gemini-code-assist bot Sep 22, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a comprehensive set of unit tests for the urlContext APIs for both GoogleAI and VertexAI models, covering unary and streaming responses. The tests validate various scenarios like successful retrieval, mixed validity, and empty metadata. The changes are well-structured and significantly improve test coverage for the URL context feature. I've provided a few minor suggestions to enhance test code clarity, consistency, and maintainability by refactoring duplicated code and improving assertions.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copy link
Contributor

@andrewheard andrewheard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Paul!

@paulb777 paulb777 merged commit 0e9df30 into ah/firebaseai-urlcontext Sep 22, 2025
43 of 44 checks passed
@paulb777 paulb777 deleted the pb-url-context-unit-tests branch September 22, 2025 21:03
paulb777 added a commit that referenced this pull request Sep 22, 2025
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Andrew Heard <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants