2021-03-04 05:56:28 +08:00
|
|
|
defmodule LivebookWeb.HomeLiveTest do
|
|
|
|
use LivebookWeb.ConnCase
|
2021-01-08 04:16:54 +08:00
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
|
|
|
|
2021-03-26 02:04:49 +08:00
|
|
|
alias Livebook.{SessionSupervisor, Session}
|
2021-02-21 23:54:44 +08:00
|
|
|
|
2021-01-08 04:16:54 +08:00
|
|
|
test "disconnected and connected render", %{conn: conn} do
|
2021-01-08 21:14:26 +08:00
|
|
|
{:ok, view, disconnected_html} = live(conn, "/")
|
2021-03-26 02:04:49 +08:00
|
|
|
assert disconnected_html =~ "Running sessions"
|
|
|
|
assert render(view) =~ "Running sessions"
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "redirects to session upon creation", %{conn: conn} do
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
assert {:error, {:live_redirect, %{to: to}}} =
|
|
|
|
view
|
2021-03-26 02:04:49 +08:00
|
|
|
|> element("button", "New notebook")
|
2021-02-21 23:54:44 +08:00
|
|
|
|> render_click()
|
|
|
|
|
|
|
|
assert to =~ "/sessions/"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "file selection" do
|
|
|
|
test "updates the list of files as the input changes", %{conn: conn} do
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
path = Path.expand("../../../lib", __DIR__) <> "/"
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("form")
|
2021-03-04 05:56:28 +08:00
|
|
|
|> render_change(%{path: path}) =~ "livebook_web"
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "allows importing when a notebook file is selected", %{conn: conn} do
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
path = test_notebook_path("basic")
|
|
|
|
|
|
|
|
view
|
|
|
|
|> element("form")
|
|
|
|
|> render_change(%{path: path})
|
|
|
|
|
|
|
|
assert assert {:error, {:live_redirect, %{to: to}}} =
|
|
|
|
view
|
2021-03-20 21:10:15 +08:00
|
|
|
|> element(~s{button[phx-click="fork"]}, "Fork")
|
2021-02-21 23:54:44 +08:00
|
|
|
|> render_click()
|
|
|
|
|
|
|
|
assert to =~ "/sessions/"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "disables import when a directory is selected", %{conn: conn} do
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
path = File.cwd!()
|
|
|
|
|
|
|
|
view
|
|
|
|
|> element("form")
|
|
|
|
|> render_change(%{path: path})
|
|
|
|
|
|
|
|
assert view
|
2021-03-20 21:10:15 +08:00
|
|
|
|> element(~s{button[phx-click="fork"][disabled]}, "Fork")
|
2021-02-21 23:54:44 +08:00
|
|
|
|> has_element?()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "disables import when a nonexistent file is selected", %{conn: conn} do
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
path = File.cwd!() |> Path.join("nonexistent.livemd")
|
|
|
|
|
|
|
|
view
|
|
|
|
|> element("form")
|
|
|
|
|> render_change(%{path: path})
|
|
|
|
|
|
|
|
assert view
|
2021-03-20 21:10:15 +08:00
|
|
|
|> element(~s{button[phx-click="fork"][disabled]}, "Fork")
|
2021-02-21 23:54:44 +08:00
|
|
|
|> has_element?()
|
|
|
|
end
|
2021-04-14 22:38:54 +08:00
|
|
|
|
2021-04-14 22:47:50 +08:00
|
|
|
@tag :tmp_dir
|
|
|
|
test "disables open when a write-protected notebook is selected",
|
|
|
|
%{conn: conn, tmp_dir: tmp_dir} do
|
2021-04-14 22:38:54 +08:00
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
2021-04-14 22:47:50 +08:00
|
|
|
path = Path.join(tmp_dir, "write_protected.livemd")
|
|
|
|
File.touch!(path)
|
|
|
|
File.chmod!(path, 0o444)
|
2021-04-14 22:38:54 +08:00
|
|
|
|
|
|
|
view
|
|
|
|
|> element("form")
|
|
|
|
|> render_change(%{path: path})
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element(~s{button[phx-click="open"][disabled]}, "Open")
|
|
|
|
|> has_element?()
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element(~s{[aria-label="This file is write-protected, please fork instead"]})
|
|
|
|
|> has_element?()
|
|
|
|
end
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "sessions list" do
|
|
|
|
test "lists running sessions", %{conn: conn} do
|
|
|
|
{:ok, id1} = SessionSupervisor.create_session()
|
|
|
|
{:ok, id2} = SessionSupervisor.create_session()
|
|
|
|
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
assert render(view) =~ id1
|
|
|
|
assert render(view) =~ id2
|
|
|
|
end
|
|
|
|
|
|
|
|
test "updates UI whenever a session is added or deleted", %{conn: conn} do
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
{:ok, id} = SessionSupervisor.create_session()
|
|
|
|
assert render(view) =~ id
|
|
|
|
|
2021-04-13 05:24:26 +08:00
|
|
|
SessionSupervisor.close_session(id)
|
2021-02-21 23:54:44 +08:00
|
|
|
refute render(view) =~ id
|
|
|
|
end
|
2021-03-26 02:04:49 +08:00
|
|
|
|
|
|
|
test "allows forking existing session", %{conn: conn} do
|
|
|
|
{:ok, id} = SessionSupervisor.create_session()
|
|
|
|
Session.set_notebook_name(id, "My notebook")
|
|
|
|
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
assert {:error, {:live_redirect, %{to: to}}} =
|
|
|
|
view
|
|
|
|
|> element(~s{[data-test-session-id="#{id}"] button}, "Fork")
|
|
|
|
|> render_click()
|
|
|
|
|
|
|
|
assert to =~ "/sessions/"
|
|
|
|
|
|
|
|
{:ok, view, _} = live(conn, to)
|
|
|
|
assert render(view) =~ "My notebook - fork"
|
|
|
|
end
|
|
|
|
|
2021-04-13 05:24:26 +08:00
|
|
|
test "allows closing session after confirmation", %{conn: conn} do
|
2021-03-26 02:04:49 +08:00
|
|
|
{:ok, id} = SessionSupervisor.create_session()
|
|
|
|
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
assert render(view) =~ id
|
|
|
|
|
|
|
|
view
|
2021-04-13 05:24:26 +08:00
|
|
|
|> element(~s{[data-test-session-id="#{id}"] a}, "Close")
|
2021-03-26 02:04:49 +08:00
|
|
|
|> render_click()
|
|
|
|
|
|
|
|
view
|
2021-04-13 05:24:26 +08:00
|
|
|
|> element(~s{button}, "Close session")
|
2021-03-26 02:04:49 +08:00
|
|
|
|> render_click()
|
|
|
|
|
|
|
|
refute render(view) =~ id
|
|
|
|
end
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
2021-03-31 03:42:02 +08:00
|
|
|
test "link to introductory notebook correctly creates a new session", %{conn: conn} do
|
|
|
|
{:ok, view, _} = live(conn, "/")
|
|
|
|
|
|
|
|
assert {:error, {:live_redirect, %{to: to}}} =
|
|
|
|
view
|
|
|
|
|> element(~s{[aria-label="Introduction"] button})
|
|
|
|
|> render_click()
|
|
|
|
|
|
|
|
assert to =~ "/sessions/"
|
|
|
|
|
|
|
|
{:ok, view, _} = live(conn, to)
|
2021-04-01 18:56:19 +08:00
|
|
|
assert render(view) =~ "Welcome to Livebook"
|
2021-03-31 03:42:02 +08:00
|
|
|
end
|
|
|
|
|
2021-04-23 23:40:13 +08:00
|
|
|
describe "notebook import" do
|
|
|
|
test "allows importing notebook directly from content", %{conn: conn} do
|
|
|
|
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions")
|
|
|
|
|
|
|
|
{:ok, view, _} = live(conn, "/home/import/content")
|
|
|
|
|
|
|
|
notebook_content = """
|
|
|
|
# My notebook
|
|
|
|
"""
|
|
|
|
|
|
|
|
view
|
|
|
|
|> element("form", "Import")
|
|
|
|
|> render_submit(%{data: %{content: notebook_content}})
|
|
|
|
|
|
|
|
assert_receive {:session_created, id}
|
|
|
|
|
|
|
|
{:ok, view, _} = live(conn, "/sessions/#{id}")
|
|
|
|
assert render(view) =~ "My notebook"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
# Helpers
|
|
|
|
|
|
|
|
defp test_notebook_path(name) do
|
|
|
|
path =
|
|
|
|
["../../support/notebooks", name <> ".livemd"]
|
|
|
|
|> Path.join()
|
|
|
|
|> Path.expand(__DIR__)
|
|
|
|
|
|
|
|
unless File.exists?(path) do
|
|
|
|
raise "Cannot find test notebook with the name: #{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
path
|
2021-01-08 04:16:54 +08:00
|
|
|
end
|
|
|
|
end
|