"""The active-output collector must observe the scripted prefix and completion. A real daemon normally sends both. Use authored frames to verify that a same-length replacement or a clean EOF cannot satisfy the assertions. """ import io import json from pathlib import Path import sys from types import SimpleNamespace import unittest from unittest.mock import Mock sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "e2e")) from active_output_test import PausedOutput class ActiveOutputReaderTests(unittest.TestCase): def collect(self, prefix): app = Mock() app.session.return_value = "session" stream = b"".join( b"data: " + json.dumps({"events": [event]}).encode() + b"\n\n" for event in [ {"type": "reset"}, {"type": "text", "data": {"text": prefix}}, ] ) app.api.side_effect = [io.BytesIO(stream), io.BytesIO()] return PausedOutput(self, app, SimpleNamespace(), ["expected"]) def test_rejects_replaced_prefix_of_the_same_length(self): with self.assertRaisesRegex(AssertionError, "streamed prefix differs"): self.collect("replaced") def test_rejects_eof_without_turn_completed(self): with self.assertRaisesRegex( AssertionError, "stream ended before turn_completed" ): self.collect("expected").finish(self)