"""Sequential stream assertions must see later events in the same SSE batch. The real daemon can split or coalesce these events, so feed one authored batch to the test reader to exercise the coalesced schedule deterministically. """ import json from pathlib import Path import sys from types import SimpleNamespace import unittest from unittest.mock import patch sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "e2e")) from session_status_test import Stream class SessionStreamReaderTests(unittest.TestCase): def stream(self, *events): batch = json.dumps({"events": events}).encode() app = SimpleNamespace(connection={"port": 1, "token": "fixture"}) with patch("session_status_test.http.client.HTTPConnection") as connect: connect.return_value.getresponse.return_value = [ b"data: " + batch + b"\n\n" ] stream = Stream(app, "session") self.addCleanup(stream.close) return stream def test_second_match_survives_in_the_same_batch(self): started = {"type": "kernel", "data": {"live_job_count": 1}} ended = {"type": "kernel", "data": {"live_job_count": 0}} stream = self.stream(started, ended) self.assertEqual(stream.pushed(lambda event: event == started, 1), started) self.assertEqual(stream.pushed(lambda event: event == ended, 1), ended) def test_expiry_while_scanning_the_pending_events(self): stream = self.stream({"type": "first"}, {"type": "pending"}) stream.pushed(lambda event: event["type"] == "first", 1) with patch("session_status_test.time.monotonic", side_effect=[0, 0, 2]): with self.assertRaisesRegex(AssertionError, "no pushed event"): stream.pushed(lambda _: False, 1)