#
# This file was generated from the asynchronous client at test_async_requests.py by generate_synchronous_client.sh
# Do not edit this file directly, instead edit the original file and regenerate this file.
#




from yarl import URL

from nrp_cmd.sync_client.invenio import SyncInvenioRepositoryClient
from nrp_cmd.types.records import Record
from nrp_cmd.types.requests import RequestPayload


def test_publish_request(
    draft_record: Record, local_client: SyncInvenioRepositoryClient
):
    records_client = local_client.records
    requests_client = local_client.requests

    assert draft_record.files_ and draft_record.files_.enabled is False

    applicable = requests_client.applicable_requests(draft_record)
    assert applicable.keys() == {"publish_draft"}

    request = requests_client.create(draft_record, "publish_draft", {})
    assert request.status == "created"

    print(request.payload)
    assert request.payload == RequestPayload(published_record=None, draft_record=None)

    requests = requests_client.created()
    assert any(r.id == request.id for r in requests.hits)

    submitted_request = requests_client.submit(request)
    assert submitted_request.status == "submitted"

    requests = requests_client.submitted()
    assert any(r.id == request.id for r in requests.hits)

    accepted_request = requests_client.accept(submitted_request)
    assert accepted_request.status == "accepted"

    requests = requests_client.accepted()
    assert any(r.id == request.id for r in requests.hits)

    assert accepted_request.payload is not None
    assert accepted_request.payload.draft_record is None
    assert accepted_request.payload.published_record is not None
    assert accepted_request.payload.published_record.links.self_ == URL(
        f'https://127.0.0.1:5000/api/simple/{accepted_request.topic["simple"]}'
    )

