foreseen

A text-based gemini browser with prefetching
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

pure-test.cpp (18172B)


      1 // Testing pure functions
      2 
      3 #include <rapidcheck.h>
      4 #include <rapidcheck/Check.h>
      5 
      6 #include <catch2/catch_test_macros.hpp>
      7 #include <catch2/generators/catch_generators.hpp>
      8 #include <cstdlib>
      9 #include <ctime>
     10 #include <filesystem>
     11 #include <optional>
     12 #include <utility>
     13 
     14 #include "../include/browser.hpp"
     15 #include "../include/shared.hpp"
     16 #include "../include/utils.hpp"
     17 
     18 TEST_CASE("Test cli input handling") {
     19     SECTION("CLI inputs maintain arbitrary schemas") {
     20         std::unordered_map<std::string, std::string> expectations;
     21         expectations["https://github.com"] = "https://github.com";
     22         expectations["whatever://github.com"] = "whatever://github.com";
     23         expectations["about://github.com"] = "about://github.com";
     24         expectations["gopher://github.com"] = "gopher://github.com";
     25         expectations["gemini://github.com"] = "gemini://github.com";
     26         expectations["file:///home/whatever"] = "file:///home/whatever";
     27         expectations["localhost"] = "gemini://localhost";
     28         for (auto& expect : expectations) {
     29             REQUIRE(
     30                 handleDestinationResolution(expect.first, true).destination ==
     31                 expect.second);
     32             REQUIRE(handleDestinationResolution(expect.first, true).t ==
     33                     STRING_DESTINATION);
     34         }
     35     }
     36 
     37     SECTION("Resolve relative file paths via the CLI if they exist") {
     38         std::unordered_map<std::string, std::string> expectations;
     39         std::string cwd = std::filesystem::current_path();
     40         expectations["tests/sites/basic.gmi"] =
     41             "file://" + cwd + "/tests/sites/basic.gmi";
     42         expectations["tests/sites/basic_2.gmi"] =
     43             "file://" + cwd + "/tests/sites/basic_2.gmi";
     44         expectations[cwd + "/tests/sites/basic_2.gmi"] =
     45             "file://" + cwd + "/tests/sites/basic_2.gmi";
     46         for (auto& expect : expectations) {
     47             REQUIRE(
     48                 handleDestinationResolution(expect.first, true).destination ==
     49                 expect.second);
     50             REQUIRE(handleDestinationResolution(expect.first, true).t ==
     51                     STRING_DESTINATION);
     52         }
     53     }
     54 
     55     SECTION("CLI inputs without schema that aren't files") {
     56         std::unordered_map<std::string, std::string> expectations;
     57         expectations["tlgs.one"] = "gemini://tlgs.one";
     58         expectations["laack.co"] = "gemini://laack.co";
     59         expectations["blog.laack.co/pygame-vs-raylib.gmi"] =
     60             "gemini://blog.laack.co/pygame-vs-raylib.gmi";
     61         for (auto& expect : expectations) {
     62             REQUIRE(
     63                 handleDestinationResolution(expect.first, true).destination ==
     64                 expect.second);
     65             REQUIRE(handleDestinationResolution(expect.first, true).t ==
     66                     STRING_DESTINATION);
     67         }
     68     }
     69 }
     70 
     71 TEST_CASE("Verify urls with numeric prefix are resolved correctly") {
     72     REQUIRE(handleDestinationResolution("123movies.com", false).t ==
     73             STRING_DESTINATION);
     74     REQUIRE(handleDestinationResolution("123movies.com", false).destination ==
     75             "gemini://123movies.com");
     76 }
     77 
     78 TEST_CASE("Inputs with spaces outside of the cli are always searched") {
     79     if (DEFAULT_SEARCH_ENGINE.substr(DEFAULT_SEARCH_ENGINE.size() - 1) != "?") {
     80         DEFAULT_SEARCH_ENGINE += "?";
     81     }
     82 
     83     auto d1 = handleDestinationResolution("what :// :// test", false);
     84     REQUIRE(d1.destination ==
     85             "gemini://tlgs.one/search?what%20%3A%2F%2F%20%3A%2F%2F%20test");
     86     rc::check(
     87         "Inputs with spaces outside of the cli are always searched",
     88         [](const std::string& st) {
     89             auto dest = handleDestinationResolution(st, false);
     90             if (st.find(' ') != std::string::npos) {
     91                 RC_ASSERT(dest.destination.find("tlgs.one") !=
     92                           std::string::npos);
     93                 REQUIRE(dest.destination.find("tlgs.one") != std::string::npos);
     94             }
     95         });
     96 }
     97 
     98 TEST_CASE("Test user input handling for destinations") {
     99     if (DEFAULT_SEARCH_ENGINE.substr(DEFAULT_SEARCH_ENGINE.size() - 1) != "?") {
    100         DEFAULT_SEARCH_ENGINE += "?";
    101     }
    102 
    103     SECTION("Convert numbers to link numbers") {
    104         for (int i = 0; i < 10000; ++i) {
    105             REQUIRE(handleDestinationResolution(std::to_string(i), false)
    106                         .linkNumber == i);
    107             REQUIRE(handleDestinationResolution(std::to_string(i), false).t ==
    108                     NUMBER_DESTINATION);
    109         }
    110     }
    111 
    112     SECTION("Transparently resolve gemini:// prefixed urls") {
    113         REQUIRE(handleDestinationResolution("gemini://test.com", false).t ==
    114                 STRING_DESTINATION);
    115         REQUIRE(handleDestinationResolution("gemini://test.com", false)
    116                     .destination == "gemini://test.com");
    117     }
    118 
    119     SECTION("Convert input with spaces into query") {
    120         REQUIRE(handleDestinationResolution("what is the capital of scotland?",
    121                                             false)
    122                     .t == STRING_DESTINATION);
    123         REQUIRE(handleDestinationResolution("what is the capital of scotland?",
    124                                             false)
    125                     .destination ==
    126                 "gemini://tlgs.one/search?" +
    127                     urlEncode("what is the capital of scotland?"));
    128     }
    129 
    130     SECTION("Convert {domain} without scheme -> gemini://{domain}") {
    131         REQUIRE(handleDestinationResolution("laack.co", false).t ==
    132                 STRING_DESTINATION);
    133         REQUIRE(handleDestinationResolution("laack.co", false).destination ==
    134                 "gemini://laack.co");
    135     }
    136 
    137     SECTION("Support localhost") {
    138         REQUIRE(handleDestinationResolution("localhost", false).destination ==
    139                 "gemini://localhost");
    140         REQUIRE(handleDestinationResolution("gemini://localhost", false)
    141                     .destination == "gemini://localhost");
    142         REQUIRE(handleDestinationResolution("gemini://localhost:1965", false)
    143                     .destination == "gemini://localhost:1965");
    144         REQUIRE(
    145             handleDestinationResolution("localhost:1965", false).destination ==
    146             "gemini://localhost:1965");
    147     }
    148 
    149     SECTION("Transparently resolve file names") {
    150         REQUIRE(handleDestinationResolution("file:///test/whatever", false).t ==
    151                 STRING_DESTINATION);
    152         REQUIRE(handleDestinationResolution("file:///test/whatever", false)
    153                     .destination == "file:///test/whatever");
    154     }
    155 
    156     SECTION("Convert empty string to no-destination") {
    157         REQUIRE(handleDestinationResolution("", false).t == NO_DESTINATION);
    158     }
    159 
    160     SECTION("Handle arbitrary string inputs for CLI input") {
    161         rc::check("Handle arbitrary string inputs for CLI input",
    162                   [](const std::string& st) {
    163                       auto dest = handleDestinationResolution(st, true);
    164                       RC_ASSERT(dest.t != NUMBER_DESTINATION);
    165                       REQUIRE(dest.t != NUMBER_DESTINATION);
    166                       switch (dest.t) {
    167                           case NO_DESTINATION:
    168                               break;
    169                           case STRING_DESTINATION:
    170                               break;
    171                           case NUMBER_DESTINATION:
    172                               break;
    173                       }
    174                   });
    175     }
    176 
    177     SECTION("Handle arbitrary string inputs for non-CLI input") {
    178         rc::check("Handle arbitrary string inputs for non-CLI input",
    179                   [](const std::string& st) {
    180                       auto dest = handleDestinationResolution(st, false);
    181 
    182                       if (dest.t == STRING_DESTINATION) {
    183                           RC_ASSERT(dest.destination != "");
    184                           REQUIRE(dest.destination != "");
    185                       }
    186                   });
    187     }
    188 }
    189 
    190 TEST_CASE("Test width == 0 doesn't crash program") {
    191     std::vector<std::pair<std::string, TextRender>> strLs{};
    192     for (int i = 0; i < 10; ++i) {
    193         std::pair<std::string, TextRender> current{"this is a simple test line",
    194                                                    TextRender{10, false}};
    195         strLs.push_back(current);
    196     }
    197     REQUIRE_NOTHROW(breakLines(strLs, 0, 80));
    198 }
    199 
    200 TEST_CASE("Don't fold doesn't fold lines") {
    201     std::vector<std::pair<std::string, TextRender>> strLs{};
    202     for (int i = 0; i < 10; ++i) {
    203         std::pair<std::string, TextRender> current{"this is a simple test line",
    204                                                    TextRender{10, true, false}};
    205         strLs.push_back(current);
    206     }
    207     auto result = breakLines(strLs, 10, 80);
    208     REQUIRE(result.size() == strLs.size());
    209 }
    210 
    211 TEST_CASE("Test trivial line breaking") {
    212     std::vector<std::pair<std::string, TextRender>> strLs{};
    213     for (int i = 0; i < 10; ++i) {
    214         std::pair<std::string, TextRender> current{"this is a simple test line",
    215                                                    TextRender{10, false}};
    216         strLs.push_back(current);
    217     }
    218     auto result = breakLines(strLs, 10, 80);
    219     REQUIRE(result.size() == 30);
    220 }
    221 
    222 static std::string charset =
    223     "abc defghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    224 
    225 TEST_CASE("Test width invariant") {
    226     std::vector<std::pair<std::string, TextRender>> strLs{};
    227 
    228     srand(std::time(NULL));
    229 
    230     for (int i = 0; i < 100; ++i) {
    231         std::string strRnd;
    232         int ub = rand() % 100;
    233         for (int x = 0; x < ub; ++x) {
    234             strRnd += charset[rand() % charset.length()];
    235         }
    236         std::pair<std::string, TextRender> current{strRnd,
    237                                                    TextRender{10, false}};
    238         strLs.push_back(current);
    239     }
    240 
    241     auto result = breakLines(strLs, 80, 80);
    242 
    243     for (auto& res : result) {
    244         REQUIRE(res.first.size() <= 80);
    245     }
    246 }
    247 
    248 TEST_CASE("Test lots of spaces") {
    249     std::vector<std::pair<std::string, TextRender>> strLs{};
    250 
    251     srand(std::time(NULL));
    252 
    253     for (int i = 0; i < 10; ++i) {
    254         std::string strRnd;
    255         int ub = rand() % 100;
    256         for (int x = 0; x < ub; ++x) {
    257             strRnd += ' ';
    258         }
    259         std::pair<std::string, TextRender> current{strRnd,
    260                                                    TextRender{10, false}};
    261         strLs.push_back(current);
    262     }
    263 
    264     auto result = breakLines(strLs, 80, 80);
    265 
    266     for (auto& res : result) {
    267         REQUIRE(res.first.size() <= 80);
    268     }
    269 }
    270 
    271 TEST_CASE("Test normal line classification") {
    272     SECTION("Plaintext classification") {
    273         auto res1 = lineToLine("", std::nullopt, -1, false);
    274         REQUIRE(res1->type() == PLAINTEXT);
    275     }
    276 
    277     SECTION("Preformatted classification") {
    278         auto res2 = lineToLine("", std::nullopt, -1, true);
    279         REQUIRE(res2->type() == PREFORMATTED);
    280     }
    281 
    282     SECTION("Link classification") {
    283         auto res3 = lineToLine("=> gemini://laack.co", std::nullopt, 1, false);
    284         REQUIRE(res3->type() == LINK);
    285     }
    286 
    287     SECTION("Format switch classification") {
    288         auto res4 = lineToLine("```", std::nullopt, 1, false);
    289         REQUIRE(res4->type() == FORMAT_SWITCH);
    290     }
    291 
    292     SECTION("H1 classification") {
    293         auto res5 = lineToLine("# H1 Heading", std::nullopt, 1, false);
    294         REQUIRE(res5->type() == H1);
    295     }
    296 
    297     SECTION("H2 classification") {
    298         auto res6 = lineToLine("## H2 Heading", std::nullopt, 1, false);
    299         REQUIRE(res6->type() == H2);
    300     }
    301 
    302     SECTION("H3 classification") {
    303         auto res7 = lineToLine("### H3 Heading", std::nullopt, 1, false);
    304         REQUIRE(res7->type() == H3);
    305     }
    306 
    307     SECTION("Quote classification") {
    308         auto res8 = lineToLine("> test quote", std::nullopt, 1, false);
    309         REQUIRE(res8->type() == QUOTE);
    310     }
    311 
    312     SECTION("List item classification") {
    313         auto res9 = lineToLine("* test li", std::nullopt, 1, false);
    314         REQUIRE(res9->type() == LIST_ITEM);
    315     }
    316 }
    317 
    318 TEST_CASE("Test line parsing handles whitespace correctly") {
    319     std::unordered_map<std::string, LineType> prefixes{};
    320 
    321     prefixes[">"] = QUOTE;
    322     prefixes["#"] = H1;
    323     prefixes["##"] = H2;
    324     prefixes["###"] = H3;
    325 
    326     std::vector<std::string> options{" ", "\t"};
    327 
    328     for (auto& prefix : prefixes) {
    329         for (int x = 0; x < 100; ++x) {
    330             std::string acc = options[rand() % 2];
    331 
    332             for (int i = 0; i < 1000; ++i) {
    333                 std::string check = prefix.first + acc + "test line content";
    334                 auto ln = lineToLine(check, std::nullopt, 1, false);
    335                 REQUIRE(ln->type() == prefix.second);
    336                 REQUIRE(ln->textToDraw() ==
    337                         prefix.first + " test line content\n");
    338                 acc += options[rand() % 2];
    339             }
    340         }
    341     }
    342 
    343     std::string acc = options[rand() % 2];
    344 
    345     rc::check(
    346         "any non-zero number of whitespaces results in the same link text",
    347         [](const std::vector<bool>& l0) {
    348             std::string acc = "";
    349             if (l0.size() == 0) {
    350                 if (rand() % 2 == 0) {
    351                     acc = " ";
    352                 } else {
    353                     acc = "\t";
    354                 }
    355             }
    356             for (bool i : l0) {
    357                 if (i) {
    358                     acc += '\t';
    359                 } else {
    360                     acc += ' ';
    361                 }
    362             }
    363             std::string check =
    364                 "=>" + acc + "gemini://laack.co" + acc + "link human text";
    365             auto ln = lineToLine(check, std::nullopt, 1, false);
    366             RC_ASSERT(ln->type() == LINK);
    367             RC_ASSERT(ln->textToDraw() == "[1] link human text\n");
    368             REQUIRE(ln->type() == LINK);
    369             REQUIRE(ln->textToDraw() == "[1] link human text\n");
    370         });
    371 }
    372 
    373 TEST_CASE("Proper whitespace compliance") {
    374     SECTION("Line types without white spacing") {
    375         auto res1 = lineToLine("", std::nullopt, -1, false);
    376         REQUIRE(res1->type() == PLAINTEXT);
    377 
    378         auto res3 = lineToLine("=>gemini://laack.co", std::nullopt, 1, false);
    379         REQUIRE(res3->type() == LINK);
    380 
    381         auto res5 = lineToLine("#H1 Heading", std::nullopt, 1, false);
    382         REQUIRE(res5->type() == H1);
    383 
    384         auto res6 = lineToLine("##H2 Heading", std::nullopt, 1, false);
    385         REQUIRE(res6->type() == H2);
    386 
    387         auto res7 = lineToLine("###H3 Heading", std::nullopt, 1, false);
    388         REQUIRE(res7->type() == H3);
    389 
    390         auto res8 = lineToLine(">test quote", std::nullopt, 1, false);
    391         REQUIRE(res8->type() == QUOTE);
    392 
    393         // the only one that requires a whitespace, and it must be a space, not
    394         // tab
    395         auto res9 = lineToLine("*test li", std::nullopt, 1, false);
    396         REQUIRE(res9->type() == PLAINTEXT);
    397 
    398         auto res10 = lineToLine("*\ttest li", std::nullopt, 1, false);
    399         REQUIRE(res10->type() == PLAINTEXT);
    400 
    401         auto res11 = lineToLine("*  test li", std::nullopt, 1, false);
    402         REQUIRE(res11->type() == LIST_ITEM);
    403         REQUIRE(res11->textToDraw() == "*  test li\n");
    404     }
    405 
    406     SECTION("Non-whitespace based line types don't require whitespace") {
    407         auto res2 = lineToLine("", std::nullopt, -1, true);
    408         REQUIRE(res2->type() == PREFORMATTED);
    409 
    410         auto res4 = lineToLine("```", std::nullopt, 1, false);
    411         REQUIRE(res4->type() == FORMAT_SWITCH);
    412     }
    413 }
    414 
    415 TEST_CASE(
    416     "Line parser handles lines that are only format characters with "
    417     "whitespaces") {
    418     auto res1 = lineToLine(" ", std::nullopt, -1, false);
    419     REQUIRE(res1->type() == PLAINTEXT);
    420 
    421     auto res2 = lineToLine(" ", std::nullopt, -1, true);
    422     REQUIRE(res2->type() == PREFORMATTED);
    423 
    424     auto res3 = lineToLine("=> ", std::nullopt, 1, false);
    425     REQUIRE(res3->type() == LINK);
    426 
    427     auto res4 = lineToLine("``` ", std::nullopt, 1, false);
    428     REQUIRE(res4->type() == FORMAT_SWITCH);
    429 
    430     auto res5 = lineToLine("# ", std::nullopt, 1, false);
    431     REQUIRE(res5->type() == H1);
    432 
    433     auto res6 = lineToLine("## ", std::nullopt, 1, false);
    434     REQUIRE(res6->type() == H2);
    435 
    436     auto res7 = lineToLine("### ", std::nullopt, 1, false);
    437     REQUIRE(res7->type() == H3);
    438 
    439     auto res8 = lineToLine("> ", std::nullopt, 1, false);
    440     REQUIRE(res8->type() == QUOTE);
    441 
    442     auto res9 = lineToLine("* ", std::nullopt, 1, false);
    443     REQUIRE(res9->type() == LIST_ITEM);
    444 }
    445 
    446 // I was wrong on this originally bc the spec states whitespaces are optional.
    447 TEST_CASE(
    448     "Line parser handles lines that are only format characters without "
    449     "whitespaces") {
    450     auto res1 = lineToLine("", std::nullopt, -1, false);
    451     REQUIRE(res1->type() == PLAINTEXT);
    452 
    453     auto res2 = lineToLine("", std::nullopt, -1, true);
    454     REQUIRE(res2->type() == PREFORMATTED);
    455 
    456     auto res3 = lineToLine("=>", std::nullopt, 1, false);
    457     REQUIRE(res3->type() == LINK);
    458 
    459     auto res4 = lineToLine("```", std::nullopt, 1, false);
    460     REQUIRE(res4->type() == FORMAT_SWITCH);
    461 
    462     auto res5 = lineToLine("#", std::nullopt, 1, false);
    463     REQUIRE(res5->type() == H1);
    464 
    465     auto res6 = lineToLine("##", std::nullopt, 1, false);
    466     REQUIRE(res6->type() == H2);
    467 
    468     auto res7 = lineToLine("###", std::nullopt, 1, false);
    469     REQUIRE(res7->type() == H3);
    470 
    471     auto res8 = lineToLine(">", std::nullopt, 1, false);
    472     REQUIRE(res8->type() == QUOTE);
    473 
    474     auto res9 = lineToLine("*", std::nullopt, 1, false);
    475     REQUIRE(res9->type() == PLAINTEXT);
    476 }
    477 
    478 static std::string linecharset =
    479     "#>=abc##  de_-><0193248$#)(&@)(*&$#^*&#^%^&*&fghijklmnopqrstuvwxyz "
    480     "#````ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    481 
    482 TEST_CASE("Crash test random line strings") {
    483     rc::check("Crash test random line strings", [](const std::string& st) {
    484         auto* ln = lineToLine(st, std::nullopt, 1, false);
    485         delete ln;
    486     });
    487 }
    488 
    489 TEST_CASE("Test cache never exceeds CACHE_SIZE") {
    490     const int size = CACHE_SIZE;
    491     Cache c{};
    492 
    493     for (int i = 0; i < 10000; ++i) {
    494         c.addSite("gemini://" + std::to_string(i), Site("header", "body"));
    495         if (i >= size) {
    496             REQUIRE(c.getSite("gemini://" + std::to_string(i - size)) ==
    497                     std::nullopt);
    498         }
    499         int oldest = std::max(0, i - size + 1);
    500         REQUIRE(c.getSite("gemini://" + std::to_string(oldest)) !=
    501                 std::nullopt);
    502     }
    503 }