from datetime import datetime, timezone import json import os from pathlib import Path import tempfile import unittest from unittest.mock import MagicMock import urllib.error from paper_atlas.io.html_cache import PublicHTMLCache, digest, NoRedirect URL = 'https://eccv.ecva.net/virtual/2026/poster/4048' class PublicHTMLCacheTests(unittest.TestCase): def receipt(self, raw): return {'url':URL,'final_url':URL,'http_status':200,'content_type':'text/html; charset=utf-8', 'bytes':len(raw),'sha256':digest(raw),'retrieved_at':datetime.now(timezone.utc).isoformat()} def test_old_cache_reused_without_network_or_expiry(self): with tempfile.TemporaryDirectory() as tmp: c = PublicHTMLCache(tmp, 'https://eccv.ecva.net', delay=0); raw = b'metadata' body, receipt, _ = c.paths(URL); body.write_bytes(raw); receipt.write_text(json.dumps(self.receipt(raw))); os.utime(body,(0,0)) c.opener = MagicMock(); self.assertEqual(c.get(URL)[0], raw); c.opener.open.assert_not_called() def test_pair_tampering_and_orphan_parts_fail_without_refetch(self): for kind in ('hash','orphan','part'): with self.subTest(kind=kind), tempfile.TemporaryDirectory() as tmp: c = PublicHTMLCache(tmp, 'https://eccv.ecva.net', delay=0); body, receipt, _ = c.paths(URL); body.write_bytes(b'original') if kind == 'hash': receipt.write_text(json.dumps(self.receipt(b'different'))) if kind == 'part': body.unlink(); body.with_suffix('.html.part').write_bytes(b'partial') c.opener = MagicMock() with self.assertRaises(ValueError): c.get(URL) c.opener.open.assert_not_called() def test_http_access_failure_sticky_without_automatic_retry(self): with tempfile.TemporaryDirectory() as tmp: c = PublicHTMLCache(tmp, 'https://eccv.ecva.net', delay=0); c.opener = MagicMock() c.opener.open.side_effect = urllib.error.HTTPError(URL,403,'Forbidden',{},None) with self.assertRaises(urllib.error.HTTPError): c.get(URL) with self.assertRaisesRegex(ValueError,'Previous access'): c.get(URL) self.assertEqual(c.opener.open.call_count,1) def test_redirect_nonhtml_challenge_and_oversize_rejected(self): for kind in ('redirect','type','challenge','size'): with self.subTest(kind=kind), tempfile.TemporaryDirectory() as tmp: c = PublicHTMLCache(tmp,'https://eccv.ecva.net',limit=100,delay=0); c.opener=MagicMock(); r=c.opener.open.return_value.__enter__.return_value r.status=200; r.geturl.return_value=URL; r.headers={'Content-Type':'text/html'}; r.read.return_value=b'metadata' if kind=='redirect':r.geturl.return_value='https://example.org/login' if kind=='type':r.headers={'Content-Type':'application/pdf'} if kind=='challenge':r.read.return_value=b'