"""A form's Submit/Cancel button captions live on its on_save / on_cancel
lifecycle events (FormEventAction.button_label) — a caption-only event uses
action_type "none" and runs nothing."""


async def test_button_captions_round_trip_on_events(client, auth_headers):
    resp = await client.post(
        "/forms",
        headers=auth_headers,
        json={
            "name": "Captioned Form",
            "field_definitions": [{"name": "who", "type": "text", "label": "Who"}],
            "events": [
                {"trigger": "on_save", "action_type": "none", "button_label": "Send request"},
                {"trigger": "on_cancel", "action_type": "show_message", "message": "Discarded", "button_label": "Discard"},
            ],
        },
    )
    assert resp.status_code == 201, resp.text
    events = {e["trigger"]: e for e in resp.json()["events"]}
    assert events["on_save"]["action_type"] == "none" and events["on_save"]["button_label"] == "Send request"
    assert events["on_cancel"]["button_label"] == "Discard" and events["on_cancel"]["message"] == "Discarded"

    # editing the draft keeps captions, and a new version copies them along
    form_id = resp.json()["form_id"]
    await client.post(f"/forms/{form_id}/publish", headers=auth_headers)
    v2 = await client.post(f"/forms/{form_id}/new-version", headers=auth_headers)
    assert v2.status_code == 201, v2.text
    assert {e["trigger"]: e["button_label"] for e in v2.json()["events"]} == {"on_save": "Send request", "on_cancel": "Discard"}


async def test_action_none_still_validates_real_actions(client, auth_headers):
    bad = await client.post(
        "/forms",
        headers=auth_headers,
        json={"name": "Bad", "field_definitions": [], "events": [{"trigger": "on_submit", "action_type": "run_task"}]},
    )
    assert bad.status_code == 422
