account_statuses_cleanup_service_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe AccountStatusesCleanupService, type: :service do
  4. let(:account) { Fabricate(:account, username: 'alice', domain: nil) }
  5. let(:account_policy) { Fabricate(:account_statuses_cleanup_policy, account: account) }
  6. let!(:unrelated_status) { Fabricate(:status, created_at: 3.years.ago) }
  7. describe '#call' do
  8. context 'when the account has not posted anything' do
  9. it 'returns 0 deleted toots' do
  10. expect(subject.call(account_policy)).to eq 0
  11. end
  12. end
  13. context 'when the account has posted several old statuses' do
  14. let!(:very_old_status) { Fabricate(:status, created_at: 3.years.ago, account: account) }
  15. let!(:old_status) { Fabricate(:status, created_at: 1.year.ago, account: account) }
  16. let!(:another_old_status) { Fabricate(:status, created_at: 1.year.ago, account: account) }
  17. let!(:recent_status) { Fabricate(:status, created_at: 1.day.ago, account: account) }
  18. context 'when given a budget of 1' do
  19. it 'reports 1 deleted toot' do
  20. expect(subject.call(account_policy, 1)).to eq 1
  21. end
  22. end
  23. context 'when given a normal budget of 10' do
  24. it 'reports 3 deleted statuses' do
  25. expect(subject.call(account_policy, 10)).to eq 3
  26. end
  27. it 'records the last deleted id' do
  28. subject.call(account_policy, 10)
  29. expect(account_policy.last_inspected).to eq [old_status.id, another_old_status.id].max
  30. end
  31. it 'actually deletes the statuses' do
  32. subject.call(account_policy, 10)
  33. expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id])).to be_nil
  34. end
  35. end
  36. context 'when called repeatedly with a budget of 2' do
  37. it 'reports 2 then 1 deleted statuses' do
  38. expect(subject.call(account_policy, 2)).to eq 2
  39. expect(subject.call(account_policy, 2)).to eq 1
  40. end
  41. it 'actually deletes the statuses in the expected order' do
  42. subject.call(account_policy, 2)
  43. expect(Status.find_by(id: very_old_status.id)).to be_nil
  44. subject.call(account_policy, 2)
  45. expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id])).to be_nil
  46. end
  47. end
  48. context 'when a self-faved toot is unfaved' do
  49. let!(:self_faved) { Fabricate(:status, created_at: 6.months.ago, account: account) }
  50. let!(:favourite) { Fabricate(:favourite, account: account, status: self_faved) }
  51. it 'deletes it once unfaved' do
  52. expect(subject.call(account_policy, 20)).to eq 3
  53. expect(Status.find_by(id: self_faved.id)).to_not be_nil
  54. expect(subject.call(account_policy, 20)).to eq 0
  55. favourite.destroy!
  56. expect(subject.call(account_policy, 20)).to eq 1
  57. expect(Status.find_by(id: self_faved.id)).to be_nil
  58. end
  59. end
  60. context 'when there are more un-deletable old toots than the early search cutoff' do
  61. before do
  62. stub_const 'AccountStatusesCleanupPolicy::EARLY_SEARCH_CUTOFF', 5
  63. # Old statuses that should be cut-off
  64. 10.times do
  65. Fabricate(:status, created_at: 4.years.ago, visibility: :direct, account: account)
  66. end
  67. # New statuses that prevent cut-off id to reach the last status
  68. 10.times do
  69. Fabricate(:status, created_at: 4.seconds.ago, visibility: :direct, account: account)
  70. end
  71. end
  72. it 'reports 0 deleted statuses then 0 then 3 then 0 again' do
  73. expect(subject.call(account_policy, 10)).to eq 0
  74. expect(subject.call(account_policy, 10)).to eq 0
  75. expect(subject.call(account_policy, 10)).to eq 3
  76. expect(subject.call(account_policy, 10)).to eq 0
  77. end
  78. it 'never causes the recorded id to get higher than oldest deletable toot' do
  79. subject.call(account_policy, 10)
  80. subject.call(account_policy, 10)
  81. subject.call(account_policy, 10)
  82. subject.call(account_policy, 10)
  83. expect(account_policy.last_inspected).to be < Mastodon::Snowflake.id_at(account_policy.min_status_age.seconds.ago, with_random: false)
  84. end
  85. end
  86. end
  87. end
  88. end