diff --git a/README.md b/README.md index 35369fa..20ab361 100644 --- a/README.md +++ b/README.md @@ -1,79 +1,36 @@ Blur ==== -Chat Bot for Streamers. Built to scale. Fast, efficient processing. +Twitch Chat Bot ![Elixir CI](https://github.com/rockerBOO/blur/workflows/Elixir%20CI/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/rockerBOO/blur/badge.svg)](https://coveralls.io/github/rockerBOO/blur) ## Install -To setup, the configuration options are in `.env`. `.env.example` is setup to show how to configure, and just rename to `.env`. +First, add Blur to your mix.exs dependencies: ```elixir -# The key generated from twitchtmi.com/chat -export TWITCH_CHAT_KEY=oauth: - -# Username on Twitch for the Bot. -# It needs to match to the user of the access token/chat key. -export TWITCH_USERNAME= - -# OPTIONAL. This takes precidence over the chat key for more intergrated options (authenticated calls to twitch) -# export TWITCH_ACCESS_TOKEN= +def deps do + [{:blur, "~> 0.2"}] +end ``` -## To Run - -`source .env && iex -S mix` - -## Configuration for Channels +Then, update your dependencies: -Configuration for channels are stored in `data/#channel/` +```sh-session +$ mix deps.get +``` -* `data/rockerboo/commands.json` - * Commands list -* `data/rockerboo/config.json` - * Configuration options for the channel -* `data/rockerboo/aliases.json` - * Stores aliases for commmands +To setup, the configuration options are in `.env`. `.env.example` is setup to show how to configure, and just rename to `.env`. -### `data/#channel/config.json` -```js -{ - "messages": "on" -} -``` +```elixir +# The key generated from https://twitchapps.com/tmi/. +export TWITCH_CHAT_KEY=oauth: -### `data/#channel/commands.json` -```js -{ - "hello": ["say", "Hello"], - "help": ["say", "You need help"], - "follower": ["cmd", "follower"], - "followed": ["cmd", "followed"], - "flip": ["say", "(╯°□°)╯︵┻━┻"], - "song": ["cmd", "song"], - "highlight": ["cmd", "highlight"] -} +# Username on Twitch for the Bot. +# It needs to match to the user of the access token/chat key. +export TWITCH_USERNAME= ``` -### `data/#channel/aliases.json` -```js -{ - "bealight": "bealright", - "bot": "elirc", - "glacier": "theme", - "xfile": "xfiles", - "x-file": "xfiles", - "x-files": "xfiles", - "h": "help", - "playlist": "lastfm", - "coming": "getsmeeverytime", - "63": "speedlimit", - "soundslist": "soundlist", - "sounds": "soundlist", - "table": "flip", - "tableflip": "flip" -} -``` diff --git a/config/config.exs b/config/config.exs deleted file mode 100644 index 8bd01b7..0000000 --- a/config/config.exs +++ /dev/null @@ -1,12 +0,0 @@ -import Config - -config :blur, :autojoin, [] - -config :logger, - backends: [:console] - -config :logger, :error_log, - path: "error.log", - level: :error - -import_config "#{Mix.env()}.exs" diff --git a/config/dev.exs b/config/dev.exs deleted file mode 100644 index 290a380..0000000 --- a/config/dev.exs +++ /dev/null @@ -1,3 +0,0 @@ -import Config - -config :blur, :autojoin, ["rockerboo", "adattape"] diff --git a/config/test.exs b/config/test.exs deleted file mode 100644 index c9b1e48..0000000 --- a/config/test.exs +++ /dev/null @@ -1,3 +0,0 @@ -import Config - -config :blur, :autojoin, [] diff --git a/lib/app.ex b/lib/app.ex new file mode 100644 index 0000000..a325246 --- /dev/null +++ b/lib/app.ex @@ -0,0 +1,29 @@ +defmodule Blur.App do + use Supervisor + + def start! do + Supervisor.start_link(__MODULE__, [], name: :blur) + end + + @spec start(atom, list) :: GenServer.on_start() + def start(_type, [user, channels]) do + Supervisor.start_link(__MODULE__, [user, channels], name: :blur) + end + + @impl Supervisor + def init([user, channels]) do + {:ok, client} = ExIRC.start_link!() + + children = [ + {Blur.Channels, []}, + {Blur.Users, []}, + # {Blur.IRC.Client, [client]}, + {Blur.IRC.Connection, [client, user]}, + {Blur.IRC.Login, [client, channels]}, + {Blur.IRC.Channel, [client]}, + {Blur.IRC.Message, [client]} + ] + + Supervisor.init(children, strategy: :one_for_one) + end +end diff --git a/lib/blur.ex b/lib/blur.ex index 9eb5d95..8ad81a9 100644 --- a/lib/blur.ex +++ b/lib/blur.ex @@ -1,6 +1,26 @@ defmodule Blur do @moduledoc """ Access your bot through Blur + + Client + + Handlers + - Login + on_logon -> join_channels + - Connection + %ConnState{} + on_connection -> login + on_disconnection -> logoff + - Message + pool + - Channels + on_join -> add_channel + [Channel, Channel] + - Users + on_names -> add_users + on_user_join -> add_user + on_part -> remove_user + [User, User] """ require Logger @@ -36,6 +56,7 @@ defmodule Blur do Say a message to the channel. ## Examples + Blur.say :twitch, "adattape" """ def say(client, channel, message), diff --git a/lib/handlers/IRC/channel.ex b/lib/handlers/IRC/channel.ex index a445a22..418fb60 100644 --- a/lib/handlers/IRC/channel.ex +++ b/lib/handlers/IRC/channel.ex @@ -6,8 +6,8 @@ defmodule Blur.IRC.Channel do require Logger alias ExIRC.Client - @spec start_link(client :: pid) :: GenServer.on_start() - def start_link(client) do + @spec start_link([client :: pid]) :: GenServer.on_start() + def start_link([client]) do GenServer.start_link(__MODULE__, client) end @@ -39,24 +39,13 @@ defmodule Blur.IRC.Channel do {:noreply, client} end - def handle_info({:logon, _, _nick, _, _}, state) do - {:noreply, state} - end - - def handle_info({:kicked, sender, channel}, state) do - by = sender.nick - Logger.debug("We were kicked from #{channel} by #{by}") - {:noreply, state} - end - # Drops unknown messages def handle_info(_msg, state) do {:noreply, state} end @impl true - def terminate(reason, _state) do - IO.inspect(reason) + def terminate(_reason, _state) do :ok end end diff --git a/lib/handlers/IRC/connection.ex b/lib/handlers/IRC/connection.ex index de21fed..de1b11f 100644 --- a/lib/handlers/IRC/connection.ex +++ b/lib/handlers/IRC/connection.ex @@ -20,12 +20,12 @@ defmodule Blur.IRC.Connection do client: nil end - @spec start_link(client :: GenServer.server()) :: GenServer.on_start() - def start_link(client) do - GenServer.start_link(__MODULE__, %State{client: client}) + @spec start_link(list) :: GenServer.on_start() + def start_link([client, user]) do + GenServer.start_link(__MODULE__, %State{client: client, user: user, nick: user, name: user}) end - @impl GenServer + @impl true @spec init(%State{}) :: {:ok, %State{}} def init(state) do ExIRC.Client.add_handler(state.client, self()) @@ -34,7 +34,18 @@ defmodule Blur.IRC.Connection do {:ok, state} end - @impl GenServer + def login(client, nick, token) do + # Login to IRC + case Blur.IRC.login(client, nick, token) do + {:error, :not_logged_in} -> + Logger.info("Logging in with #{nick} failed.") + + :ok -> + :ok + end + end + + @impl true @spec handle_info( {:connected, server :: binary, port :: non_neg_integer()} | {:disconnected} @@ -44,14 +55,7 @@ defmodule Blur.IRC.Connection do def handle_info({:connected, server, port}, state) do Logger.debug("Connected to #{server}:#{port}") - nick = Blur.Env.fetch!(:username) - - # Login to IRC - :ok = Blur.IRC.login(state.client, nick, Blur.token()) - - # Request Twitch Capabilities (tags) - :ok = Blur.IRC.request_twitch_capabilities(state.client) - + :ok = login(state.client, state.user, Blur.token()) {:noreply, state} end diff --git a/lib/handlers/IRC/irc.ex b/lib/handlers/IRC/irc.ex index fdcab27..53ec0b0 100644 --- a/lib/handlers/IRC/irc.ex +++ b/lib/handlers/IRC/irc.ex @@ -11,7 +11,7 @@ defmodule Blur.IRC do Request the CAP (capability) on the server ## Example - iex> Blur.IRC.cap_request client, ':twitch.tv/membership' + Blur.IRC.cap_request client, ':twitch.tv/membership' :ok """ @spec cap_request(client :: pid | atom, cap :: binary) :: :ok | {:error, atom} @@ -23,7 +23,7 @@ defmodule Blur.IRC do Request twitch for capabilities ## Example - iex> Blur.IRC.request_twitch_capabilities client + Blur.IRC.request_twitch_capabilities client :ok """ @spec request_twitch_capabilities(client :: pid | atom) :: :ok | {:error, atom} @@ -51,32 +51,18 @@ defmodule Blur.IRC do Connect to the IRC server. ## Example - iex> Blur.IRC.connect client, "irc.twitch.tv", 6667 + Blur.IRC.connect client, "irc.twitch.tv", 6667 :ok """ @spec connect!(client :: pid | atom, host :: binary, port :: non_neg_integer) :: :ok def connect!(client, host, port), do: ExIRC.Client.connect!(client, host, port) - @doc """ - Connect to the default IRC server. - - ## Example - iex> Blur.IRC.connect client - :ok - """ - @spec connect!(client :: pid | atom) :: :ok - def connect!(client) do - %{host: host, port: port} = %Blur.IRC.Connection.State{} - - ExIRC.Client.connect!(client, host, port) - end - @doc """ Login to the server. ## Example - iex> Blur.IRC.login client, "rockerBOO", "oauth:oauthhashherewithlettersandnumbers" + Blur.IRC.login client, "rockerBOO", "oauth:oauthhashherewithlettersandnumbers" :ok """ @spec login(client :: pid | atom, nick :: binary, pass :: binary) :: @@ -87,7 +73,7 @@ defmodule Blur.IRC do @doc """ Join many channels. ## Example - iex> Blur.IRC.join_many client, ["#rockerboo", "#adattape"] + Blur.IRC.join_many client, ["#rockerboo", "#adattape"] :ok """ @spec join_many(client :: pid | atom | atom, list) :: :ok | {:error, atom} @@ -99,12 +85,12 @@ defmodule Blur.IRC do Join an IRC channel. ## Example - iex> Blur.IRC.join client, "#rockerboo" + Blur.IRC.join client, "#rockerboo" :ok """ @spec join(client :: pid | atom, channel :: binary) :: :ok | {:error, atom} def join(client, "#" <> _ = channel) do - Logger.debug("Join #{channel}...") + Logger.debug("Join #{channel}") ExIRC.Client.join(client, channel) end @@ -115,12 +101,12 @@ defmodule Blur.IRC do Part from IRC channel. ## Example - iex> Blur.IRC.part client, "#rockerboo" + Blur.IRC.part client, "#rockerboo" :ok """ @spec part(client :: pid | atom, channel :: binary) :: :ok | {:error, atom} def part(client, "#" <> _ = channel) do - Logger.debug("Part #{channel}...") + Logger.debug("Part #{channel}") ExIRC.Client.part(client, channel) end @@ -131,7 +117,7 @@ defmodule Blur.IRC do Send a message to the channel ## Example - iex> Blur.IRC.say client, "#rockerboo", "Hello" + Blur.IRC.say client, "#rockerboo", "Hello" :ok """ @spec say(client :: pid | atom, channel :: binary, msg :: binary) :: :ok | {:error, atom} @@ -143,7 +129,7 @@ defmodule Blur.IRC do Quit the IRC server. ## Example - iex> Blur.IRC.quit client, "Goodbye!" + Blur.IRC.quit client, "Goodbye!" :ok """ @spec quit(client :: pid | atom, msg :: nil | binary) :: :ok | {:error, atom} @@ -155,7 +141,7 @@ defmodule Blur.IRC do Quit the IRC server with no message. ## Example - iex> Blur.IRC.quit client + Blur.IRC.quit client :ok """ @spec quit(client :: pid | atom) :: :ok | {:error, atom} @@ -167,7 +153,7 @@ defmodule Blur.IRC do Stop the IRC client process ## Example - iex> Blur.IRC.stop! client + Blur.IRC.stop! client {:stop, :normal, :ok, %ExIRC.Client{}} """ @spec stop!(client :: pid | atom) :: {:stop, :normal, :ok, %ExIRC.Client.ClientState{}} diff --git a/lib/handlers/IRC/login.ex b/lib/handlers/IRC/login.ex index fd0cd8a..1a8f2ce 100644 --- a/lib/handlers/IRC/login.ex +++ b/lib/handlers/IRC/login.ex @@ -10,35 +10,45 @@ defmodule Blur.IRC.Login do @doc """ Start login handler """ - @spec start_link(GenServer.server()) :: GenServer.on_start() - def start_link(client) do - GenServer.start_link(__MODULE__, client) + @spec start_link(list) :: GenServer.on_start() + def start_link([client, channels]) do + GenServer.start_link(__MODULE__, [client, channels]) end - @impl GenServer - @spec init(client :: GenServer.server()) :: {:ok, GenServer.server()} - def init(client) do + @impl true + # @spec init([pid, list]) :: {:ok, GenServer.server()} + def init([client, channels]) do Client.add_handler(client, self()) - {:ok, client} + {:ok, %{client: client, autojoin: channels}} end - @spec autojoin(client :: GenServer.server()) :: :ok - def autojoin(client) do - client |> Blur.IRC.join_many(Application.get_env(:blur, :autojoin)) + @spec autojoin(client :: GenServer.server(), channels :: list) :: :ok | {:error, atom} + def autojoin(client, channels) do + client |> Blur.IRC.join_many(channels) end @doc """ Handle login messages """ - @impl GenServer - @spec handle_info(:logged_in, list) :: {:noreply, list} - def handle_info(:logged_in, client) do + @impl true + @spec handle_info(:logged_in, map) :: {:noreply, list} + def handle_info(:logged_in, state) do Logger.debug("Logged in as #{Blur.Env.fetch(:username)}") - Logger.debug("Joining channels [#{Enum.join(Application.get_env(:blur, :autojoin), ", ")}]") - autojoin(client) + # Request Twitch Capabilities (tags) + case Blur.IRC.request_twitch_capabilities(state.client) do + {:error, _} -> Logger.error("Not connected to Twitch") + :ok -> :ok + end - {:noreply, client} + Logger.debug("Joining channels [#{Enum.join(state.autojoin, ", ")}]") + + case autojoin(state.client, state.autojoin) do + {:error, :not_connected} -> Logger.error("Not connected to Twitch IRC.") + :ok -> :ok + end + + {:noreply, state} end # Drops unknown messages diff --git a/lib/handlers/IRC/message.ex b/lib/handlers/IRC/message.ex index 87cb8e2..8e47074 100644 --- a/lib/handlers/IRC/message.ex +++ b/lib/handlers/IRC/message.ex @@ -6,20 +6,17 @@ defmodule Blur.IRC.Message do require Logger use GenServer - # alias Blur.Command - # alias Blur.Parser.Message - # alias Blur.Channel alias ExIRC.Client @doc """ Start message handler. """ - @spec start_link(client :: GenServer.server()) :: GenServer.on_start() - def start_link(client) do + @spec start_link([client :: GenServer.server()]) :: GenServer.on_start() + def start_link([client]) do GenServer.start_link(__MODULE__, client) end - @impl true + @impl GenServer @spec init(client :: GenServer.server()) :: {:ok, pid | atom} def init(client) do Client.add_handler(client, self()) @@ -29,22 +26,13 @@ defmodule Blur.IRC.Message do @spec parse_message(channel :: binary, user :: binary, msg :: binary) :: :ok def parse_message(channel, user, msg) do Logger.debug("Parsing message #{channel} #{user}: #{msg}") - - # if Channel.config?(channel) do - # Message.parse(msg, user, channel) - # |> Command.run(user, channel) - # |> case do - # :ok -> Logger.debug("Command send properly") - # end - # end - :ok end @doc """ Handle messages from IRC connection. """ - @impl true + @impl GenServer @spec handle_info( {:received, message :: charlist, sender :: %ExIRC.SenderInfo{}} | {:received, message :: charlist, sender :: %ExIRC.SenderInfo{}, channel :: charlist} @@ -68,13 +56,6 @@ defmodule Blur.IRC.Message do {:noreply, state} end - # Mentioned message. (Unsure if used by Twitch) - def handle_info({:mentioned, message, sender, channel}, state) do - from = sender.nick - Logger.debug("#{from} mentioned us in #{channel}: #{message}") - {:noreply, state} - end - # Uncaught end names list # {:unrecognized, "366", %ExIRC.Message{args: ["800807", "#rockerboo", "End of /NAMES list"], cmd: "366", ctcp: false, host: [], nick: [], server: "800807.tmi.twitch.tv", user: []}} def handle_info({:unrecognized, "366", _}, state) do @@ -104,7 +85,7 @@ defmodule Blur.IRC.Message do {:noreply, state} end - @impl true + @impl GenServer def terminate(reason, _state) do IO.inspect("terminate #{__MODULE__}") IO.inspect(reason) diff --git a/lib/handlers/IRC/supervisor.ex b/lib/handlers/IRC/supervisor.ex deleted file mode 100644 index c5bb4a5..0000000 --- a/lib/handlers/IRC/supervisor.ex +++ /dev/null @@ -1,52 +0,0 @@ -defmodule Blur.IRC.Supervisor do - @moduledoc """ - Blur IRC App Supervisor - - Children - * ConCache - * Blur.IRC.Connection - * Blur.IRC.Login - """ - - use Supervisor - require Logger - alias ExIRC.Client - - def start_link() do - {:ok, irc_client} = Client.start_link() - - Supervisor.start_link(__MODULE__, irc_client) - end - - @spec start(module, list) :: GenServer.on_start() - def start(_type, opts) do - {:ok, irc_client} = Client.start_link() - - Supervisor.start_link(__MODULE__, irc_client, opts) - end - - @impl true - def init(irc_client) do - children = [ - {ConCache, [name: :channel_cache, ttl_check_interval: false]}, - {Blur.IRC.Connection, irc_client}, - {Blur.IRC.Login, irc_client}, - {Blur.IRC.Channel, irc_client}, - {Blur.IRC.Message, irc_client} - # {Blur.IRC.Names, irc_client} - ] - - Supervisor.init(children, strategy: :one_for_one) - end - - def terminate(_reason, irc_client) do - # Quit the channel and close the underlying client - # connection when the process is terminating - Blur.IRC.quit(irc_client) - Blur.IRC.stop!(irc_client) - - Logger.info("Closed IRC connection.") - - :ok - end -end diff --git a/lib/handlers/IRC/names.ex b/lib/handlers/IRC/users.ex similarity index 100% rename from lib/handlers/IRC/names.ex rename to lib/handlers/IRC/users.ex diff --git a/lib/handlers/WS.7z b/lib/handlers/WS.7z new file mode 100644 index 0000000..e24cfbb Binary files /dev/null and b/lib/handlers/WS.7z differ diff --git a/lib/helpers/env.ex b/lib/helpers/env.ex index 6fc4aed..00e1eb4 100644 --- a/lib/helpers/env.ex +++ b/lib/helpers/env.ex @@ -13,7 +13,7 @@ defmodule Blur.Env do """ def fetch!(key) when is_atom(key) do case fetch(key) do - "" -> raise "Environmetal variable not found" + "" -> raise "Environmetal variable not found." value -> value end end diff --git a/lib/parsers/message.ex b/lib/parsers/message.ex index a346065..8278920 100644 --- a/lib/parsers/message.ex +++ b/lib/parsers/message.ex @@ -4,5 +4,4 @@ defmodule Blur.Parser.Message do * Fix flow """ - require Logger end diff --git a/lib/supervisor.ex b/lib/supervisor.ex deleted file mode 100644 index 900ba89..0000000 --- a/lib/supervisor.ex +++ /dev/null @@ -1,26 +0,0 @@ -defmodule Blur.Supervisor do - @moduledoc """ - Blur App Supervisor - """ - - use Supervisor - require Logger - - def start_link([]) do - Supervisor.start_link(__MODULE__, :ok) - end - - @spec start(atom, list) :: GenServer.on_start() - def start(_type, opts) do - start_link(opts) - end - - @impl Supervisor - def init(:ok) do - children = [ - {Blur.Channels, []} - ] - - Supervisor.init(children, strategy: :one_for_one) - end -end diff --git a/mix.exs b/mix.exs index 8305bb4..304cb0f 100644 --- a/mix.exs +++ b/mix.exs @@ -34,18 +34,15 @@ defmodule Blur.Mixfile do def application do [ - applications: [:logger, :exirc, :con_cache, :httpoison] + applications: [:logger, :exirc], + mod: {Blur.App, ["800807", ["harbleu"]]} ] end defp deps do [ {:exirc, "~> 1.1"}, - {:con_cache, "~> 0.13"}, - {:amnesia, "~> 0.2.0"}, - {:poison, "~> 1.5"}, - {:httpoison, "~> 1.6"}, - {:logger_file_backend, "~> 0.0.11"}, + {:poison, "~> 3.1"}, {:excoveralls, "~> 0.13", only: :test}, {:mix_test_watch, "~> 1.0", only: :dev, runtime: false}, {:ex_doc, "~> 0.22", only: :dev, runtime: false}, diff --git a/mix.lock b/mix.lock index 0d136c9..20b466f 100644 --- a/mix.lock +++ b/mix.lock @@ -29,7 +29,7 @@ "oauth2": {:hex, :oauth2, "0.1.1"}, "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, "plug": {:hex, :plug, "0.13.0"}, - "poison": {:hex, :poison, "1.5.2", "560bdfb7449e3ddd23a096929fb9fc2122f709bcc758b2d5d5a5c7d0ea848910", [:mix], [], "hexpm", "4afc59dcadf71be7edc8b934b39f554ec7b31e2b1b1a4767383a663f86958ce3"}, + "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"}, "rest_twitch": {:git, "git://github.com/rockerBOO/rest_twitch.git", "232b1d00eb74552d0e03d6b497fe86e084739403", []}, "socket": {:hex, :socket, "0.3.13", "98a2ab20ce17f95fb512c5cadddba32b57273e0d2dba2d2e5f976c5969d0c632", [:mix], [], "hexpm", "f82ea9833ef49dde272e6568ab8aac657a636acb4cf44a7de8a935acb8957c2e"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, diff --git a/test/app_test.exs b/test/app_test.exs new file mode 100644 index 0000000..2ef903b --- /dev/null +++ b/test/app_test.exs @@ -0,0 +1,7 @@ +defmodule Blur.AppTest do + # test "that the blur supervisor starts" do + # {:ok, pid} = Blur.App.start(:normal, ["", []]) + + # assert pid + # end +end diff --git a/test/irc/channel_test.exs b/test/irc/channel_test.exs new file mode 100644 index 0000000..997009a --- /dev/null +++ b/test/irc/channel_test.exs @@ -0,0 +1,33 @@ +defmodule Blur.IRC.ChannelTest do + use ExUnit.Case, async: true + doctest Blur.IRC.Channel + + test "on joining the IRC channel" do + irc_client = start_supervised!({ExIRC.Client, []}) + + {:noreply, state} = Blur.IRC.Channel.handle_info({:joined, "rockerboo"}, irc_client) + + assert irc_client === state + end + + test "on someone joining the IRC channel" do + irc_client = start_supervised!({ExIRC.Client, []}) + + {:noreply, state} = + Blur.IRC.Channel.handle_info({:joined, "rockerboo", %ExIRC.SenderInfo{}}, irc_client) + + assert irc_client === state + end + + test "to start and exit IRC channel process" do + irc_client = start_supervised!({ExIRC.Client, []}) + + pid = start_supervised!({Blur.IRC.Channel, [irc_client]}) + + Process.exit(pid, :kill) + + Process.sleep(1) + + refute Process.alive?(pid) + end +end diff --git a/test/irc/connection_test.exs b/test/irc/connection_test.exs new file mode 100644 index 0000000..ff44a46 --- /dev/null +++ b/test/irc/connection_test.exs @@ -0,0 +1,32 @@ +defmodule Blur.IRC.ConnectionTest do + use ExUnit.Case, async: true + doctest Blur.IRC.Connection + + alias Blur.IRC.Connection.State + alias Blur.IRC.Connection + + # test "on connecting to the IRC server" do + # {:noreply, state} = + # Connection.handle_info( + # {:connected, "localhost", 6667}, + # %State{} + # ) + + # assert state === %State{} + # end + + describe "on disconnecting from the channel" do + test "regular IRC" do + {:noreply, state} = Connection.handle_info({:disconnected}, %State{}) + + assert state === %State{} + end + + test "Twitch Tags" do + {:noreply, state} = + Connection.handle_info({:disconnected, "@display-name=rockerBOO"}, %State{}) + + assert state === %State{} + end + end +end diff --git a/test/irc/irc_test.exs b/test/irc/irc_test.exs new file mode 100644 index 0000000..28f0cb6 --- /dev/null +++ b/test/irc/irc_test.exs @@ -0,0 +1,4 @@ +defmodule Blur.IRCTest do + use ExUnit.Case, async: true + doctest Blur.IRC +end diff --git a/test/irc/login_test.exs b/test/irc/login_test.exs new file mode 100644 index 0000000..dc059bc --- /dev/null +++ b/test/irc/login_test.exs @@ -0,0 +1,16 @@ +defmodule Blur.IRC.LoginTest do + use ExUnit.Case, async: true + doctest Blur.IRC.Login + + test "on logged in" do + irc_client = start_supervised!({ExIRC.Client, []}) + + {:noreply, _} = Blur.IRC.Login.handle_info(:logged_in, %{client: irc_client, autojoin: []}) + end + + test "to autojoin channels in the config" do + irc_client = start_supervised!({ExIRC.Client, []}) + + :ok = Blur.IRC.Login.autojoin(irc_client, ["rockerboo"]) + end +end diff --git a/test/irc/message_test.exs b/test/irc/message_test.exs new file mode 100644 index 0000000..1e84bd2 --- /dev/null +++ b/test/irc/message_test.exs @@ -0,0 +1,73 @@ +defmodule Blur.IRC.MessageTest do + use ExUnit.Case, async: true + doctest Blur.IRC.Message + + alias Blur.IRC.Message + + describe "on receiving an IRC message" do + test "for private message" do + irc_client = start_supervised({ExIRC.Client, []}) + + {:noreply, state} = + Message.handle_info( + {:received, "Private message", %ExIRC.SenderInfo{nick: "rockerboo"}}, + irc_client + ) + + assert state === irc_client + end + + test "for message to channel" do + irc_client = start_supervised({ExIRC.Client, []}) + + {:noreply, state} = + Message.handle_info( + {:received, "Private message", %ExIRC.SenderInfo{}, "rockerboo"}, + irc_client + ) + + assert state === irc_client + end + end + + describe "on unrecognized " do + test "for code 366" do + irc_client = start_supervised({ExIRC.Client, []}) + + {:noreply, state} = + Message.handle_info( + {:unrecognized, "366", %ExIRC.SenderInfo{}}, + irc_client + ) + + assert state === irc_client + end + + test "for code CAP" do + irc_client = start_supervised({ExIRC.Client, []}) + + {:noreply, state} = + Message.handle_info( + {:unrecognized, "CAP", %ExIRC.SenderInfo{}}, + irc_client + ) + + assert state === irc_client + end + + test "for Twitch tags" do + irc_client = start_supervised({ExIRC.Client, []}) + + {:noreply, state} = + Message.handle_info( + {:unrecognized, "display-name=rockerBOO connection cmd channel msg", + %ExIRC.Message{ + args: ["@display-name=rockerBOO;color=434554; connection cmd channel msg"] + }}, + irc_client + ) + + assert state === irc_client + end + end +end diff --git a/test/supervisor_test.exs b/test/supervisor_test.exs deleted file mode 100644 index 03809a1..0000000 --- a/test/supervisor_test.exs +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Blur.SupervisorTest do - use ExUnit.Case, async: true - - doctest Blur.Supervisor - - test "that the blur supervisor starts" do - {:ok, pid} = Blur.Supervisor.start(:normal, []) - - assert pid - end -end