11from __future__ import annotations
22
3+ import pathlib
34from typing import TYPE_CHECKING
45
56import conftest
@@ -119,7 +120,7 @@ def test_image_alt(og_meta_tags):
119120
120121
121122@pytest .mark .sphinx ('html' , testroot = 'simple' )
122- def test_image_social_cards (meta_tags ):
123+ def test_image_social_cards (content , meta_tags ):
123124 """Social cards should automatically be added if no og:image is given."""
124125 pytest .importorskip ('matplotlib' )
125126 # Asserting `in` instead of `==` because of the hash that is generated
@@ -136,6 +137,39 @@ def test_image_social_cards(meta_tags):
136137 assert 'summary_large_image' in get_tag_content (
137138 meta_tags , 'card' , kind = 'name' , prefix = 'twitter'
138139 )
140+ png_file = (
141+ pathlib .Path (content .outdir )
142+ / get_tag_content (meta_tags , 'image' ).split ('/' , 5 )[- 1 ]
143+ )
144+ assert png_file .is_file ()
145+ # https://en.wikipedia.org/wiki/List_of_file_signatures
146+ assert png_file .read_bytes ()[:8 ] == b'\x89 PNG\r \n \x1a \n '
147+ assert get_tag_content (meta_tags , 'image:width' ) == '1146'
148+ assert get_tag_content (meta_tags , 'image:height' ) == '600'
149+
150+
151+ @pytest .mark .sphinx ('html' , testroot = 'custom-social-card-generation' )
152+ def test_image_social_cards_custom (content , meta_tags ):
153+ """Providing a custom generation function for social cards."""
154+ # Asserting `in` instead of `==` because of the hash that is generated
155+ assert (
156+ 'http://example.org/en/latest/_images/social_previews/summary_index'
157+ in get_tag_content (meta_tags , 'image' )
158+ )
159+ # Make sure the extra tags are in the HTML
160+ assert 'summary_large_image' in get_tag_content (
161+ meta_tags , 'card' , kind = 'name' , prefix = 'twitter'
162+ )
163+ assert get_tag_content (meta_tags , 'image:width' ) == '1'
164+ assert get_tag_content (meta_tags , 'image:height' ) == '1'
165+ png_file = (
166+ pathlib .Path (content .outdir )
167+ / get_tag_content (meta_tags , 'image' ).split ('/' , 5 )[- 1 ]
168+ )
169+ assert png_file .is_file ()
170+ # https://en.wikipedia.org/wiki/List_of_file_signatures
171+ assert png_file .read_bytes ()[:8 ] == b'\x89 PNG\r \n \x1a \n '
172+ assert len (png_file .read_bytes ()) == 95 # Size of the provided pixel.png
139173
140174
141175@pytest .mark .sphinx ('html' , testroot = 'type' )
0 commit comments