|
| 1 | +require "../../../src/invidious/search/filters" |
| 2 | +require "../../../src/invidious/search/query" |
| 3 | + |
| 4 | +require "http/params" |
| 5 | +require "spectator" |
| 6 | + |
| 7 | +Spectator.configure do |config| |
| 8 | + config.fail_blank |
| 9 | + config.randomize |
| 10 | +end |
| 11 | + |
| 12 | +Spectator.describe Invidious::Search::Query do |
| 13 | + describe Type::Regular do |
| 14 | + # ------------------- |
| 15 | + # Query parsing |
| 16 | + # ------------------- |
| 17 | + |
| 18 | + it "parses query with URL prameters (q)" do |
| 19 | + query = described_class.new( |
| 20 | + HTTP::Params.parse("q=What+is+Love+10+hour&type=video&duration=long"), |
| 21 | + Invidious::Search::Query::Type::Regular, nil |
| 22 | + ) |
| 23 | + |
| 24 | + expect(query.type).to eq(Invidious::Search::Query::Type::Regular) |
| 25 | + expect(query.channel).to be_empty |
| 26 | + expect(query.text).to eq("What is Love 10 hour") |
| 27 | + |
| 28 | + expect(query.filters).to eq( |
| 29 | + Invidious::Search::Filters.new( |
| 30 | + type: Invidious::Search::Filters::Type::Video, |
| 31 | + duration: Invidious::Search::Filters::Duration::Long |
| 32 | + ) |
| 33 | + ) |
| 34 | + end |
| 35 | + |
| 36 | + it "parses query with URL prameters (search_query)" do |
| 37 | + query = described_class.new( |
| 38 | + HTTP::Params.parse("search_query=What+is+Love+10+hour&type=video&duration=long"), |
| 39 | + Invidious::Search::Query::Type::Regular, nil |
| 40 | + ) |
| 41 | + |
| 42 | + expect(query.type).to eq(Invidious::Search::Query::Type::Regular) |
| 43 | + expect(query.channel).to be_empty |
| 44 | + expect(query.text).to eq("What is Love 10 hour") |
| 45 | + |
| 46 | + expect(query.filters).to eq( |
| 47 | + Invidious::Search::Filters.new( |
| 48 | + type: Invidious::Search::Filters::Type::Video, |
| 49 | + duration: Invidious::Search::Filters::Duration::Long |
| 50 | + ) |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + it "parses query with legacy filters (q)" do |
| 55 | + query = described_class.new( |
| 56 | + HTTP::Params.parse("q=Nyan+cat+duration:long"), |
| 57 | + Invidious::Search::Query::Type::Regular, nil |
| 58 | + ) |
| 59 | + |
| 60 | + expect(query.type).to eq(Invidious::Search::Query::Type::Regular) |
| 61 | + expect(query.channel).to be_empty |
| 62 | + expect(query.text).to eq("Nyan cat") |
| 63 | + |
| 64 | + expect(query.filters).to eq( |
| 65 | + Invidious::Search::Filters.new( |
| 66 | + duration: Invidious::Search::Filters::Duration::Long |
| 67 | + ) |
| 68 | + ) |
| 69 | + end |
| 70 | + |
| 71 | + it "parses query with legacy filters (search_query)" do |
| 72 | + query = described_class.new( |
| 73 | + HTTP::Params.parse("search_query=Nyan+cat+duration:long"), |
| 74 | + Invidious::Search::Query::Type::Regular, nil |
| 75 | + ) |
| 76 | + |
| 77 | + expect(query.type).to eq(Invidious::Search::Query::Type::Regular) |
| 78 | + expect(query.channel).to be_empty |
| 79 | + expect(query.text).to eq("Nyan cat") |
| 80 | + |
| 81 | + expect(query.filters).to eq( |
| 82 | + Invidious::Search::Filters.new( |
| 83 | + duration: Invidious::Search::Filters::Duration::Long |
| 84 | + ) |
| 85 | + ) |
| 86 | + end |
| 87 | + |
| 88 | + it "parses query with both URL params and legacy filters" do |
| 89 | + query = described_class.new( |
| 90 | + HTTP::Params.parse("q=Vamos+a+la+playa+duration:long&type=Video&date=year"), |
| 91 | + Invidious::Search::Query::Type::Regular, nil |
| 92 | + ) |
| 93 | + |
| 94 | + expect(query.type).to eq(Invidious::Search::Query::Type::Regular) |
| 95 | + expect(query.channel).to be_empty |
| 96 | + expect(query.text).to eq("Vamos a la playa duration:long") |
| 97 | + |
| 98 | + expect(query.filters).to eq( |
| 99 | + Invidious::Search::Filters.new( |
| 100 | + type: Invidious::Search::Filters::Type::Video, |
| 101 | + date: Invidious::Search::Filters::Date::Year |
| 102 | + ) |
| 103 | + ) |
| 104 | + end |
| 105 | + |
| 106 | + # ------------------- |
| 107 | + # Type switching |
| 108 | + # ------------------- |
| 109 | + |
| 110 | + it "switches to channel search (URL param)" do |
| 111 | + query = described_class.new( |
| 112 | + HTTP::Params.parse("q=thunderbolt+4&channel=UC0vBXGSyV14uvJ4hECDOl0Q"), |
| 113 | + Invidious::Search::Query::Type::Regular, nil |
| 114 | + ) |
| 115 | + |
| 116 | + expect(query.type).to eq(Invidious::Search::Query::Type::Channel) |
| 117 | + expect(query.channel).to eq("UC0vBXGSyV14uvJ4hECDOl0Q") |
| 118 | + expect(query.text).to eq("thunderbolt 4") |
| 119 | + expect(query.filters.default?).to be_true |
| 120 | + end |
| 121 | + |
| 122 | + it "switches to channel search (legacy)" do |
| 123 | + query = described_class.new( |
| 124 | + HTTP::Params.parse("q=channel%3AUCRPdsCVuH53rcbTcEkuY4uQ+rdna3"), |
| 125 | + Invidious::Search::Query::Type::Regular, nil |
| 126 | + ) |
| 127 | + |
| 128 | + expect(query.type).to eq(Invidious::Search::Query::Type::Channel) |
| 129 | + expect(query.channel).to eq("UCRPdsCVuH53rcbTcEkuY4uQ") |
| 130 | + expect(query.text).to eq("rdna3") |
| 131 | + expect(query.filters.default?).to be_true |
| 132 | + end |
| 133 | + |
| 134 | + it "switches to subscriptions search" do |
| 135 | + query = described_class.new( |
| 136 | + HTTP::Params.parse("q=subscriptions:true+tunak+tunak+tun"), |
| 137 | + Invidious::Search::Query::Type::Regular, nil |
| 138 | + ) |
| 139 | + |
| 140 | + expect(query.type).to eq(Invidious::Search::Query::Type::Subscriptions) |
| 141 | + expect(query.channel).to be_empty |
| 142 | + expect(query.text).to eq("tunak tunak tun") |
| 143 | + expect(query.filters.default?).to be_true |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + describe Type::Channel do |
| 148 | + it "ignores extra parameters" do |
| 149 | + query = described_class.new( |
| 150 | + HTTP::Params.parse("q=Take+on+me+channel%3AUC12345679&type=video&date=year"), |
| 151 | + Invidious::Search::Query::Type::Channel, nil |
| 152 | + ) |
| 153 | + |
| 154 | + expect(query.type).to eq(Invidious::Search::Query::Type::Channel) |
| 155 | + expect(query.channel).to be_empty |
| 156 | + expect(query.text).to eq("Take on me") |
| 157 | + expect(query.filters.default?).to be_true |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + describe Type::Subscriptions do |
| 162 | + it "works" do |
| 163 | + query = described_class.new( |
| 164 | + HTTP::Params.parse("q=Harlem+shake&type=video&date=year"), |
| 165 | + Invidious::Search::Query::Type::Subscriptions, nil |
| 166 | + ) |
| 167 | + |
| 168 | + expect(query.type).to eq(Invidious::Search::Query::Type::Subscriptions) |
| 169 | + expect(query.channel).to be_empty |
| 170 | + expect(query.text).to eq("Harlem shake") |
| 171 | + |
| 172 | + expect(query.filters).to eq( |
| 173 | + Invidious::Search::Filters.new( |
| 174 | + type: Invidious::Search::Filters::Type::Video, |
| 175 | + date: Invidious::Search::Filters::Date::Year |
| 176 | + ) |
| 177 | + ) |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | + describe Type::Playlist do |
| 182 | + it "ignores extra parameters" do |
| 183 | + query = described_class.new( |
| 184 | + HTTP::Params.parse("q=Harlem+shake+type:video+date:year&channel=UC12345679"), |
| 185 | + Invidious::Search::Query::Type::Playlist, nil |
| 186 | + ) |
| 187 | + |
| 188 | + expect(query.type).to eq(Invidious::Search::Query::Type::Playlist) |
| 189 | + expect(query.channel).to be_empty |
| 190 | + expect(query.text).to eq("Harlem shake") |
| 191 | + |
| 192 | + expect(query.filters).to eq( |
| 193 | + Invidious::Search::Filters.new( |
| 194 | + type: Invidious::Search::Filters::Type::Video, |
| 195 | + date: Invidious::Search::Filters::Date::Year |
| 196 | + ) |
| 197 | + ) |
| 198 | + end |
| 199 | + end |
| 200 | +end |
0 commit comments