掲示板実装
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled

This commit is contained in:
2026-02-19 22:30:59 +09:00
parent f25fd6f802
commit bb9ec2df1d
38 changed files with 1711 additions and 13 deletions

View File

@@ -0,0 +1,79 @@
require "test_helper"
class BoardsControllerTest < ActionDispatch::IntegrationTest
setup do
@game = games(:one)
# fixtureのgames(:one)はtitleだけの状態から修正されている前提だが、
# 念のためupdateして整合性を取る
@game.update!(
status: "in_progress",
participants_count: 7,
victory_sc_count: 18,
scoring_system: "none",
turn_schedule: "0,12"
)
@user = users(:austria)
@user_germany = users(:germany)
# 参加者作成
# BoardTest同様、fixtureに依存せずParticipantを作る
@p_austria = Participant.create!(game: @game, user: @user, power: "AUSTRIA")
@p_germany = Participant.create!(game: @game, user: @user_germany, power: "GERMANY")
# 共通掲示板作成
@game.boards.create!(board_type: "global", is_public: true)
end
test "should get index" do
login_as(@user)
get game_boards_url(@game)
assert_response :success
assert_select "h2", "外交・交渉掲示板"
end
test "should get new negotiation board" do
login_as(@user)
get new_game_board_url(@game)
assert_response :success
end
test "should create negotiation board" do
login_as(@user)
assert_difference("Board.count") do
post game_boards_url(@game), params: {
invited_participant_ids: [ @p_germany.id ]
}
end
assert_redirected_to game_board_url(@game, Board.last)
assert Board.last.member?(@p_austria)
assert Board.last.member?(@p_germany)
end
test "should show board" do
login_as(@user)
board = @game.boards.global.first
get game_board_url(@game, board)
assert_response :success
end
test "should not show board to non-member" do
# 第三者作成
user_russia = users(:russia)
p_russia = Participant.create!(game: @game, user: user_russia, power: "RUSSIA")
# オーストリアとドイツの掲示板
board = @game.boards.create!(board_type: "negotiation", created_by_participant_id: @p_austria.id)
board.board_memberships.create!(participant: @p_austria, joined_at: Time.current)
board.board_memberships.create!(participant: @p_germany, joined_at: Time.current)
# ロシアでログイン
login_as(user_russia)
get game_board_url(@game, board)
assert_redirected_to game_boards_url(@game)
follow_redirect!
assert_select "div[role='alert']", /権限がありません/
end
end

View File

@@ -1,11 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
participants_count: 1
memo: MyText
title: GameOne
status: in_progress
turn_schedule: "0,12"
participants_count: 7
two:
title: MyString
title: GameTwo
status: recruiting
turn_schedule: "0,12"
participants_count: 1
memo: MyText

View File

@@ -9,3 +9,21 @@ two:
email: two@example.com
password_digest: <%= BCrypt::Password.create('password') %>
admin: false
austria:
username: AustriaUser
email: austria@example.com
password_digest: <%= BCrypt::Password.create('password') %>
admin: false
germany:
username: GermanyUser
email: germany@example.com
password_digest: <%= BCrypt::Password.create('password') %>
admin: false
russia:
username: RussiaUser
email: russia@example.com
password_digest: <%= BCrypt::Password.create('password') %>
admin: false

85
test/models/board_test.rb Normal file
View File

@@ -0,0 +1,85 @@
require "test_helper"
class BoardTest < ActiveSupport::TestCase
def setup
@game = games(:one) # fixturesから取得
@austria = users(:austria)
@germany = users(:germany)
@russia = users(:russia)
# 参加者作成fixtureに依存せず動的に作る方が安全だが簡略化
@p_austria = Participant.create!(game: @game, user: @austria, power: "AUSTRIA")
@p_germany = Participant.create!(game: @game, user: @germany, power: "GERMANY")
@p_russia = Participant.create!(game: @game, user: @russia, power: "RUSSIA")
end
test "should create global board automatically" do
new_game = Game.create!(
title: "New Game",
status: "recruiting",
turn_schedule: "0,12",
participants_count: 7,
victory_sc_count: 18,
scoring_system: "none"
)
assert new_game.boards.global.exists?
end
test "should create negotiation board" do
board = @game.boards.new(board_type: "negotiation", created_by_participant_id: @p_austria.id)
assert board.save
# メンバー追加
board.board_memberships.create!(participant: @p_austria, joined_at: Time.current)
board.board_memberships.create!(participant: @p_germany, joined_at: Time.current)
assert board.member?(@p_austria)
assert board.member?(@p_germany)
assert_not board.member?(@p_russia)
end
test "should not allow duplicate member combination" do
# 既存のボード: Austria & Germany
board1 = @game.boards.create!(board_type: "negotiation", created_by_participant_id: @p_austria.id)
board1.board_memberships.create!(participant: @p_austria, joined_at: Time.current)
board1.board_memberships.create!(participant: @p_germany, joined_at: Time.current)
# 新しいボード: Austria & Germany (重複)
board2 = @game.boards.new(board_type: "negotiation", created_by_participant_id: @p_austria.id)
board2.board_memberships.build(participant: @p_austria, joined_at: Time.current)
board2.board_memberships.build(participant: @p_germany, joined_at: Time.current)
assert_not board2.valid?
assert_includes board2.errors[:base], "同じメンバー構成の掲示板がすでに存在します"
# メンバーが違えばOK: Austria & Russia
board3 = @game.boards.new(board_type: "negotiation", created_by_participant_id: @p_austria.id)
board3.board_memberships.build(participant: @p_austria, joined_at: Time.current)
board3.board_memberships.build(participant: @p_russia, joined_at: Time.current)
assert board3.valid?
end
test "unread count" do
board = @game.boards.create!(board_type: "negotiation", created_by_participant_id: @p_austria.id)
m_austria = board.board_memberships.create!(participant: @p_austria, joined_at: Time.current)
# 投稿前の未読は0
assert_equal 0, board.unread_count_for(@p_austria)
# 投稿作成
post1 = board.board_posts.create!(participant: @p_austria, body: "Hello")
post2 = board.board_posts.create!(participant: @p_austria, body: "World")
# Last read が nil なので全件未読扱いにはならない(ロジック上は last_read_post_id || 0 なので `id > 0` の件数になる)
# `id` はauto incrementで正の数なので、全件カウントされるはず
assert_equal 2, board.unread_count_for(@p_austria)
# 既読更新
m_austria.mark_read!(post1.id)
assert_equal 1, board.unread_count_for(@p_austria)
m_austria.mark_read!(post2.id)
assert_equal 0, board.unread_count_for(@p_austria)
end
end

View File

@@ -11,5 +11,8 @@ module ActiveSupport
fixtures :all
# Add more helper methods to be used by all tests here...
def login_as(user, password: "password")
post login_url, params: { email: user.email, password: password }
end
end
end