from __future__ import annotations

import importlib.util
import sys
from pathlib import Path


def _module() -> object:
    path = (
        Path(__file__).resolve().parents[1]
        / "experiments"
        / "perception"
        / "prepare_e15_worker_package.py"
    )
    spec = importlib.util.spec_from_file_location("e15_worker_package_test", path)
    assert spec is not None and spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


def test_e15_worker_package_is_minimal_hash_addressed_projection(tmp_path: Path) -> None:
    module = _module()
    repository = Path(__file__).resolve().parents[1]

    package = module.build_worker_package(repository / "src", tmp_path)
    manifest = module.validate_worker_package(package)

    assert package.name == f"e15-worker-package-{manifest['identity_sha256']}"
    assert manifest["identity"]["classification"] == ("minimal-live-worker-import-projection")
    assert len(manifest["artifacts"]) == 12
    assert (package / "k1link" / "compute" / "inline_temporal.py").is_file()
    assert not (package / "k1link" / "device_plugins" / "xgrids_k1" / "mqtt").exists()
    assert (
        "observation"
        not in (package / "k1link" / "device_plugins" / "xgrids_k1" / "__init__.py").read_text()
    )
