import importlib.util
import unittest


spec = importlib.util.spec_from_file_location("dev_server", "dev-server.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)


class GithubPromptImportTests(unittest.TestCase):
    def test_normalizes_github_blob_and_raw_urls(self):
        expected = "https://raw.githubusercontent.com/acme/prompts/main/library.json"
        self.assertEqual(module.github_raw_url("https://github.com/acme/prompts/blob/main/library.json"), expected)
        self.assertEqual(module.github_raw_url(expected), expected)

    def test_rejects_non_github_or_non_https_urls(self):
        with self.assertRaises(ValueError):
            module.github_raw_url("http://github.com/acme/prompts/blob/main/library.json")
        with self.assertRaises(ValueError):
            module.github_raw_url("https://example.com/library.json")

    def test_parses_supported_formats_and_keeps_imports_pending(self):
        json_rows = module.prompt_rows_from_source("https://raw.githubusercontent.com/acme/prompts/main/prompts.json", '{"prompts":[{"title":"One","prompt":"a prompt"}]}')
        csv_rows = module.prompt_rows_from_source("https://raw.githubusercontent.com/acme/prompts/main/prompts.csv", "title,prompt\nTwo,b prompt\n")
        markdown_rows = module.prompt_rows_from_source("https://raw.githubusercontent.com/acme/prompts/main/prompts.md", "# Three\nmarkdown prompt\n")
        self.assertEqual(len(json_rows), 1)
        self.assertEqual(len(csv_rows), 1)
        self.assertEqual(len(markdown_rows), 1)
        imported, updated = module.normalize_imported_prompts(json_rows, "https://raw.githubusercontent.com/acme/prompts/main/prompts.json", "MIT", [])
        self.assertEqual(updated, 0)
        self.assertFalse(imported[0]["is_active"])
        self.assertEqual(imported[0]["source_license"], "MIT")


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