import json

from src.domain.codegen.dependency_contract import validate_package_lock
from src.domain.hosting.deployment_targets import generate_deploy_workflow, generate_worker_deploy_workflow


def test_lockfile_validation_is_read_only_and_conformant(tmp_path):
    (tmp_path / "package.json").write_text(json.dumps({"dependencies": {"vite": "latest"}}))
    (tmp_path / "package-lock.json").write_text(json.dumps({
        "lockfileVersion": 3,
        "packages": {"": {"dependencies": {"vite": "1.0.0"}}},
    }))
    assert validate_package_lock(tmp_path)["status"] == "conformant"


def test_workflows_fail_closed_to_locked_npm_install():
    pages = generate_deploy_workflow("react_app", "p2hs-test")
    worker = generate_worker_deploy_workflow("p2hs-test")
    assert "npm ci" in pages and "npm install" not in pages
    assert "test -f package-lock.json" in pages
    assert "npm ci" in worker and "npm install" not in worker
