Stabilize preview and sandbox cleanup

This commit is contained in:
2026-06-12 14:40:35 +02:00
parent caaec98225
commit a00e4b85ac
22 changed files with 195 additions and 34 deletions

View File

@@ -20,6 +20,8 @@ defmodule BDS.AI.ChatSandboxCleanupTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
:ok
end

View File

@@ -7,6 +7,7 @@ defmodule BDS.BlogmarkTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
temp_dir =
Path.join(System.tmp_dir!(), "bds-blogmark-#{System.unique_integer([:positive])}")

View File

@@ -9,6 +9,7 @@ defmodule BDS.CSM007ReloadShellTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
temp_dir =
Path.join(System.tmp_dir!(), "bds-csm007-#{System.unique_integer([:positive])}")

View File

@@ -9,6 +9,7 @@ defmodule BDS.CSM008RenderPathTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
temp_dir =
Path.join(System.tmp_dir!(), "bds-csm008-#{System.unique_integer([:positive])}")

View File

@@ -9,6 +9,7 @@ defmodule BDS.CSM011UrlStateTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
prev = System.get_env("BDS_DESKTOP_AUTOMATION")
System.put_env("BDS_DESKTOP_AUTOMATION", "1")

View File

@@ -9,6 +9,7 @@ defmodule BDS.CSM012FilePickerAsyncTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
prev = System.get_env("BDS_DESKTOP_AUTOMATION")
System.put_env("BDS_DESKTOP_AUTOMATION", "1")

View File

@@ -12,6 +12,7 @@ defmodule BDS.Desktop.ImportShellLiveTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
Enum.each(BDS.Tasks.list_running_tasks(), fn task ->
BDS.Tasks.cancel_task(task.id)

View File

@@ -11,6 +11,7 @@ defmodule BDS.Desktop.MiscEditorTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
temp_dir =
Path.join(System.tmp_dir!(), "bds-misc-editor-test-#{System.unique_integer([:positive])}")

View File

@@ -87,8 +87,19 @@ defmodule BDS.Desktop.ShellCommandsTest do
end
setup do
if :ets.whereis(BDS.Preview.ServerTable) != :undefined do
case :ets.lookup(BDS.Preview.ServerTable, :current) do
[{:current, %{project_id: project_id}}] ->
_ = BDS.Preview.stop_preview(project_id)
_other ->
:ok
end
end
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
:ok = Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), Process.whereis(BDS.Preview))
:ok = Ecto.Adapters.SQL.Sandbox.allow(BDS.Repo, self(), Process.whereis(BDS.Publishing))
:ok = BDS.Tasks.clear_finished()
@@ -117,7 +128,7 @@ defmodule BDS.Desktop.ShellCommandsTest do
assert result.kind == "open_url"
assert result.action == "open_in_browser"
assert result.url == "http://127.0.0.1:4123/"
assert result.url == BDS.Preview.base_url() <> "/"
assert result.project_id == project.id
end

View File

