Use validation matchers for StatusLengthValidator spec (#37905)

This commit is contained in:
Matt Jankowski
2026-02-18 10:22:22 -05:00
committed by GitHub
parent e8ecf1719c
commit 66052e3ddd

View File

@@ -3,123 +3,88 @@
require 'rails_helper'
RSpec.describe StatusLengthValidator do
describe '#validate' do
before { stub_const("#{described_class}::MAX_CHARS", 500) } # Example values below are relative to this baseline
subject { Fabricate.build :status }
it 'does not add errors onto remote statuses' do
status = instance_double(Status, local?: false)
allow(status).to receive(:errors)
before { stub_const 'StatusLengthValidator::MAX_CHARS', 100 }
subject.validate(status)
let(:over_limit_text) { 'a' * described_class::MAX_CHARS * 2 }
expect(status).to_not have_received(:errors)
end
context 'when status is remote' do
before { subject.update! account: Fabricate(:account, domain: 'host.example') }
it 'does not add errors onto local reblogs' do
status = instance_double(Status, local?: false, reblog?: true)
allow(status).to receive(:errors)
it { is_expected.to allow_value(over_limit_text).for(:text) }
it { is_expected.to allow_value(over_limit_text).for(:spoiler_text).against(:text) }
end
subject.validate(status)
context 'when status is a local reblog' do
before { subject.update! reblog: Fabricate(:status) }
expect(status).to_not have_received(:errors)
end
it { is_expected.to allow_value(over_limit_text).for(:text) }
it { is_expected.to allow_value(over_limit_text).for(:spoiler_text).against(:text) }
end
it 'adds an error when content warning is over character limit' do
status = status_double(spoiler_text: 'a' * 520)
subject.validate(status)
expect(status.errors).to have_received(:add)
end
context 'when text is over character limit' do
it { is_expected.to_not allow_value(over_limit_text).for(:text).with_message(too_long_message) }
end
it 'adds an error when text is over character limit' do
status = status_double(text: 'a' * 520)
subject.validate(status)
expect(status.errors).to have_received(:add)
end
context 'when content warning text is over character limit' do
it { is_expected.to_not allow_value(over_limit_text).for(:spoiler_text).against(:text).with_message(too_long_message) }
end
it 'adds an error when text and content warning are over character limit total' do
status = status_double(spoiler_text: 'a' * 250, text: 'b' * 251)
subject.validate(status)
expect(status.errors).to have_received(:add)
end
context 'when text and content warning combine to exceed limit' do
before { subject.text = 'a' * 50 }
it 'reduces calculated length of auto-linkable space-separated URLs' do
text = [starting_string, example_link].join(' ')
status = status_double(text: text)
it { is_expected.to_not allow_value('a' * 55).for(:spoiler_text).against(:text).with_message(too_long_message) }
end
subject.validate(status)
expect(status.errors).to_not have_received(:add)
end
context 'when text has space separated linkable URLs' do
let(:text) { [starting_string, example_link].join(' ') }
it 'does not reduce calculated length of non-autolinkable URLs' do
text = [starting_string, example_link].join
status = status_double(text: text)
it { is_expected.to allow_value(text).for(:text) }
end
subject.validate(status)
expect(status.errors).to have_received(:add)
end
context 'when text has non-separated URLs' do
let(:text) { [starting_string, example_link].join }
it 'does not reduce calculated length of count overly long URLs' do
text = "http://example.com/valid?#{'#foo?' * 1000}"
status = status_double(text: text)
subject.validate(status)
expect(status.errors).to have_received(:add)
end
it { is_expected.to_not allow_value(text).for(:text).with_message(too_long_message) }
end
it 'counts only the front part of remote usernames' do
text = ('a' * 475) + " @alice@#{'b' * 30}.com"
status = status_double(text: text)
context 'with excessively long URLs' do
let(:text) { "http://example.com/valid?#{'#foo?' * 1000}" }
subject.validate(status)
expect(status.errors).to_not have_received(:add)
end
it { is_expected.to_not allow_value(text).for(:text).with_message(too_long_message) }
end
it 'does count both parts of remote usernames for overly long domains' do
text = "@alice@#{'b' * 500}.com"
status = status_double(text: text)
context 'when remote account usernames cause limit excess' do
let(:text) { ('a' * 75) + " @alice@#{'b' * 30}.com" }
subject.validate(status)
expect(status.errors).to have_received(:add)
end
it { is_expected.to allow_value(text).for(:text) }
end
it 'counts multi byte emoji as single character' do
text = '' * 500
status = status_double(text: text)
context 'when remote usernames are attached to long domains' do
let(:text) { "@alice@#{'b' * Extractor::MAX_DOMAIN_LENGTH * 2}.com" }
subject.validate(status)
expect(status.errors).to_not have_received(:add)
end
it { is_expected.to_not allow_value(text).for(:text).with_message(too_long_message) }
end
it 'counts ZWJ sequence emoji as single character' do
text = '🏳️‍⚧️' * 500
status = status_double(text: text)
context 'with special character strings' do
let(:multibyte_emoji) { '✨' * described_class::MAX_CHARS }
let(:zwj_sequence) { '🏳️‍⚧️' * described_class::MAX_CHARS }
subject.validate(status)
expect(status.errors).to_not have_received(:add)
end
it { is_expected.to allow_values(multibyte_emoji, zwj_sequence).for(:text) }
end
private
def too_long_message
I18n.t('statuses.over_character_limit', max: described_class::MAX_CHARS)
end
def starting_string
'a' * 476
'a' * 76
end
def example_link
"http://#{'b' * 30}.com/example"
end
def status_double(spoiler_text: '', text: '')
instance_double(
Status,
spoiler_text: spoiler_text,
text: text,
errors: activemodel_errors,
local?: true,
reblog?: false
)
end
def activemodel_errors
instance_double(ActiveModel::Errors, add: nil)
end
end