test: cover untested branches in cli.py (#59)#61
Conversation
There was a problem hiding this comment.
Pull request overview
This PR expands the CLI test suite to cover previously untested branches in copilot_usage/cli.py (interactive auto-refresh paths, uppercase commands, events_path=None, group-level --path propagation, and _ensure_aware).
Changes:
- Add direct unit tests for
_ensure_awareand_show_session_by_indexwhenevents_path=None. - Add interactive-mode tests for uppercase commands and auto-refresh behavior across views.
- Add tests intended to verify group-level
--pathpropagation to subcommands.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return original_start_observer(session_path, change_event) | ||
|
|
||
| monkeypatch.setattr("copilot_usage.cli._start_observer", _capture_event_start) | ||
|
|
| result = runner.invoke(main, ["--path", str(tmp_path), "summary"]) | ||
| assert result.exit_code == 0 | ||
| assert "GroupSum" in result.output or "Summary" in result.output | ||
|
|
| def _fake_discover(_base_path: Path | None = None) -> list[Path]: | ||
| return sorted( | ||
| tmp_path.glob("*/events.jsonl"), | ||
| key=lambda p: p.stat().st_mtime, | ||
| reverse=True, | ||
| ) | ||
|
|
||
| monkeypatch.setattr("copilot_usage.cli.discover_sessions", _fake_discover) | ||
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path), "session", "grpd0000"]) |
| ) | ||
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path), "live"]) | ||
| assert result.exit_code == 0 |
| _write_session(tmp_path, "upq00000-0000-0000-0000-000000000000", name="QuitUpper") | ||
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path)], input="Q\n") | ||
| assert result.exit_code == 0 |
| ) | ||
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path)], input="R\nq\n") | ||
| assert result.exit_code == 0 |
| return original_start_observer(session_path, change_event) | ||
|
|
| session_path: Path, change_event: threading.Event | ||
| ) -> object: | ||
| nonlocal event_ref | ||
| event_ref = change_event | ||
| return original_start_observer(session_path, change_event) | ||
|
|
Add tests for: - Auto-refresh branches (home, cost, detail views) - Uppercase interactive commands (Q, C, R) - _show_session_by_index with events_path=None - Group-level --path propagation to all subcommands - _ensure_aware unit tests (None, aware, naive) Coverage increased from 78% to 98%. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ffe1a3b to
a000e9b
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds targeted tests to cover previously untested branches in copilot_usage/cli.py, addressing Issue #59 and significantly increasing branch coverage for interactive-mode behaviors and small helper branches.
Changes:
- Add unit tests for
_ensure_awareand_show_session_by_indexwhenevents_path=None. - Add interactive-mode tests for uppercase commands (
Q,C,R) and auto-refresh behavior across home/cost/detail views. - Add tests validating group-level
--pathpropagation to subcommands.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| event_ref: threading.Event | None = None | ||
| original_start_observer = cli_mod._start_observer # pyright: ignore[reportPrivateUsage] | ||
|
|
||
| def _capture_event_and_start( | ||
| session_path: Path, change_event: threading.Event | ||
| ) -> object: | ||
| nonlocal event_ref | ||
| event_ref = change_event |
| _write_session(tmp_path, "upq00000-0000-0000-0000-000000000000", name="QuitUpper") | ||
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path)], input="Q\n") | ||
| assert result.exit_code == 0 |
| ) | ||
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path)], input="R\nq\n") | ||
| assert result.exit_code == 0 |
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path), "summary"]) | ||
| assert result.exit_code == 0 | ||
| assert "GroupSum" in result.output or "Summary" in result.output |
| runner = CliRunner() | ||
| result = runner.invoke(main, ["--path", str(tmp_path), "cost"]) | ||
| assert result.exit_code == 0 | ||
| assert "Cost" in result.output or "Total" in result.output |
|
|
||
|
|
|
Closing stale PR. Will re-dispatch implementer once pipeline improvements land (issue #86). |
Pull request was closed
Remove labels: ["aw"] from issue-implementer create-pull-request config. The config-level labels feature has a gh-aw runtime bug where the post-creation label API call fails with a node ID resolution error. This caused PR #104 to be created without the aw label. Reverts the labels portion of PR #97. The agent instruction to add the aw label remains and works reliably (worked for PRs #61-91). Closes #107, refs #108 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Closes #59
Adds 14 new tests covering all untested branches identified in the issue:
1. Auto-refresh branches (
_interactive_looplines 169–185)test_auto_refresh_home_view— pre-setschange_eventwhile in home view, asserts_draw_homeis re-calledtest_auto_refresh_cost_view— navigates to cost view, fireschange_event, assertsrender_cost_viewcalled twicetest_auto_refresh_detail_view— navigates to detail view, fireschange_event, asserts_show_session_by_indexcalled twice2. Uppercase commands (
Q,C,R)test_interactive_quit_uppercase— input"Q\n", exit code 0test_interactive_cost_view_uppercase— input"C\nq\n", asserts "Cost" in outputtest_interactive_refresh_uppercase— input"R\nq\n", exit code 03.
events_path=Nonebranchtest_show_session_by_index_events_path_none—SessionSummary(events_path=None)→ "No events path" message4. Group-level
--pathpropagationtest_group_path_propagates_to_summarytest_group_path_propagates_to_sessiontest_group_path_propagates_to_costtest_group_path_propagates_to_live5.
_ensure_awareunit teststest_none_returns_none—None→Nonetest_aware_datetime_unchanged— already-aware → same objecttest_naive_datetime_gets_utc— naive → UTC-attachedResults
test_cli.py)ruff check✅ruff format✅pyright✅Warning
The following domain was blocked by the firewall during workflow execution:
astral.shTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.