# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
from pathlib import Path

import pytest

from playwright.async_api import Browser, Page
from tests.server import Server


@pytest.mark.only_browser("chromium")
async def test_should_output_a_trace(
    browser: Browser, page: Page, server: Server, tmp_path: Path
) -> None:
    output_file = tmp_path / "trace.json"
    await browser.start_tracing(page=page, screenshots=True, path=output_file)
    await page.goto(server.PREFIX + "/grid.html")
    await browser.stop_tracing()
    assert os.path.getsize(output_file) > 0


@pytest.mark.only_browser("chromium")
async def test_should_create_directories_as_needed(
    browser: Browser, page: Page, server: Server, tmp_path: Path
) -> None:
    output_file = tmp_path / "these" / "are" / "directories" / "trace.json"
    await browser.start_tracing(page=page, screenshots=True, path=output_file)
    await page.goto(server.PREFIX + "/grid.html")
    await browser.stop_tracing()
    assert os.path.getsize(output_file) > 0


async def rafraf(target: Page, count: int = 1) -> None:
    for _ in range(count):
        await target.evaluate(
            "async () => await new Promise(f => window.requestAnimationFrame(() => window.requestAnimationFrame(f)));"
        )


@pytest.mark.only_browser("chromium")
async def test_should_run_with_custom_categories_if_provided(
    browser: Browser, page: Page, tmp_path: Path
) -> None:
    output_file = tmp_path / "trace.json"
    await browser.start_tracing(
        page=page,
        path=output_file,
        categories=["disabled-by-default-cc.debug"],
    )
    await rafraf(page)
    await browser.stop_tracing()
    with open(output_file, mode="r") as of:
        trace_json = json.load(of)
        trace_config = trace_json["metadata"].get("trace-config")
        trace_events = trace_json["traceEvents"]
        assert (
            trace_config is not None and "disabled-by-default-cc.debug" in trace_config
        ) or any(
            event.get("cat") == "disabled-by-default-cc.debug" for event in trace_events
        )


@pytest.mark.only_browser("chromium")
async def test_should_throw_if_tracing_on_two_pages(
    browser: Browser, page: Page, tmp_path: Path
) -> None:
    output_file_1 = tmp_path / "trace1.json"
    await browser.start_tracing(page=page, screenshots=True, path=output_file_1)
    output_file_2 = tmp_path / "trace2.json"
    with pytest.raises(Exception):
        await browser.start_tracing(page=page, screenshots=True, path=output_file_2)
    await browser.stop_tracing()


@pytest.mark.only_browser("chromium")
async def test_should_return_a_buffer(
    browser: Browser, page: Page, server: Server, tmp_path: Path
) -> None:
    output_file = tmp_path / "trace.json"
    await browser.start_tracing(page=page, path=output_file, screenshots=True)
    await page.goto(server.PREFIX + "/grid.html")
    value = await browser.stop_tracing()
    with open(output_file, mode="r") as trace_file:
        assert trace_file.read() == value.decode()


@pytest.mark.only_browser("chromium")
async def test_should_work_without_options(
    browser: Browser, page: Page, server: Server
) -> None:
    await browser.start_tracing()
    await page.goto(server.PREFIX + "/grid.html")
    trace = await browser.stop_tracing()
    assert trace


@pytest.mark.only_browser("chromium")
async def test_should_support_a_buffer_without_a_path(
    browser: Browser, page: Page, server: Server
) -> None:
    await browser.start_tracing(page=page, screenshots=True)
    await page.goto(server.PREFIX + "/grid.html")
    await rafraf(page)
    trace = await browser.stop_tracing()
    assert "screenshot" in trace.decode()
