Skip to content

test(core): add missing tests for prompts/utils.ts#19941

Merged
jacob314 merged 7 commits intogoogle-gemini:mainfrom
krrishverma1805-web:test-add-prompts-utils-tests
Mar 11, 2026
Merged

test(core): add missing tests for prompts/utils.ts#19941
jacob314 merged 7 commits intogoogle-gemini:mainfrom
krrishverma1805-web:test-add-prompts-utils-tests

Conversation

@krrishverma1805-web
Copy link
Copy Markdown
Contributor

Summary

Adds comprehensive test coverage for packages/core/src/prompts/utils.ts,
which previously had no corresponding test file despite containing
three exported utility functions used in prompt construction.

Fixes #19940

Changes Made

  • Created packages/core/src/prompts/utils.test.ts with 27 tests
    covering all three exported functions:

resolvePathFromEnv() (14 tests)

  • Undefined, empty, and whitespace-only input
  • Boolean switch values (true, false, 1, 0)
  • Case-insensitive switch handling
  • Whitespace trimming
  • Absolute path resolution
  • Tilde (~) expansion to home directory
  • Bare tilde resolution
  • Graceful handling of home directory resolution failure

isSectionEnabled() (8 tests)

  • Unset environment variable (defaults to enabled)
  • Explicit enable values (1, true)
  • Explicit disable values (0, false)
  • Case-insensitive key conversion
  • Whitespace handling around values
  • Non-standard truthy values

applySubstitutions() (8 tests)

  • ${AgentSkills} single and multiple replacements
  • ${SubAgents} with Gemini 3 vs legacy snippets
  • ${AvailableTools} with and without tools
  • Tool-specific ${toolName_ToolName} variables
  • Prompts with no substitution placeholders

Testing

  • All new tests pass (npx vitest run packages/core/src/prompts/utils.test.ts)
  • Full core test suite passes (npm test -w @google/gemini-cli-core)
  • Follows existing test patterns (Vitest, vi.stubEnv(),
    vi.mock() for dependencies)
  • Includes Apache-2.0 license header with correct year (2026)

Context

This fills a test coverage gap identified by comparing source files
to test files in packages/core/src/prompts/. All other files in
this directory (promptProvider.ts, prompt-registry.ts,
mcp-prompts.ts) have corresponding test files.

@krrishverma1805-web krrishverma1805-web requested a review from a team as a code owner February 22, 2026 19:39
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @krrishverma1805-web, 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 a significant test coverage gap within the packages/core/src/prompts/ directory by introducing a dedicated test file for utils.ts. The newly added tests ensure the robustness and correctness of three critical utility functions responsible for environment path resolution, section enablement based on environment variables, and prompt string substitutions, thereby improving the overall reliability of the prompt construction logic.

Highlights

  • New Test File: A new test file, packages/core/src/prompts/utils.test.ts, was created to provide comprehensive test coverage for previously untested utility functions.
  • resolvePathFromEnv() Tests: Added 14 tests covering various scenarios for path resolution, including undefined/empty input, boolean switches, case-insensitivity, whitespace trimming, absolute paths, tilde expansion, and home directory resolution failures.
  • isSectionEnabled() Tests: Added 8 tests to validate environment variable-based section enablement, covering unset variables, explicit true/false values, case-insensitive keys, whitespace handling, and non-standard truthy values.
  • applySubstitutions() Tests: Added 8 tests to ensure correct substitution logic for prompt placeholders like ${AgentSkills}, ${SubAgents}, ${AvailableTools}, and tool-specific variables, including handling of multiple occurrences and prompts without placeholders.
Changelog
  • packages/core/src/prompts/utils.test.ts
    • Added new test file to cover utility functions.
Activity
  • All 27 newly added tests for prompts/utils.ts passed successfully.
  • The full core test suite passed, confirming no regressions were introduced.
  • The new tests adhere to existing project patterns, utilizing Vitest, vi.stubEnv(), and vi.mock().
  • The new file includes the required Apache-2.0 license header.
  • This PR resolves issue test(core): add missing tests for prompts/utils.ts #19940 by filling a previously identified test coverage gap.
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
Copy Markdown
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

