|
1 | 1 | require "test_helper" |
| 2 | +require "support/cache_helper" |
2 | 3 |
|
3 | 4 | class Telegram::ChatRelayTest < ActiveSupport::TestCase |
4 | 5 | include ActiveJob::TestHelper |
| 6 | + include CacheHelper |
5 | 7 |
|
6 | 8 | setup do |
7 | 9 | @court = Court.create!(name: "Relay Court", city_name: "Yekaterinburg") |
@@ -53,6 +55,174 @@ class Telegram::ChatRelayTest < ActiveSupport::TestCase |
53 | 55 | assert_not params.key?("parse_mode") |
54 | 56 | end |
55 | 57 |
|
| 58 | + # Ответ в том же окне — первое, что делает получатель. Раньше он пропадал: |
| 59 | + # у человека не было указателя, и Relay не знал, в какую игру его отдать. |
| 60 | + test "delivery turns the chat mode on for a recipient who has none" do |
| 61 | + params = nil |
| 62 | + |
| 63 | + with_memory_cache do |
| 64 | + stub_singleton(Telegram::Api, :post, ->(_path, sent) { params = sent; { "ok" => true } }) do |
| 65 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 66 | + end |
| 67 | + |
| 68 | + assert_equal @game.id, Telegram::Chat::Session.active_game(@owner.telegram_chat_id, @owner).id |
| 69 | + end |
| 70 | + |
| 71 | + buttons = JSON.parse(params["reply_markup"])["inline_keyboard"].first |
| 72 | + assert_equal [ "chat:pick", "chat:exit" ], buttons.map { |button| button["callback_data"] } |
| 73 | + end |
| 74 | + |
| 75 | + test "delivery leaves an existing chat choice alone" do |
| 76 | + other = Game.create!(court: @court, user: @owner, date: Date.current, kind: "game") |
| 77 | + params = nil |
| 78 | + |
| 79 | + with_memory_cache do |
| 80 | + Telegram::Chat::Session.start(@owner.telegram_chat_id, other) |
| 81 | + |
| 82 | + stub_singleton(Telegram::Api, :post, ->(_path, sent) { params = sent; { "ok" => true } }) do |
| 83 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 84 | + end |
| 85 | + |
| 86 | + assert_equal other.id, Telegram::Chat::Session.game_id(@owner.telegram_chat_id) |
| 87 | + end |
| 88 | + |
| 89 | + assert_not params.key?("reply_markup") |
| 90 | + ensure |
| 91 | + other&.destroy |
| 92 | + end |
| 93 | + |
| 94 | + test "a later automatic delivery becomes the reply target" do |
| 95 | + other = Game.create!(court: @court, user: @owner, date: Date.current, kind: "game") |
| 96 | + second_delivery = nil |
| 97 | + |
| 98 | + with_memory_cache do |
| 99 | + stub_singleton(Telegram::Api, :post, ->(*) { { "ok" => true } }) do |
| 100 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "из первой игры") |
| 101 | + end |
| 102 | + |
| 103 | + stub_singleton(Telegram::Api, :post, ->(_path, sent) { second_delivery = sent; { "ok" => true } }) do |
| 104 | + Telegram::DeliverChatMessageJob.perform_now(other.id, @owner.id, "из второй игры") |
| 105 | + end |
| 106 | + |
| 107 | + assert_equal other.id, Telegram::Chat::Session.active_game(@owner.telegram_chat_id, @owner).id |
| 108 | + end |
| 109 | + |
| 110 | + assert second_delivery.key?("reply_markup") |
| 111 | + ensure |
| 112 | + other&.destroy |
| 113 | + end |
| 114 | + |
| 115 | + test "a failed delivery leaves no chat mode behind" do |
| 116 | + # 403 — постоянная ошибка: сообщение с кнопками до человека не дошло, и |
| 117 | + # оказаться в чате втихаря он не должен. |
| 118 | + with_memory_cache do |
| 119 | + stub_singleton(Telegram::Api, :post, ->(*) { { "ok" => false, "error_code" => 403, "description" => "Forbidden" } }) do |
| 120 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 121 | + end |
| 122 | + |
| 123 | + assert_nil Telegram::Chat::Session.game_id(@owner.telegram_chat_id) |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + test "a throttled delivery arms nothing and sends the buttons again on the retry" do |
| 128 | + throttled = { "ok" => false, "error_code" => 429, "parameters" => { "retry_after" => 7 } } |
| 129 | + later = Class.new { def perform_later(*) = true }.new |
| 130 | + second_attempt = nil |
| 131 | + |
| 132 | + with_memory_cache do |
| 133 | + stub_singleton(Telegram::Api, :post, ->(*) { throttled }) do |
| 134 | + stub_singleton(Telegram::DeliverChatMessageJob, :set, ->(wait:) { later }) do |
| 135 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + assert_nil Telegram::Chat::Session.game_id(@owner.telegram_chat_id) |
| 140 | + |
| 141 | + stub_singleton(Telegram::Api, :post, ->(_path, sent) { second_attempt = sent; { "ok" => true } }) do |
| 142 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 143 | + end |
| 144 | + |
| 145 | + assert_equal @game.id, Telegram::Chat::Session.game_id(@owner.telegram_chat_id) |
| 146 | + end |
| 147 | + |
| 148 | + assert second_attempt.key?("reply_markup") |
| 149 | + end |
| 150 | + |
| 151 | + test "a retried server error leaves no chat mode behind" do |
| 152 | + with_memory_cache do |
| 153 | + assert_enqueued_jobs 1, only: Telegram::DeliverChatMessageJob do |
| 154 | + stub_singleton(Telegram::Api, :post, ->(*) { { "ok" => false, "error_code" => 503 } }) do |
| 155 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + assert_nil Telegram::Chat::Session.game_id(@owner.telegram_chat_id) |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + # Указатель на игру, которой больше нет, — мусор: он не должен навсегда |
| 164 | + # запирать включение чата для живой игры. |
| 165 | + test "a stale pointer does not block arming the chat mode" do |
| 166 | + other = Game.create!(court: @court, user: @owner, date: Date.current, kind: "game") |
| 167 | + params = nil |
| 168 | + |
| 169 | + with_memory_cache do |
| 170 | + Telegram::Chat::Session.start(@owner.telegram_chat_id, other) |
| 171 | + other.destroy |
| 172 | + |
| 173 | + stub_singleton(Telegram::Api, :post, ->(_path, sent) { params = sent; { "ok" => true } }) do |
| 174 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 175 | + end |
| 176 | + |
| 177 | + assert_equal @game.id, Telegram::Chat::Session.active_game(@owner.telegram_chat_id, @owner).id |
| 178 | + end |
| 179 | + |
| 180 | + assert params.key?("reply_markup") |
| 181 | + end |
| 182 | + |
| 183 | + # Отправка не мгновенна: пока она идёт, человек мог сам открыть другую игру. |
| 184 | + test "a choice made while the message was in flight is not overwritten" do |
| 185 | + other = Game.create!(court: @court, user: @owner, date: Date.current, kind: "game") |
| 186 | + |
| 187 | + with_memory_cache do |
| 188 | + posting = lambda do |*| |
| 189 | + Telegram::Chat::Session.start(@owner.telegram_chat_id, other) |
| 190 | + { "ok" => true } |
| 191 | + end |
| 192 | + |
| 193 | + stub_singleton(Telegram::Api, :post, posting) do |
| 194 | + Telegram::DeliverChatMessageJob.perform_now(@game.id, @owner.id, "во сколько?") |
| 195 | + end |
| 196 | + |
| 197 | + assert_equal other.id, Telegram::Chat::Session.game_id(@owner.telegram_chat_id) |
| 198 | + end |
| 199 | + ensure |
| 200 | + other&.destroy |
| 201 | + end |
| 202 | + |
| 203 | + test "stale validation does not delete a concurrent explicit choice" do |
| 204 | + stale = Game.create!(court: @court, user: @owner, date: Date.current, kind: "game") |
| 205 | + fresh = Game.create!(court: @court, user: @owner, date: Date.current, kind: "game") |
| 206 | + |
| 207 | + with_memory_cache do |
| 208 | + Telegram::Chat::Session.start(@owner.telegram_chat_id, stale) |
| 209 | + stale.destroy |
| 210 | + |
| 211 | + lookup = lambda do |**| |
| 212 | + Telegram::Chat::Session.start(@owner.telegram_chat_id, fresh) |
| 213 | + nil |
| 214 | + end |
| 215 | + stub_singleton(Game, :find_by, lookup) do |
| 216 | + assert_nil Telegram::Chat::Session.active_game(@owner.telegram_chat_id, @owner) |
| 217 | + end |
| 218 | + |
| 219 | + assert_equal fresh.id, Telegram::Chat::Session.game_id(@owner.telegram_chat_id) |
| 220 | + end |
| 221 | + ensure |
| 222 | + fresh&.destroy |
| 223 | + stale&.destroy |
| 224 | + end |
| 225 | + |
56 | 226 | test "delivery re-checks membership right before sending" do |
57 | 227 | @participation.destroy |
58 | 228 |
|
|
0 commit comments