defmodule BDS.GenerationTest do
use ExUnit.Case, async: false
import Ecto.Query
alias BDS.Media
alias BDS.Metadata
alias BDS.Posts
alias BDS.Repo
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
temp_dir = Path.join(System.tmp_dir!(), "bds-generation-#{System.unique_integer([:positive])}")
File.mkdir_p!(temp_dir)
on_exit(fn -> File.rm_rf(temp_dir) end)
{:ok, project} = BDS.Projects.create_project(%{name: "Generation", data_path: temp_dir})
%{project: project, temp_dir: temp_dir}
end
test "write_generated_file writes under html output and skips unchanged content by hash", %{project: project, temp_dir: temp_dir} do
assert {:ok, first_write} = BDS.Generation.write_generated_file(project.id, "index.html", "hello")
assert first_write.written? == true
output_path = Path.join([temp_dir, "html", "index.html"])
assert File.read!(output_path) == "hello"
assert {:ok, [tracked_file]} = BDS.Generation.list_generated_files(project.id)
assert tracked_file.relative_path == "index.html"
assert tracked_file.content_hash == first_write.content_hash
assert {:ok, second_write} = BDS.Generation.write_generated_file(project.id, "index.html", "hello")
assert second_write.written? == false
assert second_write.content_hash == first_write.content_hash
assert {:ok, third_write} = BDS.Generation.write_generated_file(project.id, "index.html", "updated")
assert third_write.written? == true
assert third_write.content_hash != first_write.content_hash
assert File.read!(output_path) == "updated"
end
test "delete_generated_file removes tracked output and forgets its hash", %{project: project, temp_dir: temp_dir} do
assert {:ok, _write} = BDS.Generation.write_generated_file(project.id, "tag/elixir/index.html", "tag")
output_path = Path.join([temp_dir, "html", "tag", "elixir", "index.html"])
assert File.exists?(output_path)
assert :ok = BDS.Generation.delete_generated_file(project.id, "tag/elixir/index.html")
refute File.exists?(output_path)
assert {:ok, files} = BDS.Generation.list_generated_files(project.id)
assert files == []
end
test "plan_generation derives generation settings from project metadata and core generation writes tracked files", %{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",
blog_languages: ["en", "de"],
max_posts_per_page: 25,
pico_theme: "amber"
})
assert {:ok, plan} = BDS.Generation.plan_generation(project.id, [:core])
assert plan.project_id == project.id
assert plan.base_url == "https://example.com/blog"
assert plan.language == "en"
assert plan.blog_languages == ["en", "de"]
assert plan.max_posts_per_page == 25
assert plan.pico_theme == "amber"
assert plan.sections == [:core]
assert plan.generated_files == []
assert {:ok, result} = BDS.Generation.generate_site(project.id, [:core])
assert result.sections == [:core]
expected_paths = [
"404.html",
"index.html",
"sitemap.xml",
"feed.xml",
"atom.xml",
"calendar.json",
"de/404.html",
"de/index.html",
"de/feed.xml",
"de/atom.xml"
]
assert Enum.sort(Enum.map(result.generated_files, & &1.relative_path)) == Enum.sort(expected_paths)
for relative_path <- expected_paths do
assert File.exists?(Path.join([temp_dir, "html", relative_path]))
end
assert File.read!(Path.join([temp_dir, "html", "sitemap.xml"])) =~ "https://example.com/blog/"
end
test "generation renders published list and post templates for core and single pages", %{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",
blog_languages: ["en"]
})
assert {:ok, list_template} =
BDS.Templates.create_template(%{
project_id: project.id,
title: "List View",
kind: :list,
content: "{{ page_title }}
{% for post in posts %}{{ post.title }}{% endfor %}"
})
assert {:ok, _published_list} = BDS.Templates.publish_template(list_template.id)
assert {:ok, post_template} =
BDS.Templates.create_template(%{
project_id: project.id,
title: "Post View",
kind: :post,
content: "{{ post.title }}
{{ post.content }}
"
})
assert {:ok, published_post_template} = BDS.Templates.publish_template(post_template.id)
assert {:ok, post} =
Posts.create_post(%{
project_id: project.id,
title: "Rendered Post",
content: "Rendered body",
language: "en",
template_slug: published_post_template.slug
})
assert {:ok, published_post} = Posts.publish_post(post.id)
assert {:ok, result} = BDS.Generation.generate_site(project.id, [:core, :single])
post_path = BDS.Generation.post_output_path(published_post)
relative_paths = Enum.map(result.generated_files, & &1.relative_path)
assert "index.html" in relative_paths
assert post_path in relative_paths
index_html = File.read!(Path.join([temp_dir, "html", "index.html"]))
assert index_html =~ "list-template"
assert index_html =~ "Rendered Post"
post_html = File.read!(Path.join([temp_dir, "html", post_path]))
assert post_html =~ "post-template"
assert post_html =~ "Rendered body"
end
test "generation renders copied starter templates with partials, i18n, and markdown", %{project: project, temp_dir: temp_dir} do
assert {:ok, _menu} =
BDS.Menu.update_menu(project.id, [
%{kind: :page, label: "Notes", slug: "notes"}
])
assert {:ok, _metadata} =
Metadata.update_project_metadata(project.id, %{
public_url: "https://example.com/blog",
main_language: "en",
blog_languages: ["en", "de"]
})
assert {:ok, post} =
Posts.create_post(%{
project_id: project.id,
title: "Starter Rendered Post",
content: "**Rendered** body",
language: "en",
categories: ["notes"],
tags: ["Elixir"]
})
assert {:ok, published_post} = Posts.publish_post(post.id)
assert {:ok, _tags} = BDS.Tags.sync_tags_from_posts(project.id)
assert {:ok, result} = BDS.Generation.generate_site(project.id, [:core, :single])
post_path = BDS.Generation.post_output_path(published_post)
relative_paths = Enum.map(result.generated_files, & &1.relative_path)
assert "index.html" in relative_paths
assert post_path in relative_paths
index_html = File.read!(Path.join([temp_dir, "html", "index.html"]))
assert index_html =~ ~s(