The pull request introduces comprehensive test coverage for packages/core/src/prompts/utils.ts, which is a significant improvement for code quality and maintainability. The tests cover various scenarios for resolvePathFromEnv, isSectionEnabled, and applySubstitutions, including edge cases and environment variable handling. The use of vi.stubEnv and vi.mock demonstrates good testing practices. The new test file also includes the correct license header and year, adhering to the repository's guidelines.

@gemini-cli gemini-cli bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Feb 22, 2026
Copy link
Copy Markdown
Contributor

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

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

lgtm

@jacob314 jacob314 enabled auto-merge February 26, 2026 19:02
@gemini-cli gemini-cli bot added priority/p3 Backlog - a good idea but not currently a priority. help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Feb 26, 2026
Comment on lines +166 to +168
it('should return true when the env var is not set', () => {
expect(isSectionEnabled('SOME_KEY')).toBe(true);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here, the test assumes GEMINI_PROMPT_SOME_KEY won't exist. However, if a
developer happens to have set this in their local system environment:

export GEMINI_PROMPT_SOME_KEY=0

This test will fail locally.

While you correctly included vi.unstubAllEnvs() in the afterEach block, this
Vitest function only restores the environment back to its initial state before
the test started—it doesn't aggressively clear pre-existing variables.

To make this test block 100% bulletproof and isolated from global state leakage,
we should explicitly clear the variable inside the test itself (similar to how
you use vi.stubEnv in the other tests):

it('should return true when the env var is not set', () => {
  vi.stubEnv('GEMINI_PROMPT_SOME_KEY', '');
  expect(isSectionEnabled('SOME_KEY')).toBe(true);
});

Let me know what you think.

it('should resolve a regular path', () => {
const result = resolvePathFromEnv('/some/absolute/path');
expect(result.isSwitch).toBe(false);
expect(result.value).toBe('/some/absolute/path');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In here, the expected strings hardcode POSIX (/) forward slashes:

// Line 127 and Line 135
expect(result.value).toBe('/some/absolute/path');

When resolvePathFromEnv ( line 125 ) runs on a Windows computer, it uses Node's path.resolve and path.join to return a proper Windows-style path (like "C:\some\absolute\path"). However, the test is trying to match that result
against the hardcoded string "/some/absolute/path". Because of this mismatch,
the test will always fail on Windows.

To guarantee cross-platform compatibility without changing your test logic, we
should wrap the expected strings in path.resolve() and path.join(). This
delegates the slash-formatting to Node.js natively:

import path from 'node:path';

// ...

it('should resolve a regular path', () => {
  // Dynamically resolves to C:\... on Windows and /... on POSIX
  expect(result.value).toBe(path.resolve('/some/absolute/path'));
});

Let me know what you think?

const result = resolvePathFromEnv('~/my/custom/path');
expect(result.isSwitch).toBe(false);
expect(result.value).toContain('/mock/home');
expect(result.value).toContain('my/custom/path');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same thing as my previous comment on line 127

Copy link
Copy Markdown
Contributor

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

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

lgtm

@jacob314 jacob314 disabled auto-merge March 9, 2026 23:27
@jacob314 jacob314 enabled auto-merge March 9, 2026 23:27
@jacob314 jacob314 added this pull request to the merge queue Mar 9, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 9, 2026
@jacob314 jacob314 added this pull request to the merge queue Mar 11, 2026
Merged via the queue into google-gemini:main with commit c2d38ba Mar 11, 2026
27 checks passed
e-kotov pushed a commit to e-kotov/gemini-cli that referenced this pull request Mar 11, 2026
Co-authored-by: Jacob Richman <jacob314@gmail.com>
JaisalJain pushed a commit to JaisalJain/gemini-cli that referenced this pull request Mar 11, 2026
Co-authored-by: Jacob Richman <jacob314@gmail.com>
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
Co-authored-by: Jacob Richman <jacob314@gmail.com>
ruomengz pushed a commit that referenced this pull request Mar 13, 2026
Co-authored-by: Jacob Richman <jacob314@gmail.com>
SUNDRAM07 pushed a commit to SUNDRAM07/gemini-cli that referenced this pull request Mar 30, 2026
Co-authored-by: Jacob Richman <jacob314@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p3 Backlog - a good idea but not currently a priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(core): add missing tests for prompts/utils.ts

3 participants