|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +ENV['RACK_ENV'] = 'test' |
| 4 | +require 'test/unit' |
| 5 | +require 'rack/test' |
| 6 | + |
| 7 | +require 'cgi' |
| 8 | + |
| 9 | +require 'bcdice_api' |
| 10 | + |
| 11 | +class V2APITest < Test::Unit::TestCase |
| 12 | + include Rack::Test::Methods |
| 13 | + |
| 14 | + def app |
| 15 | + BCDiceAPI::App |
| 16 | + end |
| 17 | + |
| 18 | + def test_version |
| 19 | + get '/v2/version' |
| 20 | + json = JSON.parse(last_response.body) |
| 21 | + |
| 22 | + assert last_response.ok? |
| 23 | + assert_equal '*', last_response.headers['Access-Control-Allow-Origin'] |
| 24 | + assert json.key?('bcdice') |
| 25 | + assert json.key?('api') |
| 26 | + end |
| 27 | + |
| 28 | + def test_game_system_list |
| 29 | + get '/v2/game_system' |
| 30 | + json = JSON.parse(last_response.body) |
| 31 | + |
| 32 | + assert last_response.ok? |
| 33 | + assert_false json['game_system'].empty? |
| 34 | + assert_instance_of Array, json['game_system'] |
| 35 | + |
| 36 | + first = json['game_system'].first |
| 37 | + assert first.key?('name') |
| 38 | + assert first.key?('id') |
| 39 | + assert first.key?('sort_key') |
| 40 | + end |
| 41 | + |
| 42 | + def test_game_system_info |
| 43 | + get '/v2/game_system/DiceBot' |
| 44 | + json = JSON.parse(last_response.body) |
| 45 | + |
| 46 | + assert last_response.ok? |
| 47 | + assert json['ok'] |
| 48 | + assert_equal json['id'], 'DiceBot' |
| 49 | + assert_instance_of String, json['name'] |
| 50 | + assert_instance_of String, json['command_pattern'] |
| 51 | + assert_instance_of String, json['sort_key'] |
| 52 | + assert_instance_of String, json['help_message'] |
| 53 | + assert_false json['name'].empty? |
| 54 | + assert_false json['command_pattern'].empty? |
| 55 | + assert_false json['sort_key'].empty? |
| 56 | + |
| 57 | + pend 'DiceBot::HELP_MESSAGE will be supported in BCDice v3' |
| 58 | + assert_false json['help_message'].empty? |
| 59 | + end |
| 60 | + |
| 61 | + def test_diceroll |
| 62 | + get '/v2/game_system/DiceBot/roll?command=1d100<=70' |
| 63 | + |
| 64 | + json = JSON.parse(last_response.body) |
| 65 | + |
| 66 | + assert last_response.ok? |
| 67 | + assert json['ok'] |
| 68 | + assert json['text'] |
| 69 | + assert_false json['secret'] |
| 70 | + assert_boolean json['success'] |
| 71 | + assert_boolean json['failure'] |
| 72 | + assert_false json['critical'] |
| 73 | + assert_false json['fumble'] |
| 74 | + assert_instance_of Array, json['rands'] |
| 75 | + end |
| 76 | + |
| 77 | + def test_diceroll_with_post |
| 78 | + post '/v2/game_system/DiceBot/roll', { command: '1d100<=70' } |
| 79 | + |
| 80 | + json = JSON.parse(last_response.body) |
| 81 | + |
| 82 | + assert last_response.ok? |
| 83 | + assert json['ok'] |
| 84 | + assert json['text'] |
| 85 | + assert_false json['secret'] |
| 86 | + assert_boolean json['success'] |
| 87 | + assert_boolean json['failure'] |
| 88 | + assert_false json['critical'] |
| 89 | + assert_false json['fumble'] |
| 90 | + assert_instance_of Array, json['rands'] |
| 91 | + end |
| 92 | + |
| 93 | + def test_detailed |
| 94 | + get '/v2/game_system/Cthulhu7th/roll?command=CC1' |
| 95 | + |
| 96 | + json = JSON.parse(last_response.body) |
| 97 | + |
| 98 | + assert last_response.ok? |
| 99 | + assert json['ok'] |
| 100 | + assert_not_empty(json['rands'].select { |r| r['kind'] == 'tens_d10' }) |
| 101 | + end |
| 102 | + |
| 103 | + def test_unexpected_game_system |
| 104 | + get '/v2/game_system/Hoge/roll?command=1d100<=70' |
| 105 | + |
| 106 | + json = JSON.parse(last_response.body) |
| 107 | + |
| 108 | + assert last_response.bad_request? |
| 109 | + assert_false json['ok'] |
| 110 | + assert_equal json['reason'], 'unsupported game system' |
| 111 | + end |
| 112 | + |
| 113 | + def test_unexpected_command |
| 114 | + get '/v2/game_system/DiceBot/roll?command=a' |
| 115 | + |
| 116 | + json = JSON.parse(last_response.body) |
| 117 | + |
| 118 | + assert last_response.bad_request? |
| 119 | + assert_false json['ok'] |
| 120 | + assert_equal json['reason'], 'unsupported command' |
| 121 | + end |
| 122 | + |
| 123 | + def test_no_command |
| 124 | + get '/v2/game_system/DiceBot/roll' |
| 125 | + |
| 126 | + json = JSON.parse(last_response.body) |
| 127 | + |
| 128 | + assert last_response.bad_request? |
| 129 | + assert_false json['ok'] |
| 130 | + assert_equal json['reason'], 'unsupported command' |
| 131 | + end |
| 132 | + |
| 133 | + def test_blank_command |
| 134 | + get '/v2/game_system/DiceBot/roll?command=' |
| 135 | + |
| 136 | + json = JSON.parse(last_response.body) |
| 137 | + |
| 138 | + assert last_response.bad_request? |
| 139 | + assert_false json['ok'] |
| 140 | + assert_equal json['reason'], 'unsupported command' |
| 141 | + end |
| 142 | + |
| 143 | + def test_original_table |
| 144 | + table_text = <<~TABLE |
| 145 | + 飲み物表 |
| 146 | + 1D6 |
| 147 | + 1:水 |
| 148 | + 2:緑茶 |
| 149 | + 3:麦茶 |
| 150 | + 4:コーラ |
| 151 | + 5:オレンジジュース |
| 152 | + 6:選ばれし者の知的飲料 |
| 153 | + TABLE |
| 154 | + |
| 155 | + post '/v2/original_table', { table: table_text } |
| 156 | + |
| 157 | + json = JSON.parse(last_response.body) |
| 158 | + |
| 159 | + assert last_response.ok? |
| 160 | + assert_true json['ok'] |
| 161 | + assert_instance_of String, json['text'] |
| 162 | + assert_true json['text'].start_with?('飲み物表(') |
| 163 | + assert_instance_of Array, json['rands'] |
| 164 | + end |
| 165 | +end |
0 commit comments