Cotton code
43 lines · 2.3 KB · PythonRaw
import importlib.utilfrom pathlib import Pathimport unittestspec = importlib.util.spec_from_file_location('speed_report', Path(__file__).with_name('report.py'))report = importlib.util.module_from_spec(spec)spec.loader.exec_module(report)class SpeedReportTests(unittest.TestCase):    # RULE: Missing or invalid timing samples fail a cut; an empty measurement must never look like a fast app.    def test_missing_and_invalid_samples_fail(self):        valid = {'cold_start_ms': [640] * 5, 'record_page_ms': {kind: [72] * 10 for kind in report.KINDS}}        self.assertEqual(report.figures(valid), (72, 640))        slow_person = {**valid, 'record_page_ms': {**valid['record_page_ms'], 'person': [105] * 10}}        self.assertEqual(report.figures(slow_person), (105, 640))        for broken in [{}, {**valid, 'cold_start_ms': []},                       {**valid, 'record_page_ms': {'todo': [72] * 10}},                       {**valid, 'cold_start_ms': [float('nan')] * 5},                       {**valid, 'cold_start_ms': [0] * 5}]:            with self.assertRaises(ValueError):                report.figures(broken)    # RULE: The first open is reported with AND without the record page's warm-up (architect, 14 Oct 2026), so a warm-up can never hide what a page costs cold; a missing half fails the cut.    def test_first_open_is_reported_both_ways(self):        both = {'first_open_ms': {'warmed': {kind: [80] * 5 for kind in report.KINDS},                                  'unwarmed': {kind: [240] * 5 for kind in report.KINDS}}}        self.assertEqual(report.first_open(both, 'warmed'), 80)        self.assertEqual(report.first_open(both, 'unwarmed'), 240)        for broken in [{}, {'first_open_ms': {'warmed': both['first_open_ms']['warmed']}},                       {'first_open_ms': {**both['first_open_ms'], 'unwarmed': {'todo': [240] * 5}}}]:            with self.assertRaises(ValueError):                report.first_open(broken, 'unwarmed')    def test_a_figure_names_its_runtime(self):        self.assertEqual(report.runtime({'runtime_name': 'iOS 26.5'}), 'iOS 26.5')        for broken in [{}, {'runtime_name': ''}, {'runtime_name': None}]:            with self.assertRaises(ValueError):                report.runtime(broken)if __name__ == '__main__':    unittest.main()