{"content":"\"\"\"The full source-safety contract plus event loss/fallback/performance checks.\"\"\"\nimport importlib.util\nimport os\nfrom pathlib import Path\n\nimport pytest\n\nfrom pallet.pallet_description_generation_guard import GenerationSourceGuard,FileGenerations,WatchUnavailable\nfrom pallet.cluster_targets import digest\n\nspec=importlib.util.spec_from_file_location('_generation_guard_contract',Path(__file__).with_name('test_mask_taxonomy_source_guard.py'))\ncontract=importlib.util.module_from_spec(spec);spec.loader.exec_module(contract);contract.SourceGuard=GenerationSourceGuard\nfixture=contract.fixture\nfor name in dir(contract):\n    if name.startswith('test_'):globals()[name]=getattr(contract,name)\n\n\ndef test_unchanged_generation_does_not_rehash_file_contents(fixture):\n    guard,db,path,members=fixture\n    assert guard.current() and guard.stats()['generation_watch_enabled']\n    count=guard.content_hash_passes\n    for _ in range(20):assert guard.current()\n    assert guard.content_hash_passes==count==1\n\n\ndef test_same_size_write_is_detected_even_with_identical_stat_stamps(fixture,monkeypatch):\n    guard,db,path,members=fixture;assert guard.current();original=path.read_bytes();stamp=guard.stamp(path);real=guard.stamp\n    monkeypatch.setattr(guard,'stamp',lambda p:stamp if Path(p)==path else real(p))\n    path.write_bytes(original.replace(b'saved',b'other'))\n    assert not guard.current()\n    path.write_bytes(original);assert guard.current()\n\n\ndef test_atomic_replace_is_detected_with_same_size_and_mtime(fixture):\n    guard,db,path,members=fixture;assert guard.current();before=path.stat()\n    other=path.with_name('replacement.jsonl');other.write_bytes(path.read_bytes().replace(b'saved',b'other'))\n    os.utime(other,ns=(before.st_atime_ns,before.st_mtime_ns));os.replace(other,path)\n    assert not guard.current()\n\n\ndef test_unrelated_file_write_does_not_invalidate_pinned_content(fixture):\n    guard,db,path,members=fixture;assert guard.current();count=guard.content_hash_passes\n    path.with_name('unrelated.txt').write_text('not a bound source')\n    assert guard.current() and guard.content_hash_passes==count\n\n\ndef test_overflow_and_watch_loss_use_full_hash_fallback(fixture,monkeypatch):\n    guard,db,path,members=fixture;assert guard.current()\n    monkeypatch.setattr(guard.watcher,'drain',lambda:(_ for _ in ()).throw(WatchUnavailable('overflow')))\n    assert guard.current() and not guard.stats()['generation_watch_enabled']\n    before=path.stat();path.write_text(path.read_text().replace('saved','other'));os.utime(path,ns=(before.st_atime_ns,before.st_mtime_ns))\n    assert not guard.current()\n\n\ndef test_no_watcher_available_is_conservative(tmp_path,monkeypatch):\n    from pallet import pallet_description_generation_guard as module\n    monkeypatch.setattr(module,'FileGenerations',lambda paths:(_ for _ in ()).throw(WatchUnavailable('unsupported')))\n    path=tmp_path/'source';path.write_text('original');h=digest(path)\n    guard=GenerationSourceGuard(project=tmp_path,databases={},files={'source':h},inventories={},discover_databases=lambda:set(),discover_inventories=lambda:set())\n    try:\n        assert guard.current() and not guard.stats()['generation_watch_enabled']\n        n=guard.content_hash_passes;assert guard.current() and guard.content_hash_passes>n\n        path.write_text('replaced');assert not guard.current()\n    finally:guard.close()\n\n\ndef test_nonlocal_filesystem_does_not_enable_generation_cache(tmp_path,monkeypatch):\n    monkeypatch.setattr(FileGenerations,'local_filesystem',staticmethod(lambda libc,path:False))\n    path=tmp_path/'one';path.write_text('source');h=digest(path)\n    guard=GenerationSourceGuard(project=tmp_path,databases={},files={'one':h},inventories={},discover_databases=lambda:set(),discover_inventories=lambda:set())\n    try:assert guard.current() and not guard.stats()['generation_watch_enabled']\n    finally:guard.close()\n\n\ndef test_invalid_event_frames_and_queue_overflow_are_not_trusted(tmp_path):\n    path=tmp_path/'one';path.write_text('data');watch=FileGenerations([path])\n    try:\n        with pytest.raises(WatchUnavailable):watch.feed(b'bad')\n        with pytest.raises(WatchUnavailable):watch.feed(watch.HEADER.pack(-1,watch.OVERFLOW,0,0))\n        wd=next(iter(watch.names))\n        with pytest.raises(WatchUnavailable):watch.feed(watch.HEADER.pack(wd,watch.IGNORED,0,0))\n    finally:watch.close()\n\n\ndef test_write_during_hash_cannot_be_trusted(fixture,monkeypatch):\n    guard,db,path,members=fixture;real=guard._hashes\n    def changing():\n        result=real();path.write_text(path.read_text()+' ');return result\n    monkeypatch.setattr(guard,'_hashes',changing)\n    assert not guard.current() and not guard.valid\n\n\ndef test_close_releases_watcher_and_rejects_operations(fixture):\n    guard,db,path,members=fixture;assert guard.current();watch=guard.watcher;guard.close()\n    assert watch.fd is None and not guard.current()\n","path":"tests/test_pallet_description_generation_guard.py"}
