"""Tests for the XML parser."""
from __future__ import annotations
import sys
import textwrap
from pathlib import Path
import pytest

PKG = Path(__file__).parent.parent  # alteryx_runner/
if str(PKG) not in sys.path:
    sys.path.insert(0, str(PKG))

from engine.parser import parse_workflow


def _write_yxmd(tmp_path: Path, body: str) -> Path:
    content = f'<AlteryxDocument yxmdVer="2022.1">{body}<Properties/></AlteryxDocument>'
    p = tmp_path / "test.yxmd"
    p.write_text(content)
    return p


class TestParser:
    def test_simple_nodes(self, tmp_path):
        body = textwrap.dedent("""\
            <Nodes>
              <Node ToolID="1">
                <GuiSettings Plugin="AlteryxBasePluginsGui.TextInput.TextInput">
                  <Position x="0" y="0"/>
                </GuiSettings>
                <Properties><Configuration/></Properties>
              </Node>
              <Node ToolID="2">
                <GuiSettings Plugin="AlteryxBasePluginsGui.Filter.Filter">
                  <Position x="100" y="0"/>
                </GuiSettings>
                <Properties><Configuration><Expression>True</Expression></Configuration></Properties>
              </Node>
            </Nodes>
            <Connections>
              <Connection>
                <Origin ToolID="1" Connection="Output"/>
                <Destination ToolID="2" Connection="Input"/>
              </Connection>
            </Connections>
        """)
        path = _write_yxmd(tmp_path, body)
        graph = parse_workflow(str(path))
        assert 1 in graph.nodes
        assert 2 in graph.nodes
        assert len(graph.connections) == 1
        assert graph.connections[0].origin_id == 1
        assert graph.connections[0].dest_id == 2

    def test_wireless_connection(self, tmp_path):
        body = textwrap.dedent("""\
            <Nodes>
              <Node ToolID="10">
                <GuiSettings Plugin="AlteryxBasePluginsGui.TextInput.TextInput">
                  <Position x="0" y="0"/>
                </GuiSettings>
                <Properties><Configuration/></Properties>
              </Node>
            </Nodes>
            <Connections>
              <Connection Wireless="True">
                <Origin ToolID="10" Connection="Output"/>
                <Destination ToolID="20" Connection="Input"/>
              </Connection>
            </Connections>
        """)
        path = _write_yxmd(tmp_path, body)
        graph = parse_workflow(str(path))
        assert graph.connections[0].wireless is True

    def test_node_position(self, tmp_path):
        body = textwrap.dedent("""\
            <Nodes>
              <Node ToolID="5">
                <GuiSettings Plugin="AlteryxBasePluginsGui.Sort.Sort">
                  <Position x="42" y="99"/>
                </GuiSettings>
                <Properties><Configuration/></Properties>
              </Node>
            </Nodes>
            <Connections/>
        """)
        path = _write_yxmd(tmp_path, body)
        graph = parse_workflow(str(path))
        assert graph.nodes[5].position == (42, 99)
