feat: more stuff on publishing
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
defmodule BDS.GenerationTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias BDS.Metadata
|
||||
alias BDS.Posts
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-generation-#{System.unique_integer([:positive])}")
|
||||
@@ -44,4 +47,83 @@ defmodule BDS.GenerationTest do
|
||||
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 = [
|
||||
"index.html",
|
||||
"sitemap.xml",
|
||||
"feed.xml",
|
||||
"atom.xml",
|
||||
"calendar.json",
|
||||
"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 "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, %{
|
||||
public_url: "https://example.com/blog",
|
||||
main_language: "en",
|
||||
blog_languages: ["en", "de"]
|
||||
})
|
||||
|
||||
assert {:ok, post} =
|
||||
Posts.create_post(%{
|
||||
project_id: project.id,
|
||||
title: "My Post",
|
||||
content: "Hello generated world",
|
||||
language: "en"
|
||||
})
|
||||
|
||||
assert {:ok, _translation} =
|
||||
Posts.upsert_post_translation(post.id, "de", %{
|
||||
title: "Mein Beitrag",
|
||||
content: "Hallo generierte Welt"
|
||||
})
|
||||
|
||||
assert {:ok, published_post} = Posts.publish_post(post.id)
|
||||
|
||||
assert {:ok, result} = BDS.Generation.generate_site(project.id, [:single])
|
||||
|
||||
post_path = BDS.Generation.post_output_path(published_post)
|
||||
translation_path = BDS.Generation.post_output_path(published_post, "de")
|
||||
|
||||
assert Enum.map(result.generated_files, & &1.relative_path) |> Enum.sort() == Enum.sort([post_path, translation_path])
|
||||
|
||||
assert File.read!(Path.join([temp_dir, "html", post_path])) =~ "Hello generated world"
|
||||
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
|
||||
end
|
||||
|
||||
62
test/bds/preview_test.exs
Normal file
62
test/bds/preview_test.exs
Normal file
@@ -0,0 +1,62 @@
|
||||
defmodule BDS.PreviewTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias BDS.Generation
|
||||
alias BDS.Metadata
|
||||
alias BDS.Posts
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-preview-#{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: "Preview", data_path: temp_dir})
|
||||
%{project: project, temp_dir: temp_dir}
|
||||
end
|
||||
|
||||
test "start_preview binds localhost and request resolves generated routes, assets, media, and draft previews", %{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"]
|
||||
})
|
||||
|
||||
assert {:ok, _} = Generation.write_generated_file(project.id, "index.html", "<html>home</html>")
|
||||
assert {:ok, _} = Generation.write_generated_file(project.id, "de/index.html", "<html>startseite</html>")
|
||||
assert {:ok, _} = Generation.write_generated_file(project.id, "tag/elixir/index.html", "<html>tag archive</html>")
|
||||
assert {:ok, _} = Generation.write_generated_file(project.id, "pagefind/pagefind-ui.js", "console.log('pagefind')")
|
||||
|
||||
media_dir = Path.join([temp_dir, "media", "2026", "04"])
|
||||
File.mkdir_p!(media_dir)
|
||||
File.write!(Path.join(media_dir, "image.txt"), "media body")
|
||||
|
||||
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 server.host == "127.0.0.1"
|
||||
assert server.port == 4123
|
||||
assert server.is_running == true
|
||||
|
||||
assert {:ok, %{body: "<html>home</html>", content_type: "text/html"}} = BDS.Preview.request(project.id, "/")
|
||||
assert {:ok, %{body: "<html>startseite</html>", content_type: "text/html"}} = BDS.Preview.request(project.id, "/de/")
|
||||
assert {:ok, %{body: "<html>tag archive</html>", content_type: "text/html"}} = BDS.Preview.request(project.id, "/tag/elixir")
|
||||
assert {:ok, %{body: "console.log('pagefind')", content_type: "application/javascript"}} = BDS.Preview.request(project.id, "/pagefind/pagefind-ui.js")
|
||||
assert {:ok, %{body: "media body", content_type: "text/plain"}} = BDS.Preview.request(project.id, "/media/2026/04/image.txt")
|
||||
|
||||
assert {:ok, %{body: draft_html, content_type: "text/html"}} =
|
||||
BDS.Preview.preview_draft(project.id, "/draft/draft-post", post.id)
|
||||
|
||||
assert draft_html =~ "Draft preview body"
|
||||
assert {:error, :not_found} = BDS.Preview.request(project.id, "/media/../../secret.txt")
|
||||
|
||||
assert :ok = BDS.Preview.stop_preview(project.id)
|
||||
end
|
||||
end
|
||||
90
test/bds/publishing_test.exs
Normal file
90
test/bds/publishing_test.exs
Normal file
@@ -0,0 +1,90 @@
|
||||
defmodule BDS.PublishingTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-publishing-#{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 "upload_site creates a publish job, uploads all targets, and excludes media sidecars", %{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")
|
||||
|
||||
uploader = fn target, files, credentials ->
|
||||
send(test_pid, {:uploaded, target.kind, target.remote_dir, Enum.sort(files), credentials.ssh_mode})
|
||||
:ok
|
||||
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, uploader: uploader)
|
||||
assert job.status in [:pending, :running]
|
||||
|
||||
assert wait_for_publish_job(job.id, &(&1.status == :completed)).status == :completed
|
||||
|
||||
assert_receive {:uploaded, :html, "/srv/blog", ["index.html"], :rsync}
|
||||
assert_receive {:uploaded, :thumbnails, "/srv/blog/thumbnails", ["thumb.jpg"], :rsync}
|
||||
assert_receive {:uploaded, :media, "/srv/blog/media", ["asset.jpg"], :rsync}
|
||||
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 />")
|
||||
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")
|
||||
|
||||
uploader = fn target, _files, _credentials ->
|
||||
if target.kind == :thumbnails, do: {:error, "thumbnail failure"}, else: :ok
|
||||
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, uploader: uploader)
|
||||
|
||||
failed_job = wait_for_publish_job(job.id, &(&1.status == :failed))
|
||||
assert failed_job.error == "thumbnail failure"
|
||||
end
|
||||
|
||||
defp wait_for_publish_job(job_id, predicate, attempts \\ 100)
|
||||
|
||||
defp wait_for_publish_job(job_id, predicate, attempts) when attempts > 0 do
|
||||
job = BDS.Publishing.get_job(job_id)
|
||||
|
||||
if predicate.(job) do
|
||||
job
|
||||
else
|
||||
Process.sleep(20)
|
||||
wait_for_publish_job(job_id, predicate, attempts - 1)
|
||||
end
|
||||
end
|
||||
|
||||
defp wait_for_publish_job(_job_id, _predicate, 0) do
|
||||
flunk("publish job did not reach expected state")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user