from src.workers.tasks.codegen import _build_user_response


def test_prefers_p2hs_user_response_marker() -> None:
    raw = (
        "create mode 100644 server/api/jobs/reminder.send.post.ts\n"
        "Authored by P2HS only\n"
        "P2HS_USER_RESPONSE:\n"
        "Added a forgot-password link on the staff login screen and wired it to the "
        "existing reset flow. The login card layout is unchanged."
    )
    text = _build_user_response(raw, "abc123", ["pages/login.vue"])
    assert "forgot-password" in text
    assert "create mode" not in text
    assert "Authored by P2HS" not in text


def test_skips_git_plumbing_and_keeps_paragraph() -> None:
    raw = (
        "create mode 100644 server/api/jobs/reminder.send.post.ts\n"
        "[Shell]\n"
        "Updated the homepage hero so the banner speaks directly to students planning "
        "to study abroad. The Start Your Application and Contact Us buttons are unchanged."
    )
    text = _build_user_response(raw, "abc123", ["pages/index.vue"])
    assert text.startswith("Updated the homepage hero")
    assert "create mode" not in text
    assert "[Shell]" not in text


def test_does_not_treat_changes_applied_as_applied_prefix() -> None:
    text = _build_user_response(
        "Changes applied.",
        "abc123",
        ["pages/index.vue", "components/Hero.vue"],
    )
    assert "Changes applied" not in text
    assert "index.vue" in text
    assert "Hero.vue" in text


def test_applied_colon_line_still_works() -> None:
    text = _build_user_response(
        "Applied: Added a TOTP option on the staff login page.",
        "abc123",
    )
    assert text == "Added a TOTP option on the staff login page."


def test_empty_summary_uses_file_fallback() -> None:
    text = _build_user_response(None, "abc123", ["pages/login.vue"])
    assert text == "Updated login.vue to apply your request."


def test_no_commit_and_no_summary() -> None:
    assert _build_user_response(None, None) == "No changes were made."


def test_replaces_unified_diff_with_file_fallback() -> None:
    raw = """+++ b[internal path redacted]
@import url('[internal URL redacted]');
+@media(prefers-reduced-motion:reduce){*,*::before{scroll-behavior:auto!important}}
"""
    text = _build_user_response(raw, "abc123", ["src/styles.css"])
    assert text == "Updated styles.css to apply your request."
    assert "internal" not in text.lower()
    assert "+++" not in text


def test_platform_projection_files_are_not_the_customer_change() -> None:
    files = [
        "components/registration/RegistrationFormField.vue",
        "functions/_lib/p2hs-port-contract.mjs",
        "functions/api/ai/complete.js",
        "pages/register.vue",
    ]
    text = _build_user_response("Changes applied.", "abc123", files)
    assert text == "Updated RegistrationFormField.vue and register.vue to apply your request."
    assert "p2hs-port-contract" not in text
    assert "complete.js" not in text
    assert "other files" not in text


def test_customer_response_marker_is_kept_as_written() -> None:
    text = _build_user_response(
        "P2HS_USER_RESPONSE:\nUpdated the homepage headline.",
        "abc123",
        ["functions/_lib/p2hs-port-contract.mjs", "pages/index.vue"],
    )
    assert text == "Updated the homepage headline."


def test_replaces_javascript_fragment_with_file_fallback() -> None:
    raw = """const fileInput = useRef(null);
    function updateValue(event) {
      setValues((current) => ({ ...current, [event.target.name]: event.target.value }));
    }
+ function chooseFile(event) { setFile(nextFile); }
event.
"""
    text = _build_user_response(raw, "abc123", ["src/components/QuoteForm.jsx"])
    assert text == "Updated QuoteForm.jsx to apply your request."
