foxygit / Torlinkc Log in
commits tags

/tests/test_filter.cpp · 2.37 KB

raw
#include <doctest/doctest.h>

#include "torlinkc/sources/registry.hpp"
#include "torlinkc/ui/filter.hpp"

using namespace torlinkc;
using namespace torlinkc::ui;

namespace {

TorrentResult r(std::string infoHash, std::string name, int seeders, std::string source = "yts") {
  TorrentResult t;
  t.infoHash = infoHash;
  t.name = std::move(name);
  t.seeders = seeders;
  t.source = std::move(source);
  t.magnet = "magnet:?xt=urn:btih:" + infoHash;
  return t;
}

std::vector<std::string> ids(const std::vector<TorrentResult>& list) {
  std::vector<std::string> out;
  for (const auto& t : list) out.push_back(t.infoHash);
  return out;
}

}  // namespace

TEST_CASE("filterResults passes everything through when hideDead is off") {
  const auto sources = sourceById();
  std::vector<TorrentResult> list = {r("a", "a", 0), r("b", "b", 5)};
  CHECK(ids(filterResults(list, false, sources)) == std::vector<std::string>{"a", "b"});
}

TEST_CASE("filterResults drops zero-seeder results when hideDead is on") {
  const auto sources = sourceById();
  std::vector<TorrentResult> list = {r("a", "a", 0), r("b", "b", 1), r("c", "c", 0)};
  CHECK(ids(filterResults(list, true, sources)) == std::vector<std::string>{"b"});
}

TEST_CASE("filterResults keeps zero-seeder rows from sources that report no health data") {
  const auto sources = sourceById();
  std::vector<TorrentResult> list = {
      r("a", "a", 0, "fitgirl"),
      r("b", "b", 0, "yts"),
      r("c", "c", 0, "subsplease"),
      r("d", "d", 3, "yts"),
  };
  CHECK(ids(filterResults(list, true, sources)) == std::vector<std::string>{"a", "c", "d"});
}

TEST_CASE("filterResults does not mutate the input") {
  const auto sources = sourceById();
  std::vector<TorrentResult> list = {r("a", "a", 0), r("b", "b", 2)};
  const auto before = ids(list);
  filterResults(list, true, sources);
  CHECK(ids(list) == before);
}

TEST_CASE("filterResults filters by text matching all tokens and ranks exact matches higher") {
  const auto sources = sourceById();
  std::vector<TorrentResult> list = {
      r("a", "ubuntu 24 desktop", 0),
      r("b", "ubuntu desktop 24.04", 0),
      r("c", "debian 12", 0),
      r("d", "24 ubuntu desktop", 0),
  };
  // "ubuntu 24" -> a: exact substring (score 60), b: in-order (score 30),
  // d: out of order (score 10), c: no match.
  CHECK(ids(filterResults(list, false, sources, "ubuntu 24")) == std::vector<std::string>{"a", "b", "d"});
}