"""Idle Worker polling must not repeatedly parse completed scene geometry.""" import json import os from pathlib import Path import pytest from k1link.simulation.ai_polygon.route_preparation import RoutePreparationStore from k1link.simulation.ai_polygon.run_inputs import digest def put(store, identity=None, status="failed", owner=None): identity = identity or {"case": 1} key = digest(identity) row = dict(key=key, identity=identity, status=status, owner=owner, result=None) path = store.path(key) path.write_text(json.dumps(row)) return path, row def test_idle_scan_skips_unchanged_completed_payload(tmp_path, monkeypatch): store = RoutePreparationStore(tmp_path) path, _ = put(store) assert store.pending() is None original = Path.read_text def forbid_read(candidate, *args, **kwargs): assert candidate != path, "unchanged completed geometry was parsed again" return original(candidate, *args, **kwargs) monkeypatch.setattr(Path, "read_text", forbid_read) assert store.pending() is None assert store.pending() is None def test_external_same_size_change_preserved_mtime_is_not_hidden(tmp_path): store = RoutePreparationStore(tmp_path) path, row = put(store) assert store.pending() is None before = path.stat() row["status"] = "queued" # exactly the same encoded length path.write_text(json.dumps(row)) os.utime(path, ns=(before.st_atime_ns, before.st_mtime_ns)) assert path.stat().st_size == before.st_size assert store.pending() == row assert store.claim(row["key"], "worker")["status"] == "preparing" with pytest.raises(RuntimeError, match="another Worker"): store.claim(row["key"], "other") def test_atomic_replacement_and_new_jobs_are_visible(tmp_path): store = RoutePreparationStore(tmp_path) path, row = put(store) assert store.pending() is None row["status"] = "queued" replacement = path.with_suffix(".tmp") replacement.write_text(json.dumps(row)) replacement.replace(path) assert store.pending() == row path.unlink() assert store.pending() is None assert not store._inactive_files _, other = put(store, {"case": 2}, "queued") assert store.pending() == other def test_pending_and_claim_still_validate_identity(tmp_path): store = RoutePreparationStore(tmp_path) path, row = put(store) assert store.pending() is None row.update(status="queued", identity={"forged": True}) path.write_text(json.dumps(row)) with pytest.raises(ValueError, match="identity changed"): store.pending() with pytest.raises(ValueError, match="identity changed"): store.claim(row["key"], "worker") def test_broken_changed_file_and_symlink_fail_closed(tmp_path): store = RoutePreparationStore(tmp_path) path, _ = put(store) assert store.pending() is None path.write_text("{") with pytest.raises(json.JSONDecodeError): store.pending() path.unlink() target = tmp_path / "target.json" target.write_text("{}") path.symlink_to(target) with pytest.raises(ValueError, match="Invalid route"): store.pending() def test_metadata_index_bounded_and_contains_no_geometry(tmp_path): store = RoutePreparationStore(tmp_path) for number in range(514): put(store, {"case": number}) assert store.pending() is None assert len(store._inactive_files) == 512 assert all(len(stamp) == 5 and all(isinstance(n, int) for n in stamp) for stamp in store._inactive_files.values()) def test_file_changed_during_parse_is_not_cached(tmp_path, monkeypatch): store = RoutePreparationStore(tmp_path) path, row = put(store) original = Path.read_text def mutate(candidate, *args, **kwargs): content = original(candidate, *args, **kwargs) if candidate == path: row["status"] = "queued" path.write_text(json.dumps(row)) return content monkeypatch.setattr(Path, "read_text", mutate) assert store.pending() is None assert not store._inactive_files monkeypatch.setattr(Path, "read_text", original) assert store.pending() == row