Skip to content

Add hook script tests #18

@roeimichael

Description

@roeimichael

Problem

The hook scripts in hooks/ contain meaningful logic — build_envelope constructs event payloads with token estimation, format_statusline builds the statusline string, estimate_tokens performs chars-to-tokens conversion — but none of this is tested. The hooks are also excluded from pyright type checking.

What to do

Create tests/test_hooks.py that imports and tests the pure functions:

  1. build_envelope — verify truncation, token estimation, payload structure
  2. format_statusline — verify output format with various input combinations
  3. estimate_tokens — verify calculation matches chars / chars_per_token_estimate
  4. get_session_alert — test with mocked HTTP responses (collector up vs down)

Approach

These functions use only stdlib + httpx, so they can be imported and tested directly. The main() functions can be tested with stdin mocking.

Files to look at

  • hooks/post_tool_use.pybuild_envelope, estimate_tokens
  • hooks/statusline.pyformat_statusline
  • hooks/_hook_config.py — config loading

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: hooksClaude Code hook scriptsarea: testsTest coverage and qualitydifficulty: intermediateRequires some familiarity with the codebaseenhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions