"""Tests for web UI prompt library (#428)."""
import json
import sys
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from starlette.testclient import TestClient

from webui.app import create_app
from webui.prompt_library import find_prompt, library_to_dict, load_prompt_library, prompt_to_dict

REQUIRED_PROMPT_SLUGS = frozenset({
    "review-pr",
    "work-issue",
    "create-issue",
    "comment-issue",
    "cleanup",
    "audit",
    "onboarding",
})


class TestPromptLibraryLoader(unittest.TestCase):
    def test_library_loads_required_prompts(self):
        entries = load_prompt_library()
        slugs = {entry.slug for entry in entries}
        self.assertEqual(slugs, REQUIRED_PROMPT_SLUGS)

    def test_workflow_hashes_present(self):
        review = find_prompt("review-pr")
        self.assertIsNotNone(review)
        assert review is not None
        self.assertTrue(review.workflow_hash)
        self.assertEqual(len(review.workflow_hash), 64)

    def test_prompt_text_is_short(self):
        for entry in load_prompt_library():
            self.assertLess(len(entry.prompt_text), 400)
            self.assertNotIn("Do not improvise around the gates", entry.prompt_text)


class TestPromptLibraryRoutes(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(create_app())

    def test_prompts_page_lists_all_entries(self):
        response = self.client.get("/prompts")
        self.assertEqual(response.status_code, 200)
        for label in (
            "Review PR",
            "Work issue",
            "Create issue",
            "Comment on issue",
            "Post-merge cleanup",
            "Reconciliation audit",
            "Project onboarding",
        ):
            self.assertIn(label, response.text)
        self.assertIn("Copy prompt", response.text)
        self.assertIn("sha256:", response.text)
        self.assertNotIn("child issue", response.text.lower())

    def test_prompt_detail_route(self):
        response = self.client.get("/prompts/work-issue")
        self.assertEqual(response.status_code, 200)
        self.assertIn("work-issue.md", response.text)
        self.assertIn("Copy prompt", response.text)

    def test_prompt_detail_404(self):
        self.assertEqual(self.client.get("/prompts/missing").status_code, 404)

    def test_api_prompts_json(self):
        response = self.client.get("/api/prompts")
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertEqual(data["count"], 7)
        review = next(item for item in data["prompts"] if item["slug"] == "review-pr")
        self.assertIn("workflow_hash", review)
        self.assertIn("review-merge-pr.md", review["workflow_path"])

    def test_prompt_to_dict_roundtrip(self):
        entry = load_prompt_library()[0]
        encoded = json.dumps(prompt_to_dict(entry))
        self.assertIn("workflow_path", encoded)


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