1589
assert str(len(long)) in truncated
1590
1591
1592
+class TestChunkedEventResilience:
1593
+ """A malformed chunked frame must never tear down the subscription.
1594
+
1595
+ ``_handle_chunked_events`` is bound directly as a pysher channel callback,
1596
+ and pysher invokes bound callbacks without a try/except. Before the fix,
1597
+ any malformed frame (bad JSON, missing key, wrong type) propagated up and
1598
+ killed the subscription — the same failure mode already guarded against in
1599
+ ``_parse_payload``.
1600
+ """
1601
+
1602
+ @pytest.fixture
1603
+ def subscription(self):
1604
+ """Create a TriggerSubscription with a mock client."""
1605
+ return TriggerSubscription(client=Mock())
1606
+
1607
+ def test_malformed_json_does_not_raise(self, subscription):
1608
+ """A non-JSON frame is logged and skipped, not raised."""
1609
+ # Must not raise — pysher's dispatch loop has no try/except.
1610
+ subscription._handle_chunked_events("not valid json {")
1611
+ # No chunks were buffered for the bad frame.
1612
+ assert subscription._chunks == {}
1613
+
1614
+ @pytest.mark.parametrize(
1615
+ "event",
1616
+ [
1617
+ [],
1618
+ {"id": "evt-1", "index": 0},
1619
+ {"id": [], "index": 0, "chunk": "x", "final": True},
1620
+ {"id": "evt-1", "index": "zero", "chunk": "x", "final": True},
1621
+ {"id": "evt-1", "index": True, "chunk": "x", "final": True},
1622
+ {"id": "evt-1", "index": 0, "chunk": 1, "final": True},
1623
+ {"id": "evt-1", "index": 0, "chunk": "x", "final": "true"},
1624
+ ],
1625
+ )
1626
+ def test_invalid_frame_is_skipped(self, subscription, event):
1627
+ """Missing or wrongly typed fields are skipped without dispatching."""
1628
+ with patch.object(subscription, "_handle_event") as mock_handle:
1629
+ subscription._handle_chunked_events(json.dumps(event))
1630
+
1631
+ mock_handle.assert_not_called()
1632
+ assert subscription._chunks == {}
1633
+
1634
+ def test_decoder_failure_does_not_raise(self, subscription):
1635
+ """Unexpected decoder failures are contained at the callback boundary."""
1636
+ with patch(
1637
+ "composio.core.models.triggers.json.loads", side_effect=RecursionError
1638
+ ):
1639
+ subscription._handle_chunked_events("[]")
1640
+
1641
+ assert subscription._chunks == {}
1642
+
1643
+ def test_valid_chunks_reassemble_after_bad_frame_for_same_id(self, subscription):
1644
+ """A bad frame clears partial state so the same id can be reused."""
1645
+ with patch.object(subscription, "_handle_event") as mock_handle:
1646
+ subscription._handle_chunked_events(
1647
+ json.dumps(
1648
+ {"id": "evt-1", "index": 0, "chunk": "stale", "final": False}
1649
+ )
1650
+ )
1651
+ subscription._handle_chunked_events(
1652
+ json.dumps(
1653
+ {"id": "evt-1", "index": "bad", "chunk": "x", "final": False}
1654
+ )
1655
+ )
1656
+ subscription._handle_chunked_events(
1657
+ json.dumps({"id": "evt-1", "index": 0, "chunk": "hel", "final": False})
1658
+ )
1659
+ subscription._handle_chunked_events(
1660
+ json.dumps({"id": "evt-1", "index": 1, "chunk": "lo", "final": True})
1661
+ )
1662
+
1663
+ mock_handle.assert_called_once_with(event="hello")
1664
+ assert subscription._chunks == {}
1665
+
1666
+
1667
class TestTriggerSubscriptionStop:
1668
"""Tests for TriggerSubscription.stop lifecycle handling."""
1669