Channel.Test

Testing

Call a channel's join and push from selm test, in this process, with no browser and no websocket.

join returns Ok reply or Err Refusal. push joins, sends one up value, and returns the first down message the handler broadcasts with Channel.broadcast. A refused join is Err. If the handler does not broadcast, the result is Err Invalid.

run joins once, applies a list of up values in order, and returns every broadcast down (useful for streaming uploads that send several chunks).

session is whatever that channel's join expects. In the chat example it is the client's name, and topic is Chat.Lobby.

module ChatTest exposing (tests)

import Channel.Test
import Chat
import Expect
import Test exposing (Test, describe, test)

tests : Test ()
tests =
    describe "Chat"
        ()
        [ test "join returns the current history"
              <| (\_ctx -> Expect.equal (Ok []) (Channel.Test.join Chat.channel "guest" Chat.Lobby))
        , test "a send is broadcast to the room"
              <| (\_ctx ->
                     Expect.equal
                         (Ok (Chat.Posted "guest: hi"))
                         (Channel.Test.push Chat.channel "guest" Chat.Lobby (Chat.Send "hi"))
                 )
        ]

That suite is examples/05_chat/tests/ChatTest.selm. The channel itself is examples/05_chat/src/channel/Chat.selm.

Values

join

join : Channel topic up down reply state msg -> session -> topic -> Result Refusal reply

Run join for topic as session.

Ok is the reply the channel returned from join. Err is the Refusal.

Examples

Channel.Test.join Chat.channel "guest" Chat.Lobby

push

push : Channel topic up down reply state msg -> session -> topic -> up -> Result Refusal down

Send one up message on topic.

push joins first, then runs handleIn. Ok is the first down value that handler broadcasts. A refused join is Err. If nothing is broadcast, the result is Err Invalid.

Examples

Channel.Test.push chat "Ada" Chat.Lobby (Send "hi")

run

run : Channel topic up down reply state msg -> session -> topic -> List up -> Result Refusal (reply, List down)

Join once, apply each up in order, and collect every broadcast down.

Ok is (reply, downs) from the single join plus each handleIn. Use this when a protocol needs several messages on the same topic (chunked uploads). An empty downs list is still Ok when nothing was broadcast.

Examples

Channel.Test.run upload session topic [ Chunk rows, Finish ]