Skip to content

test(gmail): add unit tests for +triage argument parsing#445

Open
anshul-garg27 wants to merge 1 commit intogoogleworkspace:mainfrom
anshul-garg27:test/gmail-triage-coverage
Open

test(gmail): add unit tests for +triage argument parsing#445
anshul-garg27 wants to merge 1 commit intogoogleworkspace:mainfrom
anshul-garg27:test/gmail-triage-coverage

Conversation

@anshul-garg27
Copy link
Contributor

Summary

  • Add 8 unit tests for gmail +triage argument parsing — previously the only helper file with zero test coverage
  • Tests cover: default/explicit/invalid --max, --query override, --labels flag, and --format selection

Details

src/helpers/gmail/triage.rs had no #[cfg(test)] module. While the core logic involves network calls (Gmail API), the argument parsing and format selection are purely deterministic and worth covering.

Tests added

  • defaults_max_to_20_and_query_to_unread — verifies default values
  • explicit_max_overrides_default--max 5 parses correctly
  • non_numeric_max_falls_back_to_20--max abc silently falls back
  • custom_query_overrides_default--query 'from:boss' works
  • labels_flag_defaults_to_false — default behavior
  • labels_flag_set_when_passed--labels sets the flag
  • format_defaults_to_table_when_absent — table is the default
  • format_json_when_specified--format json switches output

triage.rs was the only helper file with zero test coverage.
Add tests for: default max (20), explicit max, non-numeric max
fallback, custom query, labels flag, and output format selection.
@googleworkspace-bot googleworkspace-bot added the area: core Core CLI parsing, commands, error handling, utilities label Mar 12, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 significantly enhances the robustness of the gmail +triage command by introducing a comprehensive set of unit tests for its argument parsing logic. This ensures that various command-line inputs are handled correctly, improving the reliability and predictability of the tool's behavior without altering its core functionality.

Highlights

  • Unit Test Coverage: Added 8 new unit tests for the gmail +triage argument parsing, addressing a previously untested helper file.
  • Argument Parsing Validation: Tests cover various scenarios for --max (default, explicit, and non-numeric fallback), --query overrides, the --labels flag, and --format selection (default table and explicit JSON).
  • Documentation Update: Updated the module-level documentation for src/helpers/gmail/triage.rs to provide a more detailed description of its functionality.
Changelog
  • .changeset/test-gmail-triage-coverage.md
    • Added a new changeset file to document the addition of unit tests for Gmail triage argument parsing.
  • src/helpers/gmail/triage.rs
    • Added a new #[cfg(test)] module containing 8 unit tests for argument parsing.
    • Updated the module-level documentation with a more detailed description of the +triage helper's capabilities.
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 Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@changeset-bot
Copy link

changeset-bot bot commented Mar 12, 2026

🦋 Changeset detected

Latest commit: 84cdd27

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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 valuable unit tests for the gmail +triage argument parsing. However, the current implementation duplicates the parsing logic from the production code within the tests, which makes them brittle. I've provided a suggestion to refactor the tests to centralize this logic, making them cleaner and more maintainable. This also highlights an opportunity to make the production code more testable by extracting the argument parsing logic.

Comment on lines +181 to +290
#[cfg(test)]
mod tests {
use clap::{Arg, ArgAction, Command};

/// Build a clap command matching the +triage definition so we can
/// unit-test argument parsing without needing a live GmailHelper.
fn triage_cmd() -> Command {
Command::new("triage")
.arg(
Arg::new("max")
.long("max")
.default_value("20")
.value_name("N"),
)
.arg(Arg::new("query").long("query").value_name("QUERY"))
.arg(
Arg::new("labels")
.long("labels")
.action(ArgAction::SetTrue),
)
.arg(Arg::new("format").long("format").value_name("FMT"))
}

#[test]
fn defaults_max_to_20_and_query_to_unread() {
let m = triage_cmd().try_get_matches_from(["triage"]).unwrap();
let max: u32 = m
.get_one::<String>("max")
.and_then(|s| s.parse().ok())
.unwrap_or(20);
let query = m
.get_one::<String>("query")
.map(|s| s.as_str())
.unwrap_or("is:unread");
assert_eq!(max, 20);
assert_eq!(query, "is:unread");
}

#[test]
fn explicit_max_overrides_default() {
let m = triage_cmd()
.try_get_matches_from(["triage", "--max", "5"])
.unwrap();
let max: u32 = m
.get_one::<String>("max")
.and_then(|s| s.parse().ok())
.unwrap_or(20);
assert_eq!(max, 5);
}

#[test]
fn non_numeric_max_falls_back_to_20() {
let m = triage_cmd()
.try_get_matches_from(["triage", "--max", "abc"])
.unwrap();
let max: u32 = m
.get_one::<String>("max")
.and_then(|s| s.parse().ok())
.unwrap_or(20);
assert_eq!(max, 20);
}

#[test]
fn custom_query_overrides_default() {
let m = triage_cmd()
.try_get_matches_from(["triage", "--query", "from:boss"])
.unwrap();
let query = m
.get_one::<String>("query")
.map(|s| s.as_str())
.unwrap_or("is:unread");
assert_eq!(query, "from:boss");
}

#[test]
fn labels_flag_defaults_to_false() {
let m = triage_cmd().try_get_matches_from(["triage"]).unwrap();
assert!(!m.get_flag("labels"));
}

#[test]
fn labels_flag_set_when_passed() {
let m = triage_cmd()
.try_get_matches_from(["triage", "--labels"])
.unwrap();
assert!(m.get_flag("labels"));
}

#[test]
fn format_defaults_to_table_when_absent() {
let m = triage_cmd().try_get_matches_from(["triage"]).unwrap();
let fmt = m
.get_one::<String>("format")
.map(|s| crate::formatter::OutputFormat::from_str(s))
.unwrap_or(crate::formatter::OutputFormat::Table);
assert!(matches!(fmt, crate::formatter::OutputFormat::Table));
}

#[test]
fn format_json_when_specified() {
let m = triage_cmd()
.try_get_matches_from(["triage", "--format", "json"])
.unwrap();
let fmt = m
.get_one::<String>("format")
.map(|s| crate::formatter::OutputFormat::from_str(s))
.unwrap_or(crate::formatter::OutputFormat::Table);
assert!(matches!(fmt, crate::formatter::OutputFormat::Json));
}
}
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 current test implementation duplicates the argument parsing logic from handle_triage within each test case. This is a violation of the Don't Repeat Yourself (DRY) principle and makes the tests brittle. If the parsing logic in handle_triage were to change, these tests would need to be updated in lockstep, and might not catch regressions.

