Skip to content

Commit c24fc8e

Browse files
committed
try to maintain the og images
1 parent 6027e54 commit c24fc8e

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
defmodule JolaDevWeb.Helpers.OGImageTest do
2+
use JolaDevWeb.ConnCase, async: true
3+
4+
@skip_plugs [
5+
JolaDevWeb.RssController,
6+
JolaDevWeb.SitemapController,
7+
JolaDevWeb.LlmsController,
8+
Phoenix.LiveView.Plug,
9+
Phoenix.LiveDashboard.Assets
10+
]
11+
12+
test "every public page has a working OG image" do
13+
for route <- JolaDevWeb.Router.__routes__(),
14+
route.verb == :get,
15+
route.plug not in @skip_plugs do
16+
path = concrete_path(route.path)
17+
page = get(build_conn(), path)
18+
assert page.status == 200, "GET #{path} returned #{page.status}"
19+
20+
og_url = og_image_url(page.resp_body, path)
21+
og_path = URI.parse(og_url).path
22+
og_conn = get(build_conn(), og_path)
23+
24+
assert og_conn.status == 200,
25+
"page #{path} declares og:image #{og_url} but it returned #{og_conn.status}. " <>
26+
"Add a catalog entry for slug \"#{slug(og_path)}\"."
27+
end
28+
end
29+
30+
defp og_image_url(body, path) do
31+
case body
32+
|> LazyHTML.from_document()
33+
|> LazyHTML.query("meta[property='og:image']")
34+
|> LazyHTML.attribute("content") do
35+
[url] -> url
36+
[] -> flunk("no og:image meta tag on #{path}")
37+
urls -> flunk("multiple og:image meta tags on #{path}: #{inspect(urls)}")
38+
end
39+
end
40+
41+
defp concrete_path("/posts/:id"),
42+
do: "/posts/#{List.first(JolaDev.Blog.all_posts()).id}"
43+
44+
defp concrete_path("/posts/tag/:tag"),
45+
do: "/posts/tag/#{List.first(JolaDev.Blog.all_tags())}"
46+
47+
defp concrete_path(path), do: path
48+
49+
defp slug("/images/og/" <> rest), do: String.replace_suffix(rest, ".png", "")
50+
end

0 commit comments

Comments
 (0)