feat: added liquid templates

This commit is contained in:
2026-04-23 21:37:45 +02:00
parent b48bed8823
commit 4e46e1b393
42 changed files with 2470 additions and 53 deletions

View File

@@ -1,8 +1,11 @@
defmodule BDS.GenerationTest do
use ExUnit.Case, async: false
import Ecto.Query
alias BDS.Metadata
alias BDS.Posts
alias BDS.Repo
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
@@ -91,6 +94,108 @@ defmodule BDS.GenerationTest do
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: "<main class=\"list-template\"><h1>{{ page_title }}</h1>{% for post in posts %}<a href=\"{{ post.href }}\">{{ post.title }}</a>{% endfor %}</main>"
})
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: "<article class=\"post-template\"><h1>{{ post.title }}</h1><div class=\"body\">{{ post.content }}</div></article>"
})
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(<nav class="blog-menu">)
assert index_html =~ ~s(/assets/pico.min.css)
assert index_html =~ "Starter Rendered Post"
post_html = File.read!(Path.join([temp_dir, "html", post_path]))
assert post_html =~ ~s(data-template="single-post")
assert post_html =~ ~s(<strong>Rendered</strong> body)
assert post_html =~ "Taxonomy"
assert post_html =~ "Language"
end
test "single generation writes canonical post pages and language-prefixed translation pages", %{project: project, temp_dir: temp_dir} do
assert {:ok, _metadata} =
Metadata.update_project_metadata(project.id, %{
@@ -126,4 +231,53 @@ defmodule BDS.GenerationTest do
assert File.read!(Path.join([temp_dir, "html", translation_path])) =~ "Hallo generierte Welt"
assert File.read!(Path.join([temp_dir, "html", post_path])) =~ ~s(data-pagefind-body)
end
test "archive generation writes paginated category, tag, and date 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", "de"],
max_posts_per_page: 2
})
for index <- 1..3 do
assert {:ok, post} =
Posts.create_post(%{
project_id: project.id,
title: "Archive #{index}",
content: "Archive body #{index}",
language: "en",
categories: ["notes"],
tags: ["Elixir"]
})
created_at = DateTime.to_unix(~U[2026-04-15 12:00:00Z]) + index
Repo.update_all(from(p in BDS.Posts.Post, where: p.id == ^post.id), set: [created_at: created_at, updated_at: created_at])
assert {:ok, _published} = Posts.publish_post(post.id)
end
assert {:ok, _tags} = BDS.Tags.sync_tags_from_posts(project.id)
assert {:ok, result} = BDS.Generation.generate_site(project.id, [:category, :tag, :date])
expected_paths = [
"category/notes/index.html",
"category/notes/page/2/index.html",
"tag/elixir/index.html",
"2026/index.html",
"2026/04/index.html",
"de/category/notes/index.html",
"de/tag/elixir/index.html",
"de/2026/index.html",
"de/2026/04/index.html"
]
assert expected_paths -- Enum.map(result.generated_files, & &1.relative_path) == []
assert File.read!(Path.join([temp_dir, "html", "category", "notes", "index.html"])) =~ "Archive 1"
assert File.read!(Path.join([temp_dir, "html", "category", "notes", "page", "2", "index.html"])) =~ "Archive 3"
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"
end
end

View File

@@ -109,7 +109,7 @@ defmodule BDS.MaintenanceTest do
assert length(scripts) == 1
assert {:ok, templates} = BDS.Maintenance.rebuild_from_filesystem(project.id, "template")
assert length(templates) == 1
assert length(templates) == 4
assert Repo.get(BDS.Posts.Post, "dispatch-post") != nil
assert Repo.get(BDS.Media.Media, "dispatch-media") != nil

View File

