"""Unit tests for the staleness-check logic (canlex/refresh.py). Offline only.""" import unittest from canlex import refresh class ExtractCurrentDateTests(unittest.TestCase): def test_reads_root_attribute(self): head = (b'\n') self.assertEqual(refresh._extract_current_date(head), "2026-03-31") def test_strips_utf8_bom(self): head = b'\xef\xbb\xbf' self.assertEqual(refresh._extract_current_date(head), "2019-06-21") def test_returns_empty_when_absent(self): self.assertEqual(refresh._extract_current_date(b""), "") def test_takes_the_current_date_not_amended_date(self): # lastAmendedDate must not be mistaken for current-date. head = b'' self.assertEqual(refresh._extract_current_date(head), "2026-05-26") class PendingInForceTests(unittest.TestCase): def test_surfaces_a_bill_once_its_date_passes(self): # C-16 in force 2026-07-18 in the tracked list. after = refresh.pending_in_force("2026-08-01") self.assertTrue(any(b["bill"] == "C-16" for b in after)) def test_hides_a_bill_before_its_date(self): before = refresh.pending_in_force("2026-07-01") self.assertFalse(any(b["bill"] == "C-16" for b in before)) class CbsaInstrumentRegexTests(unittest.TestCase): def test_extracts_dated_instruments_from_index_html(self): html = ('base' 'amend' 'peace officer') found = sorted(set(refresh._CBSA_INSTRUMENT_RE.findall(html))) self.assertEqual(found, ["2023-05-08", "2025-07-10"]) # the peace-officer designation is a different filename and is not picked up self.assertNotIn("2022-08", "".join(found)) def test_new_instrument_is_the_set_difference(self): have = ["2023-05-08", "2025-07-10"] published = ["2023-05-08", "2025-07-10", "2026-02-01"] self.assertEqual(sorted(set(published) - set(have)), ["2026-02-01"]) def test_il3_version_stamp_regex(self): m = refresh._IL3_VERSION_RE.search("IL 3 ... Fall 2025 ... Operational") self.assertEqual(m.group(0), "Fall 2025") class DriftSemanticsTests(unittest.TestCase): def test_iso_date_string_compare_detects_newer_upstream(self): # The check uses plain string comparison on ISO dates; verify ordering. self.assertTrue("2026-05-26" > "2026-03-31") self.assertFalse("2026-03-31" > "2026-03-31") if __name__ == "__main__": unittest.main()