Skip to content

Improve backend coverage for utility serialization and inbox queue behavior#25

Merged
thinkwee merged 6 commits intomainfrom
copilot/enhance-test-coverage
Apr 25, 2026
Merged

Improve backend coverage for utility serialization and inbox queue behavior#25
thinkwee merged 6 commits intomainfrom
copilot/enhance-test-coverage

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 25, 2026

Coverage analysis showed thin test depth around shared utility serialization logic and message queue/debounce behavior, leaving important edge paths unverified. This PR adds targeted tests for those high-leverage paths and hardens one serialization branch discovered during coverage-driven testing.

  • Coverage focus: backend/utils.py

    • Added direct tests for:
      • UTC timestamp normalization/parsing (ts_fmt, parse_db_iso_utc)
      • timezone resolution + invalid timezone fallback (app_timezone, now_*)
      • DataFrame JSON sanitization (dataframe_to_json_safe) across NaN, Inf, datetime, and empty-frame inputs
      • recursive dict/list scalar serialization (clean_dict_for_json, serialize_value)
  • Coverage focus: backend/messaging/inbox.py

    • Added queue behavior tests for:
      • push/pop roundtrip and empty drains
      • bounded queue overflow semantics (drop oldest)
      • debounce merge behavior within window
      • no merge across sender/channel boundaries
      • wait_and_pop_all burst-drain behavior
  • Targeted runtime hardening

    • Updated serialize_value to handle np.ndarray before pd.isna(...) checks, avoiding ambiguous truth-value failures on array inputs.
    • Simplified control flow to early-return style for readability.
def serialize_value(value: Any) -> Any:
    if isinstance(value, np.ndarray):
        return value.tolist()
    if isinstance(value, (pd.Timestamp, np.datetime64)):
        return ts_fmt(value.to_pydatetime()) if hasattr(value, 'to_pydatetime') else str(value)
    if isinstance(value, (np.integer, np.floating)):
        if np.isnan(value) or np.isinf(value):
            return None
        return value.item()
    try:
        if pd.isna(value):
            return None
    except (TypeError, ValueError):
        pass
    return value

Copilot AI and others added 6 commits April 25, 2026 10:37
Agent-Logs-Url: https://github.com/thinkwee/HiMe/sessions/12ef4c67-b97d-49c8-99de-b4afe7136ddc

Co-authored-by: thinkwee <11889052+thinkwee@users.noreply.github.com>
Agent-Logs-Url: https://github.com/thinkwee/HiMe/sessions/12ef4c67-b97d-49c8-99de-b4afe7136ddc

Co-authored-by: thinkwee <11889052+thinkwee@users.noreply.github.com>
Agent-Logs-Url: https://github.com/thinkwee/HiMe/sessions/12ef4c67-b97d-49c8-99de-b4afe7136ddc

Co-authored-by: thinkwee <11889052+thinkwee@users.noreply.github.com>
Agent-Logs-Url: https://github.com/thinkwee/HiMe/sessions/12ef4c67-b97d-49c8-99de-b4afe7136ddc

Co-authored-by: thinkwee <11889052+thinkwee@users.noreply.github.com>
Agent-Logs-Url: https://github.com/thinkwee/HiMe/sessions/12ef4c67-b97d-49c8-99de-b4afe7136ddc

Co-authored-by: thinkwee <11889052+thinkwee@users.noreply.github.com>
Agent-Logs-Url: https://github.com/thinkwee/HiMe/sessions/12ef4c67-b97d-49c8-99de-b4afe7136ddc

Co-authored-by: thinkwee <11889052+thinkwee@users.noreply.github.com>
@thinkwee thinkwee marked this pull request as ready for review April 25, 2026 11:34
Copilot AI review requested due to automatic review settings April 25, 2026 11:34
@thinkwee thinkwee merged commit 6962407 into main Apr 25, 2026
4 checks passed
@thinkwee thinkwee deleted the copilot/enhance-test-coverage branch April 25, 2026 11:34
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 improves backend test coverage around shared utility serialization/timestamp helpers and async inbox queue behavior, and hardens serialize_value to avoid ambiguous truth-value errors on NumPy arrays.

Changes:

  • Added unit tests for UTC timestamp formatting/parsing, timezone selection/fallback, DataFrame-to-JSON sanitization, and recursive scalar serialization.
  • Added async tests for InboxQueue push/pop, bounded overflow (drop-oldest), debounce merge rules, and wait_and_pop_all drain behavior.
  • Updated serialize_value to handle np.ndarray before pd.isna(...) and to use an early-return flow with guarded pd.isna evaluation.

Reviewed changes

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

File Description
tests/test_utils.py Adds coverage for timestamp/timezone helpers and JSON-safe serialization utilities.
tests/test_inbox_queue.py Adds coverage for inbox queue draining, maxsize overflow behavior, and debounce semantics.
backend/utils.py Hardens serialize_value for NumPy arrays and simplifies the control flow.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_utils.py
Comment on lines +14 to +18
naive = datetime(2026, 1, 2, 3, 4, 5)
aware = datetime(2026, 1, 2, 11, 4, 5, tzinfo=ZoneInfo("Asia/Shanghai"))

assert utils.ts_fmt(naive) == "2026-01-02T03:04:05"
assert utils.ts_fmt(aware) == "2026-01-02T03:04:05"
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

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

These assertions depend on IANA tzdata being available for ZoneInfo("Asia/Shanghai"). The repo doesn’t declare a tzdata dependency and the Docker image is based on python:*-slim (often missing /usr/share/zoneinfo), so this test can fail with ZoneInfoNotFoundError in some environments. Consider using a fixed-offset datetime.timezone(timedelta(hours=8)) here, or skipping the test when ZoneInfo("Asia/Shanghai") isn’t available.

Copilot uses AI. Check for mistakes.
Comment thread tests/test_utils.py
Comment on lines +33 to +38
def test_app_timezone_uses_config_and_falls_back_to_utc(monkeypatch) -> None:
monkeypatch.setattr(settings, "TIMEZONE", "Asia/Shanghai")
assert utils.app_timezone().key == "Asia/Shanghai"

monkeypatch.setattr(settings, "TIMEZONE", "Invalid/Timezone")
assert utils.app_timezone().key == "UTC"
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

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

This test assumes ZoneInfo("Asia/Shanghai") is resolvable on the runner. On environments without tzdata (common for slim Docker bases), utils.app_timezone() can’t reliably return non-UTC zones and this test will fail. Consider guarding with a try/except ZoneInfoNotFoundError and pytest.skip(...), or ensuring tzdata is installed as part of the test/runtime deps.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants