Cotton code
89 lines · 5.2 KB · PythonRaw
import importlib.utilimport jsonfrom pathlib import Pathimport subprocessimport sysimport tempfileimport unittestHERE = Path(__file__).resolve().parentspec = importlib.util.spec_from_file_location('speed_record', HERE / 'record.py')record = importlib.util.module_from_spec(spec)spec.loader.exec_module(record)# A real line from a phone cut on 14 Oct 2026 (iOS 27.0 simulator without its dyld cache).REAL = ('[speed] record page 60.8 ms (budget 100; first open 77.3 ms with the warm-up, 193.4 ms without) '        '· cold start 2199.4 ms (budget 1000) · iOS 27.0 (no dyld cache) simulator')FAST = ('[speed] record page 72.0 ms (budget 100; first open 80.0 ms with the warm-up, 240.0 ms without) '        '· cold start 640.0 ms (budget 1000) · iOS 26.5 simulator')class SpeedRecordTests(unittest.TestCase):    # RULE: The [speed] line parses exactly into typed figures; a changed wording or a missing figure fails loudly and is never read as zero.    def test_the_line_parses_exactly(self):        said = record.parse(REAL, '2026-10-14')        self.assertEqual(said['record_page_ms'], 60.8)        self.assertEqual(said['cold_start_ms'], 2199.4)        self.assertEqual(said['first_open_warmed_ms'], 77.3)        self.assertEqual(said['first_open_unwarmed_ms'], 193.4)        self.assertEqual((said['record_page_budget_ms'], said['cold_start_budget_ms']), (100, 1000))        self.assertEqual(said['measured_on'], 'iOS 27.0 (no dyld cache) simulator')        self.assertEqual(record.parse('step-0-tools=green\n' + FAST + '\n', '2026-10-14')['cold_start_ms'], 640.0)        for broken in ['', 'speed=synthetic',                       REAL.replace('record page', 'record-page'),                       REAL.replace('60.8 ms', 'ms'),                       REAL.replace('cold start 2199.4', 'cold start 2199'),                       REAL.replace(' · iOS 27.0 (no dyld cache) simulator', ''),                       '[speed] record page 72.0 ms (budget 100) · cold start 640.0 ms (budget 1000)',                       REAL + '\n' + FAST,                       REAL.replace('(budget 100;', '(budget 150;')]:            with self.assertRaises(ValueError, msg=broken):                record.parse(broken, '2026-10-14')    # RULE: Speed is reported in every cut and gates only from 1 Nov 2026 (architect, 14 Oct 2026); the cut's date decides, and a refusal names the figure and its budget.    def test_the_gate_is_dated_and_names_figure_and_budget(self):        before = record.parse(REAL, '2026-10-31')        self.assertFalse(before['gates'])        self.assertEqual(before['gates_from'], '2026-11-01')        self.assertIsNone(record.refusal(before))        on = record.parse(REAL, '2026-11-01')        self.assertTrue(on['gates'])        self.assertEqual(record.refusal(on), '[speed] REFUSED: cold start 2199.4 ms is over its budget of 1000 ms (the budget gates from 2026-11-01)')        slow_page = record.parse(FAST.replace('record page 72.0', 'record page 109.2'), '2026-11-02')        self.assertIn('record page 109.2 ms is over its budget of 100 ms', record.refusal(slow_page))        self.assertIsNone(record.refusal(record.parse(FAST, '2026-11-01')))        at_budget = FAST.replace('record page 72.0', 'record page 100.0').replace('cold start 640.0', 'cold start 1000.0')        self.assertIsNone(record.refusal(record.parse(at_budget, '2027-01-01')))    # RULE: A Mac cut's record says the Mac app is not measured as a typed absence beside the simulator figures; an empty Mac figure must never read as a fast Mac.    def test_a_mac_record_carries_the_typed_absence(self):        mac = record.parse(FAST, '2026-10-14', mac=True)        self.assertIsNone(mac['mac_record_page_ms'])        self.assertIsNone(mac['mac_cold_start_ms'])        self.assertEqual(mac['mac_measured_on'], 'not measured on Mac')        self.assertNotIn('mac_measured_on', record.parse(FAST, '2026-10-14'))        said = record.lines(mac)        self.assertIn('record_page_ms=72.0', said)        self.assertIn('gates=false', said)        self.assertIn('mac_record_page_ms=', said)        self.assertIn('mac_measured_on=not measured on Mac', said)    def test_the_command_writes_the_record_and_its_lines(self):        with tempfile.TemporaryDirectory() as temp:            line, out = Path(temp, 'speed.txt'), Path(temp, 'speed.json')            line.write_text(FAST + '\n')            run = subprocess.run([sys.executable, HERE / 'record.py', 'check', line, out, '--mac'], capture_output=True, text=True)            self.assertEqual(run.returncode, 0, run.stderr)            self.assertEqual(json.loads(out.read_text())['cold_start_ms'], 640.0)            lines = subprocess.run([sys.executable, HERE / 'record.py', 'lines', out], capture_output=True, text=True, check=True)            self.assertIn('measured_on=iOS 26.5 simulator', lines.stdout.splitlines())            line.write_text('speed=synthetic\n')            out.unlink()            run = subprocess.run([sys.executable, HERE / 'record.py', 'check', line, out], capture_output=True, text=True)            self.assertNotEqual(run.returncode, 0)            self.assertIn('[speed] FAILED', run.stderr)            self.assertFalse(out.exists())if __name__ == '__main__':    unittest.main()