account_statuses_cleanup_service_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe AccountStatusesCleanupService 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 and records last deleted id, deletes statuses, preserves recent unrelated statuses' do
  25. expect(subject.call(account_policy, 10))
  26. .to eq(3)
  27. expect(account_policy.last_inspected)
  28. .to eq [old_status.id, another_old_status.id].max
  29. expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id]))
  30. .to be_nil
  31. expect { recent_status.reload }
  32. .to_not raise_error
  33. expect { unrelated_status.reload }
  34. .to_not raise_error
  35. expect { recent_status.reload }
  36. .to_not raise_error
  37. end
  38. end
  39. context 'when called repeatedly with a budget of 2' do
  40. it 'reports 2 then 1 deleted statuses and deletes in expected order' do
  41. expect(subject.call(account_policy, 2))
  42. .to eq(2)
  43. expect(Status.find_by(id: very_old_status.id))
  44. .to be_nil
  45. expect(subject.call(account_policy, 2))
  46. .to eq(1)
  47. expect(Status.find_by(id: [very_old_status.id, old_status.id, another_old_status.id]))
  48. .to be_nil
  49. end
  50. end
  51. context 'when a self-faved toot is unfaved' do
  52. let!(:self_faved) { Fabricate(:status, created_at: 6.months.ago, account: account) }
  53. let!(:favourite) { Fabricate(:favourite, account: account, status: self_faved) }
  54. it 'deletes it once unfaved' do
  55. expect(subject.call(account_policy, 20)).to eq 3
  56. expect(Status.find_by(id: self_faved.id)).to_not be_nil
  57. expect(subject.call(account_policy, 20)).to eq 0
  58. favourite.destroy!
  59. expect(subject.call(account_policy, 20)).to eq 1
  60. expect(Status.find_by(id: self_faved.id)).to be_nil
  61. end
  62. end
  63. context 'when there are more un-deletable old toots than the early search cutoff' do
  64. before do
  65. stub_const 'AccountStatusesCleanupPolicy::EARLY_SEARCH_CUTOFF', 5
  66. # Old statuses that should be cut-off
  67. 10.times do
  68. Fabricate(:status, created_at: 4.years.ago, visibility: :direct, account: account)
  69. end
  70. # New statuses that prevent cut-off id to reach the last status
  71. 10.times do
  72. Fabricate(:status, created_at: 4.seconds.ago, visibility: :direct, account: account)
  73. end
  74. end
  75. it 'reports 0 deleted statuses then 0 then 3 then 0 again, and keeps id under oldest deletable record' do
  76. expect(subject.call(account_policy, 10))
  77. .to eq(0)
  78. expect(subject.call(account_policy, 10))
  79. .to eq(0)
  80. expect(subject.call(account_policy, 10))
  81. .to eq(3)
  82. expect(subject.call(account_policy, 10))
  83. .to eq(0)
  84. expect(account_policy.last_inspected)
  85. .to be < oldest_deletable_record_id
  86. end
  87. def oldest_deletable_record_id
  88. Mastodon::Snowflake.id_at(
  89. account_policy.min_status_age.seconds.ago,
  90. with_random: false
  91. )
  92. end
  93. end
  94. end
  95. end
  96. end