require 'test/unit'
require './K5Bot'
require 'fileutils'

class TestMyBot < Test::Unit::TestCase

  def setup
    if File.exist?('K5agent.rb')
      FileUtils.copy('K5agent.rb', 'K5agent.rb.save')
      begin 
        FileUtils.copy('K5agent-api.yaml', 'K5agent-api.yaml.save') 
      rescue; end
    end
    @agent = MyBot.new
  end

  def test_name
    actual, s = @agent.send("what is your name?")
    assert_match(/K5bot/, actual)
  end

  def test_volume
    actual, s = @agent.send("K5Bot: what is your volume?")
    assert_match(/1/, actual)
  end

  def test_rules
    actual, s = @agent.send("K5Bot: loud")
    assert_match(/0/, actual)

    actual, s = @agent.send("K5Bot: if response =~ /default response/i then response = 'foo!' end")
    assert_match(/Okay/, actual)

    actual, s = @agent.send("testing")
    assert_match(/foo!/, actual)

    actual, s = @agent.send("K5Bot: if response =~ /foo!/ then response = 'bar' end")
    assert_match(/Okay/, actual)

    actual, s = @agent.send("testing")
    assert_match(/bar/, actual)

    actual, s = @agent.send("K5Bot: delete rule 1")
    assert_match(/Okay/, actual)

    actual, s = @agent.send("testing")
    assert_match(/default response/i, actual)

    actual, s = @agent.send("K5Bot: if response =~ /default response/i then response = 'fubar' end")
    assert_match(/Okay/, actual)

    actual, s = @agent.send("testing")
    assert_match(/fubar/, actual)

    actual, s = @agent.send("K5Bot: delete all rules")
    assert_match(/Okay/, actual)

    actual, s = @agent.send("K5Bot: show rules")
    assert_match(//, actual)

    actual, s = @agent.send("testing")
    assert_match(/Default response/, actual)

    actual, s = @agent.send("what is the debug mode?")
    assert_match(/off/i, actual)

    actual, s = @agent.send("K5bot: turn debug mode on")
    assert_match(/on/i, actual)

    actual, s = @agent.send("K5bot: what is the debug mode?")
    assert_match(/on/i, actual)

    actual, s = @agent.send("K5bot: please turn off the debug mode.")
    assert_match(/off/i, actual)

    actual, s = @agent.send("K5bot: what is the debug mode?")
    assert_match(/off/i, actual)
  end

  def teardown
    File.delete('K5agent.rb') unless !File.exist?('K5agent.rb.save')
    begin; File.delete('K5agent-api.yaml'); rescue; end
    if File.exist?('K5agent.rb.save')
      FileUtils.copy('K5agent.rb.save', 'K5agent.rb')
      begin; FileUtils.copy('K5agent-api.yaml.save', 'K5agent-api.yaml'); rescue; end
    end
    if File.exist?('K5agent.rb') then File.delete('K5agent.rb.save') end
    if File.exist?('K5agent-api.yaml.save') then File.delete('K5agent-api.yaml.save') end
  end

end
