import pytest
from webdriver.bidi.modules.script import ContextTarget, ScriptEvaluateResultException

pytestmark = pytest.mark.asyncio


@pytest.mark.parametrize("await_promise", [True, False])
@pytest.mark.parametrize(
    "expression",
    [
        "null",
        "{ toString: 'not a function' }",
        "{ toString: () => {{ throw 'toString not allowed'; }} }",
        "{ toString: () => true }",
    ],
)
async def test_evaluate_without_to_string_interface(
    bidi_session, top_context, await_promise, expression
):
    if await_promise:
        expression = f"Promise.reject({expression})"
    else:
        expression = f"throw {expression}"

    with pytest.raises(ScriptEvaluateResultException) as exception:
        await bidi_session.script.evaluate(
            expression=expression,
            await_promise=await_promise,
            target=ContextTarget(top_context["context"]),
        )

    assert isinstance(exception.value.text, str)
