feat: metadata diff hopefully implemented now
This commit is contained in:
@@ -864,18 +864,34 @@ defmodule BDS.Desktop.ShellLiveTest do
|
||||
%{label: "Orphans", value: "1"}
|
||||
],
|
||||
payload: %{
|
||||
summary: %{diff_count: 1, orphan_count: 1},
|
||||
summary: %{diff_count: 3, orphan_count: 2},
|
||||
diff_reports: [
|
||||
%{
|
||||
entity_type: "post",
|
||||
entity_id: "post-1",
|
||||
differences: [
|
||||
%{field: "slug", db_value: "hello-db", file_value: "hello-file"}
|
||||
%{field: "slug", db_value: "hello-db", file_value: "hello-file"},
|
||||
%{field: "title", db_value: "Hello DB", file_value: "Hello File"}
|
||||
]
|
||||
},
|
||||
%{
|
||||
entity_type: "post_translation",
|
||||
entity_id: "post-1-de",
|
||||
differences: [
|
||||
%{field: "excerpt", db_value: "Kurz DB", file_value: "Kurz Datei"}
|
||||
]
|
||||
},
|
||||
%{
|
||||
entity_type: "media",
|
||||
entity_id: "media-1",
|
||||
differences: [
|
||||
%{field: "alt", db_value: "Alt DB", file_value: "Alt Datei"}
|
||||
]
|
||||
}
|
||||
],
|
||||
orphan_reports: [
|
||||
%{path: "posts/2026/04/orphan.md", entity_type: "post"}
|
||||
%{path: "posts/2026/04/orphan.md", entity_type: "post"},
|
||||
%{path: "media/2026/04/orphan.txt.meta", entity_type: "media"}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -909,10 +925,173 @@ defmodule BDS.Desktop.ShellLiveTest do
|
||||
|
||||
assert html =~ ~s(data-tab-type="metadata_diff")
|
||||
assert html =~ "Metadaten-Diff"
|
||||
assert html =~ ~s(data-testid="metadata-diff-tab")
|
||||
assert html =~ ~s(data-entity-tab="posts")
|
||||
assert html =~ ~s(data-entity-tab="media")
|
||||
assert html =~ ~s(data-testid="metadata-diff-field-pill")
|
||||
assert html =~ "slug"
|
||||
assert html =~ "title"
|
||||
assert html =~ "slug"
|
||||
assert html =~ "hello-db"
|
||||
assert html =~ "hello-file"
|
||||
assert html =~ "posts/2026/04/orphan.md"
|
||||
|
||||
html =
|
||||
view
|
||||
|> element("[data-testid='metadata-diff-tab'][data-entity-tab='media']")
|
||||
|> render_click()
|
||||
|
||||
assert html =~ "Alt DB"
|
||||
assert html =~ "Alt Datei"
|
||||
refute html =~ "hello-db"
|
||||
refute html =~ "posts/2026/04/orphan.md"
|
||||
assert html =~ "media/2026/04/orphan.txt.meta"
|
||||
|
||||
_html =
|
||||
view
|
||||
|> element("[data-testid='metadata-diff-tab'][data-entity-tab='posts']")
|
||||
|> render_click()
|
||||
|
||||
html =
|
||||
view
|
||||
|> element("[data-testid='metadata-diff-field-pill'][data-field='slug']")
|
||||
|> render_click()
|
||||
|
||||
assert html =~ "hello-db"
|
||||
assert html =~ "hello-file"
|
||||
refute html =~ "Kurz DB"
|
||||
refute html =~ "posts/2026/04/orphan.md"
|
||||
end
|
||||
|
||||
test "metadata diff repair actions queue a repair task and refresh the diff result", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
:ok = BDS.Tasks.clear_finished()
|
||||
|
||||
assert {:ok, post} =
|
||||
Posts.create_post(%{
|
||||
project_id: project.id,
|
||||
title: "Database Post",
|
||||
content: "Body",
|
||||
excerpt: "Summary",
|
||||
language: "en"
|
||||
})
|
||||
|
||||
assert {:ok, published_post} = Posts.publish_post(post.id)
|
||||
|
||||
post_path = Path.join(temp_dir, published_post.file_path)
|
||||
|
||||
File.write!(
|
||||
post_path,
|
||||
[
|
||||
"---",
|
||||
"id: #{published_post.id}",
|
||||
"title: Filesystem Post",
|
||||
"slug: #{published_post.slug}",
|
||||
"excerpt: Summary",
|
||||
"status: published",
|
||||
"language: en",
|
||||
"createdAt: #{published_post.created_at}",
|
||||
"updatedAt: #{published_post.updated_at + 1}",
|
||||
"publishedAt: #{published_post.published_at}",
|
||||
"tags:",
|
||||
"categories:",
|
||||
"---",
|
||||
"Body",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
{:ok, view, _html} = live_isolated(build_conn(), BDS.Desktop.ShellLive)
|
||||
|
||||
assert {:ok, queued} = BDS.Desktop.ShellCommands.execute("metadata_diff")
|
||||
completed_task!(queued.task_id)
|
||||
send(view.pid, :refresh_task_status)
|
||||
|
||||
html = render(view)
|
||||
assert html =~ ~s(data-testid="metadata-diff-repair-button")
|
||||
assert html =~ ~s(data-field="title")
|
||||
|
||||
existing_ids = MapSet.new(Enum.map(BDS.Tasks.list_tasks(), & &1.id))
|
||||
|
||||
html =
|
||||
view
|
||||
|> element("[data-testid='metadata-diff-repair-button'][data-direction='file_to_db'][data-field='title']")
|
||||
|> render_click()
|
||||
|
||||
assert html =~ "Repair Metadata Diff"
|
||||
|
||||
repair_task = new_task!(existing_ids, "Repair Metadata Diff")
|
||||
completed_task!(repair_task.id)
|
||||
send(view.pid, :refresh_task_status)
|
||||
_html = render(view)
|
||||
|
||||
assert Repo.get!(Post, published_post.id).title == "Filesystem Post"
|
||||
|
||||
assert {:ok, diff} = BDS.Maintenance.metadata_diff(project.id)
|
||||
refute Enum.any?(diff.diff_reports, &(&1.entity_id == published_post.id))
|
||||
end
|
||||
|
||||
test "metadata diff orphan import action queues an import task and removes the orphan", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
:ok = BDS.Tasks.clear_finished()
|
||||
|
||||
orphan_relative_path = Path.join(["posts", "2026", "04", "orphan-post.md"])
|
||||
orphan_full_path = Path.join(temp_dir, orphan_relative_path)
|
||||
File.mkdir_p!(Path.dirname(orphan_full_path))
|
||||
|
||||
File.write!(
|
||||
orphan_full_path,
|
||||
[
|
||||
"---",
|
||||
"id: orphan-post",
|
||||
"title: Orphan Post",
|
||||
"slug: orphan-post",
|
||||
"status: published",
|
||||
"createdAt: 1",
|
||||
"updatedAt: 1",
|
||||
"publishedAt: 1",
|
||||
"tags:",
|
||||
"categories:",
|
||||
"---",
|
||||
"Orphan body",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
{:ok, view, _html} = live_isolated(build_conn(), BDS.Desktop.ShellLive)
|
||||
|
||||
assert {:ok, queued} = BDS.Desktop.ShellCommands.execute("metadata_diff")
|
||||
completed_task!(queued.task_id)
|
||||
send(view.pid, :refresh_task_status)
|
||||
|
||||
html = render(view)
|
||||
assert html =~ ~s(data-testid="metadata-diff-import-button")
|
||||
assert html =~ orphan_relative_path
|
||||
|
||||
existing_ids = MapSet.new(Enum.map(BDS.Tasks.list_tasks(), & &1.id))
|
||||
|
||||
html =
|
||||
view
|
||||
|> element("[data-testid='metadata-diff-import-button']")
|
||||
|> render_click()
|
||||
|
||||
assert html =~ "Import Metadata Diff Orphans"
|
||||
|
||||
import_task = new_task!(existing_ids, "Import Metadata Diff Orphans")
|
||||
completed_task!(import_task.id)
|
||||
send(view.pid, :refresh_task_status)
|
||||
_html = render(view)
|
||||
|
||||
assert Repo.get_by(Post, project_id: project.id, file_path: orphan_relative_path)
|
||||
|
||||
assert {:ok, diff} = BDS.Maintenance.metadata_diff(project.id)
|
||||
refute orphan_relative_path in Enum.map(diff.orphan_reports, & &1.file_path)
|
||||
end
|
||||
|
||||
test "post tabs render a real editor and drive save publish discard flows", %{project: project} do
|
||||
@@ -1053,6 +1232,36 @@ defmodule BDS.Desktop.ShellLiveTest do
|
||||
assert discarded_post.title == "Updated Shell Post"
|
||||
end
|
||||
|
||||
defp completed_task!(task_id, attempts \\ 50)
|
||||
|
||||
defp completed_task!(_task_id, 0), do: flunk("task did not complete in time")
|
||||
|
||||
defp completed_task!(task_id, attempts) do
|
||||
case Enum.find(BDS.Tasks.list_tasks(), &(&1.id == task_id and &1.status == :completed)) do
|
||||
nil ->
|
||||
Process.sleep(20)
|
||||
completed_task!(task_id, attempts - 1)
|
||||
|
||||
task ->
|
||||
task
|
||||
end
|
||||
end
|
||||
|
||||
defp new_task!(existing_ids, name, attempts \\ 50)
|
||||
|
||||
defp new_task!(_existing_ids, _name, 0), do: flunk("new task was not created in time")
|
||||
|
||||
defp new_task!(existing_ids, name, attempts) do
|
||||
case Enum.find(BDS.Tasks.list_tasks(), &(&1.name == name and not MapSet.member?(existing_ids, &1.id))) do
|
||||
nil ->
|
||||
Process.sleep(20)
|
||||
new_task!(existing_ids, name, attempts - 1)
|
||||
|
||||
task ->
|
||||
task
|
||||
end
|
||||
end
|
||||
|
||||
test "published post editor loads body from file and renders markdown-only editor", %{project: project} do
|
||||
{:ok, post} =
|
||||
Posts.create_post(%{
|
||||
|
||||
@@ -813,6 +813,404 @@ defmodule BDS.MaintenanceTest do
|
||||
end)
|
||||
end
|
||||
|
||||
test "repair_metadata_diff syncs supported filesystem metadata back into the database", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
fixture = seed_metadata_repair_fixture(project, temp_dir)
|
||||
|
||||
File.write!(
|
||||
Path.join([temp_dir, "meta", "project.json"]),
|
||||
Jason.encode!(%{
|
||||
"name" => "Filesystem Blog",
|
||||
"description" => "Filesystem description",
|
||||
"publicUrl" => "https://filesystem.example",
|
||||
"mainLanguage" => "fr",
|
||||
"defaultAuthor" => "Filesystem Author",
|
||||
"maxPostsPerPage" => 12,
|
||||
"blogmarkCategory" => "notes",
|
||||
"picoTheme" => "slate",
|
||||
"semanticSimilarityEnabled" => true,
|
||||
"blogLanguages" => ["fr", "de"]
|
||||
})
|
||||
)
|
||||
|
||||
File.write!(Path.join([temp_dir, "meta", "categories.json"]), Jason.encode!(["notes", "updates"]))
|
||||
|
||||
File.write!(
|
||||
Path.join([temp_dir, "meta", "category-meta.json"]),
|
||||
Jason.encode!(%{
|
||||
"notes" => %{"title" => "Filesystem Notes", "showTitle" => false, "renderInLists" => true}
|
||||
})
|
||||
)
|
||||
|
||||
File.write!(
|
||||
Path.join([temp_dir, "meta", "publishing.json"]),
|
||||
Jason.encode!(%{
|
||||
"sshHost" => "files.example",
|
||||
"sshUser" => "files-user",
|
||||
"sshRemotePath" => "/srv/files",
|
||||
"sshMode" => "rsync"
|
||||
})
|
||||
)
|
||||
|
||||
write_post_frontmatter(fixture.post_path, %{
|
||||
"id" => fixture.post.id,
|
||||
"title" => "Filesystem Post",
|
||||
"slug" => fixture.post.slug,
|
||||
"excerpt" => "Filesystem summary",
|
||||
"status" => "published",
|
||||
"author" => "Filesystem Writer",
|
||||
"language" => "fr",
|
||||
"doNotTranslate" => false,
|
||||
"templateSlug" => nil,
|
||||
"createdAt" => fixture.post.created_at,
|
||||
"updatedAt" => fixture.post.updated_at + 1,
|
||||
"publishedAt" => fixture.post.published_at,
|
||||
"tags" => ["beta"],
|
||||
"categories" => ["updates"]
|
||||
}, "Filesystem body")
|
||||
|
||||
write_post_frontmatter(fixture.post_translation_path, %{
|
||||
"id" => fixture.post_translation.id,
|
||||
"translationFor" => fixture.post_translation.translation_for,
|
||||
"language" => fixture.post_translation.language,
|
||||
"title" => "Datei Beitrag",
|
||||
"excerpt" => "Datei Zusammenfassung",
|
||||
"status" => "published",
|
||||
"createdAt" => fixture.post_translation.created_at,
|
||||
"updatedAt" => fixture.post_translation.updated_at + 1,
|
||||
"publishedAt" => fixture.post_translation.published_at
|
||||
}, "Datei Inhalt")
|
||||
|
||||
File.write!(
|
||||
fixture.media_sidecar_path,
|
||||
[
|
||||
"id: #{fixture.media.id}",
|
||||
"originalName: #{fixture.media.original_name}",
|
||||
"mimeType: #{fixture.media.mime_type}",
|
||||
"size: #{fixture.media.size}",
|
||||
"title: Filesystem media title",
|
||||
"alt: Filesystem alt",
|
||||
"caption: Filesystem caption",
|
||||
"author: Filesystem Photographer",
|
||||
"language: fr",
|
||||
"createdAt: #{fixture.media.created_at}",
|
||||
"updatedAt: #{fixture.media.updated_at + 1}",
|
||||
"tags:",
|
||||
" - beta",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
File.write!(
|
||||
fixture.media_translation_sidecar_path,
|
||||
[
|
||||
"translationFor: #{fixture.media.id}",
|
||||
"language: #{fixture.media_translation.language}",
|
||||
"title: Datei Medium",
|
||||
"alt: Datei Alt",
|
||||
"caption: Datei Bildtext",
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
|
||||
write_script_frontmatter(fixture.script_path, fixture.script, %{
|
||||
"title" => "Filesystem Script",
|
||||
"entrypoint" => "run",
|
||||
"enabled" => false
|
||||
}, "function run() return false end")
|
||||
|
||||
write_template_frontmatter(fixture.template_path, fixture.template, %{
|
||||
"title" => "Filesystem Template",
|
||||
"enabled" => false
|
||||
}, "<section>Filesystem</section>")
|
||||
|
||||
items = [
|
||||
%{entity_type: "project", entity_id: project.id},
|
||||
%{entity_type: "categories", entity_id: project.id},
|
||||
%{entity_type: "category_meta", entity_id: project.id},
|
||||
%{entity_type: "publishing", entity_id: project.id},
|
||||
%{entity_type: "post", entity_id: fixture.post.id},
|
||||
%{entity_type: "post_translation", entity_id: fixture.post_translation.id},
|
||||
%{entity_type: "media", entity_id: fixture.media.id},
|
||||
%{entity_type: "media_translation", entity_id: fixture.media_translation.id},
|
||||
%{entity_type: "script", entity_id: fixture.script.id},
|
||||
%{entity_type: "template", entity_id: fixture.template.id}
|
||||
]
|
||||
|
||||
assert {:ok, %{repaired: 10, failed: 0}} =
|
||||
BDS.Maintenance.repair_metadata_diff(project.id, "file_to_db", items)
|
||||
|
||||
assert {:ok, metadata} = BDS.Metadata.get_project_metadata(project.id)
|
||||
assert metadata.name == "Filesystem Blog"
|
||||
assert metadata.categories == ["notes", "updates"]
|
||||
assert metadata.category_settings["notes"]["title"] == "Filesystem Notes"
|
||||
assert metadata.publishing_preferences["ssh_mode"] == "rsync"
|
||||
|
||||
repaired_post = Repo.get!(BDS.Posts.Post, fixture.post.id)
|
||||
assert repaired_post.title == "Filesystem Post"
|
||||
assert repaired_post.excerpt == "Filesystem summary"
|
||||
assert repaired_post.author == "Filesystem Writer"
|
||||
assert repaired_post.language == "fr"
|
||||
assert repaired_post.tags == ["beta"]
|
||||
assert repaired_post.categories == ["updates"]
|
||||
|
||||
repaired_translation = Repo.get!(BDS.Posts.Translation, fixture.post_translation.id)
|
||||
assert repaired_translation.title == "Datei Beitrag"
|
||||
assert repaired_translation.excerpt == "Datei Zusammenfassung"
|
||||
|
||||
repaired_media = Repo.get!(BDS.Media.Media, fixture.media.id)
|
||||
assert repaired_media.title == "Filesystem media title"
|
||||
assert repaired_media.alt == "Filesystem alt"
|
||||
assert repaired_media.author == "Filesystem Photographer"
|
||||
assert repaired_media.language == "fr"
|
||||
assert repaired_media.tags == ["beta"]
|
||||
|
||||
repaired_media_translation = Repo.get!(BDS.Media.Translation, fixture.media_translation.id)
|
||||
assert repaired_media_translation.title == "Datei Medium"
|
||||
assert repaired_media_translation.alt == "Datei Alt"
|
||||
|
||||
repaired_script = Repo.get!(BDS.Scripts.Script, fixture.script.id)
|
||||
assert repaired_script.title == "Filesystem Script"
|
||||
assert repaired_script.entrypoint == "run"
|
||||
refute repaired_script.enabled
|
||||
|
||||
repaired_template = Repo.get!(BDS.Templates.Template, fixture.template.id)
|
||||
assert repaired_template.title == "Filesystem Template"
|
||||
refute repaired_template.enabled
|
||||
end
|
||||
|
||||
test "repair_metadata_diff syncs supported database metadata back into files", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
fixture = seed_metadata_repair_fixture(project, temp_dir)
|
||||
|
||||
assert {:ok, _metadata} =
|
||||
BDS.Metadata.update_project_metadata(project.id, %{
|
||||
name: "Database Blog",
|
||||
description: "Database description",
|
||||
public_url: "https://database.example",
|
||||
main_language: "en",
|
||||
default_author: "Database Author",
|
||||
max_posts_per_page: 25,
|
||||
blogmark_category: "article",
|
||||
pico_theme: "blue",
|
||||
semantic_similarity_enabled: false,
|
||||
blog_languages: ["en", "de"]
|
||||
})
|
||||
|
||||
assert {:ok, _metadata} =
|
||||
BDS.Metadata.set_publishing_preferences(project.id, %{
|
||||
ssh_host: "db.example",
|
||||
ssh_user: "db-user",
|
||||
ssh_remote_path: "/srv/db",
|
||||
ssh_mode: "scp"
|
||||
})
|
||||
|
||||
assert {:ok, _metadata} = BDS.Metadata.add_category(project.id, "guides")
|
||||
assert {:ok, _metadata} = BDS.Metadata.update_category_settings(project.id, "notes", %{title: "DB Notes", show_title: true})
|
||||
|
||||
from(post in BDS.Posts.Post, where: post.id == ^fixture.post.id)
|
||||
|> Repo.update_all(set: [
|
||||
title: "Database Post",
|
||||
excerpt: "Database summary",
|
||||
author: "Database Writer",
|
||||
language: "en",
|
||||
tags: ["gamma"],
|
||||
categories: ["guides"],
|
||||
updated_at: fixture.post.updated_at + 2
|
||||
])
|
||||
|
||||
from(translation in BDS.Posts.Translation, where: translation.id == ^fixture.post_translation.id)
|
||||
|> Repo.update_all(set: [title: "DB Beitrag", excerpt: "DB Zusammenfassung", updated_at: fixture.post_translation.updated_at + 2])
|
||||
|
||||
from(media in BDS.Media.Media, where: media.id == ^fixture.media.id)
|
||||
|> Repo.update_all(set: [
|
||||
title: "Database media title",
|
||||
alt: "Database alt",
|
||||
caption: "Database caption",
|
||||
author: "Database Photographer",
|
||||
language: "en",
|
||||
tags: ["gamma"],
|
||||
updated_at: fixture.media.updated_at + 2
|
||||
])
|
||||
|
||||
from(translation in BDS.Media.Translation, where: translation.id == ^fixture.media_translation.id)
|
||||
|> Repo.update_all(set: [title: "DB Medium", alt: "DB Alt", caption: "DB Bildtext"])
|
||||
|
||||
from(script in BDS.Scripts.Script, where: script.id == ^fixture.script.id)
|
||||
|> Repo.update_all(set: [title: "Database Script", entrypoint: "run", enabled: false, updated_at: fixture.script.updated_at + 2])
|
||||
|
||||
from(template in BDS.Templates.Template, where: template.id == ^fixture.template.id)
|
||||
|> Repo.update_all(set: [title: "Database Template", enabled: false, updated_at: fixture.template.updated_at + 2])
|
||||
|
||||
items = [
|
||||
%{entity_type: "project", entity_id: project.id},
|
||||
%{entity_type: "categories", entity_id: project.id},
|
||||
%{entity_type: "category_meta", entity_id: project.id},
|
||||
%{entity_type: "publishing", entity_id: project.id},
|
||||
%{entity_type: "post", entity_id: fixture.post.id},
|
||||
%{entity_type: "post_translation", entity_id: fixture.post_translation.id},
|
||||
%{entity_type: "media", entity_id: fixture.media.id},
|
||||
%{entity_type: "media_translation", entity_id: fixture.media_translation.id},
|
||||
%{entity_type: "script", entity_id: fixture.script.id},
|
||||
%{entity_type: "template", entity_id: fixture.template.id}
|
||||
]
|
||||
|
||||
assert {:ok, %{repaired: 10, failed: 0}} =
|
||||
BDS.Maintenance.repair_metadata_diff(project.id, "db_to_file", items)
|
||||
|
||||
project_json = Jason.decode!(File.read!(Path.join([temp_dir, "meta", "project.json"])))
|
||||
assert project_json["name"] == "Database Blog"
|
||||
assert project_json["mainLanguage"] == "en"
|
||||
|
||||
categories_json = Jason.decode!(File.read!(Path.join([temp_dir, "meta", "categories.json"])))
|
||||
assert "guides" in categories_json
|
||||
|
||||
category_meta_json = Jason.decode!(File.read!(Path.join([temp_dir, "meta", "category-meta.json"])))
|
||||
assert category_meta_json["notes"]["title"] == "DB Notes"
|
||||
|
||||
publishing_json = Jason.decode!(File.read!(Path.join([temp_dir, "meta", "publishing.json"])))
|
||||
assert publishing_json["sshMode"] == "scp"
|
||||
|
||||
assert File.read!(fixture.post_path) =~ "title: Database Post"
|
||||
assert File.read!(fixture.post_path) =~ "excerpt: Database summary"
|
||||
assert File.read!(fixture.post_translation_path) =~ "title: DB Beitrag"
|
||||
assert File.read!(fixture.media_sidecar_path) =~ ~s(title: "Database media title")
|
||||
assert File.read!(fixture.media_translation_sidecar_path) =~ ~s(title: "DB Medium")
|
||||
assert File.read!(fixture.script_path) =~ "title: Database Script"
|
||||
assert File.read!(fixture.script_path) =~ "entrypoint: run"
|
||||
assert File.read!(fixture.template_path) =~ "title: Database Template"
|
||||
assert File.read!(fixture.template_path) =~ "enabled: false"
|
||||
end
|
||||
|
||||
test "import_metadata_diff_orphans imports orphan files for supported entity types", %{
|
||||
project: project,
|
||||
temp_dir: temp_dir
|
||||
} do
|
||||
fixture = seed_metadata_repair_fixture(project, temp_dir)
|
||||
|
||||
post_orphan_path = "posts/2026/04/orphan-post.md"
|
||||
post_translation_orphan_path = "posts/2026/04/orphan-post.es.md"
|
||||
media_orphan_file_path = Path.join([temp_dir, "media", "2026", "04", "orphan.txt"])
|
||||
media_orphan_path = "media/2026/04/orphan.txt.meta"
|
||||
media_translation_orphan_path = "media/2026/04/orphan.txt.es.meta"
|
||||
script_orphan_path = "scripts/orphan.lua"
|
||||
template_orphan_path = "templates/orphan-view.liquid"
|
||||
|
||||
File.write!(Path.join(temp_dir, post_orphan_path), [
|
||||
"---",
|
||||
"id: orphan-post",
|
||||
"title: Orphan Post",
|
||||
"slug: orphan-post",
|
||||
"status: published",
|
||||
"createdAt: 1",
|
||||
"updatedAt: 1",
|
||||
"publishedAt: 1",
|
||||
"tags:",
|
||||
" - orphan",
|
||||
"categories:",
|
||||
" - notes",
|
||||
"---",
|
||||
"Orphan body",
|
||||
""
|
||||
] |> Enum.join("\n"))
|
||||
|
||||
File.write!(Path.join(temp_dir, post_translation_orphan_path), [
|
||||
"---",
|
||||
"id: orphan-post-es",
|
||||
"translationFor: #{fixture.post.id}",
|
||||
"language: es",
|
||||
"title: Verwaister Beitrag",
|
||||
"excerpt: Verwaiste Zusammenfassung",
|
||||
"status: published",
|
||||
"createdAt: 1",
|
||||
"updatedAt: 1",
|
||||
"publishedAt: 1",
|
||||
"---",
|
||||
"Verwaister Inhalt",
|
||||
""
|
||||
] |> Enum.join("\n"))
|
||||
|
||||
File.write!(media_orphan_file_path, "orphan media")
|
||||
|
||||
File.write!(Path.join(temp_dir, media_orphan_path), [
|
||||
"id: orphan-media",
|
||||
"originalName: orphan.txt",
|
||||
"mimeType: text/plain",
|
||||
"size: 12",
|
||||
"title: Orphan Media",
|
||||
"createdAt: 1",
|
||||
"updatedAt: 1",
|
||||
"tags:",
|
||||
" - orphan",
|
||||
""
|
||||
] |> Enum.join("\n"))
|
||||
|
||||
File.write!(Path.join(temp_dir, media_translation_orphan_path), [
|
||||
"translationFor: orphan-media",
|
||||
"language: es",
|
||||
"title: Verwaistes Medium",
|
||||
"alt: Verwaister Alt",
|
||||
"caption: Verwaister Bildtext",
|
||||
""
|
||||
] |> Enum.join("\n"))
|
||||
|
||||
File.write!(Path.join(temp_dir, script_orphan_path), [
|
||||
"---",
|
||||
"id: orphan-script",
|
||||
"projectId: #{project.id}",
|
||||
"slug: orphan-script",
|
||||
"title: Orphan Script",
|
||||
"kind: utility",
|
||||
"entrypoint: main",
|
||||
"enabled: true",
|
||||
"version: 1",
|
||||
"createdAt: 1",
|
||||
"updatedAt: 1",
|
||||
"---",
|
||||
"function main() return true end",
|
||||
""
|
||||
] |> Enum.join("\n"))
|
||||
|
||||
File.write!(Path.join(temp_dir, template_orphan_path), [
|
||||
"---",
|
||||
"id: orphan-template",
|
||||
"projectId: #{project.id}",
|
||||
"slug: orphan-view",
|
||||
"title: Orphan View",
|
||||
"kind: list",
|
||||
"enabled: true",
|
||||
"version: 1",
|
||||
"createdAt: 1",
|
||||
"updatedAt: 1",
|
||||
"---",
|
||||
"<section>Orphan</section>",
|
||||
""
|
||||
] |> Enum.join("\n"))
|
||||
|
||||
assert {:ok, %{imported: 6, failed: 0}} =
|
||||
BDS.Maintenance.import_metadata_diff_orphans(project.id, [
|
||||
%{file_path: post_orphan_path},
|
||||
%{file_path: post_translation_orphan_path},
|
||||
%{file_path: media_orphan_path},
|
||||
%{file_path: media_translation_orphan_path},
|
||||
%{file_path: script_orphan_path},
|
||||
%{file_path: template_orphan_path}
|
||||
])
|
||||
|
||||
assert Repo.get_by(BDS.Posts.Post, project_id: project.id, file_path: post_orphan_path)
|
||||
assert Repo.get_by(BDS.Posts.Translation, project_id: project.id, file_path: post_translation_orphan_path)
|
||||
assert Repo.get_by(BDS.Media.Media, project_id: project.id, sidecar_path: media_orphan_path)
|
||||
assert Repo.get_by(BDS.Media.Translation, project_id: project.id, translation_for: "orphan-media", language: "es")
|
||||
assert Repo.get_by(BDS.Scripts.Script, project_id: project.id, file_path: script_orphan_path)
|
||||
assert Repo.get_by(BDS.Templates.Template, project_id: project.id, file_path: template_orphan_path)
|
||||
end
|
||||
|
||||
defp collect_progress_events(acc \\ []) do
|
||||
receive do
|
||||
{:rebuild_progress, value, message} -> collect_progress_events([{value, message} | acc])
|
||||
@@ -821,6 +1219,196 @@ defmodule BDS.MaintenanceTest do
|
||||
end
|
||||
end
|
||||
|
||||
defp seed_metadata_repair_fixture(project, temp_dir) do
|
||||
source_path = Path.join(temp_dir, "repair-source.txt")
|
||||
File.write!(source_path, "repair media")
|
||||
|
||||
assert {:ok, _metadata} =
|
||||
BDS.Metadata.update_project_metadata(project.id, %{
|
||||
name: "Initial Blog",
|
||||
description: "Initial description",
|
||||
public_url: "https://initial.example",
|
||||
main_language: "en",
|
||||
default_author: "Initial Author",
|
||||
max_posts_per_page: 10,
|
||||
blogmark_category: "article",
|
||||
pico_theme: "amber",
|
||||
semantic_similarity_enabled: false,
|
||||
blog_languages: ["en"]
|
||||
})
|
||||
|
||||
assert {:ok, _metadata} = BDS.Metadata.add_category(project.id, "notes")
|
||||
|
||||
assert {:ok, _metadata} =
|
||||
BDS.Metadata.update_category_settings(project.id, "notes", %{
|
||||
title: "Initial Notes",
|
||||
show_title: true,
|
||||
render_in_lists: true
|
||||
})
|
||||
|
||||
assert {:ok, _metadata} =
|
||||
BDS.Metadata.set_publishing_preferences(project.id, %{
|
||||
ssh_host: "initial.example",
|
||||
ssh_user: "initial-user",
|
||||
ssh_remote_path: "/srv/initial",
|
||||
ssh_mode: "scp"
|
||||
})
|
||||
|
||||
assert {:ok, post} =
|
||||
BDS.Posts.create_post(%{
|
||||
project_id: project.id,
|
||||
title: "Initial Post",
|
||||
content: "Initial body",
|
||||
excerpt: "Initial summary",
|
||||
author: "Initial Writer",
|
||||
language: "en",
|
||||
tags: ["alpha"],
|
||||
categories: ["notes"]
|
||||
})
|
||||
|
||||
assert {:ok, published_post} = BDS.Posts.publish_post(post.id)
|
||||
|
||||
assert {:ok, post_translation} =
|
||||
BDS.Posts.upsert_post_translation(published_post.id, "de", %{
|
||||
title: "Initial Beitrag",
|
||||
excerpt: "Initial Zusammenfassung",
|
||||
content: "Initial Inhalt"
|
||||
})
|
||||
|
||||
assert {:ok, _republished_post} = BDS.Posts.publish_post(published_post.id)
|
||||
published_post_translation = Repo.get!(BDS.Posts.Translation, post_translation.id)
|
||||
|
||||
assert {:ok, media} =
|
||||
BDS.Media.import_media(%{
|
||||
project_id: project.id,
|
||||
source_path: source_path,
|
||||
title: "Initial media title",
|
||||
alt: "Initial alt",
|
||||
caption: "Initial caption",
|
||||
author: "Initial Photographer",
|
||||
language: "en",
|
||||
tags: ["alpha"]
|
||||
})
|
||||
|
||||
assert {:ok, _media_translation} =
|
||||
BDS.Media.upsert_media_translation(media.id, "de", %{
|
||||
title: "Initial Medium",
|
||||
alt: "Initial Alt",
|
||||
caption: "Initial Bildtext"
|
||||
})
|
||||
|
||||
assert {:ok, script} =
|
||||
BDS.Scripts.create_script(%{
|
||||
project_id: project.id,
|
||||
title: "Initial Script",
|
||||
kind: :utility,
|
||||
entrypoint: "main",
|
||||
content: "function main() return true end"
|
||||
})
|
||||
|
||||
assert {:ok, published_script} = BDS.Scripts.publish_script(script.id)
|
||||
|
||||
assert {:ok, template} =
|
||||
BDS.Templates.create_template(%{
|
||||
project_id: project.id,
|
||||
title: "Initial Template",
|
||||
kind: :list,
|
||||
content: "<section>Initial</section>"
|
||||
})
|
||||
|
||||
assert {:ok, published_template} = BDS.Templates.publish_template(template.id)
|
||||
|
||||
%{
|
||||
post: Repo.get!(BDS.Posts.Post, published_post.id),
|
||||
post_path: Path.join(temp_dir, published_post.file_path),
|
||||
post_translation: published_post_translation,
|
||||
post_translation_path: Path.join(temp_dir, published_post_translation.file_path),
|
||||
media: Repo.get!(BDS.Media.Media, media.id),
|
||||
media_sidecar_path: Path.join(temp_dir, media.sidecar_path),
|
||||
media_translation: Repo.get_by!(BDS.Media.Translation, translation_for: media.id, language: "de"),
|
||||
media_translation_sidecar_path: Path.join(temp_dir, "#{media.file_path}.de.meta"),
|
||||
script: Repo.get!(BDS.Scripts.Script, published_script.id),
|
||||
script_path: Path.join(temp_dir, published_script.file_path),
|
||||
template: Repo.get!(BDS.Templates.Template, published_template.id),
|
||||
template_path: Path.join(temp_dir, published_template.file_path)
|
||||
}
|
||||
end
|
||||
|
||||
defp write_post_frontmatter(path, fields, body) do
|
||||
frontmatter =
|
||||
[
|
||||
"---",
|
||||
"id: #{fields["id"]}",
|
||||
if(fields["translationFor"], do: "translationFor: #{fields["translationFor"]}", else: nil),
|
||||
if(fields["title"], do: "title: #{fields["title"]}", else: nil),
|
||||
if(fields["slug"], do: "slug: #{fields["slug"]}", else: nil),
|
||||
if(fields["excerpt"], do: "excerpt: #{fields["excerpt"]}", else: nil),
|
||||
if(fields["status"], do: "status: #{fields["status"]}", else: nil),
|
||||
if(fields["author"], do: "author: #{fields["author"]}", else: nil),
|
||||
if(fields["language"], do: "language: #{fields["language"]}", else: nil),
|
||||
if(Map.has_key?(fields, "doNotTranslate"), do: "doNotTranslate: #{fields["doNotTranslate"]}", else: nil),
|
||||
if(Map.has_key?(fields, "templateSlug"), do: "templateSlug: #{fields["templateSlug"] || ""}", else: nil),
|
||||
"createdAt: #{fields["createdAt"]}",
|
||||
"updatedAt: #{fields["updatedAt"]}",
|
||||
if(fields["publishedAt"], do: "publishedAt: #{fields["publishedAt"]}", else: nil),
|
||||
if(Map.has_key?(fields, "tags"), do: ["tags:" | Enum.map(fields["tags"], &" - #{&1}")], else: nil),
|
||||
if(Map.has_key?(fields, "categories"), do: ["categories:" | Enum.map(fields["categories"], &" - #{&1}")], else: nil),
|
||||
"---",
|
||||
body,
|
||||
""
|
||||
]
|
||||
|> List.flatten()
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|> Enum.join("\n")
|
||||
|
||||
File.write!(path, frontmatter)
|
||||
end
|
||||
|
||||
defp write_script_frontmatter(path, script, overrides, body) do
|
||||
File.write!(
|
||||
path,
|
||||
[
|
||||
"---",
|
||||
"id: #{script.id}",
|
||||
"projectId: #{script.project_id}",
|
||||
"slug: #{script.slug}",
|
||||
"title: #{Map.get(overrides, "title", script.title)}",
|
||||
"kind: #{script.kind}",
|
||||
"entrypoint: #{Map.get(overrides, "entrypoint", script.entrypoint)}",
|
||||
"enabled: #{Map.get(overrides, "enabled", script.enabled)}",
|
||||
"version: #{script.version}",
|
||||
"createdAt: #{script.created_at}",
|
||||
"updatedAt: #{script.updated_at}",
|
||||
"---",
|
||||
body,
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
end
|
||||
|
||||
defp write_template_frontmatter(path, template, overrides, body) do
|
||||
File.write!(
|
||||
path,
|
||||
[
|
||||
"---",
|
||||
"id: #{template.id}",
|
||||
"projectId: #{template.project_id}",
|
||||
"slug: #{template.slug}",
|
||||
"title: #{Map.get(overrides, "title", template.title)}",
|
||||
"kind: #{template.kind}",
|
||||
"enabled: #{Map.get(overrides, "enabled", template.enabled)}",
|
||||
"version: #{template.version}",
|
||||
"createdAt: #{template.created_at}",
|
||||
"updatedAt: #{template.updated_at}",
|
||||
"---",
|
||||
body,
|
||||
""
|
||||
]
|
||||
|> Enum.join("\n")
|
||||
)
|
||||
end
|
||||
|
||||
defp assert_incremental_progress(events) do
|
||||
assert Enum.any?(events, fn {value, _message} -> value > 0.0 and value < 1.0 end)
|
||||
assert Enum.any?(events, fn {value, _message} -> value == 1.0 end)
|
||||
|
||||
Reference in New Issue
Block a user