Skip to content

fix(sdk): move dynamic instructions error test to unit test#20409

Open
Rawdyrathaur wants to merge 4 commits intogoogle-gemini:mainfrom
Rawdyrathaur:fix/sdk-dynamic-instructions-error-test
Open

fix(sdk): move dynamic instructions error test to unit test#20409
Rawdyrathaur wants to merge 4 commits intogoogle-gemini:mainfrom
Rawdyrathaur:fix/sdk-dynamic-instructions-error-test

Conversation

@Rawdyrathaur
Copy link

@Rawdyrathaur Rawdyrathaur commented Feb 26, 2026

Summary

The SDK test propagates errors from dynamic instructions always failed outside GCP because it was missing fakeResponses, causing initialize() to hit the real GCP metadata server. Moved the test to a unit test file and mocked initialize() so no network call is made.

Details

  • Moved test from agent.integration.test.ts to agent.test.ts
  • Used vi.spyOn to mock GeminiCliSession.prototype.initialize
  • Test now runs in ~3ms with no network dependency

How to Validat?

npx vitest run packages/sdk/src/agent.test.ts --reporter=verbose

Related Issues

Fixes #20408

How to Validate

npx vitest run packages/sdk/src/agent.test.ts --reporter=verbose

## Pre-Merge Checklist
- [x] Added/updated tests (if needed)

@Rawdyrathaur Rawdyrathaur requested a review from a team as a code owner February 26, 2026 08:55
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Rawdyrathaur, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where a specific SDK test, designed to propagate errors from dynamic instructions, consistently failed outside of GCP environments due to unintended network calls. The solution involves refactoring this test into a dedicated unit test file and implementing mocking for the initialize() method, thereby eliminating its dependency on external services and ensuring consistent, fast execution across all environments.

Highlights

  • Test Relocation: The propagates errors from dynamic instructions test was moved from agent.integration.test.ts to agent.test.ts to improve its reliability and performance.
  • Test Isolation: The GeminiCliSession.prototype.initialize method is now mocked using vi.spyOn to prevent network calls and ensure the test runs independently.
  • Performance Improvement: The test now executes significantly faster, completing in approximately 3ms without external network dependencies.
Changelog
  • .gitignore
    • Added .claude/ to ignore Claude-related files.
    • Added ISSUE_LIST.md to ignore a markdown file.
  • packages/sdk/src/agent.integration.test.ts
    • Removed the integration test for dynamic instruction error propagation.
  • packages/sdk/src/agent.test.ts
    • Created a new unit test file for GeminiCliSession.
    • Implemented a mocked test for dynamic instruction error propagation, spying on initialize.
    • Added beforeEach hook to restore all mocks before each test.
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
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 successfully addresses the issue of a flaky integration test by moving it to a unit test and mocking external dependencies. This significantly improves test reliability and execution speed. The suggestion to make the mock for the initialize method more robust is valid and should be considered to ensure it fully covers the expected behavior of the mocked object, even for code paths not currently exercised by this specific test.

Comment on lines 17 to 22
vi.spyOn(GeminiCliSession.prototype, 'initialize').mockImplementation(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function (this: any) {
this.client = { getHistory: () => [] };
this.initialized = true;
},
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The mock for GeminiCliSession.prototype.initialize is incomplete. While it correctly mocks getHistory, the sendStream method also calls client.sendMessageStream. If the instructions function did not throw an error, the test would likely fail with a TypeError because sendMessageStream is not defined on the mocked client. A more robust mock should include all methods that sendStream expects to find on the client object to prevent future breakage or unexpected behavior if the test path changes.

    vi.spyOn(GeminiCliSession.prototype, 'initialize').mockImplementation(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      async function (this: any) {
        this.client = {
          getHistory: () => [],
          sendMessageStream: async function* () {},
        };
        this.initialized = true;
      },
    );

@Rawdyrathaur Rawdyrathaur force-pushed the fix/sdk-dynamic-instructions-error-test branch from 5a6ee60 to 32fa265 Compare February 26, 2026 09:04
@gemini-cli gemini-cli bot added the area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt label Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SDK test "propagates errors from dynamic instructions" always fails outside GCP

1 participant