You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
4.2 KiB

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