# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project

import unittest
import os

from opentimelineio.console import (
    autogen_serialized_datamodel as asd,
    autogen_plugin_documentation as apd,
)


@unittest.skipIf(
    os.environ.get("OTIO_DISABLE_SERIALIZED_SCHEMA_TEST"),
    "Serialized schema test disabled because "
    "$OTIO_DISABLE_SERIALIZED_SCHEMA_TEST is set to something other than ''"
)
class SerializedSchemaTester(unittest.TestCase):
    def test_serialized_schema(self):
        """Test if the schema has changed since last time the serialized schema
        documentation was generated.
        """

        pt = os.path.dirname(os.path.dirname(__file__))
        fp = os.path.join(pt, "docs", "tutorials", "otio-serialized-schema.md")
        with open(fp) as fi:
            baseline_text = fi.read()

        test_text, _ = asd.generate_and_write_documentation()

        self.maxDiff = None
        self.longMessage = True
        self.assertMultiLineEqual(
            baseline_text,
            test_text,
            "\n The schema has changed and the autogenerated documentation in {}"
            " needs to be updated.  run: `make doc-model-update`".format(fp)
        )


@unittest.skipIf(
    os.environ.get("OTIO_DISABLE_SERIALIZED_SCHEMA_TEST"),
    "Serialized schema test disabled because "
    "$OTIO_DISABLE_SERIALIZED_SCHEMA_TEST is set to something other than ''"
)
class PluginDocumentationTester(unittest.TestCase):
    def test_plugin_documentation(self):
        """Verify that the plugin manifest matches what is checked into the
        documentation.
        """

        pt = os.path.dirname(os.path.dirname(__file__))
        fp = os.path.join(pt, "docs", "tutorials", "otio-plugins.md")
        with open(fp) as fi:
            baseline_text = fi.read()

        test_text = apd.generate_and_write_documentation_plugins(
            public_only=True,
            sanitized_paths=True
        )

        self.maxDiff = None
        self.longMessage = True
        self.assertMultiLineEqual(
            baseline_text,
            test_text,
            "\n The schema has changed and the autogenerated documentation in {}"
            " needs to be updated.  run: `make doc-plugins-update`".format(fp)
        )


if __name__ == '__main__':
    unittest.main()
