(ns ajet.chat.shared.markdown-unit-test
  (:require [clojure.test :refer [deftest testing is]]
            [ajet.chat.shared.markdown :as md]))

;;; ---------------------------------------------------------------------------
;;; ->html tests
;;; ---------------------------------------------------------------------------

(deftest html-bold
  (is (= "<strong>hello</strong>" (md/->html "**hello**"))))

(deftest html-italic-star
  (is (= "<em>hello</em>" (md/->html "*hello*"))))

(deftest html-italic-underscore
  (is (= "<em>hello</em>" (md/->html "_hello_"))))

(deftest html-underline
  (is (= "<u>hello</u>" (md/->html "__hello__"))))

(deftest html-strikethrough
  (is (= "<del>hello</del>" (md/->html "~~hello~~"))))

(deftest html-inline-code
  (is (= "<code>foo</code>" (md/->html "`foo`"))))

(deftest html-fenced-code-block
  (let [result (md/->html "```clj\n(+ 1 2)\n```")]
    (is (re-find #"<pre><code class=\"language-clj\">" result))
    (is (re-find #"\(&#43; 1 2\)|\(\+ 1 2\)" result))))

(deftest html-spoiler
  (is (= "<span class=\"spoiler\">secret</span>" (md/->html "||secret||"))))

(deftest html-blockquote
  (testing "block quote at start of line"
    (let [result (md/->html "> quoted text")]
      (is (re-find #"<blockquote>" result)))))

(deftest html-autolink
  (let [result (md/->html "visit https://example.com now")]
    (is (re-find #"<a href=\"https://example.com\"" result))))

(deftest html-emoji-shortcode
  (is (re-find #"\u2764" (md/->html ":heart:"))))

(deftest html-escapes-xss
  (let [result (md/->html "<script>alert('xss')</script>")]
    (is (not (re-find #"<script>" result)))
    (is (re-find #"&lt;script&gt;" result))))

(deftest html-newlines-to-br
  (is (re-find #"<br>" (md/->html "line1\nline2"))))

(deftest html-code-not-formatted-inside
  (testing "bold syntax inside inline code is not rendered"
    (let [result (md/->html "`**not bold**`")]
      (is (not (re-find #"<strong>" result)))
      (is (re-find #"<code>" result)))))

(deftest html-combined-formatting
  (let [result (md/->html "**bold** and *italic* and `code`")]
    (is (re-find #"<strong>bold</strong>" result))
    (is (re-find #"<em>italic</em>" result))
    (is (re-find #"<code>code</code>" result))))

;;; ---------------------------------------------------------------------------
;;; ->ansi tests
;;; ---------------------------------------------------------------------------

(deftest ansi-bold
  (let [result (md/->ansi "**hello**")]
    (is (re-find #"\u001b\[1m" result))
    (is (re-find #"hello" result))))

(deftest ansi-italic
  (let [result (md/->ansi "*hello*")]
    (is (re-find #"\u001b\[3m" result))))

(deftest ansi-inline-code
  (let [result (md/->ansi "`code`")]
    (is (re-find #"\u001b\[2m" result))
    (is (re-find #"code" result))))

(deftest ansi-fenced-code-block
  (let [result (md/->ansi "```clj\n(+ 1 2)\n```")]
    (is (re-find #"\(\\+ 1 2\)|\(\+ 1 2\)" result))))

(deftest ansi-emoji-shortcode
  (is (re-find #"\u2764" (md/->ansi ":heart:"))))

(deftest ansi-spoiler-hidden
  (let [result (md/->ansi "||secret||")]
    (is (re-find #"SPOILER" result))
    (is (not (re-find #"secret" result)))))

(deftest ansi-url-rendered
  (let [result (md/->ansi "https://example.com")]
    (is (re-find #"example\.com" result))))
