from pathlib import Path

import omni.kit.test

from ..reporter import _calculate_durations, _load_report_data, _load_coverage_results, _generate_html_report

CURRENT_PATH = Path(__file__).parent
DATA_TESTS_PATH = CURRENT_PATH.parent.parent.parent.parent.joinpath("data/tests")


class TestReporter(omni.kit.test.AsyncTestCase):
    async def test_success_report_data(self):
        """
        omni_kit_test_success_report.jsonl contains the report.jsonl of a successful run testing omni.kit.test
        """
        path = DATA_TESTS_PATH.joinpath("omni_kit_test_success_report.jsonl")
        report_data = _load_report_data(path)
        self.assertEqual(len(report_data), 11)
        result = report_data[10]
        test_result = result.get("result", None)
        self.assertNotEqual(test_result, None)
        # make sure durations are good
        _calculate_durations(report_data)
        startup_duration = test_result["startup_duration"]
        tests_duration = test_result["tests_duration"]
        self.assertAlmostEqual(startup_duration, 1.040, places=3)
        self.assertAlmostEqual(tests_duration, 0.007, places=3)
        # make sure our ratio are good
        duration = test_result["duration"]
        startup_ratio = test_result["startup_ratio"]
        tests_ratio = test_result["tests_ratio"]
        self.assertAlmostEqual(startup_ratio, 100 * (startup_duration / duration), places=3)
        self.assertAlmostEqual(tests_ratio, 100 * (tests_duration / duration), places=3)

    async def test_fail_report_data(self):
        """
        omni_kit_test_fail_report.jsonl contains the report.jsonl of a failed run of testing omni.kit.test
        with a few failed tests and also a test that crash
        """
        path = DATA_TESTS_PATH.joinpath("omni_kit_test_fail_report.jsonl")
        report_data = _load_report_data(path)
        self.assertEqual(len(report_data), 18)
        result = report_data[17]
        test_result = result.get("result", None)
        self.assertNotEqual(test_result, None)
        # make sure durations are good
        _calculate_durations(report_data)
        startup_duration = test_result["startup_duration"]
        tests_duration = test_result["tests_duration"]
        self.assertAlmostEqual(startup_duration, 0.950, places=3)
        self.assertAlmostEqual(tests_duration, 0.006, places=3)
        # make sure our ratio are good
        duration = test_result["duration"]
        startup_ratio = test_result["startup_ratio"]
        tests_ratio = test_result["tests_ratio"]
        self.assertAlmostEqual(startup_ratio, 100 * (startup_duration / duration), places=3)
        self.assertAlmostEqual(tests_ratio, 100 * (tests_duration / duration), places=3)

    async def test_html_report(self):
        path = DATA_TESTS_PATH.joinpath("omni_kit_test_success_report.jsonl")
        report_data = _load_report_data(path)
        _calculate_durations(report_data)
        merged_results, _ = _load_coverage_results(report_data, read_coverage=False)
        html = _generate_html_report(report_data, merged_results)
        # total duration is 1.32 seconds, in the hmtl report we keep 1 decimal so it will be shown as 1.3
        self.assertTrue(html.find("<td>1.3</td>") != -1)
        # startup duration will be 78.8 %
        self.assertTrue(html.find("<td>78.8</td>") != -1)
