@@ -404,4 +404,55 @@ class GameTest < ActiveSupport::TestCase
404404 ensure
405405 game &.destroy
406406 end
407+
408+ test "chat stays open until the weekly reset, not for a fixed day" do
409+ game = Game . create! ( court : courts ( :one ) , user : users ( :one ) , date : Date . new ( 2026 , 9 , 3 ) , kind : "game" )
410+
411+ # Среда: игра в четверг, разберёт её состав ближайшая суббота.
412+ travel_to Time . zone . local ( 2026 , 9 , 2 , 21 , 0 ) do
413+ assert_equal Time . zone . local ( 2026 , 9 , 5 , 4 , 0 ) , Game . next_weekly_reset_at
414+ assert_equal Time . zone . local ( 2026 , 9 , 5 , 4 , 0 ) , game . chat_open_until
415+ assert game . chat_open?
416+ end
417+ ensure
418+ game &.destroy
419+ end
420+
421+ # Ближайшая суббота ничего не делает с составом игры, назначенной после неё:
422+ # чистка сносит только прошедшие разовые игры, сброс — только отыгранные серии.
423+ test "a game scheduled beyond the coming reset keeps its chat until its own" do
424+ one_off = Game . create! ( court : courts ( :one ) , user : users ( :one ) , date : Date . new ( 2026 , 9 , 20 ) , kind : "game" )
425+ # Серия с ближайшим вхождением в воскресенье — уже за субботней границей.
426+ series = Game . create! ( court : courts ( :one ) , user : users ( :one ) , date : Date . new ( 2026 , 9 , 6 ) , recurring : true , kind : "game" )
427+
428+ travel_to Time . zone . local ( 2026 , 9 , 2 , 21 , 0 ) do
429+ assert_equal Time . zone . local ( 2026 , 9 , 26 , 4 , 0 ) , one_off . chat_open_until
430+ assert_equal Time . zone . local ( 2026 , 9 , 12 , 4 , 0 ) , series . chat_open_until
431+ assert one_off . chat_open?
432+ assert series . chat_open?
433+ end
434+ ensure
435+ [ one_off , series ] . compact . each ( &:destroy )
436+ end
437+
438+ test "chat closes once the cleanup that removes the game has passed" do
439+ game = Game . create! ( court : courts ( :one ) , user : users ( :one ) , date : Date . new ( 2026 , 8 , 25 ) , kind : "game" )
440+
441+ travel_to Time . zone . local ( 2026 , 9 , 2 , 21 , 0 ) do
442+ assert_nil game . chat_open_until
443+ assert_not game . chat_open?
444+ end
445+ ensure
446+ game &.destroy
447+ end
448+
449+ test "the chat window rolls over to the next week right at the reset" do
450+ travel_to Time . zone . local ( 2026 , 9 , 5 , 3 , 59 ) do
451+ assert_equal Time . zone . local ( 2026 , 9 , 5 , 4 , 0 ) , Game . next_weekly_reset_at
452+ end
453+
454+ travel_to Time . zone . local ( 2026 , 9 , 5 , 4 , 0 ) do
455+ assert_equal Time . zone . local ( 2026 , 9 , 12 , 4 , 0 ) , Game . next_weekly_reset_at
456+ end
457+ end
407458end
0 commit comments