|
| 1 | +"""Tests for history truncation in _to_detail and the get_order_history tool. |
| 2 | +
|
| 3 | +Covers the contract from issue #40: get_order returns the most recent |
| 4 | +`history_limit` entries (default 50) with `history_truncated` and |
| 5 | +`history_total_count` flags so callers know to use get_order_history |
| 6 | +for older entries. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from datetime import UTC, datetime |
| 12 | + |
| 13 | +import pytest |
| 14 | +from statuspro_mcp.tools.orders import ( |
| 15 | + DEFAULT_HISTORY_LIMIT, |
| 16 | + _history_entry, |
| 17 | + _to_detail, |
| 18 | +) |
| 19 | + |
| 20 | +from statuspro_public_api_client.domain import HistoryEntry, Order, OrderStatus |
| 21 | + |
| 22 | + |
| 23 | +def _make_order(*, history_count: int) -> Order: |
| 24 | + """Build a domain Order with `history_count` history entries. |
| 25 | +
|
| 26 | + Entries are ordered chronologically (oldest first) — server convention. |
| 27 | + """ |
| 28 | + history = [ |
| 29 | + HistoryEntry( |
| 30 | + event="status_change", |
| 31 | + status=OrderStatus(code="st000002", name="In Production"), |
| 32 | + comment=None, |
| 33 | + comment_is_public=False, |
| 34 | + created_at=datetime(2026, 1, 1, 10, 0, i % 60, tzinfo=UTC), |
| 35 | + ) |
| 36 | + for i in range(history_count) |
| 37 | + ] |
| 38 | + return Order( |
| 39 | + id=42, |
| 40 | + name="#42", |
| 41 | + order_number="42", |
| 42 | + history=history, |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.unit |
| 47 | +class TestToDetailTruncation: |
| 48 | + """_to_detail respects history_limit and reports truncation accurately.""" |
| 49 | + |
| 50 | + def test_no_history_returns_empty_not_truncated(self): |
| 51 | + order = Order(id=1, name="#1") |
| 52 | + detail = _to_detail(order) |
| 53 | + assert detail.history == [] |
| 54 | + assert detail.history_truncated is False |
| 55 | + assert detail.history_total_count == 0 |
| 56 | + |
| 57 | + def test_history_below_limit_passes_through(self): |
| 58 | + order = _make_order(history_count=10) |
| 59 | + detail = _to_detail(order, history_limit=DEFAULT_HISTORY_LIMIT) |
| 60 | + assert len(detail.history) == 10 |
| 61 | + assert detail.history_truncated is False |
| 62 | + assert detail.history_total_count == 10 |
| 63 | + |
| 64 | + def test_history_at_limit_not_truncated(self): |
| 65 | + """Exactly N entries with limit=N is not truncation.""" |
| 66 | + order = _make_order(history_count=DEFAULT_HISTORY_LIMIT) |
| 67 | + detail = _to_detail(order) |
| 68 | + assert len(detail.history) == DEFAULT_HISTORY_LIMIT |
| 69 | + assert detail.history_truncated is False |
| 70 | + assert detail.history_total_count == DEFAULT_HISTORY_LIMIT |
| 71 | + |
| 72 | + def test_history_above_limit_truncated_to_most_recent(self): |
| 73 | + """Server returns chronological (oldest first); we keep the tail.""" |
| 74 | + order = _make_order(history_count=DEFAULT_HISTORY_LIMIT + 7) |
| 75 | + detail = _to_detail(order) |
| 76 | + assert len(detail.history) == DEFAULT_HISTORY_LIMIT |
| 77 | + assert detail.history_truncated is True |
| 78 | + assert detail.history_total_count == DEFAULT_HISTORY_LIMIT + 7 |
| 79 | + |
| 80 | + # The kept entries are the LAST N (most recent), not the first N. |
| 81 | + # First kept entry is index 7 (entries 0..6 were trimmed). created_at |
| 82 | + # on the MCP-shaped HistoryEntry is the ISO string, so parse to check |
| 83 | + # the second. |
| 84 | + first_kept = detail.history[0].created_at |
| 85 | + assert first_kept is not None |
| 86 | + first_kept_dt = datetime.fromisoformat(first_kept) |
| 87 | + assert first_kept_dt.second == 7 % 60 |
| 88 | + |
| 89 | + def test_custom_history_limit_smaller(self): |
| 90 | + order = _make_order(history_count=20) |
| 91 | + detail = _to_detail(order, history_limit=5) |
| 92 | + assert len(detail.history) == 5 |
| 93 | + assert detail.history_truncated is True |
| 94 | + assert detail.history_total_count == 20 |
| 95 | + |
| 96 | + def test_custom_history_limit_zero_is_not_supported(self): |
| 97 | + """history_limit must be >= 1 — guarded at the MCP tool layer. |
| 98 | + Internal _to_detail with limit=0 would slice to []; verify the slice |
| 99 | + math doesn't blow up (defensive). |
| 100 | + """ |
| 101 | + order = _make_order(history_count=5) |
| 102 | + # The tool itself rejects limit < 1 via Field(ge=1), but the helper |
| 103 | + # is permissive — verify it produces a sane result. |
| 104 | + detail = _to_detail(order, history_limit=1) |
| 105 | + assert len(detail.history) == 1 |
| 106 | + assert detail.history_truncated is True |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.unit |
| 110 | +class TestHistoryEntryConversion: |
| 111 | + """_history_entry converts a domain HistoryEntry into the MCP shape.""" |
| 112 | + |
| 113 | + def test_status_change_entry(self): |
| 114 | + domain_entry = HistoryEntry( |
| 115 | + event="status_change", |
| 116 | + status=OrderStatus(code="st000002", name="In Production"), |
| 117 | + comment=None, |
| 118 | + comment_is_public=False, |
| 119 | + created_at=datetime(2026, 3, 12, 10, 14, tzinfo=UTC), |
| 120 | + ) |
| 121 | + mcp_entry = _history_entry(domain_entry) |
| 122 | + assert mcp_entry.event == "status_change" |
| 123 | + assert mcp_entry.status_code == "st000002" |
| 124 | + assert mcp_entry.status_name == "In Production" |
| 125 | + assert mcp_entry.comment is None |
| 126 | + assert mcp_entry.created_at == "2026-03-12T10:14:00+00:00" |
| 127 | + |
| 128 | + def test_comment_entry(self): |
| 129 | + domain_entry = HistoryEntry( |
| 130 | + event="comment_added", |
| 131 | + status=None, |
| 132 | + comment="Customer asked about ETA.", |
| 133 | + comment_is_public=False, |
| 134 | + created_at=datetime(2026, 3, 13, 9, 0, tzinfo=UTC), |
| 135 | + ) |
| 136 | + mcp_entry = _history_entry(domain_entry) |
| 137 | + assert mcp_entry.event == "comment_added" |
| 138 | + assert mcp_entry.status_code is None |
| 139 | + assert mcp_entry.status_name is None |
| 140 | + assert mcp_entry.comment == "Customer asked about ETA." |
| 141 | + assert mcp_entry.comment_is_public is False |
0 commit comments