defmodule LivebookWeb.SessionLiveTest do use LivebookWeb.ConnCase import Phoenix.LiveViewTest alias Livebook.{Sessions, Session, Delta, Runtime, Users, FileSystem} alias Livebook.Notebook.Cell alias Livebook.Users.User setup do {:ok, session} = Sessions.create_session(notebook: Livebook.Notebook.new()) %{session: session} end test "disconnected and connected render", %{conn: conn, session: session} do {:ok, view, disconnected_html} = live(conn, "/sessions/#{session.id}") assert disconnected_html =~ "Untitled notebook" assert render(view) =~ "Untitled notebook" end describe "asynchronous updates" do test "renders an updated notebook name", %{conn: conn, session: session} do {:ok, view, _} = live(conn, "/sessions/#{session.id}") Session.set_notebook_name(session.pid, "My notebook") wait_for_session_update(session.pid) assert render(view) =~ "My notebook" end test "renders a newly inserted section", %{conn: conn, session: session} do {:ok, view, _} = live(conn, "/sessions/#{session.id}") section_id = insert_section(session.pid) assert render(view) =~ section_id end test "renders an updated section name", %{conn: conn, session: session} do section_id = insert_section(session.pid) {:ok, view, _} = live(conn, "/sessions/#{session.id}") Session.set_section_name(session.pid, section_id, "My section") wait_for_session_update(session.pid) assert render(view) =~ "My section" end test "renders a newly inserted cell", %{conn: conn, session: session} do section_id = insert_section(session.pid) {:ok, view, _} = live(conn, "/sessions/#{session.id}") cell_id = insert_text_cell(session.pid, section_id, :markdown) assert render(view) =~ "cell-" <> cell_id end test "un-renders a deleted cell", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_text_cell(session.pid, section_id, :markdown) {:ok, view, _} = live(conn, "/sessions/#{session.id}") Session.delete_cell(session.pid, cell_id) wait_for_session_update(session.pid) refute render(view) =~ "cell-" <> cell_id end end describe "UI events update the shared state" do test "adding a new section", %{conn: conn, session: session} do {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element("button", "New section") |> render_click() assert %{notebook: %{sections: [_section]}} = Session.get_data(session.pid) end test "adding a new cell", %{conn: conn, session: session} do Session.insert_section(session.pid, 0) {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element("button", "+ Markdown") |> render_click() assert %{notebook: %{sections: [%{cells: [%Cell.Markdown{}]}]}} = Session.get_data(session.pid) end test "queueing cell evaluation", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_text_cell(session.pid, section_id, :elixir, "Process.sleep(50)") {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s{[data-element="session"]}) |> render_hook("queue_cell_evaluation", %{"cell_id" => cell_id}) assert %{cell_infos: %{^cell_id => %{evaluation_status: :evaluating}}} = Session.get_data(session.pid) end test "cancelling cell evaluation", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_text_cell(session.pid, section_id, :elixir, "Process.sleep(2000)") {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s{[data-element="session"]}) |> render_hook("queue_cell_evaluation", %{"cell_id" => cell_id}) view |> element(~s{[data-element="session"]}) |> render_hook("cancel_cell_evaluation", %{"cell_id" => cell_id}) assert %{cell_infos: %{^cell_id => %{evaluation_status: :ready}}} = Session.get_data(session.pid) end test "inserting a cell below the given cell", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_text_cell(session.pid, section_id, :elixir) {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s{[data-element="session"]}) |> render_hook("insert_cell_below", %{"cell_id" => cell_id, "type" => "markdown"}) assert %{notebook: %{sections: [%{cells: [_first_cell, %Cell.Markdown{}]}]}} = Session.get_data(session.pid) end test "inserting a cell at section start", %{conn: conn, session: session} do section_id = insert_section(session.pid) _cell_id = insert_text_cell(session.pid, section_id, :elixir) {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s{[data-element="session"]}) |> render_hook("insert_cell_below", %{"section_id" => section_id, "type" => "markdown"}) assert %{notebook: %{sections: [%{cells: [%Cell.Markdown{}, _first_cell]}]}} = Session.get_data(session.pid) end test "deleting the given cell", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_text_cell(session.pid, section_id, :elixir) {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s{[data-element="session"]}) |> render_hook("delete_cell", %{"cell_id" => cell_id}) assert %{notebook: %{sections: [%{cells: []}]}} = Session.get_data(session.pid) end test "newlines in input values are normalized", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_input_cell(session.pid, section_id) {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s/form[phx-change="set_cell_value"]/) |> render_change(%{"value" => "line\r\nline"}) assert %{notebook: %{sections: [%{cells: [%{id: ^cell_id, value: "line\nline"}]}]}} = Session.get_data(session.pid) end end describe "runtime settings" do test "connecting to elixir standalone updates connect button to reconnect", %{conn: conn, session: session} do {:ok, view, _} = live(conn, "/sessions/#{session.id}/settings/runtime") Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}") view |> element("button", "Elixir standalone") |> render_click() [elixir_standalone_view] = live_children(view) elixir_standalone_view |> element("button", "Connect") |> render_click() assert_receive {:operation, {:set_runtime, _pid, %Runtime.ElixirStandalone{} = runtime}} page = render(view) assert page =~ Atom.to_string(runtime.node) assert page =~ "Reconnect" assert page =~ "Disconnect" end end describe "persistence settings" do @tag :tmp_dir test "saving to file shows the newly created file in file selector", %{conn: conn, session: session, tmp_dir: tmp_dir} do {:ok, view, _} = live(conn, "/sessions/#{session.id}/settings/file") assert view = find_live_child(view, "persistence") path = Path.join(tmp_dir, "notebook.livemd") view |> element("button", "Choose a file") |> render_click() view |> element(~s{form[phx-change="set_path"]}) |> render_change(%{path: path}) view |> element(~s{button[phx-click="confirm_file"]}, "Choose") |> render_click() view |> element(~s{button}, "Save now") |> render_click() view |> element("button", "Change file") |> render_click() assert view |> element("button", "notebook.livemd") |> has_element?() end @tag :tmp_dir test "changing output persistence updates data", %{conn: conn, session: session, tmp_dir: tmp_dir} do {:ok, view, _} = live(conn, "/sessions/#{session.id}/settings/file") assert view = find_live_child(view, "persistence") path = Path.join(tmp_dir, "notebook.livemd") view |> element("button", "Choose a file") |> render_click() view |> element(~s{form[phx-change="set_path"]}) |> render_change(%{path: path}) view |> element(~s{button[phx-click="confirm_file"]}, "Choose") |> render_click() view |> element(~s{form[phx-change="set_options"]}) |> render_change(%{persist_outputs: "true"}) view |> element(~s{button}, "Save now") |> render_click() assert %{notebook: %{persist_outputs: true}} = Session.get_data(session.pid) end end describe "completion" do test "replies with nil completion reference when no runtime is started", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_text_cell(session.pid, section_id, :elixir, "Process.sleep(10)") {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s{[data-element="session"]}) |> render_hook("intellisense_request", %{ "cell_id" => cell_id, "type" => "completion", "hint" => "System.ver" }) assert_reply view, %{"ref" => nil} end test "replies with completion reference and then sends asynchronous response", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_text_cell(session.pid, section_id, :elixir, "Process.sleep(10)") {:ok, runtime} = Livebook.Runtime.Embedded.init() Session.connect_runtime(session.pid, runtime) {:ok, view, _} = live(conn, "/sessions/#{session.id}") view |> element(~s{[data-element="session"]}) |> render_hook("intellisense_request", %{ "cell_id" => cell_id, "type" => "completion", "hint" => "System.ver" }) assert_reply view, %{"ref" => ref} assert ref != nil assert_push_event(view, "intellisense_response", %{ "ref" => ^ref, "response" => %{items: [%{label: "version/0"}]} }) end end test "forking the session", %{conn: conn, session: session} do Session.set_notebook_name(session.pid, "My notebook") wait_for_session_update(session.pid) {:ok, view, _} = live(conn, "/sessions/#{session.id}") assert {:error, {:live_redirect, %{to: to}}} = view |> element("button", "Fork") |> render_click() assert to =~ "/sessions/" {:ok, view, _} = live(conn, to) assert render(view) =~ "My notebook - fork" end describe "connected users" do test "lists connected users", %{conn: conn, session: session} do user1 = create_user_with_name("Jake Peralta") client_pid = spawn_link(fn -> Session.register_client(session.pid, self(), user1) receive do :stop -> :ok end end) {:ok, view, _} = live(conn, "/sessions/#{session.id}") assert render(view) =~ "Jake Peralta" send(client_pid, :stop) end test "updates users list whenever a user joins or leaves", %{conn: conn, session: session} do {:ok, view, _} = live(conn, "/sessions/#{session.id}") Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}") user1 = create_user_with_name("Jake Peralta") client_pid = spawn_link(fn -> Session.register_client(session.pid, self(), user1) receive do :stop -> :ok end end) assert_receive {:operation, {:client_join, ^client_pid, _user}} assert render(view) =~ "Jake Peralta" send(client_pid, :stop) assert_receive {:operation, {:client_leave, ^client_pid}} refute render(view) =~ "Jake Peralta" end test "updates users list whenever a user changes his data", %{conn: conn, session: session} do user1 = create_user_with_name("Jake Peralta") client_pid = spawn_link(fn -> Session.register_client(session.pid, self(), user1) receive do :stop -> :ok end end) {:ok, view, _} = live(conn, "/sessions/#{session.id}") Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session.id}") assert render(view) =~ "Jake Peralta" Users.broadcast_change(%{user1 | name: "Raymond Holt"}) assert_receive {:operation, {:update_user, _pid, _user}} refute render(view) =~ "Jake Peralta" assert render(view) =~ "Raymond Holt" send(client_pid, :stop) end end describe "input cell settings" do test "setting input cell attributes updates data", %{conn: conn, session: session} do section_id = insert_section(session.pid) cell_id = insert_input_cell(session.pid, section_id) {:ok, view, _} = live(conn, "/sessions/#{session.id}/cell-settings/#{cell_id}") form_selector = ~s/[role="dialog"] form/ assert view |> element(form_selector) |> render_change(%{attrs: %{type: "range"}}) =~ ~s{