from __future__ import annotations
import unittest
from app.simulation.benchmark_performance import (
_duration_summary,
_load_factory_xml,
_named_value,
_parse_arguments,
_serialize_result_event,
)
def sample_xml_factory() -> bytes:
return b""
class PerformanceBenchmarkToolTests(unittest.TestCase):
def test_named_value_requires_an_explicit_name(self) -> None:
self.assertEqual(
_named_value("chain=tests.example:project", option="--factory"),
("chain", "tests.example:project"),
)
with self.assertRaisesRegex(ValueError, "NAME=VALUE"):
_named_value("tests.example:project", option="--factory")
def test_duration_summary_reports_repeatable_order_statistics(self) -> None:
summary = _duration_summary([5.0, 1.0, 3.0, 2.0, 4.0])
self.assertEqual(summary["minimumMs"], 1.0)
self.assertEqual(summary["medianMs"], 3.0)
self.assertEqual(summary["p95Ms"], 5.0)
self.assertEqual(summary["maximumMs"], 5.0)
def test_factory_loader_accepts_a_bytes_factory(self) -> None:
self.assertEqual(
_load_factory_xml(
"tests.test_performance_benchmark:sample_xml_factory"
),
b"",
)
def test_result_event_serialization_uses_ndjson_shape(self) -> None:
payload = _serialize_result_event(
{
"success": True,
"status": "completed",
"simulatedUntil": 1.0,
"requestedStopTime": 1.0,
}
)
self.assertTrue(payload.endswith(b"\n"))
self.assertIn(b'"event":"result"', payload)
self.assertIn(b'"result":{"success":true', payload)
def test_cache_ab_flag_is_explicit(self) -> None:
arguments = _parse_arguments(
[
"--factory",
"sample=tests.test_performance_benchmark:sample_xml_factory",
"--disable-property-cache",
]
)
self.assertTrue(arguments.disable_property_cache)
if __name__ == "__main__":
unittest.main()