#!/usr/bin/env zunit

@setup {
    load "../you-should-use.plugin.zsh"
    unset YSU_MESSAGE_POSITION
    _YSU_BUFFER=""
}

@teardown {
    rm -f output.txt
}

# We work around not being able to use `run` AND test variable values by redirecting
# all output to a temporary file from which we can read.

@test 'ysu - _write_ysu_buffer before' {
    YSU_MESSAGE_POSITION="before"
    export _YSU_BUFFER

    _write_ysu_buffer "hello world" 2> output.txt

    assert $state equals 0

    assert "$(< output.txt)" same_as "hello world"
    assert "$_YSU_BUFFER" is_empty
}

@test 'ysu - _write_ysu_buffer after' {
    YSU_MESSAGE_POSITION="after"
    export _YSU_BUFFER

    _write_ysu_buffer "hello world" 2> output.txt

    assert $state equals 0

    assert "$(< output.txt)" is_empty
    assert "$_YSU_BUFFER" same_as "hello world"
}

@test 'ysu - _write_ysu_buffer invalid' {
    YSU_MESSAGE_POSITION="invalid"
    export _YSU_BUFFER

    _write_ysu_buffer "" 2> output.txt

    assert $state equals 0

    expected="$(tput setaf 1)$(tput bold)Unknown value for YSU_MESSAGE_POSITION 'invalid'. "
    expected+="Expected value 'before' or 'after'$(tput sgr0)\n"

    assert "$(< output.txt)" same_as "$expected"
    assert "$_YSU_BUFFER" is_empty
}