feat: persisting elements in filesystem

This commit is contained in:
2026-04-23 14:26:16 +02:00
parent 6f57728a6d
commit 5374ffe613
8 changed files with 811 additions and 53 deletions

View File

@@ -1,10 +1,17 @@
defmodule BDS.ScriptsTest do
use ExUnit.Case, async: false
alias BDS.Repo
alias BDS.Scripts.Script
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
{:ok, project} = BDS.Projects.create_project(%{name: "Scripts"})
%{project: project}
temp_dir = Path.join(System.tmp_dir!(), "bds-scripts-#{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: "Scripts", data_path: temp_dir})
%{project: project, temp_dir: temp_dir}
end
test "create_script creates draft scripts with default entrypoints by kind", %{project: project} do
@@ -34,4 +41,78 @@ defmodule BDS.ScriptsTest do
assert macro_script.entrypoint == "render"
assert macro_script.slug == "render-card"
end
test "publish_script writes a lua file with frontmatter and clears draft content", %{project: project, temp_dir: temp_dir} do
assert {:ok, script} =
BDS.Scripts.create_script(%{
project_id: project.id,
title: "Process Feed",
kind: :utility,
content: "function main() return 'ok' end"
})
assert {:ok, published} = BDS.Scripts.publish_script(script.id)
assert published.status == :published
assert published.content == nil
assert published.file_path == "scripts/process-feed.lua"
full_path = Path.join(temp_dir, published.file_path)
assert File.exists?(full_path)
contents = File.read!(full_path)
assert contents =~ "---\nid: #{published.id}\n"
assert contents =~ "slug: process-feed\n"
assert contents =~ "title: Process Feed\n"
assert contents =~ "kind: utility\n"
assert contents =~ "entrypoint: main\n"
assert contents =~ "enabled: true\n"
assert contents =~ "version: 1\n"
assert contents =~ "created_at: #{published.created_at}\n"
assert contents =~ "updated_at: #{published.updated_at}\n"
assert contents =~ "\n---\nfunction main() return 'ok' end\n"
end
test "update_script bumps version and reopens a published script when content changes", %{project: project} do
assert {:ok, script} =
BDS.Scripts.create_script(%{
project_id: project.id,
title: "Render Card",
kind: :macro,
content: "function render() return 'v1' end"
})
assert {:ok, published} = BDS.Scripts.publish_script(script.id)
assert published.status == :published
assert {:ok, updated} =
BDS.Scripts.update_script(script.id, %{
content: "function render() return 'v2' end",
enabled: false
})
assert updated.version == 2
assert updated.status == :draft
assert updated.enabled == false
assert updated.file_path == "scripts/render-card.lua"
assert updated.content == "function render() return 'v2' end"
assert updated.updated_at >= published.updated_at
end
test "delete_script removes the published file and database row", %{project: project, temp_dir: temp_dir} do
assert {:ok, script} =
BDS.Scripts.create_script(%{
project_id: project.id,
title: "Cleanup",
kind: :utility,
content: "function main() return true end"
})
assert {:ok, published} = BDS.Scripts.publish_script(script.id)
assert File.exists?(Path.join(temp_dir, published.file_path))
assert {:ok, :deleted} = BDS.Scripts.delete_script(published.id)
assert Repo.get(Script, published.id) == nil
refute File.exists?(Path.join(temp_dir, published.file_path))
end
end

View File

@@ -1,6 +1,9 @@
defmodule BDS.TagsTest do
use ExUnit.Case, async: false
alias BDS.Posts.Post
alias BDS.Repo
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
temp_dir = Path.join(System.tmp_dir!(), "bds-tags-#{System.unique_integer([:positive])}")
@@ -32,6 +35,86 @@ defmodule BDS.TagsTest do
assert "has already been taken" in errors_on(changeset).name
end
test "update_tag rewrites the tag row and meta/tags.json", %{project: project, temp_dir: temp_dir} do
assert {:ok, tag} = BDS.Tags.create_tag(%{project_id: project.id, name: "Alpha"})
assert {:ok, updated} =
BDS.Tags.update_tag(tag.id, %{
color: "#112233",
post_template_slug: "article"
})
assert updated.name == "Alpha"
assert updated.color == "#112233"
assert updated.post_template_slug == "article"
assert updated.updated_at >= tag.updated_at
tags_path = Path.join([temp_dir, "meta", "tags.json"])
assert %{"tags" => [%{"name" => "Alpha", "color" => "#112233", "post_template_slug" => "article"}]} =
Jason.decode!(File.read!(tags_path))
end
test "rename_tag updates post tag arrays, rewrites published post files, and refreshes tags.json", %{project: project, temp_dir: temp_dir} do
assert {:ok, tag} = BDS.Tags.create_tag(%{project_id: project.id, name: "Alpha"})
assert {:ok, post} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Tagged Post",
content: "Body",
tags: ["Alpha", "Other"]
})
assert {:ok, published_post} = BDS.Posts.publish_post(post.id)
assert {:ok, renamed} = BDS.Tags.rename_tag(tag.id, "Beta")
assert renamed.name == "Beta"
reloaded_post = Repo.get!(Post, published_post.id)
assert reloaded_post.tags == ["Beta", "Other"]
post_path = Path.join(temp_dir, reloaded_post.file_path)
contents = File.read!(post_path)
assert contents =~ "tags:\n - Beta\n - Other\n"
assert contents =~ "\n---\nBody\n"
tags_path = Path.join([temp_dir, "meta", "tags.json"])
assert %{"tags" => [%{"name" => "Beta"}]} = Jason.decode!(File.read!(tags_path))
end
test "merge_tags moves source tags onto the target, deduplicates post tags, deletes sources, and refreshes tags.json", %{project: project, temp_dir: temp_dir} do
assert {:ok, source_a} = BDS.Tags.create_tag(%{project_id: project.id, name: "Alpha"})
assert {:ok, source_b} = BDS.Tags.create_tag(%{project_id: project.id, name: "Beta"})
assert {:ok, target} = BDS.Tags.create_tag(%{project_id: project.id, name: "Gamma"})
assert {:ok, post} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Merge Me",
content: "Body",
tags: ["Alpha", "Beta", "Gamma"]
})
assert {:ok, published_post} = BDS.Posts.publish_post(post.id)
assert {:ok, :merged} = BDS.Tags.merge_tags([source_a.id, source_b.id], target.id)
reloaded_post = Repo.get!(Post, published_post.id)
assert reloaded_post.tags == ["Gamma"]
assert Repo.get(BDS.Tags.Tag, source_a.id) == nil
assert Repo.get(BDS.Tags.Tag, source_b.id) == nil
assert Repo.get(BDS.Tags.Tag, target.id) != nil
post_path = Path.join(temp_dir, reloaded_post.file_path)
contents = File.read!(post_path)
assert contents =~ "tags:\n - Gamma\n"
assert contents =~ "\n---\nBody\n"
tags_path = Path.join([temp_dir, "meta", "tags.json"])
assert %{"tags" => [%{"name" => "Gamma"}]} = Jason.decode!(File.read!(tags_path))
end
defp errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->

