diff --git a/server.rb b/server.rb index aece287..506d418 100644 --- a/server.rb +++ b/server.rb @@ -32,6 +32,10 @@ def diceroll(system, command) result, secret = bcdice.dice_command dices = bcdice.getRandResults.map {|dice| {faces: dice[1], value: dice[0]}} + if result.nil? + raise CommandError + end + return result, secret, dices end end @@ -59,9 +63,6 @@ def diceroll(system, command) get "/v1/diceroll" do result, secret, dices = diceroll(params[:system], params[:command]) - if result.nil? - raise CommandError - end jsonp ok: true, result: result, secret: secret, dices: dices end @@ -71,12 +72,11 @@ def diceroll(system, command) return BCDice::SYSTEMS.join("\n") end - result, secret, dices = diceroll(params[:sys] || "DiceBot", params[:text]) - - if result.nil? - "error" - else + begin + result, secret, dices = diceroll(params[:sys] || "DiceBot", params[:text]) "onset" + result + rescue UnsupportedDicebot, CommandError + "error" end end diff --git a/test/test_api.rb b/test/test_api.rb index 10b371a..a45d510 100644 --- a/test/test_api.rb +++ b/test/test_api.rb @@ -111,6 +111,36 @@ def test_no_command assert_equal json["reason"], "unsupported command" end + def test_onset_list + get "/v1/onset?list=1" + + list = last_response.body.split("\n") + + assert last_response.ok? + assert_include list, "Amadeus" + end + + def test_onset_diceroll + get "/v1/onset?sys=Cthulhu&text=1d20" + + assert last_response.ok? + assert last_response.body.start_with?("onset: (1D20)") + end + + def test_onset_unexpected_dicebot + get "/v1/onset?sys=AwesomeDicebot&text=1d20" + + assert last_response.ok? + assert_equal last_response.body, "error" + end + + def test_onset_unexpected_command + get "/v1/onset?sys=DiceBot&text=a" + + assert last_response.ok? + assert_equal last_response.body, "error" + end + def test_not_found get "/hogehoge" json = JSON.parse(last_response.body)