fix: issue #15 calendar index not properly created, also some smaller bugs
This commit is contained in:
53
test/bds/generation/calendar_json_test.exs
Normal file
53
test/bds/generation/calendar_json_test.exs
Normal file
@@ -0,0 +1,53 @@
|
||||
defmodule BDS.Generation.CalendarJsonTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias BDS.Generation.Paths
|
||||
alias BDS.Generation.Sitemap
|
||||
|
||||
defp unix_ms(date_string) do
|
||||
# Local-noon timestamp so the local-time date matches date_string regardless of TZ.
|
||||
date = Date.from_iso8601!(date_string)
|
||||
naive = NaiveDateTime.new!(date, ~T[12:00:00])
|
||||
:calendar.local_time_to_universal_time_dst(NaiveDateTime.to_erl(naive))
|
||||
|> List.first()
|
||||
|> :calendar.datetime_to_gregorian_seconds()
|
||||
|> Kernel.-(62_167_219_200)
|
||||
|> Kernel.*(1000)
|
||||
end
|
||||
|
||||
defp post(date_string, slug) do
|
||||
%{created_at: unix_ms(date_string), slug: slug, title: slug}
|
||||
end
|
||||
|
||||
test "render_calendar emits years/months/days count maps for the calendar runtime" do
|
||||
posts = [
|
||||
post("2024-05-17", "a"),
|
||||
post("2024-05-17", "b"),
|
||||
post("2024-06-01", "c"),
|
||||
post("2023-12-31", "d")
|
||||
]
|
||||
|
||||
decoded = posts |> Sitemap.render_calendar() |> Jason.decode!()
|
||||
|
||||
assert decoded == %{
|
||||
"years" => %{"2024" => 3, "2023" => 1},
|
||||
"months" => %{"2024-05" => 2, "2024-06" => 1, "2023-12" => 1},
|
||||
"days" => %{
|
||||
"2024-05-17" => 2,
|
||||
"2024-06-01" => 1,
|
||||
"2023-12-31" => 1
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
test "render_calendar of no posts emits empty count maps" do
|
||||
assert Sitemap.render_calendar([]) |> Jason.decode!() ==
|
||||
%{"years" => %{}, "months" => %{}, "days" => %{}}
|
||||
end
|
||||
|
||||
test "day keys use the local date used for post routes" do
|
||||
ts = unix_ms("2024-05-17")
|
||||
decoded = Sitemap.render_calendar([%{created_at: ts, slug: "a", title: "a"}]) |> Jason.decode!()
|
||||
assert Map.keys(decoded["days"]) == [Paths.local_date_iso8601!(ts)]
|
||||
end
|
||||
end
|
||||
@@ -103,7 +103,7 @@ defmodule BDS.GenerationTest do
|
||||
"404.html",
|
||||
"index.html",
|
||||
"sitemap.xml",
|
||||
"feed.xml",
|
||||
"rss.xml",
|
||||
"atom.xml",
|
||||
"calendar.json",
|
||||
"pagefind/index.json",
|
||||
@@ -111,7 +111,7 @@ defmodule BDS.GenerationTest do
|
||||
"pagefind/pagefind-ui.js",
|
||||
"de/404.html",
|
||||
"de/index.html",
|
||||
"de/feed.xml",
|
||||
"de/rss.xml",
|
||||
"de/atom.xml",
|
||||
"de/pagefind/index.json",
|
||||
"de/pagefind/pagefind-ui.css",
|
||||
@@ -214,7 +214,7 @@ defmodule BDS.GenerationTest do
|
||||
"https://example.com/blog" <>
|
||||
"/" <> String.trim_trailing(BDS.Generation.post_output_path(published_post), "index.html")
|
||||
|
||||
feed_xml = File.read!(Path.join([temp_dir, "html", "feed.xml"]))
|
||||
feed_xml = File.read!(Path.join([temp_dir, "html", "rss.xml"]))
|
||||
atom_xml = File.read!(Path.join([temp_dir, "html", "atom.xml"]))
|
||||
|
||||
assert feed_xml =~ "<rss>"
|
||||
@@ -854,12 +854,13 @@ defmodule BDS.GenerationTest do
|
||||
|
||||
assert expected_paths -- Enum.map(result.generated_files, & &1.relative_path) == []
|
||||
|
||||
# Archives paginate newest-first (issue #14), so page 1 holds the newest posts.
|
||||
assert File.read!(Path.join([temp_dir, "html", "category", "notes", "index.html"])) =~
|
||||
"Archive 1"
|
||||
"Archive 3"
|
||||
|
||||
assert File.read!(
|
||||
Path.join([temp_dir, "html", "category", "notes", "page", "2", "index.html"])
|
||||
) =~ "Archive 3"
|
||||
) =~ "Archive 1"
|
||||
|
||||
assert File.read!(Path.join([temp_dir, "html", "tag", "Elixir", "index.html"])) =~ "Elixir"
|
||||
assert File.read!(Path.join([temp_dir, "html", "2026", "04", "index.html"])) =~ "2026-04"
|
||||
@@ -936,7 +937,7 @@ defmodule BDS.GenerationTest do
|
||||
assert {:ok, _result} =
|
||||
BDS.Generation.generate_site(project.id, [:core, :single, :category, :tag, :date])
|
||||
|
||||
feed_path = Path.join([temp_dir, "html", "feed.xml"])
|
||||
feed_path = Path.join([temp_dir, "html", "rss.xml"])
|
||||
atom_path = Path.join([temp_dir, "html", "atom.xml"])
|
||||
not_found_path = Path.join([temp_dir, "html", "404.html"])
|
||||
pagefind_path = Path.join([temp_dir, "html", "pagefind", "index.json"])
|
||||
@@ -1146,7 +1147,7 @@ defmodule BDS.GenerationTest do
|
||||
|
||||
# Ancillary artifacts are all present.
|
||||
assert File.exists?(Path.join([temp_dir, "html", "sitemap.xml"]))
|
||||
assert File.exists?(Path.join([temp_dir, "html", "feed.xml"]))
|
||||
assert File.exists?(Path.join([temp_dir, "html", "rss.xml"]))
|
||||
assert File.exists?(Path.join([temp_dir, "html", "calendar.json"]))
|
||||
assert File.exists?(Path.join([temp_dir, "html", "pagefind", "index.json"]))
|
||||
|
||||
|
||||
@@ -170,6 +170,87 @@ defmodule BDS.PreviewTest do
|
||||
assert :ok = BDS.Preview.stop_preview(project.id)
|
||||
end
|
||||
|
||||
test "preview renders calendar.json dynamically with count maps, ignoring stale files",
|
||||
%{project: project, temp_dir: temp_dir} do
|
||||
assert {:ok, post} =
|
||||
Posts.create_post(%{
|
||||
project_id: project.id,
|
||||
title: "Calendar Post",
|
||||
content: "Calendar body",
|
||||
language: "en"
|
||||
})
|
||||
|
||||
assert {:ok, _published} = Posts.publish_post(post.id)
|
||||
|
||||
# A stale calendar.json in the old (array) format must not be served.
|
||||
File.mkdir_p!(Path.join(temp_dir, "html"))
|
||||
File.write!(Path.join([temp_dir, "html", "calendar.json"]), ~s([{"date":"2000-01-01"}]))
|
||||
|
||||
assert {:ok, _server} = BDS.Preview.start_preview(project.id)
|
||||
|
||||
assert {:ok, %{body: body, content_type: "application/json"}} =
|
||||
BDS.Preview.request(project.id, "/calendar.json")
|
||||
|
||||
decoded = Jason.decode!(body)
|
||||
day_key = BDS.Generation.Paths.local_date_iso8601!(Posts.get_post!(post.id).created_at)
|
||||
|
||||
assert decoded["days"] == %{day_key => 1}
|
||||
assert decoded["months"] == %{binary_part(day_key, 0, 7) => 1}
|
||||
assert decoded["years"] == %{binary_part(day_key, 0, 4) => 1}
|
||||
|
||||
assert :ok = BDS.Preview.stop_preview(project.id)
|
||||
end
|
||||
|
||||
test "preview renders content dynamically and never serves generated content files from disk",
|
||||
%{project: project, temp_dir: temp_dir} do
|
||||
assert {:ok, _metadata} =
|
||||
Metadata.update_project_metadata(project.id, %{
|
||||
public_url: "https://example.com/blog",
|
||||
main_language: "en"
|
||||
})
|
||||
|
||||
assert {:ok, post} =
|
||||
Posts.create_post(%{
|
||||
project_id: project.id,
|
||||
title: "Dynamic Feed Post",
|
||||
content: "Feed body",
|
||||
language: "en"
|
||||
})
|
||||
|
||||
assert {:ok, _published} = Posts.publish_post(post.id)
|
||||
|
||||
# Stale generated content files must never be served.
|
||||
html_dir = Path.join(temp_dir, "html")
|
||||
File.mkdir_p!(Path.join(html_dir, "pagefind"))
|
||||
File.write!(Path.join(html_dir, "rss.xml"), "<rss>stale feed</rss>")
|
||||
File.write!(Path.join(html_dir, "atom.xml"), "<feed>stale atom</feed>")
|
||||
File.write!(Path.join(html_dir, "sitemap.xml"), "<urlset>stale</urlset>")
|
||||
File.write!(Path.join([html_dir, "pagefind", "pagefind-ui.js"]), "console.log('pf')")
|
||||
|
||||
assert {:ok, _server} = BDS.Preview.start_preview(project.id)
|
||||
|
||||
assert {:ok, %{body: feed_xml, content_type: "application/rss+xml"}} =
|
||||
BDS.Preview.request(project.id, "/rss.xml")
|
||||
|
||||
assert feed_xml =~ "Dynamic Feed Post"
|
||||
refute feed_xml =~ "stale feed"
|
||||
|
||||
assert {:ok, %{body: atom_xml, content_type: "application/atom+xml"}} =
|
||||
BDS.Preview.request(project.id, "/atom.xml")
|
||||
|
||||
assert atom_xml =~ "Dynamic Feed Post"
|
||||
refute atom_xml =~ "stale atom"
|
||||
|
||||
# Content files without a dynamic route render the 404 page instead.
|
||||
assert {:ok, %{status: 404}} = BDS.Preview.request(project.id, "/sitemap.xml")
|
||||
|
||||
# Generated static assets (pagefind search index) still come from disk.
|
||||
assert {:ok, %{body: "console.log('pf')", content_type: "application/javascript"}} =
|
||||
BDS.Preview.request(project.id, "/pagefind/pagefind-ui.js")
|
||||
|
||||
assert :ok = BDS.Preview.stop_preview(project.id)
|
||||
end
|
||||
|
||||
test "draft preview renders through the published post template", %{project: project} do
|
||||
assert {:ok, template} =
|
||||
BDS.Templates.create_template(%{
|
||||
|
||||
Reference in New Issue
Block a user