diff --git a/lib/livebook/session.ex b/lib/livebook/session.ex index af1c1250f..f27771445 100644 --- a/lib/livebook/session.ex +++ b/lib/livebook/session.ex @@ -891,7 +891,11 @@ defmodule Livebook.Session do """ @spec default_notebook() :: Notebook.t() def default_notebook() do - %{Notebook.new() | sections: [%{Section.new() | cells: [Cell.new(:code)]}]} + %{ + Notebook.new() + | sections: [%{Section.new() | cells: [Cell.new(:code)]}], + hub_id: Livebook.Hubs.get_default_hub().id + } end defp schedule_autosave(state) do diff --git a/test/livebook/session_test.exs b/test/livebook/session_test.exs index 521d804fa..cb057b6b8 100644 --- a/test/livebook/session_test.exs +++ b/test/livebook/session_test.exs @@ -1906,6 +1906,40 @@ defmodule Livebook.SessionTest do assert_receive {:operation, {:add_cell_evaluation_output, _, ^cell_id, ^expected_output}} end + defmodule Global do + use ExUnit.Case, async: false + + describe "default hub for new notebooks" do + test "use the default hub as default for new notebooks" do + hub = Livebook.Factory.insert_hub(:team) + Livebook.Hubs.set_default_hub(hub.id) + notebook = Livebook.Session.default_notebook() + + assert notebook.hub_id == hub.id + Livebook.Hubs.delete_hub(hub.id) + end + + test "fallback to personal-hub when there's no default" do + hub = Livebook.Factory.insert_hub(:team) + Livebook.Hubs.unset_default_hub(hub.id) + notebook = Livebook.Session.default_notebook() + + assert notebook.hub_id == "personal-hub" + Livebook.Hubs.delete_hub(hub.id) + end + + test "fallback to personal-hub when the default doesn't exist" do + hub = Livebook.Factory.insert_hub(:team) + Livebook.Hubs.set_default_hub(hub.id) + Livebook.Hubs.delete_hub(hub.id) + notebook = Livebook.Session.default_notebook() + + refute Livebook.Hubs.hub_exists?(hub.id) + assert notebook.hub_id == "personal-hub" + end + end + end + defp start_session(opts \\ []) do opts = Keyword.merge([id: Utils.random_id()], opts) pid = start_supervised!({Session, opts}, id: opts[:id]) diff --git a/test/livebook_teams/web/hub/edit_live_test.exs b/test/livebook_teams/web/hub/edit_live_test.exs index 56644aba0..28e7f8825 100644 --- a/test/livebook_teams/web/hub/edit_live_test.exs +++ b/test/livebook_teams/web/hub/edit_live_test.exs @@ -7,7 +7,7 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do alias Livebook.Hubs setup %{user: user, node: node} do - Livebook.Hubs.subscribe([:crud, :connection, :secrets, :default]) + Livebook.Hubs.subscribe([:crud, :connection, :secrets]) hub = create_team_hub(user, node) id = hub.id