"""Check strategy attribution without treating the whole DB as validation cache.""" import json from pathlib import Path import tempfile import unittest from cache_size import COMPONENTS, account, file_inventory class CacheSizeTests(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) self.root = Path(self.tmp.name) self.db = self.root / "state" self.db.mkdir() self.stats = self.root / "stats.txt" lines = [f"db={self.db}", "mode=exact", "logical_bytes_available=true"] for cf in COMPONENTS.values(): lines += [f"cf.{cf}.{key}={value}" for key, value in (("keys", 2), ("key_bytes", 10), ("value_bytes", 90), ("live_sst_size_bytes", 55))] # Large public object data must never enter the validation cache total. lines += ["cf.raw_by_hash.value_bytes=900000000", "db.files.total_size_bytes=999999999"] self.stats.write_text("\n".join(lines)) self.contract = self.root / "contract.json" def contract_for(self, **changes): cache = dict(publicationPoint=False, roa=False, childCertificate=False, cryptoSignature=False, transportPrefetch=False) cache.update(changes) self.contract.write_text(json.dumps({"cache": cache})) def test_baseline_reports_common_retention_separately(self): self.contract_for() report = account(self.db, self.stats, self.contract) self.assertEqual(report["required_serialized_bytes"], 0) self.assertEqual(report["all_retained_validation_cf_key_value_bytes"], 700) self.assertEqual(report["profile"], "baseline") def test_hybrid_counts_selected_projections_and_real_index_copy(self): self.contract_for(publicationPoint=True, roa=True, childCertificate=True) index = self.root / "state.pp-cache-index" index.mkdir() (index / "current.idx").write_bytes(b"index copy") child = self.root / "state.child-cert-cache-index" child.mkdir() (child / "pp.idx").write_bytes(b"child index") # A disabled but retained signature sidecar must be exposed, not attributed to Hybrid. (self.root / "state.crypto-sig-cache").write_bytes(b"stale signature") report = account(self.db, self.stats, self.contract) self.assertEqual(report["required_serialized_bytes"], 321) self.assertEqual(report["required_cf_key_value_bytes"], 300) self.assertEqual(report["all_retained_sidecar_file_bytes"], 36) self.assertGreaterEqual(report["sidecars"]["publication_point"]["allocated_bytes"], 10) def test_signature_counts_file_without_object_or_pp_projections(self): self.contract_for(cryptoSignature=True) (self.root / "state.crypto-sig-cache").write_bytes(b"serialized signature conclusions") report = account(self.db, self.stats, self.contract) self.assertEqual(report["profile"], "signature") self.assertEqual(report["required_cf_key_value_bytes"], 0) self.assertEqual(report["required_serialized_bytes"], len(b"serialized signature conclusions")) def test_rejects_estimates_other_db_and_out_of_matrix_contract(self): self.contract_for() text = self.stats.read_text() self.stats.write_text(text.replace("mode=exact", "mode=estimate")) with self.assertRaisesRegex(ValueError, "--exact"): account(self.db, self.stats, self.contract) self.stats.write_text(text) with self.assertRaisesRegex(ValueError, "different state"): account(self.root / "other", self.stats, self.contract) for changes in ({"transportPrefetch": True}, {"roa": True}, {"cryptoSignature": "false"}): self.contract_for(**changes) with self.assertRaises(ValueError): account(self.db, self.stats, self.contract) def test_sidecar_symlink_never_scans_another_profiles_files(self): (self.root / "outside").mkdir() path = self.root / "state.pp-cache-index" path.symlink_to(self.root / "outside", target_is_directory=True) with self.assertRaisesRegex(ValueError, "symlink"): file_inventory(path) if __name__ == "__main__": unittest.main()