"""One push starts CI. Workers do not start a second build for the same change."""

from src.domain.hosting.deploy_trigger import (
    is_generated_app_deploy_workflow,
    should_dispatch_workflow,
    should_promote_production,
)


def test_pages_and_w4p_workflow_names_are_the_completion_path():
    assert is_generated_app_deploy_workflow("Deploy to Cloudflare Pages")
    assert is_generated_app_deploy_workflow("Deploy to Cloudflare W4P")
    assert not is_generated_app_deploy_workflow("P2HS Codegen")


def test_merge_and_webhook_do_not_promote_or_dispatch_again():
    assert should_promote_production({"triggered_by_merge": True}) is False
    assert should_promote_production({"triggered_by": "workflow_run_webhook"}) is False
    assert should_promote_production({}) is True
    assert should_dispatch_workflow(
        {"triggered_by_merge": True}, job_try=1, ci_just_pushed=False
    ) is False
    assert should_dispatch_workflow(
        {"triggered_by": "workflow_run_webhook"}, job_try=5, ci_just_pushed=False
    ) is False
    assert should_dispatch_workflow({}, job_try=1, ci_just_pushed=True) is False


def test_dispatch_is_only_a_later_backfill():
    assert should_dispatch_workflow({}, job_try=1, ci_just_pushed=False) is False
    assert should_dispatch_workflow({}, job_try=2, ci_just_pushed=False) is True