@@ -3,8 +3,14 @@ defmodule BDS.PostsTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
{:ok, project} = BDS.Projects.create_project(%{name: "Publishing"})
%{project: project}
temp_dir = Path.join(System.tmp_dir!(), "bds-posts-#{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: "Publishing", data_path: temp_dir})
%{project: project, temp_dir: temp_dir}
end
test "create_post slugifies titles, stores list fields, and defaults draft fields", %{project: project} do
@@ -42,7 +48,7 @@ defmodule BDS.PostsTest do
assert duplicate_slug_post.categories == []
end
test "create_post falls back to untitled and keeps slug uniqueness scoped to a project", %{project: project} do
test "create_post falls back to untitled and keeps slug uniqueness scoped to a project", %{project: project, temp_dir: temp_dir} do
assert {:ok, first} = BDS.Posts.create_post(%{project_id: project.id, title: nil})
assert first.title == ""
assert first.slug == "untitled"
@@ -50,7 +56,10 @@ defmodule BDS.PostsTest do
assert {:ok, second} = BDS.Posts.create_post(%{project_id: project.id, title: nil})
assert second.slug == "untitled-2"
assert {:ok, other_project} = BDS.Projects.create_project(%{name: "Elsewhere"})
other_temp_dir = Path.join(temp_dir, "elsewhere")
File.mkdir_p!(other_temp_dir)
assert {:ok, other_project} = BDS.Projects.create_project(%{name: "Elsewhere", data_path: other_temp_dir})
assert {:ok, other_post} = BDS.Posts.create_post(%{project_id: other_project.id, title: nil})
assert other_post.slug == "untitled"
end

View File

@@ -59,4 +59,105 @@ defmodule BDS.PreviewTest do
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(%{
project_id: project.id,
title: "Preview Post",
kind: :post,
content: "<article class=\"preview-template\"><h1>{{ post.title }}</h1><div>{{ post.content }}</div></article>"
})
assert {:ok, published_template} = BDS.Templates.publish_template(template.id)
assert {:ok, post} =
Posts.create_post(%{
project_id: project.id,
title: "Draft Post",
content: "Draft preview body",
language: "en",
template_slug: published_template.slug
})
assert {:ok, _server} = BDS.Preview.start_preview(project.id)
assert {:ok, %{body: draft_html, content_type: "text/html"}} =
BDS.Preview.preview_draft(project.id, "/draft/draft-post", post.id)
assert draft_html =~ "preview-template"
assert draft_html =~ "Draft Post"
assert draft_html =~ "Draft preview body"
assert :ok = BDS.Preview.stop_preview(project.id)
end
test "draft preview renders through copied starter templates with markdown and i18n", %{project: project} 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: "Draft Post",
content: "**Draft** preview body",
language: "en"
})
assert {:ok, _server} = BDS.Preview.start_preview(project.id)
assert {:ok, %{body: draft_html, content_type: "text/html"}} =
BDS.Preview.preview_draft(project.id, "/draft/draft-post", post.id)
assert draft_html =~ ~s(data-template="single-post")
assert draft_html =~ ~s(<strong>Draft</strong> preview body)
assert draft_html =~ "Language"
assert :ok = BDS.Preview.stop_preview(project.id)
end
test "start_preview serves generated and draft routes over real HTTP on localhost", %{project: project} do
:inets.start()
assert {:ok, _metadata} =
Metadata.update_project_metadata(project.id, %{
public_url: "https://example.com/blog",
main_language: "en",
blog_languages: ["en"]
})
assert {:ok, _} = Generation.write_generated_file(project.id, "index.html", "<html>http home</html>")
assert {:ok, post} =
Posts.create_post(%{
project_id: project.id,
title: "HTTP Draft",
content: "Draft over HTTP",
language: "en"
})
assert {:ok, server} = BDS.Preview.start_preview(project.id)
assert {:ok, {{_version, 200, _reason}, headers, body}} =
:httpc.request(:get, {to_charlist("http://#{server.host}:#{server.port}/"), []}, [], body_format: :binary)
assert body == "<html>http home</html>"
assert Enum.any?(headers, fn {name, value} -> String.downcase(to_string(name)) == "content-type" and to_string(value) =~ "text/html" end)
assert {:ok, {{_version, 200, _reason}, _headers, draft_body}} =
:httpc.request(:get, {to_charlist("http://#{server.host}:#{server.port}/draft/http-draft?post_id=#{post.id}"), []}, [], body_format: :binary)
assert draft_body =~ "Draft over HTTP"
assert :ok = BDS.Preview.stop_preview(project.id)
end
end

View File

