(ns weepinbell.jf2-test (:require [clojure.test :refer [deftest testing is]] [hickory.core :as hickory] [weepinbell.metadata.microformats :refer [parse-microformats]] [weepinbell.metadata.json-ld :refer [parse-json-ld]] [weepinbell.metadata.html-head :refer [parse-meta]] [weepinbell.metadata.jf2 :refer [parse supplement-merge]])) (def to-hick (comp hickory/as-hickory hickory/parse)) ;; microformats -> JF2 (def test-data [{:in {:items [{:type ["h-entry"], :properties {:author [{:type ["h-card"], :properties {:name ["A. Developer"], :url ["https://example.com/"]}, :value "A. Developer"}], :name ["Hello World"], :summary ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus imperdiet ultrices pulvinar."], :url ["https://example.com/2015/10/21"], :published ["2015-10-21T12:00:00-0700"], :content [{:html "

Donec dapibus enim lacus, a vehicula magna bibendum non. Phasellus id lacinia felis, vitae pellentesque enim. Sed at quam dui. Suspendisse accumsan, est id pulvinar consequat, urna ex tincidunt enim, nec sodales lectus nulla et augue. Cras venenatis vehicula molestie. Donec sagittis elit orci, sit amet egestas ex pharetra in.

", :value "Donec dapibus enim lacus, a vehicula magna bibendum non. Phasellus id lacinia felis, vitae pellentesque enim. Sed at quam dui. Suspendisse accumsan, est id pulvinar consequat, urna ex tincidunt enim, nec sodales lectus nulla et augue. Cras venenatis vehicula molestie. Donec sagittis elit orci, sit amet egestas ex pharetra in."}]}}]} :out {:type "entry", :author {:type "card", :url "https://example.com", :name "A. Developer"}, :url "https://example.com/2015/10/21", :published "2015-10-21T12:00:00-0700", :name "Hello World", :summary "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus imperdiet ultrices pulvinar.", :content {:html "

Donec dapibus enim lacus, a vehicula magna bibendum non. Phasellus id lacinia felis, vitae pellentesque enim. Sed at quam dui. Suspendisse accumsan, est id pulvinar consequat, urna ex tincidunt enim, nec sodales lectus nulla et augue. Cras venenatis vehicula molestie. Donec sagittis elit orci, sit amet egestas ex pharetra in.

", :text "Donec dapibus enim lacus, a vehicula magna bibendum non. Phasellus id lacinia felis, vitae pellentesque enim. Sed at quam dui. Suspendisse accumsan, est id pulvinar consequat, urna ex tincidunt enim, nec sodales lectus nulla et augue. Cras venenatis vehicula molestie. Donec sagittis elit orci, sit amet egestas ex pharetra in."}, :wm-property :mention}}]) (deftest jf2-parse-microformats-test (testing "Parsing example microformats to J2F, from https://jf2.spec.indieweb.org/" (doseq [data test-data] (is (= (:out data) (parse-microformats (:in data))))))) ;; JSON-LD -> JF2 (def test-jsonld " ") (def jsonld->jf2 {:type "entry", :name "What is Open Graph and how can I use it for my website?", :url "https://www.freecodecamp.org/news/what-is-open-graph-and-how-can-i-use-it-for-my-website/", :author {:type "card", :name "Colby Fayock", :url "https://www.freecodecamp.org/news/author/colbyfayock/", :photo "https://cdn.hashnode.com/res/hashnode/image/upload/v1601829804572/1eKdHWWIz.jpeg"}, :published "2020-03-26T14:51:33.000Z", :content "", :wm-property :mention, :summary "It can take a lot of time to build content and maintain a website. How can we make sure our content stands out when getting shared on social feeds around the internet?\n\nWhat is Open Graph?\nWhy do I need it?\nWhat happens if I don’t have it?\nStarting w..."}) (def test-jsonld-html (str "" test-jsonld "")) (deftest jf2-parse-jsonld-test (testing "parse JSON-LD from a document" (is (= (-> test-jsonld-html to-hick parse-json-ld) jsonld->jf2))) (testing "parse JSON-LD from raw script element" (is (= (-> test-jsonld to-hick parse-json-ld) jsonld->jf2)))) ;; Open Graph, tags -> JF2 (def link-and-meta-tags {:open-graph " Fallback title " :twitter " Title fallback " :profile-username " " :profile-fullname " " :basic " Document title "}) (deftest jf2-parse-head-test (testing "Open Graph to JF2" (is (= (parse-meta (to-hick (:open-graph link-and-meta-tags))) {:type "entry" :name "Open Graph title" :url "https://example.com/open-graph" :author {:type "card" :name "Open Graph author" :url nil :photo "https://example.com/favicon.ico"} :published "2024-01-02T03:04:05Z" :content "" :wm-property :mention :summary "Open Graph description"})) (is (= (parse-meta (to-hick (:twitter link-and-meta-tags))) {:type "entry" :name "Twitter title" :url "https://example.com/twitter" :author {:type "card" :name "@writer" :url nil :photo nil} :published nil :content "" :wm-property :mention :summary "Twitter description"})) (is (= (parse-meta (to-hick (:profile-username link-and-meta-tags))) {:type "entry" :name "Profile title" :url nil :author {:type "card" :name "profile-username" :url nil :photo nil} :published nil :content "" :wm-property :mention :summary nil})) (is (= (parse-meta (to-hick (:profile-fullname link-and-meta-tags))) {:type "entry" :name nil :url nil :author {:type "card" :name "Rich Hickey" :url nil :photo nil} :published nil :content "" :wm-property :mention :summary nil})) (is (= (parse-meta (to-hick (:basic link-and-meta-tags))) {:type "entry" :name "Document title" :url "https://example.com/cononical" :author {:type "card" :name nil :url nil :photo nil} :published nil :content "" :wm-property :mention :summary "Document description"})))) ;; utilities (deftest supplement-merge-test (testing "supplement-merge supplements A with values A lacks but B has" (is (= (supplement-merge {:a nil :b 1} {:a 1 :b 3}) {:a 1 :b 1}))) (testing "supplement-merge never overrides existing values, except nil" (is (= (supplement-merge {:a "hi"} {:a "bye"}) {:a "hi"}))) (testing "supplement-merge does deep merge as well" (is (= (supplement-merge {:a {:aa "hello" :ab nil} :b nil} {:a {:aa "byebye" :ab "yes"} :b nil}) {:a {:aa "hello" :ab "yes"} :b nil}))) (testing "supplement-merge adds missing keys" (is (= (supplement-merge {:a "n"} {:a "m" :b "s"}) {:a "n" :b "s"})))) ;; compile all sources (def compiling-test-data [{:html "

Microformats title

Permalink
Microformats author Author
Microformats content
Original
" :out {:type "entry" :name "Microformats title" :url "https://example.com/microformats" :author {:type "card" :name "Microformats author" :url "https://example.com/author" :photo nil} :published "2024-02-03T04:05:06Z" :content {:html "Microformats content" :text "Microformats content"} :wm-property :in-reply-to :in-reply-to ["https://example.com/original"] :summary nil}} {:html " Document title

Microformats title

Permalink
Microformats author Author
Microformats content
" :out {:type "entry" :name "Microformats title" :url "https://example.com/microformats-url" :author {:type "card" :name "Microformats author" :url "https://example.com/microformats-author" :photo "https://example.com/author.jpg"} :published "2024-01-02T03:04:05Z" :content {:html "Microformats content" :text "Microformats content"} :wm-property :mention :summary "Open Graph summary"}} {:html " " :out {:type "entry" :name "Twitter title" :url "https://example.com/twitter-url" :author {:type "card" :name "@head-author" :url "https://example.com/json-ld-author" :photo nil} :published "2024-05-06T07:08:09Z" :content "" :wm-property :mention :summary "Head description"}}]) (deftest jf2-parse-test (testing "compiles JF2 from all available metadata sources" (doseq [{:keys [html out]} compiling-test-data] (is (= out (parse html))))))