Files
mastodon/spec/lib/search_query_transformer_spec.rb
jcoffey-dev 319ed76dbd
Crowdin / Upload translations / upload-translations (push) Skipped
Historical data migration test / test (14-alpine) (push) Canceled after 0s
Historical data migration test / test (15-alpine) (push) Canceled after 0s
Historical data migration test / test (16-alpine) (push) Canceled after 0s
Historical data migration test / test (17-alpine) (push) Canceled after 0s
Bundler Audit / security (push) Canceled after 0s
Haml Linting / lint (push) Canceled after 0s
JavaScript Linting / lint (push) Canceled after 0s
Ruby Linting / lint (push) Canceled after 0s
JavaScript Testing / test (push) Canceled after 0s
Ruby Testing / build (production) (push) Canceled after 0s
Ruby Testing / test (.ruby-version) (push) Canceled after 0s
Ruby Testing / test (3.3) (push) Canceled after 0s
Ruby Testing / test (3.4) (push) Canceled after 0s
Ruby Testing / End to End testing (.ruby-version) (push) Canceled after 0s
Ruby Testing / End to End testing (3.3) (push) Canceled after 0s
Ruby Testing / End to End testing (3.4) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check i18n / check-i18n (push) Canceled after 0s
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
CodeQL / Analyze (ruby) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check formatting / lint (push) Canceled after 0s
CSS Linting / lint (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Canceled after 0s
Chromatic / Check for relevant changes (push) Canceled after 0s
Chromatic / Run Chromatic (push) Canceled after 0s
Import Mastodon v4.7.3
Snapshot of upstream tag v4.7.3 (commit 8ff11c2bb7b9f0714e947b10fa3629c6542c7121) from
https://github.com/mastodon/mastodon, unmodified.
2026-10-05 22:03:13 -07:00

153 lines
4.6 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SearchQueryTransformer do
subject { described_class.new.apply(parser, current_account: account) }
let(:account) { Fabricate(:account) }
let(:parser) { SearchQueryParser.new.parse(query) }
shared_examples 'date operator' do |operator|
let(:statement_operations) { [] }
[
['2022-01-01', '2022-01-01'],
['"2022-01-01"', '2022-01-01'],
['12345678', '12345678'],
['"12345678"', '12345678'],
['"2024-10-31T23:47:20Z"', '2024-10-31T23:47:20Z'],
].each do |value, parsed|
context "with #{operator}:#{value}" do
let(:query) { "#{operator}:#{value}" }
it 'transforms clauses' do
ops = statement_operations.index_with { |_op| parsed }
expect(subject.send(:must_clauses)).to be_empty
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses).map(&:term)).to contain_exactly(**ops, time_zone: 'UTC')
end
end
end
context "with #{operator}:\"abc\"" do
let(:query) { "#{operator}:\"abc\"" }
it 'raises an exception' do
expect { subject }.to raise_error(Date::Error)
end
end
end
context 'with "hello world"' do
let(:query) { 'hello world' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello world)
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses)).to be_empty
end
end
context 'with "hello -world"' do
let(:query) { 'hello -world' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
expect(subject.send(:must_not_clauses).map(&:term)).to match_array %w(world)
expect(subject.send(:filter_clauses)).to be_empty
end
end
context 'with "hello is:reply"' do
let(:query) { 'hello is:reply' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses).map(&:term)).to match_array %w(reply)
end
end
context 'with "foo: bar"' do
let(:query) { 'foo: bar' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(foo bar)
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses)).to be_empty
end
end
context 'with "foo:bar"' do
let(:query) { 'foo:bar' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:term)).to contain_exactly('foo bar')
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses)).to be_empty
end
end
context 'with \'"hello world"\'' do
let(:query) { '"hello world"' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:phrase)).to contain_exactly('hello world')
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses)).to be_empty
end
end
context 'with \'is:"foo bar"\'' do
let(:query) { 'is:"foo bar"' }
it 'transforms clauses' do
expect(subject.send(:must_clauses)).to be_empty
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses).map(&:term)).to contain_exactly('foo bar')
end
end
context 'with date operators' do
context 'with "before"' do
it_behaves_like 'date operator', 'before' do
let(:statement_operations) { [:lt] }
end
end
context 'with "after"' do
it_behaves_like 'date operator', 'after' do
let(:statement_operations) { [:gt] }
end
end
context 'with "during"' do
it_behaves_like 'date operator', 'during' do
let(:statement_operations) { [:gte, :lte] }
end
end
end
context 'with multiple prefix clauses before a search term' do
let(:query) { 'from:me has:media foo' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:term)).to contain_exactly('foo')
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses).map(&:prefix)).to contain_exactly('from', 'has')
end
end
context 'with a search term between two prefix clauses' do
let(:query) { 'from:me foo has:media' }
it 'transforms clauses' do
expect(subject.send(:must_clauses).map(&:term)).to contain_exactly('foo')
expect(subject.send(:must_not_clauses)).to be_empty
expect(subject.send(:filter_clauses).map(&:prefix)).to contain_exactly('from', 'has')
end
end
end