@@ -1,31 +1,72 @@
defmodule BDS.ProjectsTest do
use ExUnit.Case, async: false
import Ecto.Query
alias BDS.Projects.Project
alias BDS.Repo
alias BDS.Templates.Template
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
temp_root = Path.join(System.tmp_dir!(), "bds-projects-#{System.unique_integer([:positive])}")
File.mkdir_p!(temp_root)
on_exit(fn -> File.rm_rf(temp_root) end)
%{temp_root: temp_root}
end
test "create_project slugifies names, keeps new projects inactive, and deduplicates slugs" do
assert {:ok, first} = BDS.Projects.create_project(%{name: "Föö Bär Blog", data_path: "/tmp/blog"})
test "create_project slugifies names, keeps new projects inactive, and deduplicates slugs", %{temp_root: temp_root} do
first_dir = Path.join(temp_root, "first")
second_dir = Path.join(temp_root, "second")
File.mkdir_p!(first_dir)
File.mkdir_p!(second_dir)
assert {:ok, first} = BDS.Projects.create_project(%{name: "Föö Bär Blog", data_path: first_dir})
assert first.name == "Föö Bär Blog"
assert first.slug == "foo-bar-blog"
assert first.data_path == "/tmp/blog"
assert first.data_path == first_dir
assert first.is_active == false
assert is_integer(first.created_at)
assert is_integer(first.updated_at)
assert {:ok, second} = BDS.Projects.create_project(%{name: "Föö Bär Blog"})
assert {:ok, second} = BDS.Projects.create_project(%{name: "Föö Bär Blog", data_path: second_dir})
assert second.slug == "foo-bar-blog-2"
assert second.is_active == false
end
test "set_active_project clears the previous active project and activates the target" do
assert {:ok, first} = BDS.Projects.create_project(%{name: "First"})
assert {:ok, second} = BDS.Projects.create_project(%{name: "Second"})
test "create_project installs starter templates into the project data directory", %{temp_root: temp_root} do
temp_dir = Path.join(temp_root, "starter")
File.mkdir_p!(temp_dir)
assert {:ok, project} = BDS.Projects.create_project(%{name: "Starter Blog", data_path: temp_dir})
assert File.exists?(Path.join([temp_dir, "templates", "single-post.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "post-list.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "not-found.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "partials", "head.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "partials", "menu-items.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "macros", "gallery.liquid"]))
starter_slugs =
Repo.all(from template in Template, where: template.project_id == ^project.id, select: {template.slug, template.kind})
assert {"single-post", :post} in starter_slugs
assert {"post-list", :list} in starter_slugs
assert {"not-found", :not_found} in starter_slugs
end
test "set_active_project clears the previous active project and activates the target", %{temp_root: temp_root} do
first_dir = Path.join(temp_root, "active-first")
second_dir = Path.join(temp_root, "active-second")
File.mkdir_p!(first_dir)
File.mkdir_p!(second_dir)
assert {:ok, first} = BDS.Projects.create_project(%{name: "First", data_path: first_dir})
assert {:ok, second} = BDS.Projects.create_project(%{name: "Second", data_path: second_dir})
assert {:ok, active_first} = BDS.Projects.set_active_project(first.id)
assert active_first.is_active == true

View File

@@ -46,6 +46,96 @@ defmodule BDS.PublishingTest do
assert_receive {:uploaded, :media, "/srv/blog/media", ["asset.jpg"], :rsync}
end
test "upload_site runs rsync commands with SSH agent env and media exclude filters", %{project: project, temp_dir: temp_dir} do
test_pid = self()
File.mkdir_p!(Path.join([temp_dir, "html"]))
File.write!(Path.join([temp_dir, "html", "index.html"]), "<html />")
File.mkdir_p!(Path.join([temp_dir, "thumbnails"]))
File.write!(Path.join([temp_dir, "thumbnails", "thumb.jpg"]), "thumb")
File.mkdir_p!(Path.join([temp_dir, "media"]))
File.write!(Path.join([temp_dir, "media", "asset.jpg"]), "asset")
File.write!(Path.join([temp_dir, "media", "asset.jpg.meta"]), "meta")
runner = fn command, args, opts ->
send(test_pid, {:command_run, command, args, opts})
{"", 0}
end
credentials = %{
ssh_host: "example.com",
ssh_user: "deploy",
ssh_remote_path: "/srv/blog",
ssh_mode: :rsync
}
assert {:ok, job} =
BDS.Publishing.upload_site(project.id, credentials,
command_runner: runner,
ssh_auth_sock: "/tmp/test-agent.sock"
)
assert wait_for_publish_job(job.id, &(&1.status == :completed)).status == :completed
assert_receive {:command_run, "rsync", html_args, html_opts}
assert html_args == ["--update", "--compress", "--verbose", "-e", "ssh", Path.join([temp_dir, "html"]) <> "/", "deploy@example.com:/srv/blog/"]
assert html_opts[:env] == [{"SSH_AUTH_SOCK", "/tmp/test-agent.sock"}]
assert_receive {:command_run, "rsync", thumb_args, _thumb_opts}
assert thumb_args == ["--update", "--compress", "--verbose", "-e", "ssh", Path.join([temp_dir, "thumbnails"]) <> "/", "deploy@example.com:/srv/blog/thumbnails/"]
assert_receive {:command_run, "rsync", media_args, _media_opts}
assert media_args == ["--update", "--compress", "--verbose", "--exclude=*.meta", "-e", "ssh", Path.join([temp_dir, "media"]) <> "/", "deploy@example.com:/srv/blog/media/"]
end
test "upload_site runs scp commands for each eligible file and fails when a command exits non-zero", %{project: project, temp_dir: temp_dir} do
test_pid = self()
html_index = Path.join([temp_dir, "html", "index.html"])
html_entry = Path.join([temp_dir, "html", "posts", "entry.html"])
thumb_path = Path.join([temp_dir, "thumbnails", "thumb.jpg"])
File.mkdir_p!(Path.join([temp_dir, "html", "posts"]))
File.write!(html_index, "<html />")
File.write!(html_entry, "<html />")
File.mkdir_p!(Path.join([temp_dir, "thumbnails"]))
File.write!(thumb_path, "thumb")
File.mkdir_p!(Path.join([temp_dir, "media"]))
File.write!(Path.join([temp_dir, "media", "asset.jpg"]), "asset")
File.write!(Path.join([temp_dir, "media", "asset.jpg.meta"]), "meta")
runner = fn command, args, opts ->
send(test_pid, {:command_run, command, args, opts})
if List.last(args) == "deploy@example.com:/srv/blog/thumbnails/thumb.jpg" do
{"thumbnail failure", 1}
else
{"", 0}
end
end
credentials = %{
ssh_host: "example.com",
ssh_user: "deploy",
ssh_remote_path: "/srv/blog",
ssh_mode: :scp
}
assert {:ok, job} =
BDS.Publishing.upload_site(project.id, credentials,
command_runner: runner,
ssh_auth_sock: "/tmp/test-agent.sock"
)
failed_job = wait_for_publish_job(job.id, &(&1.status == :failed))
assert failed_job.error =~ "thumbnail failure"
assert_receive {:command_run, "scp", ["-q", ^html_index, "deploy@example.com:/srv/blog/index.html"], opts_a}
assert opts_a[:env] == [{"SSH_AUTH_SOCK", "/tmp/test-agent.sock"}]
assert_receive {:command_run, "scp", ["-q", ^html_entry, "deploy@example.com:/srv/blog/posts/entry.html"], _opts_b}
assert_receive {:command_run, "scp", ["-q", ^thumb_path, "deploy@example.com:/srv/blog/thumbnails/thumb.jpg"], _opts_c}
refute_receive {:command_run, "scp", ["-q", _, "deploy@example.com:/srv/blog/media/asset.jpg"], _opts_d}
end
test "upload_site marks the publish job failed when a target upload fails", %{project: project, temp_dir: temp_dir} do
File.mkdir_p!(Path.join([temp_dir, "html"]))
File.write!(Path.join([temp_dir, "html", "index.html"]), "<html />")

View File

@@ -228,9 +228,9 @@ defmodule BDS.TemplatesTest do
)
assert {:ok, templates} = BDS.Templates.rebuild_templates_from_files(project.id)
assert length(templates) == 1
assert length(templates) == 4
[template] = Repo.all(BDS.Templates.Template)
template = Repo.get!(BDS.Templates.Template, "template-from-file")
assert template.id == "template-from-file"
assert template.slug == "recovered-view"
assert template.title == "Recovered View"