From b95a15dfb35cc8197832339f3cb0b006cd1391e5 Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 13 Nov 2024 12:05:31 +0900 Subject: [PATCH] =?UTF-8?q?Add:=20`UpdateStatusExpirationService`=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduled_expiration_status_fabricator.rb | 7 ++ .../update_status_expiration_service_spec.rb | 74 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 spec/fabricators/scheduled_expiration_status_fabricator.rb create mode 100644 spec/services/update_status_expiration_service_spec.rb diff --git a/spec/fabricators/scheduled_expiration_status_fabricator.rb b/spec/fabricators/scheduled_expiration_status_fabricator.rb new file mode 100644 index 00000000000000..9cbacc9b76583a --- /dev/null +++ b/spec/fabricators/scheduled_expiration_status_fabricator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +Fabricator(:scheduled_expiration_status) do + account { Fabricate.build(:account) } + status { Fabricate.build(:status) } + scheduled_at { 20.hours.from_now } +end diff --git a/spec/services/update_status_expiration_service_spec.rb b/spec/services/update_status_expiration_service_spec.rb new file mode 100644 index 00000000000000..a84a2913cb77f7 --- /dev/null +++ b/spec/services/update_status_expiration_service_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe UpdateStatusExpirationService do + subject { described_class.new.call(status) } + + let(:status) { Fabricate(:status, text: text) } + + shared_examples 'set expire date' do |offset| + it 'set expire date' do + travel_to '2023-01-01T00:00:00Z' + subject + expect(ScheduledExpirationStatus.where(status: status).count).to eq 1 + expect(ScheduledExpirationStatus.exists?(scheduled_at: Time.now.utc + offset, status: status)).to be true + end + end + + shared_examples 'did not set expire date' do + it 'did not set expire date' do + subject + expect(ScheduledExpirationStatus.exists?(status: status)).to be false + end + end + + context 'when 30 minutes' do + let(:text) { 'ohagi #exp30m' } + + it_behaves_like 'set expire date', 30.minutes + end + + context 'when 1 hour' do + let(:text) { 'ohagi #exp1h' } + + it_behaves_like 'set expire date', 1.hour + end + + context 'with multiple tags' do + let(:text) { 'ohagi #exp3h #exp1d' } + + it_behaves_like 'set expire date', 3.hours + end + + context 'when too long hours' do + let(:text) { 'ohagi #exp9999999999h' } + + it_behaves_like 'did not set expire date' + end + + context 'without tags' do + let(:text) { 'ohagi is ohagi' } + + it_behaves_like 'did not set expire date' + end + + context 'when update status text' do + before do + travel_to '2023-01-01T00:00:00Z' + Fabricate(:scheduled_expiration_status, account: status.account, status: status) + end + + context 'with new date' do + let(:text) { 'ohagi #exp1h' } + + it_behaves_like 'set expire date', 1.hour + end + + context 'without new date' do + let(:text) { 'ohagi' } + + it_behaves_like 'did not set expire date' + end + end +end