from __future__ import annotations

import pytest

plotly = pytest.importorskip("plotly")

import plotly.graph_objects as go  # noqa: E402

from maidr.api import _is_plotly_figure, render, save_html, close  # noqa: E402


class TestApiPlotlyDetection:
    """Test that the API correctly detects and routes Plotly figures."""

    def test_is_plotly_figure_positive(self):
        fig = go.Figure()
        assert _is_plotly_figure(fig) is True

    def test_is_plotly_figure_negative(self):
        assert _is_plotly_figure("not a figure") is False
        assert _is_plotly_figure(42) is False
        assert _is_plotly_figure(None) is False

    def test_render_plotly(self, plotly_bar_fig):
        tag = render(plotly_bar_fig)
        assert tag is not None

    def test_save_html_plotly(self, plotly_bar_fig, tmp_path):
        output = tmp_path / "api_plotly_test.html"
        result = save_html(plotly_bar_fig, file=str(output))
        assert result is not None

    def test_close_plotly_no_error(self, plotly_bar_fig):
        # close() for Plotly should be a no-op without errors
        close(plotly_bar_fig)
