"""Запуск: cd scripts && ./venv/bin/python -m unittest discover -s tests"""
import os
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from home_link import build_classic_editpost  # noqa: E402

CLASSIC = """
<form name="post" id="post" method="post" action="post.php">
  <input type="hidden" id="post_ID" name="post_ID" value="35">
  <input type="hidden" name="action" value="editpost">
  <input type="hidden" id="_wpnonce" name="_wpnonce" value="abc123">
  <input type="hidden" name="_wp_http_referer" value="/wp-admin/post.php?post=35&action=edit">
  <input type="hidden" name="post_type" value="page">
  <textarea id="content" name="content">Старый контент страницы.</textarea>
  <input type="hidden" name="original_publish" value="Update">
  <input type="submit" name="save" id="publish" value="Update">
</form>
"""

BLOCK_EDITOR = '<div class="block-editor"></div><form id="other"></form>'


class BuildClassicEditpostTest(unittest.TestCase):
    def test_appends_paragraph_and_keeps_fields(self):
        action, payload = build_classic_editpost(CLASSIC, "<p>НОВОЕ</p>")

        self.assertTrue(action.endswith("post.php"))
        self.assertEqual(payload["post_ID"], "35")
        self.assertEqual(payload["action"], "editpost")
        self.assertEqual(payload["_wpnonce"], "abc123")
        self.assertEqual(payload["post_type"], "page")
        self.assertIn("Старый контент страницы.", payload["content"])
        self.assertIn("<p>НОВОЕ</p>", payload["content"])
        # publish-кнопка включена, чтобы страница сохранилась опубликованной
        self.assertIn("save", payload)

    def test_returns_none_without_classic_content_field(self):
        self.assertIsNone(build_classic_editpost(BLOCK_EDITOR, "<p>x</p>"))

    def test_returns_none_without_nonce(self):
        html = CLASSIC.replace('name="_wpnonce" value="abc123"', 'name="_wpnonce" value=""')
        self.assertIsNone(build_classic_editpost(html, "<p>x</p>"))


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