このコミットが含まれているのは:
@@ -14,10 +14,17 @@ RSpec.describe 'Theatres API', type: :request do
|
||||
let(:member) { create(:user, :member, name: 'member user') }
|
||||
let(:other_user) { create(:user, :member, name: 'other user') }
|
||||
|
||||
let!(:youtube_post) do
|
||||
let!(:niconico_post) do
|
||||
Post.create!(
|
||||
title: 'youtube post',
|
||||
url: 'https://www.youtube.com/watch?v=spec123'
|
||||
title: 'niconico post',
|
||||
url: 'https://www.nicovideo.jp/watch/sm123'
|
||||
)
|
||||
end
|
||||
|
||||
let!(:second_niconico_post) do
|
||||
Post.create!(
|
||||
title: 'second niconico post',
|
||||
url: 'https://www.nicovideo.jp/watch/sm456'
|
||||
)
|
||||
end
|
||||
|
||||
@@ -120,7 +127,8 @@ RSpec.describe 'Theatres API', type: :request do
|
||||
expect(json).to include(
|
||||
'host_flg' => true,
|
||||
'post_id' => nil,
|
||||
'post_started_at' => nil
|
||||
'post_started_at' => nil,
|
||||
'post_elapsed_ms' => nil
|
||||
)
|
||||
|
||||
expect(json.fetch('watching_users')).to contain_exactly(
|
||||
@@ -177,7 +185,8 @@ RSpec.describe 'Theatres API', type: :request do
|
||||
expect(json).to include(
|
||||
'host_flg' => false,
|
||||
'post_id' => nil,
|
||||
'post_started_at' => nil
|
||||
'post_started_at' => nil,
|
||||
'post_elapsed_ms' => nil
|
||||
)
|
||||
|
||||
expect(json.fetch('watching_users')).to contain_exactly(
|
||||
@@ -204,7 +213,7 @@ RSpec.describe 'Theatres API', type: :request do
|
||||
)
|
||||
theatre.update!(
|
||||
host_user: other_user,
|
||||
current_post: youtube_post,
|
||||
current_post: niconico_post,
|
||||
current_post_started_at: started_at
|
||||
)
|
||||
sign_in_as(member)
|
||||
@@ -220,9 +229,11 @@ RSpec.describe 'Theatres API', type: :request do
|
||||
expect(theatre.host_user_id).to eq(member.id)
|
||||
|
||||
expect(json['host_flg']).to eq(true)
|
||||
expect(json['post_id']).to eq(youtube_post.id)
|
||||
expect(json['post_id']).to eq(niconico_post.id)
|
||||
expect(Time.zone.parse(json['post_started_at']))
|
||||
.to be_within(1.second).of(started_at)
|
||||
expect(json['post_elapsed_ms'])
|
||||
.to be_within(1_000).of(120_000)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -273,17 +284,20 @@ RSpec.describe 'Theatres API', type: :request do
|
||||
it 'sets current_post to an eligible post and updates current_post_started_at' do
|
||||
expect { do_request }
|
||||
.to change { theatre.reload.current_post_id }
|
||||
.from(nil).to(youtube_post.id)
|
||||
|
||||
expect(response).to have_http_status(:no_content)
|
||||
expect([niconico_post.id, second_niconico_post.id])
|
||||
.to include(theatre.reload.current_post_id)
|
||||
expect(theatre.reload.current_post_started_at)
|
||||
.to be_within(1.second).of(Time.current)
|
||||
expect(theatre.programmes.count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when current user is host and no eligible post exists' do
|
||||
before do
|
||||
youtube_post.destroy!
|
||||
niconico_post.destroy!
|
||||
second_niconico_post.destroy!
|
||||
theatre.update!(
|
||||
host_user: member,
|
||||
current_post: other_post,
|
||||
@@ -299,9 +313,117 @@ RSpec.describe 'Theatres API', type: :request do
|
||||
|
||||
theatre.reload
|
||||
expect(theatre.current_post_id).to be_nil
|
||||
expect(theatre.current_post_started_at)
|
||||
.to be_within(1.second).of(Time.current)
|
||||
expect(theatre.current_post_started_at).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /theatres/:id/skip_vote' do
|
||||
subject(:do_request) do
|
||||
put "/theatres/#{theatre.id}/skip_vote"
|
||||
end
|
||||
|
||||
let(:third_user) { create(:user, :member, name: 'third user') }
|
||||
|
||||
before do
|
||||
theatre.update!(current_post: niconico_post, current_post_started_at: 10.seconds.ago)
|
||||
[member, other_user, third_user].each do |user|
|
||||
TheatreWatchingUser.create!(
|
||||
theatre:,
|
||||
user:,
|
||||
expires_at: 10.seconds.from_now
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
it 'records a vote and returns the current vote status before majority' do
|
||||
sign_in_as(member)
|
||||
|
||||
expect { do_request }.to change(TheatreSkipVote, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json['skipped']).to eq(false)
|
||||
expect(json['post_id']).to eq(niconico_post.id)
|
||||
expect(json['skip_vote']).to include(
|
||||
'votes_count' => 1,
|
||||
'required_count' => 2,
|
||||
'watching_users_count' => 3,
|
||||
'voted' => true
|
||||
)
|
||||
end
|
||||
|
||||
it 'finalizes skip when votes reach majority and stores voters and tag snapshots' do
|
||||
tag = create(:tag, name: 'skip-target')
|
||||
PostTag.create!(post: niconico_post, tag:)
|
||||
|
||||
TheatreSkipVote.create!(theatre:, post: niconico_post, user: member)
|
||||
sign_in_as(other_user)
|
||||
|
||||
expect { do_request }
|
||||
.to change(TheatreSkipEvent, :count).by(1)
|
||||
.and change(TheatreSkipEventVoter, :count).by(2)
|
||||
.and change(TheatreSkipEventTag, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json['skipped']).to eq(true)
|
||||
expect(json['post_id']).to eq(second_niconico_post.id)
|
||||
|
||||
event = TheatreSkipEvent.last
|
||||
expect(event.post).to eq(niconico_post)
|
||||
expect(event.users).to contain_exactly(member, other_user)
|
||||
expect(event.tags).to contain_exactly(tag)
|
||||
expect(TheatreSkipVote.where(theatre:, post: niconico_post)).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /theatres/:id/skip_vote' do
|
||||
before do
|
||||
theatre.update!(current_post: niconico_post, current_post_started_at: 10.seconds.ago)
|
||||
TheatreWatchingUser.create!(theatre:, user: member, expires_at: 10.seconds.from_now)
|
||||
TheatreSkipVote.create!(theatre:, post: niconico_post, user: member)
|
||||
sign_in_as(member)
|
||||
end
|
||||
|
||||
it 'removes the current user vote' do
|
||||
expect {
|
||||
delete "/theatres/#{theatre.id}/skip_vote"
|
||||
}.to change(TheatreSkipVote, :count).by(-1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json['skip_vote']).to include(
|
||||
'votes_count' => 0,
|
||||
'required_count' => 1,
|
||||
'watching_users_count' => 1,
|
||||
'voted' => false
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /theatres/:id/post_selection_weights' do
|
||||
before do
|
||||
theatre.update!(current_post: niconico_post)
|
||||
TheatreWatchingUser.create!(theatre:, user: member, expires_at: 10.seconds.from_now)
|
||||
sign_in_as(member)
|
||||
end
|
||||
|
||||
it 'returns tag penalties and candidate weights for the current watchers' do
|
||||
tag = create(:tag, name: 'heavy-tag')
|
||||
PostTag.create!(post: second_niconico_post, tag:)
|
||||
event = TheatreSkipEvent.create!(
|
||||
theatre:,
|
||||
post: niconico_post,
|
||||
skipped_by_user: member,
|
||||
created_at: Time.current
|
||||
)
|
||||
TheatreSkipEventVoter.create!(theatre_skip_event: event, user: member)
|
||||
TheatreSkipEventTag.create!(theatre_skip_event: event, tag:)
|
||||
|
||||
get "/theatres/#{theatre.id}/post_selection_weights"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json['tag_penalties'].first['penalty']).to eq(1)
|
||||
expect(json['lightest_posts'].first['post']['id']).to eq(second_niconico_post.id)
|
||||
expect(json['lightest_posts'].first['penalty']).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
新しい課題から参照
ユーザをブロックする