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

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

# _wrapwidth prints the bytes available for a message payload.
output=$(IRC_BOTID=botnick _wrapwidth "prefix" "suffix")
assert_equal $? 0
# Each IRC line will wind up looking like this:
# "$IRC_BOTID $prefix$content$suffix\r\n"
# ..so from the 512-byte limit, we should have 
# 512 - len("botnick") - 1 - len("prefix") - len("suffix") - 2 bytes left.
assert_equal "$output" 490

# A longer IRC_BOTID means less space left over.
output=$(IRC_BOTID=bigbotnick _wrapwidth "prefix" "suffix")
assert_equal $? 0
assert_equal "$output" 487

# A longer prefix means less space left over.
output=$(IRC_BOTID=botnick _wrapwidth "big prefix" "suffix")
assert_equal $? 0
assert_equal "$output" 486

# A longer suffix means less space left over.
output=$(IRC_BOTID=botnick _wrapwidth "prefix" "big suffix")
assert_equal $? 0
assert_equal "$output" 486

# _wrapwidth handles an empty suffix.
output=$(IRC_BOTID=botnick _wrapwidth "prefix" "")
assert_equal $? 0
assert_equal "$output" 496

# _wrapmsg wraps its input over multiple lines.
payload=$(make_big_payload "1234567 ")
lines=$(echo "$payload" | IRC_BOTID=botnick _wrapmsg "prefix" "suffix" | wc -l)
assert_equal "$lines" 2

# _wrapmsg sticks the prefix and suffix at the beginning and end (respectively)
# of each line.
echo "$payload" | IRC_BOTID=botnick _wrapmsg "prefix" "suffix" |
        while read line; do
                assert_equal "${line:  0:6}" "prefix"
                assert_equal "${line: -6:6}" "suffix"
        done

# _wrapmsg only wraps the bits that can't posssibly fit in the previous line.
output=$(echo "$payload" |
        IRC_BOTID=botnick _wrapmsg "prefix" "suffix" |
        tail -1)
# Given this prefix and suffix, there should be 490 bytes available, which
# should fit 61 repetitions of "1234567 ". $payload has 64 such repetitions,
# therefore 3 should wrap to the second line.
assert_equal "$output" "prefix1234567 1234567 1234567suffix"

# _chopmsg truncates its input to fit in a single line.
output=$(echo "$payload" | IRC_BOTID=botnick _chopmsg "prefix" "suffix")
assert_equal ${#output} 499
assert_equal $(echo "$output" | wc -l) 1
