From 9b1821d001be1f8eed8fc9fb0a94b9bfae64e6d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KMY=EF=BC=88=E9=9B=AA=E3=81=82=E3=81=99=E3=81=8B=EF=BC=89?= Date: Wed, 18 Oct 2023 10:09:15 +0900 Subject: [PATCH] =?UTF-8?q?Test:=20`UnEmojiReactService`=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=20(#138)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/services/emoji_react_service_spec.rb | 2 + spec/services/un_emoji_react_service_spec.rb | 139 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 spec/services/un_emoji_react_service_spec.rb diff --git a/spec/services/emoji_react_service_spec.rb b/spec/services/emoji_react_service_spec.rb index 629a7818d4..ed19538098 100644 --- a/spec/services/emoji_react_service_spec.rb +++ b/spec/services/emoji_react_service_spec.rb @@ -47,6 +47,7 @@ RSpec.describe EmojiReactService, type: :service do it 'react with emoji' do expect(subject.count).to eq 1 expect(subject.pluck(:name)).to contain_exactly('😂') + expect(EmojiReaction.where(status: status).count).to eq 2 end end @@ -56,6 +57,7 @@ RSpec.describe EmojiReactService, type: :service do it 'react with emoji' do expect(subject.count).to eq 1 expect(subject.first.name).to eq '😀' + expect(EmojiReaction.where(status: status).count).to eq 2 end end diff --git a/spec/services/un_emoji_react_service_spec.rb b/spec/services/un_emoji_react_service_spec.rb new file mode 100644 index 0000000000..393e83b2e0 --- /dev/null +++ b/spec/services/un_emoji_react_service_spec.rb @@ -0,0 +1,139 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe UnEmojiReactService, type: :service do + subject do + described_class.new.call(sender.id, status.id, emoji_reaction) + EmojiReaction.where(status: status, account: sender) + end + + let!(:emoji_reaction) { nil } + let(:sender) { Fabricate(:user).account } + let(:author) { Fabricate(:user).account } + let(:status) { Fabricate(:status, account: author) } + + context 'when a simple case' do + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: '😀') } + + it 'unreact with emoji' do + expect(subject.count).to eq 0 + end + end + + context 'when no emoji reactions' do + it 'unreact with emoji' do + expect(subject.count).to eq 0 + end + end + + context 'with custom emoji of local' do + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: 'ohagi', custom_emoji: custom_emoji) } + let(:custom_emoji) { Fabricate(:custom_emoji, shortcode: 'ohagi') } + + it 'react with emoji' do + expect(subject.count).to eq 0 + end + end + + context 'with custom emoji of remote' do + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: 'ohagi', custom_emoji: custom_emoji) } + let(:custom_emoji) { Fabricate(:custom_emoji, shortcode: 'ohagi', domain: 'foo.bar', uri: 'https://foo.bar/emoji/ohagi') } + + it 'react with emoji' do + expect(subject.count).to eq 0 + end + end + + context 'when other account already set' do + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: '😀') } + + before { Fabricate(:emoji_reaction, status: status, name: '😀') } + + it 'unreact with emoji' do + expect(subject.count).to eq 0 + expect(EmojiReaction.where(status: status).count).to eq 1 + end + end + + context 'when this account already set multiple emojis' do + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: '😀') } + + before do + Fabricate(:emoji_reaction, account: sender, status: status, name: '😂') + Fabricate(:emoji_reaction, account: sender, status: status, name: '🚗') + end + + it 'unreact with emoji' do + expect(subject.count).to eq 2 + expect(subject.pluck(:name)).to contain_exactly('😂', '🚗') + end + end + + context 'with remove all emojis' do + before do + Fabricate(:emoji_reaction, account: sender, status: status, name: '😀') + Fabricate(:emoji_reaction, account: sender, status: status, name: '😂') + Fabricate(:emoji_reaction, account: sender, status: status, name: '🚗') + end + + it 'react with emoji' do + expect(subject.count).to eq 0 + end + end + + context 'when has remote followers' do + let!(:bob) { Fabricate(:account, domain: 'foo.bar', uri: 'https://foo.bar/actor', inbox_url: 'https://foo.bar/inbox', protocol: 'activitypub') } + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: '😀') } + + before do + bob.follow!(author) + stub_request(:post, 'https://foo.bar/inbox') + end + + it 'react with emoji' do + expect(subject.count).to eq 0 + expect(a_request(:post, 'https://foo.bar/inbox').with(body: hash_including({ + type: 'Undo', + actor: ActivityPub::TagManager.instance.uri_for(sender), + content: '😀', + }))).to have_been_made.once + end + end + + context 'when has relay server' do + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: '😀') } + + before do + Fabricate(:relay, inbox_url: 'https://foo.bar/inbox', state: :accepted) + stub_request(:post, 'https://foo.bar/inbox') + end + + it 'react with emoji' do + expect(subject.count).to eq 0 + expect(a_request(:post, 'https://foo.bar/inbox').with(body: hash_including({ + type: 'Undo', + actor: ActivityPub::TagManager.instance.uri_for(sender), + content: '😀', + }))).to have_been_made.once + end + end + + context 'when has friend server' do + let(:emoji_reaction) { Fabricate(:emoji_reaction, account: sender, status: status, name: '😀') } + + before do + Fabricate(:friend_domain, inbox_url: 'https://foo.bar/inbox', active_state: :accepted, pseudo_relay: true) + stub_request(:post, 'https://foo.bar/inbox') + end + + it 'react with emoji' do + expect(subject.count).to eq 0 + expect(a_request(:post, 'https://foo.bar/inbox').with(body: hash_including({ + type: 'Undo', + actor: ActivityPub::TagManager.instance.uri_for(sender), + content: '😀', + }))).to have_been_made.once + end + end +end