feat: more stuff around persistence of data
This commit is contained in:
123
test/bds/maintenance_test.exs
Normal file
123
test/bds/maintenance_test.exs
Normal file
@@ -0,0 +1,123 @@
|
||||
defmodule BDS.MaintenanceTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias BDS.Repo
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-maintenance-#{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: "Maintenance", data_path: temp_dir})
|
||||
%{project: project, temp_dir: temp_dir}
|
||||
end
|
||||
|
||||
test "rebuild_from_filesystem dispatches to posts, media, scripts, and templates rebuilders", %{project: project, temp_dir: temp_dir} do
|
||||
posts_dir = Path.join([temp_dir, "posts", "2026", "04"])
|
||||
File.mkdir_p!(posts_dir)
|
||||
|
||||
File.write!(
|
||||
Path.join(posts_dir, "dispatch-post.md"),
|
||||
[
|
||||
"---",
|
||||
"id: dispatch-post",
|
||||
"title: Dispatch Post",
|
||||
"slug: dispatch-post",
|
||||
"status: published",
|
||||
"created_at: 1711843200",
|
||||
"updated_at: 1711929600",
|
||||
"published_at: 1712016000",
|
||||
"tags:",
|
||||
"categories:",
|
||||
"---",
|
||||
"Body",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
media_dir = Path.join([temp_dir, "media", "2026", "04"])
|
||||
File.mkdir_p!(media_dir)
|
||||
File.write!(Path.join(media_dir, "asset.txt"), "hello media")
|
||||
|
||||
File.write!(
|
||||
Path.join(media_dir, "asset.txt.meta"),
|
||||
[
|
||||
"id: dispatch-media",
|
||||
"original_name: original.txt",
|
||||
"mime_type: text/plain",
|
||||
"size: 11",
|
||||
"created_at: 1711843200",
|
||||
"updated_at: 1711929600",
|
||||
"tags:",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
template_dir = Path.join(temp_dir, "templates")
|
||||
File.mkdir_p!(template_dir)
|
||||
File.write!(
|
||||
Path.join(template_dir, "dispatch-view.liquid"),
|
||||
[
|
||||
"---",
|
||||
"id: dispatch-template",
|
||||
"slug: dispatch-view",
|
||||
"title: Dispatch View",
|
||||
"kind: list",
|
||||
"enabled: true",
|
||||
"version: 1",
|
||||
"created_at: 101",
|
||||
"updated_at: 202",
|
||||
"---",
|
||||
"<section>Template</section>",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
script_dir = Path.join(temp_dir, "scripts")
|
||||
File.mkdir_p!(script_dir)
|
||||
File.write!(
|
||||
Path.join(script_dir, "dispatch.lua"),
|
||||
[
|
||||
"---",
|
||||
"id: dispatch-script",
|
||||
"slug: dispatch",
|
||||
"title: Dispatch Script",
|
||||
"kind: utility",
|
||||
"entrypoint: main",
|
||||
"enabled: true",
|
||||
"version: 1",
|
||||
"created_at: 301",
|
||||
"updated_at: 404",
|
||||
"---",
|
||||
"function main() return true end",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
assert {:ok, posts} = BDS.Maintenance.rebuild_from_filesystem(project.id, "post")
|
||||
assert length(posts) == 1
|
||||
|
||||
assert {:ok, media_items} = BDS.Maintenance.rebuild_from_filesystem(project.id, "media")
|
||||
assert length(media_items) == 1
|
||||
|
||||
assert {:ok, scripts} = BDS.Maintenance.rebuild_from_filesystem(project.id, "script")
|
||||
assert length(scripts) == 1
|
||||
|
||||
assert {:ok, templates} = BDS.Maintenance.rebuild_from_filesystem(project.id, "template")
|
||||
assert length(templates) == 1
|
||||
|
||||
assert Repo.get(BDS.Posts.Post, "dispatch-post") != nil
|
||||
assert Repo.get(BDS.Media.Media, "dispatch-media") != nil
|
||||
assert Repo.get(BDS.Scripts.Script, "dispatch-script") != nil
|
||||
assert Repo.get(BDS.Templates.Template, "dispatch-template") != nil
|
||||
end
|
||||
|
||||
test "rebuild_from_filesystem rejects unsupported entity types", %{project: project} do
|
||||
assert {:error, :unsupported_entity_type} = BDS.Maintenance.rebuild_from_filesystem(project.id, "unknown")
|
||||
end
|
||||
end
|
||||
142
test/bds/media_test.exs
Normal file
142
test/bds/media_test.exs
Normal file
@@ -0,0 +1,142 @@
|
||||
defmodule BDS.MediaTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
alias BDS.Repo
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-media-#{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: "Media", data_path: temp_dir})
|
||||
%{project: project, temp_dir: temp_dir}
|
||||
end
|
||||
|
||||
test "import_media copies the binary, creates a sidecar, and persists the row", %{project: project, temp_dir: temp_dir} do
|
||||
source_path = Path.join(temp_dir, "sample.txt")
|
||||
File.write!(source_path, "hello media")
|
||||
|
||||
assert {:ok, media} =
|
||||
BDS.Media.import_media(%{
|
||||
project_id: project.id,
|
||||
source_path: source_path,
|
||||
title: "Sample",
|
||||
alt: "Alt text",
|
||||
caption: "Caption",
|
||||
author: "Writer",
|
||||
language: "en",
|
||||
tags: ["alpha"]
|
||||
})
|
||||
|
||||
assert media.original_name == "sample.txt"
|
||||
assert media.mime_type == "text/plain"
|
||||
assert media.size == byte_size("hello media")
|
||||
assert media.tags == ["alpha"]
|
||||
assert media.file_path =~ ~r/^media\/\d{4}\/\d{2}\/.+\.txt$/
|
||||
assert media.sidecar_path == media.file_path <> ".meta"
|
||||
|
||||
assert File.read!(Path.join(temp_dir, media.file_path)) == "hello media"
|
||||
|
||||
sidecar = File.read!(Path.join(temp_dir, media.sidecar_path))
|
||||
assert sidecar =~ "id: #{media.id}\n"
|
||||
assert sidecar =~ "original_name: sample.txt\n"
|
||||
assert sidecar =~ "mime_type: text/plain\n"
|
||||
assert sidecar =~ "title: Sample\n"
|
||||
assert sidecar =~ "alt: Alt text\n"
|
||||
assert sidecar =~ "caption: Caption\n"
|
||||
assert sidecar =~ "author: Writer\n"
|
||||
assert sidecar =~ "language: en\n"
|
||||
assert sidecar =~ "tags:\n - alpha\n"
|
||||
end
|
||||
|
||||
test "update_media rewrites the sidecar metadata", %{project: project, temp_dir: temp_dir} do
|
||||
source_path = Path.join(temp_dir, "sample.txt")
|
||||
File.write!(source_path, "hello media")
|
||||
|
||||
assert {:ok, media} = BDS.Media.import_media(%{project_id: project.id, source_path: source_path})
|
||||
|
||||
assert {:ok, updated} =
|
||||
BDS.Media.update_media(media.id, %{
|
||||
title: "Updated",
|
||||
alt: "Updated alt",
|
||||
tags: ["beta"],
|
||||
language: "de"
|
||||
})
|
||||
|
||||
assert updated.title == "Updated"
|
||||
assert updated.alt == "Updated alt"
|
||||
assert updated.tags == ["beta"]
|
||||
assert updated.language == "de"
|
||||
|
||||
sidecar = File.read!(Path.join(temp_dir, updated.sidecar_path))
|
||||
assert sidecar =~ "title: Updated\n"
|
||||
assert sidecar =~ "alt: Updated alt\n"
|
||||
assert sidecar =~ "language: de\n"
|
||||
assert sidecar =~ "tags:\n - beta\n"
|
||||
end
|
||||
|
||||
test "delete_media removes the binary, sidecar, and database row", %{project: project, temp_dir: temp_dir} do
|
||||
source_path = Path.join(temp_dir, "sample.txt")
|
||||
File.write!(source_path, "hello media")
|
||||
|
||||
assert {:ok, media} = BDS.Media.import_media(%{project_id: project.id, source_path: source_path})
|
||||
|
||||
assert {:ok, :deleted} = BDS.Media.delete_media(media.id)
|
||||
assert Repo.get(BDS.Media.Media, media.id) == nil
|
||||
refute File.exists?(Path.join(temp_dir, media.file_path))
|
||||
refute File.exists?(Path.join(temp_dir, media.sidecar_path))
|
||||
end
|
||||
|
||||
test "rebuild_media_from_files recreates media rows from sidecars", %{project: project, temp_dir: temp_dir} do
|
||||
media_dir = Path.join([temp_dir, "media", "2026", "04"])
|
||||
File.mkdir_p!(media_dir)
|
||||
|
||||
binary_path = Path.join(media_dir, "asset.txt")
|
||||
sidecar_path = binary_path <> ".meta"
|
||||
|
||||
File.write!(binary_path, "hello media")
|
||||
|
||||
File.write!(
|
||||
sidecar_path,
|
||||
[
|
||||
"id: media-from-file",
|
||||
"original_name: original.txt",
|
||||
"mime_type: text/plain",
|
||||
"size: 11",
|
||||
"width: 0",
|
||||
"height: 0",
|
||||
"title: Recovered",
|
||||
"alt: Recovered alt",
|
||||
"caption: Recovered caption",
|
||||
"author: Writer",
|
||||
"language: en",
|
||||
"created_at: 1711843200",
|
||||
"updated_at: 1711929600",
|
||||
"tags:",
|
||||
" - alpha",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
assert {:ok, media_items} = BDS.Media.rebuild_media_from_files(project.id)
|
||||
assert length(media_items) == 1
|
||||
|
||||
[media] = media_items
|
||||
assert media.id == "media-from-file"
|
||||
assert media.project_id == project.id
|
||||
assert media.filename == "asset.txt"
|
||||
assert media.original_name == "original.txt"
|
||||
assert media.mime_type == "text/plain"
|
||||
assert media.size == 11
|
||||
assert media.title == "Recovered"
|
||||
assert media.alt == "Recovered alt"
|
||||
assert media.caption == "Recovered caption"
|
||||
assert media.author == "Writer"
|
||||
assert media.language == "en"
|
||||
assert media.tags == ["alpha"]
|
||||
assert media.file_path == "media/2026/04/asset.txt"
|
||||
assert media.sidecar_path == "media/2026/04/asset.txt.meta"
|
||||
end
|
||||
end
|
||||
115
test/bds/metadata_test.exs
Normal file
115
test/bds/metadata_test.exs
Normal file
@@ -0,0 +1,115 @@
|
||||
defmodule BDS.MetadataTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
|
||||
temp_dir = Path.join(System.tmp_dir!(), "bds-metadata-#{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: "Metadata", data_path: temp_dir})
|
||||
%{project: project, temp_dir: temp_dir}
|
||||
end
|
||||
|
||||
test "update_project_metadata writes meta/project.json and load returns the saved values", %{project: project, temp_dir: temp_dir} do
|
||||
assert {:ok, metadata} =
|
||||
BDS.Metadata.update_project_metadata(project.id, %{
|
||||
name: "Renamed Blog",
|
||||
description: "Description",
|
||||
public_url: "https://example.com",
|
||||
main_language: "en",
|
||||
default_author: "Writer",
|
||||
max_posts_per_page: 25,
|
||||
blogmark_category: "links",
|
||||
pico_theme: "blue",
|
||||
semantic_similarity_enabled: true,
|
||||
blog_languages: ["de", "fr"]
|
||||
})
|
||||
|
||||
assert metadata.name == "Renamed Blog"
|
||||
assert metadata.max_posts_per_page == 25
|
||||
assert metadata.blog_languages == ["de", "fr"]
|
||||
|
||||
project_json_path = Path.join([temp_dir, "meta", "project.json"])
|
||||
|
||||
assert %{
|
||||
"name" => "Renamed Blog",
|
||||
"description" => "Description",
|
||||
"public_url" => "https://example.com",
|
||||
"main_language" => "en",
|
||||
"default_author" => "Writer",
|
||||
"max_posts_per_page" => 25,
|
||||
"blogmark_category" => "links",
|
||||
"pico_theme" => "blue",
|
||||
"semantic_similarity_enabled" => true,
|
||||
"blog_languages" => ["de", "fr"]
|
||||
} = Jason.decode!(File.read!(project_json_path))
|
||||
|
||||
assert {:ok, loaded} = BDS.Metadata.get_project_metadata(project.id)
|
||||
assert loaded.name == "Renamed Blog"
|
||||
assert loaded.public_url == "https://example.com"
|
||||
assert loaded.blog_languages == ["de", "fr"]
|
||||
end
|
||||
|
||||
test "category and publishing updates write their meta files and sync_project_metadata_from_filesystem loads them", %{project: project, temp_dir: temp_dir} do
|
||||
assert {:ok, _metadata} = BDS.Metadata.add_category(project.id, "news")
|
||||
|
||||
assert {:ok, _metadata} =
|
||||
BDS.Metadata.update_category_settings(project.id, "news", %{
|
||||
render_in_lists: false,
|
||||
show_title: true,
|
||||
post_template_slug: "article",
|
||||
list_template_slug: "listing"
|
||||
})
|
||||
|
||||
assert {:ok, _metadata} =
|
||||
BDS.Metadata.set_publishing_preferences(project.id, %{
|
||||
ssh_host: "example.com",
|
||||
ssh_user: "deploy",
|
||||
ssh_remote_path: "/srv/site",
|
||||
ssh_mode: "rsync"
|
||||
})
|
||||
|
||||
categories_path = Path.join([temp_dir, "meta", "categories.json"])
|
||||
category_meta_path = Path.join([temp_dir, "meta", "category-meta.json"])
|
||||
publishing_path = Path.join([temp_dir, "meta", "publishing.json"])
|
||||
|
||||
assert %{"categories" => ["article", "aside", "news", "page", "picture"]} =
|
||||
Jason.decode!(File.read!(categories_path))
|
||||
|
||||
assert %{
|
||||
"categories" => %{
|
||||
"news" => %{
|
||||
"render_in_lists" => false,
|
||||
"show_title" => true,
|
||||
"post_template_slug" => "article",
|
||||
"list_template_slug" => "listing"
|
||||
}
|
||||
}
|
||||
} = Jason.decode!(File.read!(category_meta_path))
|
||||
|
||||
assert %{
|
||||
"ssh_host" => "example.com",
|
||||
"ssh_user" => "deploy",
|
||||
"ssh_remote_path" => "/srv/site",
|
||||
"ssh_mode" => "rsync"
|
||||
} = Jason.decode!(File.read!(publishing_path))
|
||||
|
||||
assert {:ok, synced} = BDS.Metadata.sync_project_metadata_from_filesystem(project.id)
|
||||
assert synced.categories == ["article", "aside", "news", "page", "picture"]
|
||||
|
||||
assert synced.category_settings["news"] == %{
|
||||
"render_in_lists" => false,
|
||||
"show_title" => true,
|
||||
"post_template_slug" => "article",
|
||||
"list_template_slug" => "listing"
|
||||
}
|
||||
|
||||
assert synced.publishing_preferences == %{
|
||||
"ssh_host" => "example.com",
|
||||
"ssh_user" => "deploy",
|
||||
"ssh_remote_path" => "/srv/site",
|
||||
"ssh_mode" => "rsync"
|
||||
}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user