query_spec.cr 7.5 KB

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