defmodule BDS.MenuTest do use ExUnit.Case, async: false setup do :ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo) temp_dir = Path.join(System.tmp_dir!(), "bds-menu-#{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: "Menu", data_path: temp_dir}) %{project: project, temp_dir: temp_dir} end test "update_menu normalizes Home first, writes meta/menu.opml, and load returns nested items", %{project: project, temp_dir: temp_dir} do assert {:ok, menu} = BDS.Menu.update_menu(project.id, [ %{kind: :page, label: "About", slug: "about"}, %{ kind: :submenu, label: "Sections", children: [ %{kind: :category_archive, label: "Notes", slug: "notes"}, %{kind: :page, label: "Contact", slug: "contact"} ] }, %{kind: :home, label: "Ignored Home"} ]) assert hd(menu.items) == %{kind: :home, label: "Home", slug: nil} assert Enum.at(menu.items, 1) == %{kind: :page, label: "About", slug: "about"} assert Enum.at(menu.items, 2) == %{ kind: :submenu, label: "Sections", slug: nil, children: [ %{kind: :category_archive, label: "Notes", slug: "notes"}, %{kind: :page, label: "Contact", slug: "contact"} ] } opml_path = Path.join([temp_dir, "meta", "menu.opml"]) assert File.exists?(opml_path) contents = File.read!(opml_path) assert contents =~ ~s(), ~s(), ~s( ), ~s( Blog Menu), ~s( ), ~s( ), ~s( ), ~s( ), ~s( ), ~s( ), ~s( ), ~s( ), ~s() ] |> Enum.join("\n") ) assert {:ok, menu} = BDS.Menu.sync_menu_from_filesystem(project.id) assert menu.items == [ %{kind: :home, label: "Home", slug: nil}, %{ kind: :submenu, label: "Topics", slug: nil, children: [ %{kind: :page, label: "Blog", slug: "blog"}, %{kind: :category_archive, label: "Elixir", slug: "elixir"} ] } ] end end