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

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

# irc_msg defaults to NOTICE responses.
output=$(IRC_BOTID=shazbot irc_msg "#test" "hello")
assert_equal $? 0
assert_equal "$output" "NOTICE #test :hello"

# irc_msg switches to PRIVMSG if the first argument is "-m"
output=$(IRC_BOTID=shazbot irc_msg -m "#test" "hello")
assert_equal $? 0
assert_equal "$output" "PRIVMSG #test :hello"

# Make a payload that would cause the resulting IRC message to overflow the
# 512-byte limit.
longmsg=$(make_big_payload "1234567 ")

# irc_msg should wrap the message onto to a second line, let's check what's in
# it.  We expect 512 - len("shazbot") - len("NOTICE #test") - CRLF - 2 spaces
# - 1 colon = 488 chars. That's 61 repetitions of our string, leaving 3 on the
# second line.
output=$(IRC_BOTID=shazbot irc_msg "#test" "$longmsg" | tail -1)
assert_equal "$output" "NOTICE #test :1234567 1234567 1234567"

# If we have a longer nick, we would expect more words to wrap to the next line.
output=$(IRC_BOTID=longnickname irc_msg "#test" "$longmsg" | tail -1)
assert_equal "$output" "NOTICE #test :1234567 1234567 1234567 1234567"

# If we have a longer channel, we would expect more words to wrap to the next line.
output=$(IRC_BOTID=shazbot irc_msg "#longchannel" "$longmsg" | tail -1)
assert_equal "$output" "NOTICE #longchannel :1234567 1234567 1234567 1234567"

# If we use PRIVMSG rather than NOTICE, we would expect more words to wrap to the next line.
output=$(IRC_BOTID=shazbot irc_msg -m "#test" "$longmsg" | tail -1)
assert_equal "$output" "PRIVMSG #test :1234567 1234567 1234567 1234567"