@@ -640,8 +640,29 @@ defmodule BDS.Desktop.ShellLiveTest do
end
setup do
if :ets.whereis(BDS.Preview.ServerTable) != :undefined do
case :ets.lookup(BDS.Preview.ServerTable, :current) do
[{:current, %{project_id: project_id}}] ->
_ = BDS.Preview.stop_preview(project_id)
_other ->
:ok
end
end
if :ets.whereis(BDS.Preview.ServerTable) != :undefined do
case :ets.lookup(BDS.Preview.ServerTable, :current) do
[{:current, %{project_id: project_id}}] ->
_ = BDS.Preview.stop_preview(project_id)
_other ->
:ok
end
end
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
Enum.each(BDS.Tasks.list_running_tasks(), fn task ->
BDS.Tasks.cancel_task(task.id)
@@ -661,7 +682,8 @@ defmodule BDS.Desktop.ShellLiveTest do
DynamicSupervisor.terminate_child(BDS.Tasks.TaskSupervisor, pid)
end
Process.sleep(100)
wait_for_supervisor_clear(BDS.TCP.TaskSupervisor)
wait_for_supervisor_clear(BDS.Tasks.TaskSupervisor)
temp_dir =
Path.join(System.tmp_dir!(), "bds-shell-live-#{System.unique_integer([:positive])}")
@@ -700,6 +722,21 @@ defmodule BDS.Desktop.ShellLiveTest do
%{project: project, temp_dir: temp_dir}
end
defp wait_for_supervisor_clear(supervisor, attempts \\ 50)
defp wait_for_supervisor_clear(_supervisor, 0), do: :ok
defp wait_for_supervisor_clear(supervisor, attempts) do
case DynamicSupervisor.which_children(supervisor) do
[] ->
:ok
_children ->
Process.sleep(10)
wait_for_supervisor_clear(supervisor, attempts - 1)
end
end
test "sidebar headers expose old-app create actions for posts, media, scripts, templates, chat, and imports" do
{:ok, view, html} = live_isolated(build_conn(), BDS.Desktop.ShellLive)

View File

@@ -72,7 +72,7 @@ defmodule BDS.Desktop.ShellLive.SettingsEditor.StyleEditorTest do
result = StyleEditor.build_style(assigns)
assert result.preview_url =~ "theme=default"
assert result.preview_url =~ "mode=auto"
assert result.preview_url =~ "127.0.0.1:4123/__style-preview"
assert result.preview_url =~ BDS.Preview.base_url() <> "/__style-preview"
end
test "preview_url reflects selected_theme", %{project: project} do

View File

@@ -12,6 +12,7 @@ defmodule BDS.Desktop.TagEditorLiveTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
Enum.each(BDS.Tasks.list_running_tasks(), fn task ->
BDS.Tasks.cancel_task(task.id)

View File

@@ -15,6 +15,7 @@ defmodule BDS.Desktop.TemplateEditorLiveTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
Enum.each(BDS.Tasks.list_running_tasks(), fn task ->
BDS.Tasks.cancel_task(task.id)

View File

@@ -53,6 +53,7 @@ defmodule BDS.PostTranslationsTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
:ok = BDS.Tasks.clear_finished()
temp_dir =

View File

@@ -21,15 +21,46 @@ defmodule BDS.PreviewTest do
alias BDS.Posts
setup do
reset_preview_process()
for {_, pid, _, _} <- DynamicSupervisor.which_children(BDS.TCP.TaskSupervisor) do
DynamicSupervisor.terminate_child(BDS.TCP.TaskSupervisor, pid)
end
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
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})
on_exit(fn ->
_ = BDS.Preview.stop_preview(project.id)
end)
%{project: project, temp_dir: temp_dir}
end
defp reset_preview_process do
case :ets.whereis(BDS.Preview.ServerTable) do
:undefined ->
:ok
_table ->
case :ets.lookup(BDS.Preview.ServerTable, :current) do
[{:current, %{project_id: project_id}}] ->
_ = BDS.Preview.stop_preview(project_id)
:ok
_other ->
:ok
end
end
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} =
@@ -77,8 +108,9 @@ defmodule BDS.PreviewTest do
assert {:ok, server} = BDS.Preview.start_preview(project.id)
assert server.host == "127.0.0.1"
assert server.port == 4123
assert server.port > 0
assert server.is_running == true
assert BDS.Preview.base_url() == "http://127.0.0.1:#{server.port}"
assert {:ok, %{body: home_html, content_type: "text/html"}} =
BDS.Preview.request(project.id, "/")

View File

@@ -8,6 +8,7 @@ defmodule BDS.RealBlogRebuildDiagnosticTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo, ownership_timeout: 600_000)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
now = BDS.Persistence.now_ms()
unique = Integer.to_string(System.unique_integer([:positive]))

View File

@@ -36,6 +36,7 @@ defmodule BDS.Scripting.ApiTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
temp_dir =
Path.join(System.tmp_dir!(), "bds-scripting-api-#{System.unique_integer([:positive])}")
@@ -45,6 +46,10 @@ defmodule BDS.Scripting.ApiTest do
{:ok, project} = BDS.Projects.create_project(%{name: "Scripting API", data_path: temp_dir})
on_exit(fn ->
_ = BDS.Preview.stop_preview(project.id)
end)
%{project: project}
end

View File

@@ -7,6 +7,7 @@ defmodule BDS.Scripts.TransformsTest do
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, {:shared, self()})
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.mode(BDS.Repo, :manual) end)
temp_dir =
Path.join(System.tmp_dir!(), "bds-transforms-#{System.unique_integer([:positive])}")

View File

@@ -240,7 +240,7 @@ defmodule BDS.TemplatesTest do
Jason.decode!(File.read!(tags_path))
end
test "update_template keeps committed database changes when renaming the published file fails",
test "update_template keeps committed database changes when rewriting the published file fails",
%{project: project, temp_dir: temp_dir} do
assert {:ok, template} =
BDS.Templates.create_template(%{
@@ -269,8 +269,9 @@ defmodule BDS.TemplatesTest do
post_template_slug: published.slug
})
blocked_path = Path.join([temp_dir, "templates", "feature-view.liquid.tmp"])
File.mkdir_p!(blocked_path)
published_template_path = Path.join(temp_dir, published.file_path)
File.rm!(published_template_path)
File.mkdir_p!(published_template_path)
assert {:error, _reason} =
BDS.Templates.update_template(published.id, %{slug: "feature-view"})