this repo has no description
at main 1.5 kB view raw
1defmodule EslHnWeb.Api.SocketTest do 2 use ExUnit.Case, async: false 3 4 import EslHn.Test.Data 5 6 alias EslHn.Cache 7 8 setup do 9 assert {:ok, {host, port}} = EslHnWeb.Endpoint.server_info(:http) 10 11 on_exit(fn -> Cache.flush(EslHn) end) 12 13 {:ok, host: host, port: port} 14 end 15 16 defmodule Client do 17 use Fresh 18 19 def handle_in({:text, data}, {parent, counter}) do 20 send(parent, {:ws, counter, JSON.decode!(data)}) 21 {:ok, {parent, counter + 1}} 22 end 23 end 24 25 test "receive initial list of stories", ctx do 26 stories = Enum.take(story(), 50) 27 28 ids = Enum.map(stories, & &1.id) 29 Cache.write(EslHn, :index, ids) 30 31 Cache.write_all(EslHn, Enum.map(stories, &{&1.id, &1})) 32 33 start_supervised!( 34 {Client, uri: "ws://localhost:#{ctx.port}/websocket", state: {self(), 0}} 35 ) 36 37 assert_receive {:ws, 0, init} 38 39 recv_ids = Enum.map(init, & &1["id"]) 40 41 assert Enum.all?(ids, &(&1 in recv_ids)) 42 assert length(recv_ids) == length(ids) 43 end 44 45 test "broadcasted events are sent over socket", ctx do 46 start_supervised!( 47 {Client, uri: "ws://localhost:#{ctx.port}/websocket", state: {self(), 0}} 48 ) 49 50 assert_receive {:ws, 0, []} 51 52 for stories <- Enum.take(StreamData.list_of(story(), min_length: 1), 10) do 53 ids = Enum.map(stories, & &1.id) 54 55 EslHn.broadcast_new(stories) 56 57 assert_receive {:ws, _, msg} 58 59 recv_ids = Enum.map(msg, & &1["id"]) 60 61 assert Enum.all?(ids, &(&1 in recv_ids)) 62 assert length(recv_ids) == length(ids) 63 end 64 end 65end