#!/bin/bash
cd $(dirname "$0")

source util.sh
PATH=".." source irc.sh

# So we don't have to specify them every time, let's just set these variables
# now. We can override them later if we need to.
export IRC_BOTID="shazbot" IRC_CHANNEL="#channel" IRC_SENDER="sender"

# irc_respond takes a message on stdin and sends it as a NOTICE.
output=$(echo "hello" | irc_respond)
assert_equal "$output" "NOTICE #channel :hello"

# irc_respond can respond to any channel.
output=$(echo "hello" | IRC_CHANNEL="#otherchannel" irc_respond)
assert_equal "$output" "NOTICE #otherchannel :hello"

# irc_respond sends responses as PRIVMSG if -m is given.
output=$(echo "hello" | irc_respond -m)
assert_equal "$output" "PRIVMSG #channel :hello"

# irc_respond wraps the message onto multiple lines if necessary.
big_payload=$(make_big_payload "1234567 ")
output=$(echo "$big_payload" | irc_respond)
assert_equal "$(echo "$output" | wc -l)" 2
assert_equal "$(echo "$output" | tail -1)" \
        "NOTICE #channel :1234567 1234567 1234567 1234567"

# If the message is five or more lines long, and $IRC_CHANNEL is different
# from $IRC_SENDER, then send the whole message directly to the sender, and
# a brief notice to the channel.
very_big_payload="$big_payload$big_payload$big_payload$big_payload"
output=$(echo "$very_big_payload" | irc_respond)
assert_equal $(echo "$output" | grep "^NOTICE $IRC_CHANNEL" | wc -l) 1
assert_equal $(echo "$output" | grep "^NOTICE $IRC_SENDER" | wc -l) 5

# If the message is five or more lines long, and $IRC_CHANNEL is the same as
# $IRC_SENDER, just send the message - don't bother with a notification.
output=$(echo "$very_big_payload" | IRC_CHANNEL=$IRC_SENDER irc_respond)
# The same number of lines are sent to the sender.
assert_equal $(echo "$output" | grep "^NOTICE $IRC_SENDER" | wc -l) 5
# No additinal lines are sent.
assert_equal $(echo "$output" | wc -l) 5
