2022-02-28 20:53:33 +08:00
|
|
|
defmodule LivebookWeb.JSViewChannelTest do
|
2022-01-11 01:38:08 +08:00
|
|
|
use LivebookWeb.ChannelCase, async: true
|
2022-01-06 23:31:26 +08:00
|
|
|
|
|
|
|
setup do
|
2022-01-11 01:38:08 +08:00
|
|
|
session_id = Livebook.Utils.random_node_aware_id()
|
|
|
|
|
2022-01-06 23:31:26 +08:00
|
|
|
{:ok, _, socket} =
|
|
|
|
LivebookWeb.Socket
|
|
|
|
|> socket()
|
2022-02-28 20:53:33 +08:00
|
|
|
|> subscribe_and_join(LivebookWeb.JSViewChannel, "js_view", %{
|
2022-01-11 01:38:08 +08:00
|
|
|
"session_id" => session_id
|
|
|
|
})
|
2022-01-06 23:31:26 +08:00
|
|
|
|
|
|
|
%{socket: socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "loads initial data from the widget server and pushes to the client", %{socket: socket} do
|
|
|
|
push(socket, "connect", %{"session_token" => session_token(), "ref" => "1"})
|
|
|
|
|
|
|
|
assert_receive {:connect, from, %{}}
|
|
|
|
send(from, {:connect_reply, [1, 2, 3], %{ref: "1"}})
|
|
|
|
|
2022-02-08 04:03:25 +08:00
|
|
|
assert_push "init:1", %{"root" => [nil, [1, 2, 3]]}
|
2022-01-06 23:31:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sends client events to the corresponding widget server", %{socket: socket} do
|
|
|
|
push(socket, "connect", %{"session_token" => session_token(), "ref" => "1"})
|
|
|
|
|
|
|
|
assert_receive {:connect, from, %{}}
|
|
|
|
send(from, {:connect_reply, [1, 2, 3], %{ref: "1"}})
|
|
|
|
|
2022-02-08 04:03:25 +08:00
|
|
|
push(socket, "event", %{"root" => [["ping", "1"], [1, 2, 3]]})
|
2022-01-06 23:31:26 +08:00
|
|
|
|
|
|
|
assert_receive {:event, "ping", [1, 2, 3], %{origin: _origin}}
|
|
|
|
end
|
|
|
|
|
2022-02-08 04:03:25 +08:00
|
|
|
describe "binary payload" do
|
|
|
|
test "initial data", %{socket: socket} do
|
|
|
|
push(socket, "connect", %{"session_token" => session_token(), "ref" => "1"})
|
|
|
|
|
|
|
|
assert_receive {:connect, from, %{}}
|
|
|
|
payload = {:binary, %{message: "hey"}, <<1, 2, 3>>}
|
|
|
|
send(from, {:connect_reply, payload, %{ref: "1"}})
|
|
|
|
|
|
|
|
assert_push "init:1", {:binary, <<24::size(32), "[null,{\"message\":\"hey\"}]", 1, 2, 3>>}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "form client to server", %{socket: socket} do
|
|
|
|
push(socket, "connect", %{"session_token" => session_token(), "ref" => "1"})
|
|
|
|
|
|
|
|
assert_receive {:connect, from, %{}}
|
|
|
|
send(from, {:connect_reply, [1, 2, 3], %{ref: "1"}})
|
|
|
|
|
|
|
|
raw = {:binary, <<32::size(32), "[[\"ping\",\"1\"],{\"message\":\"hey\"}]", 1, 2, 3>>}
|
|
|
|
push(socket, "event", raw)
|
|
|
|
|
|
|
|
payload = {:binary, %{"message" => "hey"}, <<1, 2, 3>>}
|
|
|
|
assert_receive {:event, "ping", ^payload, %{origin: _origin}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-06 23:31:26 +08:00
|
|
|
defp session_token() do
|
2022-02-28 20:53:33 +08:00
|
|
|
Phoenix.Token.sign(LivebookWeb.Endpoint, "js view", %{pid: self()})
|
2022-01-06 23:31:26 +08:00
|
|
|
end
|
|
|
|
end
|