2022-06-13 11:32:34 -04:00
# frozen_string_literal: true
2023-01-09 11:18:21 +00:00
require " onebox/json_ld "
2022-06-13 11:32:34 -04:00
2022-07-28 05:27:38 +03:00
RSpec . describe Onebox :: JsonLd do
2023-01-09 11:18:21 +00:00
it " logs warning and returns an empty hash if received json is invalid " do
2022-06-13 11:32:34 -04:00
invalid_json = " { \" @type \" :invalid-json} "
2023-01-09 11:18:21 +00:00
doc = Nokogiri . HTML ( " <script type= \" application/ld+json \" > #{ invalid_json } </script> " )
2022-06-13 11:32:34 -04:00
Discourse . expects ( :warn_exception ) . with (
2023-01-09 11:18:21 +00:00
instance_of ( JSON :: ParserError ) ,
message : " Error parsing JSON-LD: #{ invalid_json } " ,
2022-06-13 11:32:34 -04:00
)
json_ld = described_class . new ( doc )
expect ( json_ld . data ) . to eq ( { } )
end
2023-01-09 11:18:21 +00:00
it " returns an empty hash if there is no JSON-LD script tag " do
doc = Nokogiri . HTML ( " <script type= \" something else \" ></script> " )
2022-06-13 11:32:34 -04:00
json_ld = described_class . new ( doc )
expect ( json_ld . data ) . to eq ( { } )
end
2023-01-09 11:18:21 +00:00
it " returns an empty hash if there is no JSON-LD data " do
doc = Nokogiri . HTML ( " <script type= \" application/ld+json \" ></script> " )
2022-06-13 11:32:34 -04:00
json_ld = described_class . new ( doc )
expect ( json_ld . data ) . to eq ( { } )
end
2023-01-09 11:18:21 +00:00
it " returns an empty hash if the type of JSON-LD data is not Movie " do
doc =
Nokogiri . HTML (
" <script type= \" application/ld+json \" >{ \" @type \" : \" Something Else \" , \" aggregateRating \" :{ \" @type \" : \" AggregateRating \" , \" ratingCount \" :806928, \" bestRating \" :10, \" worstRating \" :1}}</script> " ,
)
2022-06-13 11:32:34 -04:00
json_ld = described_class . new ( doc )
expect ( json_ld . data ) . to eq ( { } )
end
2023-01-09 11:18:21 +00:00
it " correctly normalizes the properties " do
doc = Nokogiri . HTML ( onebox_response ( " imdb " ) )
2022-06-13 11:32:34 -04:00
json_ld = described_class . new ( doc )
expect ( json_ld . data ) . to eq ( expected_movie_hash )
end
2023-01-09 11:18:21 +00:00
it " does not fail when there is more than one JSON-LD element " do
doc = Nokogiri . HTML ( onebox_response ( " imdb " ) )
doc . css ( " body " ) [
0
] << " <script type= \" application/ld+json \" >{ \" @context \" : \" http://schema.org \" , \" @type \" : \" WebPage \" , \" url \" : \" https: \ / \ /imdb.com \" , \" description \" : \" Movies \" }</script> "
2022-06-15 02:55:55 +02:00
Discourse . expects ( :warn_exception ) . never
json_ld = described_class . new ( doc )
expect ( json_ld . data ) . to eq ( expected_movie_hash )
end
2023-01-09 11:18:21 +00:00
it " returns first supported type when JSON-LD is an array " do
array_json =
'<script type="application/ld+json">[{"@type": "Something Else"}, {"@context":"https://schema.org","@type":"Movie","url":"/title/tt2358891/","name":"La grande bellezza","alternateName":"The Great Beauty"}]</script>'
doc = Nokogiri . HTML ( array_json )
2022-06-23 17:02:01 +00:00
json_ld = described_class . new ( doc )
2023-01-09 11:18:21 +00:00
expect ( json_ld . data ) . to eq (
{
description : nil ,
duration : nil ,
genres : nil ,
image : nil ,
name : " La grande bellezza " ,
rating : nil ,
} ,
)
2022-06-23 17:02:01 +00:00
end
2023-01-09 11:18:21 +00:00
it " does not fail when JSON-LD returns an array with no supported types " do
array_json =
'<script type="application/ld+json">[{"@type": "Something Else"}, {"@context":"https://schema.org","@type":"Nothing"},{"@context":"https://schema.org"}]</script>'
doc = Nokogiri . HTML ( array_json )
2022-06-23 17:02:01 +00:00
json_ld = described_class . new ( doc )
expect ( json_ld . data ) . to eq ( { } )
end
2022-06-13 11:32:34 -04:00
private
def expected_movie_hash
{
2023-01-09 11:18:21 +00:00
name : " Rudy " ,
image :
" https://m.media-amazon.com/images/M/MV5BZGUzMDU1YmQtMzBkOS00MTNmLTg5ZDQtZjY5Njk4Njk2MmRlXkEyXkFqcGdeQXVyNjc1NTYyMjg@._V1_.jpg " ,
description :
" Rudy has always been told that he was too small to play college football. But he is determined to overcome the odds and fulfill his dream of playing for Notre Dame. " ,
2022-06-13 11:32:34 -04:00
rating : 7 . 5 ,
2023-01-09 11:18:21 +00:00
genres : %w[ Biography Drama Sport ] ,
duration : " 01:54 " ,
2022-06-13 11:32:34 -04:00
}
end
end