Skip to content

test(#115): unit tests for _parse_log_summary, _age_str, and cmd_cost#116

Merged
CoreyRDean merged 1 commit into
mainfrom
#115-log-parsing-tests
Apr 19, 2026
Merged

test(#115): unit tests for _parse_log_summary, _age_str, and cmd_cost#116
CoreyRDean merged 1 commit into
mainfrom
#115-log-parsing-tests

Conversation

@CoreyRDean
Copy link
Copy Markdown
Owner

Closes #115

Motivation

`_parse_log_summary` is the core log-parsing function that drives both `clauck history` and `clauck trace`. It landed in #93 with zero unit test coverage. `cmd_cost`'s log-glob + regex parse loop is pure IO + computation — entirely testable with temp files. `_age_str` is a pure function called from both commands.

If any of these silently mis-parse (missed exit code format, wrong cost regex, wrong invocation_id composition), `clauck history` and `clauck cost` would display incorrect data with no test catching the regression.

Before / After

Before: python3 -m pytest tests/ runs 177 tests. _parse_log_summary, _age_str, and cmd_cost are entirely dark.

After: 26 new tests in tests/test_clauck_history_cost.py. python3 -m pytest tests/ runs 203 tests, all green.

What's covered

_parse_log_summary (11 tests):

  • All fields populated: name, ts_str, pid, exit_code, cost_usd, terminal_reason
  • invocation_id composed as {ts}-{pid}
  • No exit_code line → running=True, exit_code=None
  • Nonzero exit code extracted correctly
  • Cost extracted from "total_cost_usd":N.NNNN line
  • No cost line → cost_usd=None
  • terminal_reason extracted from JSON-embedded line
  • Malformed filename (no timestamp) → returns None
  • Empty file → dict returned with running=True, all fields None
  • path field is the original path object
  • Hyphenated job name parsed correctly

_age_str (6 tests):

  • Seconds boundary (< 60s)
  • Minutes boundary (>= 60s, < 3600s)
  • Hours boundary (>= 3600s, < 86400s)
  • Days boundary (>= 86400s)
  • Exactly 60 seconds → "1m ago"
  • Exactly 1 hour → "1h ago"

cmd_cost (9 tests):

  • No log files → prints "no log data found"
  • Single run: correct spend shown in table
  • Multiple jobs sorted by spend descending
  • Total row present when multiple jobs
  • --name filter: only named job shown, others excluded
  • Failure run (terminal_reason ≠ completed) counted in fails column
  • DAG orchestration log (matching -dag- pattern) excluded
  • --days filter: old log (mtime 60 days ago) excluded from 30-day window
  • --all-time: same old log appears when filter bypassed

Cost-to-Value

282-line test file. Zero production code changes. Zero new dependencies. CI adds ~0.1s.

Confidence

  • 203/203 pass locally
  • Tests import lib/clauck via SourceFileLoader (same pattern as other clauck test files)
  • cmd_cost tests patch JOBS_DIR with a tempfile.TemporaryDirectory — fully isolated from ~/.clauck

Validation Steps

git fetch origin '#115-log-parsing-tests'
git checkout '#115-log-parsing-tests'
python3 -m pytest tests/test_clauck_history_cost.py -v
# expect: 26 passed
python3 -m pytest tests/ -q
# expect: 203 passed

26 new tests covering the log-parsing infrastructure that drives clauck
history, clauck trace, and clauck cost. All three functions were entirely
dark until now.
@CoreyRDean CoreyRDean added the enhancement New feature or request label Apr 18, 2026
@CoreyRDean
Copy link
Copy Markdown
Owner Author

🤖 Autonomous Agent Disclosure

This PR was produced entirely without human oversight by an autonomous agent (Model: claude-sonnet-4-6).

Readiness: Draft. Implementation is complete — 26/26 tests pass, 203/203 full suite passes.

Known limitations: None identified. All tests are isolated from `~/.clauck` via `tempfile.TemporaryDirectory` and `JOBS_DIR` patching.

This PR must remain in draft status until a human reviewer explicitly promotes it to ready-for-review.

@CoreyRDean CoreyRDean marked this pull request as ready for review April 19, 2026 01:01
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 625e935838

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

)
out = self._run()
# The failure count should appear in the output (failures != "–")
self.assertIn("1", out)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Check fails column value explicitly

self.assertIn("1", out) can pass even when failure accounting is broken, because cmd_cost output already contains 1 in other fields (for example the runs count for a single job). In that scenario this test would still be green and would not catch a regression in the fails column logic, so it should assert the failure cell specifically (e.g., via a row-level pattern) rather than any digit anywhere in the table output.

Useful? React with 👍 / 👎.

@CoreyRDean CoreyRDean merged commit 0b90e9d into main Apr 19, 2026
2 checks passed
@CoreyRDean CoreyRDean deleted the #115-log-parsing-tests branch April 19, 2026 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: unit tests for log-parsing helpers (_parse_log_summary, cmd_cost)

1 participant