View File

@@ -1,10 +1,19 @@
defmodule BDS.TemplatesTest do
use ExUnit.Case, async: false
import Ecto.Query
alias BDS.Posts.Post
alias BDS.Repo
alias BDS.Tags.Tag
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
{:ok, project} = BDS.Projects.create_project(%{name: "Templates"})
%{project: project}
temp_dir = Path.join(System.tmp_dir!(), "bds-templates-#{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: "Templates", data_path: temp_dir})
%{project: project, temp_dir: temp_dir}
end
test "create_template creates a draft template with slug deduplication", %{project: project} do
@@ -28,4 +37,109 @@ defmodule BDS.TemplatesTest do
assert duplicate.slug == "article-view-2"
end
test "publish_template writes a liquid file with frontmatter and clears draft content", %{project: project, temp_dir: temp_dir} do
assert {:ok, template} =
BDS.Templates.create_template(%{
project_id: project.id,
title: "Landing Page",
kind: :list,
content: "<section>{{ page_title }}</section>"
})
assert {:ok, published} = BDS.Templates.publish_template(template.id)
assert published.status == :published
assert published.content == nil
assert published.file_path == "templates/landing-page.liquid"
full_path = Path.join(temp_dir, published.file_path)
assert File.exists?(full_path)
contents = File.read!(full_path)
assert contents =~ "---\nid: #{published.id}\n"
assert contents =~ "slug: landing-page\n"
assert contents =~ "title: Landing Page\n"
assert contents =~ "kind: list\n"
assert contents =~ "enabled: true\n"
assert contents =~ "version: 1\n"
assert contents =~ "created_at: #{published.created_at}\n"
assert contents =~ "updated_at: #{published.updated_at}\n"
assert contents =~ "\n---\n<section>{{ page_title }}</section>\n"
end
test "update_template bumps version and reopens a published template when content changes", %{project: project} do
assert {:ok, template} =
BDS.Templates.create_template(%{
project_id: project.id,
title: "Snippet",
kind: :partial,
content: "<span>v1</span>"
})
assert {:ok, published} = BDS.Templates.publish_template(template.id)
assert published.status == :published
assert {:ok, updated} =
BDS.Templates.update_template(template.id, %{
content: "<span>v2</span>",
enabled: false
})
assert updated.version == 2
assert updated.status == :draft
assert updated.enabled == false
assert updated.file_path == "templates/snippet.liquid"
assert updated.content == "<span>v2</span>"
assert updated.updated_at >= published.updated_at
end
test "delete_template refuses referenced templates unless forced, then clears references and deletes the file", %{project: project, temp_dir: temp_dir} do
assert {:ok, template} =
BDS.Templates.create_template(%{
project_id: project.id,
title: "Article View",
kind: :post,
content: "<article>{{ content }}</article>"
})
assert {:ok, published} = BDS.Templates.publish_template(template.id)
assert {:ok, post} =
BDS.Posts.create_post(%{
project_id: project.id,
title: "Uses Template",
content: "Body",
template_slug: published.slug
})
assert {:ok, _published_post} = BDS.Posts.publish_post(post.id)
assert {:ok, _tag} =
BDS.Tags.create_tag(%{
project_id: project.id,
name: "Feature",
post_template_slug: published.slug
})
assert {:error, {:has_references, %{posts: 1, tags: 1}}} = BDS.Templates.delete_template(published.id)
assert {:ok, :deleted} = BDS.Templates.delete_template(published.id, force: true)
reloaded_post = Repo.get(Post, hd(Repo.all(from p in Post, select: p.id)))
reloaded_tag = Repo.get(Tag, hd(Repo.all(from t in Tag, select: t.id)))
assert reloaded_post.template_slug == nil
assert reloaded_tag.post_template_slug == nil
refute File.exists?(Path.join(temp_dir, published.file_path))
post_path = Path.join(temp_dir, reloaded_post.file_path)
post_contents = File.read!(post_path)
refute post_contents =~ "template_slug:"
assert post_contents =~ "\n---\nBody\n"
tags_path = Path.join([temp_dir, "meta", "tags.json"])
assert %{"tags" => [%{"name" => "Feature"}]} = Jason.decode!(File.read!(tags_path))
end
end