Skip to content

fix(cli): reject empty session_id and add edge-case tests (#808)#998

Merged
microsasa merged 1 commit intomainfrom
fix/808-session-command-edge-cases-d8f7649b9089790e
Apr 19, 2026
Merged

fix(cli): reject empty session_id and add edge-case tests (#808)#998
microsasa merged 1 commit intomainfrom
fix/808-session-command-edge-cases-d8f7649b9089790e

Conversation

@microsasa
Copy link
Copy Markdown
Owner

Closes #808

Changes

Gap 1 — Empty-string prefix validation:
Added an early guard in the session command that rejects an empty session_id argument with exit code 1 and the message "session ID cannot be empty". Previously, str.startswith("") always returns True, so an empty argument would silently display the first session.

Gap 2 — OSError test for _show_session_by_index:
Added test_show_session_by_index_generic_oserror which patches get_cached_events to raise a generic OSError("permission denied") and verifies the red error message is printed without raising.

Gap 3 — Duplicate session_id test for _build_session_index:
Added test_build_session_index_duplicate_ids which verifies that when two SessionSummary objects share the same session_id, the dict comprehension maps to the index of the last occurrence — documenting the tiebreak rule.

Verification

  • All three new tests pass
  • ruff check and ruff format clean
  • pyright passes (0 errors)
  • bandit security scan clean
  • Full test suite: 1408 passed, coverage 98.94%

Generated by Issue Implementer · ● 9.8M ·

- Add validation guard: empty session_id now exits 1 with
  'session ID cannot be empty' instead of silently matching the first
  session (Gap 1).
- Add test for OSError path in _show_session_by_index (Gap 2).
- Add test for duplicate session_id in _build_session_index (Gap 3).

Closes #808

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsasa microsasa added the aw Created by agentic workflow label Apr 19, 2026
@microsasa microsasa enabled auto-merge April 19, 2026 07:29
@microsasa microsasa added the aw Created by agentic workflow label Apr 19, 2026
Copilot AI review requested due to automatic review settings April 19, 2026 07:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an edge case in the session CLI command where an empty session_id prefix would previously match the first session, and adds regression tests covering this and two related edge cases.

Changes:

  • Add an early guard in session to reject an empty session_id with a clear error and exit code 1.
  • Add a test ensuring _show_session_by_index gracefully handles a generic OSError from get_cached_events.
  • Add a test documenting _build_session_index’s tie-break behavior for duplicate session_id values (last occurrence wins).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/copilot_usage/cli.py Adds an early validation guard to prevent empty-prefix session matching.
tests/copilot_usage/test_cli.py Adds regression tests for empty session id handling, generic OSError, and duplicate session_id indexing behavior.

@microsasa microsasa added the aw-quality-gate-approved Quality gate approved the PR label Apr 19, 2026
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Quality Gate: APPROVED

Changes reviewed:

  • src/copilot_usage/cli.py: 4-line early guard rejecting empty session_id with click.echo to stderr + sys.exit(1) — clean input validation at the CLI boundary.
  • tests/copilot_usage/test_cli.py: 3 new tests covering empty ID rejection, generic OSError handling in _show_session_by_index, and duplicate-ID tiebreak in _build_session_index — all meaningful with clear docstrings and assertions.

Assessment:

  • Code follows repository conventions (type annotations, early returns, pytest patterns).
  • All 8 CI checks green (lint, typecheck, security, tests, CodeQL).
  • Impact: LOW — small edge-case fix with comprehensive test additions.

Auto-approving for merge.

@microsasa microsasa merged commit c302ccd into main Apr 19, 2026
8 checks passed
@microsasa microsasa deleted the fix/808-session-command-edge-cases-d8f7649b9089790e branch April 19, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aw Created by agentic workflow aw-quality-gate-approved Quality gate approved the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[aw][test audit] cli.py: session command edge cases (empty prefix, OSError handling)

2 participants