To improve this, you can centralize the parsing logic into a helper function within the test module. This makes the tests cleaner and easier to maintain.

For a more robust long-term solution, consider extracting the argument parsing logic out of handle_triage entirely, so that both the production code and test code can share it. However, the following suggestion keeps the changes confined to the new test module.

#[cfg(test)]
mod tests {
    use clap::{Arg, ArgAction, ArgMatches, Command};

    // A struct to hold parsed arguments, mirroring the logic in `handle_triage`.
    #[derive(Debug, PartialEq)]
    struct TriageArgs<'a> {
        max: u32,
        query: &'a str,
        show_labels: bool,
        output_format: crate::formatter::OutputFormat,
    }

    // Helper to parse args, centralizing the logic copied from `handle_triage`.
    fn parse_triage_args(m: &ArgMatches) -> TriageArgs {
        let max = m
            .get_one::<String>("max")
            .and_then(|s| s.parse().ok())
            .unwrap_or(20);
        let query = m
            .get_one::<String>("query")
            .map(|s| s.as_str())
            .unwrap_or("is:unread");
        let show_labels = m.get_flag("labels");
        let output_format = m
            .get_one::<String>("format")
            .map(|s| crate::formatter::OutputFormat::from_str(s))
            .unwrap_or(crate::formatter::OutputFormat::Table);

        TriageArgs {
            max,
            query,
            show_labels,
            output_format,
        }
    }

    /// Build a clap command matching the +triage definition so we can
    /// unit-test argument parsing without needing a live GmailHelper.
    fn triage_cmd() -> Command {
        Command::new("triage")
            .arg(
                Arg::new("max")
                    .long("max")
                    .default_value("20")
                    .value_name("N"),
            )
            .arg(Arg::new("query").long("query").value_name("QUERY"))
            .arg(
                Arg::new("labels")
                    .long("labels")
                    .action(ArgAction::SetTrue),
            )
            .arg(Arg::new("format").long("format").value_name("FMT"))
    }

    #[test]
    fn defaults_max_to_20_and_query_to_unread() {
        let m = triage_cmd().try_get_matches_from(["triage"]).unwrap();
        let args = parse_triage_args(&m);
        assert_eq!(args.max, 20);
        assert_eq!(args.query, "is:unread");
    }

    #[test]
    fn explicit_max_overrides_default() {
        let m = triage_cmd()
            .try_get_matches_from(["triage", "--max", "5"])
            .unwrap();
        let args = parse_triage_args(&m);
        assert_eq!(args.max, 5);
    }

    #[test]
    fn non_numeric_max_falls_back_to_20() {
        let m = triage_cmd()
            .try_get_matches_from(["triage", "--max", "abc"])
            .unwrap();
        let args = parse_triage_args(&m);
        assert_eq!(args.max, 20);
    }

    #[test]
    fn custom_query_overrides_default() {
        let m = triage_cmd()
            .try_get_matches_from(["triage", "--query", "from:boss"])
            .unwrap();
        let args = parse_triage_args(&m);
        assert_eq!(args.query, "from:boss");
    }

    #[test]
    fn labels_flag_defaults_to_false() {
        let m = triage_cmd().try_get_matches_from(["triage"]).unwrap();
        let args = parse_triage_args(&m);
        assert!(!args.show_labels);
    }

    #[test]
    fn labels_flag_set_when_passed() {
        let m = triage_cmd()
            .try_get_matches_from(["triage", "--labels"])
            .unwrap();
        let args = parse_triage_args(&m);
        assert!(args.show_labels);
    }

    #[test]
    fn format_defaults_to_table_when_absent() {
        let m = triage_cmd().try_get_matches_from(["triage"]).unwrap();
        let args = parse_triage_args(&m);
        assert_eq!(args.output_format, crate::formatter::OutputFormat::Table);
    }

    #[test]
    fn format_json_when_specified() {
        let m = triage_cmd()
            .try_get_matches_from(["triage", "--format", "json"])
            .unwrap();
        let args = parse_triage_args(&m);
        assert_eq!(args.output_format, crate::formatter::OutputFormat::Json);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Core CLI parsing, commands, error handling, utilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants