<commit_before>(ns ahnentafel.server.pages-test
  (:require [ahnentafel.server.pages :refer :all])
  (:require [ahnentafel.gedcom.data :as data])
  (:require [clojure.test :refer :all]))

(deftest home-page-test
  (with-redefs [data/header (constantly {:number-of-records 23
                                         :source "Author"
                                         :destination "Recipient"
                                         :file "test.ged"
                                         :file-time "13 SEP 2000"
                                         :gedcom {:version "5.5.1"
                                                  :type "LINEAGE-LINKED"}
                                         :encoding "ANSEL"
                                         :submitter {:xref "@I31@"
                                                     :name "<NAME>"}})]
    (let [page (apply str (home {:version "x.x.x"
                                      :get-data (fn [] 'fake-data)
                                      :file "/path/to/test.ged"}))]
      (is (.contains page "<title>Ahnentafel</title>"))
      (is (.contains page "23 records in test.ged."))
      (is (.contains page "id=\"home-contents\""))
      (is (.contains page "created by Author"))
      (is (.contains page "for Recipient"))
      (is (.contains page "on 13 SEP 2000"))
      (is (.contains page "GEDCOM version 5.5.1 (LINEAGE-LINKED ANSEL)"))
      (is (.contains page "Submitted by <a href=\"/records/@I31@\"><NAME></a>"))
      (is (.contains page "<span id=\"version\">x.x.x</span>")))))

(deftest page-not-found-test
  (let [page (apply str (not-found {:version "x.x.x"} {:uri "/unknown"}))]
    (is (.contains page "<title>Ahnentafel</title>"))
    (is (.contains page "/unknown not found."))
    (is (.contains page "<span id=\"version\">x.x.x</span>"))))

(deftest record-page-test
  (testing "individual record - maximum data"
    (with-local-vars [trapped-query nil]
      (with-redefs [data/find-record
                    (fn [data query]
                      (var-set trapped-query query)
                      {:type :individual
                       :name ["<NAME>" "<NAME>", "The Guy from The Cure"]
                       :birth {:date "1 JAN 1970 00:00:00"
                               :place "near his mother"}
                       :death {:date "1 JAN 2000 00:00:00"
                               :place "graveside"}
                       :burial {:date "2 JAN 2000 00:00:00"
                               :place "6 feet under"}
                       :sex "M"
                       :family-as-child "@FAM1@"
                       :family-as-spouse "@FAM2@"
                       })]
        (let [page (apply str (record {:get-data (fn [] 'fake-data)} "@I23@"))]
          (is (= @trapped-query {:xref "@I23@"}))
          (is (.contains page "INDIVIDUAL"))
          (is (.contains page "<NAME> (a.k.a. <NAME>, The Guy from The Cure)"))
          (is (.contains page "Sex: M"))
          (is (.contains page "Born: 1 JAN 1970 00:00:00 near his mother"))
          (is (.contains page "Died: 1 JAN 2000 00:00:00 graveside"))
          (is (.contains page "Buried: 2 JAN 2000 00:00:00 6 feet under"))
          (is (.contains page "<a href=\"/records/@FAM1@\">Go To Family (where this person was a child)</a>"))
          (is (.contains page "<a href=\"/records/@FAM2@\">Go To Family (where this person was a spouse)</a>") page)))))

  (testing "individual record - with parts missing"
    (with-redefs [data/find-record
                  (constantly {:type :individual})]
      (let [page (apply str (record {:xref "@I23@" :get-data (constantly 'fake-data)}))]
        (doseq [x '("Name" "Sex" "Born" "Died" "Buried" "Go To Family")]
          (is (not (.contains page x))
              (str "Should not find '" x "' on page.")))))))
<commit_msg>Cleaning up tests - pulling out helpers.<commit_after>(ns ahnentafel.server.pages-test
  (:require [ahnentafel.server.pages :refer :all])
  (:require [ahnentafel.gedcom.data :as data])
  (:require [clojure.test :refer :all]))

(defn get-page [page & args]
  (apply str (apply page
                    {:version "x.x.x"
                    :gedcom-file "/path/to/test.ged"
                     :get-data (fn [] 'fake-data)}
                    args)))

(defmethod assert-expr 'page-contains? [msg form]
  (let [text (nth form 1)
        msg (or msg (str "Expected to find '" text "' on the page."))]
    `(let [result# (.contains ~'page ~text)]
       (do-report {:message ~msg :expected '~form :actual ~'page
                   :type (if result# :pass :fail)}))))

(defmethod assert-expr 'page-not-contains? [msg form]
  (let [text (nth form 1)
        msg (or msg (str "Expected not to find '" text "' on the page."))]
    `(let [result# (.contains ~'page ~text)]
       (do-report {:message ~msg :expected '~form :actual ~'page
                   :type (if result# :fail :pass)}))))

(defmacro are-on-page [& texts]
  `(are [text] (~'page-contains? text) ~@texts))

(deftest home-page-test
  (with-redefs [data/header (constantly {:number-of-records 23
                                         :source "Author"
                                         :destination "Recipient"
                                         :file "test.ged"
                                         :file-time "13 SEP 2000"
                                         :gedcom {:version "5.5.1"
                                                  :type "LINEAGE-LINKED"}
                                         :encoding "ANSEL"
                                         :submitter {:xref "@I31@"
                                                     :name "<NAME>"}})]
    (let [page (get-page home)]
      (are-on-page
       "<title>Ahnentafel</title>"
       "23 records in test.ged."
       "id=\"home-contents\""
       "created by Author"
       "for Recipient"
       "on 13 SEP 2000"
       "GEDCOM version 5.5.1 (LINEAGE-LINKED ANSEL)"
       "Submitted by <a href=\"/records/@I31@\"><NAME></a>"
       "<span id=\"version\">x.x.x</span>"))))

(deftest page-not-found-test
  (let [page (get-page not-found {:uri "/unknown"})]
    (are-on-page
     "<title>Ahnentafel</title>"
     "/unknown not found."
     "<span id=\"version\">x.x.x</span>")))

(deftest record-page-test
  (testing "individual record - maximum data"
    (with-local-vars [trapped-query nil]
      (with-redefs [data/find-record
                    (fn [data query]
                      (var-set trapped-query query)
                      {:type :individual
                       :name ["<NAME>" "<NAME>", "The Guy from The Cure"]
                       :birth {:date "1 JAN 1970 00:00:00"
                               :place "near his mother"}
                       :death {:date "1 JAN 2000 00:00:00"
                               :place "graveside"}
                       :burial {:date "2 JAN 2000 00:00:00"
                               :place "6 feet under"}
                       :sex "M"
                       :family-as-child "@FAM1@"
                       :family-as-spouse "@FAM2@"
                       })]
        (let [page (get-page record "@I23@")]
          (is (= @trapped-query {:xref "@I23@"}))
          (are-on-page
           "INDIVIDUAL"
           "<NAME> (a.k.a. <NAME>, The Guy from The Cure)"
           "Sex: M"
           "Born: 1 JAN 1970 00:00:00 near his mother"
           "Died: 1 JAN 2000 00:00:00 graveside"
           "Buried: 2 JAN 2000 00:00:00 6 feet under"
           "<a href=\"/records/@FAM1@\">Go To Family (where this person was a child)</a>"
           "<a href=\"/records/@FAM2@\">Go To Family (where this person was a spouse)</a>")))))

  (testing "individual record - with parts missing"
    (with-redefs [data/find-record
                  (constantly {:type :individual})]
      (let [page (get-page record "@I23@")]
        (are [text] (page-not-contains? text)
             "Name" "Sex" "Born" "Died" "Buried" "Go To Family")))))

{'fraction_non_alphanumeric': 0.15548056492938384, 'fraction_numerical': 0.026246719160104987, 'mean_word_length': 1.5092505487613672, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '915513', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2311, 'n_words': 678}