foreseen

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

commit 78865b97c4707b36b12c530226bb9811743b7139
parent 7768e1cc4af922c3615a35fd274897768c20a3cb
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 29 Sep 2026 13:43:59 -0500

Coverage guided testing

Diffstat:
MTODO.txt | 2+-
Minclude/utils.hpp | 1-
Msrc/heading.cpp | 9+++------
Msrc/utils.cpp | 9---------
Mtests/browser-test.cpp | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/pure-test.cpp | 34++++++++++++++++++++++++++++++++++
Atests/sites/all_line_types.gmi | 40++++++++++++++++++++++++++++++++++++++++
Mtests/sites/line-return.gmi | 2++
8 files changed, 156 insertions(+), 17 deletions(-)

diff --git a/TODO.txt b/TODO.txt @@ -120,4 +120,4 @@ - proper argparse - man pages -- refactor tests to use built in catch2 generators + sections +- refactor makefile to only generate coverage when requested. diff --git a/include/utils.hpp b/include/utils.hpp @@ -15,7 +15,6 @@ struct TextRender { bool isPrefixed(std::string input, std::string prefix); std::string readFileToString(std::string filePath); bool isWhiteSpace(std::string& line, int idx); -std::string truncateAfter(std::string input, char truncate); std::vector<std::string> stringToList(std::string input); Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount, bool isPreformatted); std::string urlEncode(const std::string& value); diff --git a/src/heading.cpp b/src/heading.cpp @@ -31,9 +31,8 @@ std::string Heading::textToDraw() { return "## " + toDraw + "\n"; case 1: return "# " + toDraw + "\n"; - default: - throw NotImplemented(); } + throw NotImplemented(); } int Heading::getColor() { switch (headingLevel) { @@ -43,9 +42,8 @@ int Heading::getColor() { return COLOR_GREEN; case 1: return COLOR_RED; - default: - throw NotImplemented(); } + throw NotImplemented(); } LineType Heading::type() { switch (headingLevel) { @@ -55,9 +53,8 @@ LineType Heading::type() { return H2; case 1: return H1; - default: - throw NotImplemented(); } + throw NotImplemented(); } bool Heading::isBold() { return true; diff --git a/src/utils.cpp b/src/utils.cpp @@ -75,15 +75,6 @@ bool isWhiteSpace(std::string& line, int idx) { return false; } -std::string truncateAfter(std::string input, char truncate) { - for(int i = input.size() - 1; i >= 0; --i) { - if(input[i] == truncate) { - return input.substr(0,i+1); - } - } - return input; -} - std::vector<std::string> stringToList(std::string input) { std::vector<std::string> res; diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp @@ -27,6 +27,22 @@ std::string genRandom(const int len) { } +// TODO: Refactor this to use $HOME +TEST_CASE( "Basic rendering of all line types" ) { + Browser b{}; + b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/all_line_types.gmi", true); + REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/all_line_types.gmi")); + auto strLs = b.renderSite(); + auto res = breakLines(strLs, 80,200); + // TODO: Make this bound tighter by computing left pad amount. + for(auto& st: res) { + if(st.second.shouldFold) { + REQUIRE(st.first.size() <= 200); + } + } +} + + TEST_CASE( "Basic navigation" ) { Browser b{}; b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/basic.gmi", true); @@ -61,6 +77,33 @@ TEST_CASE("Test browser doesn't crash on invalid sites.") { REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); } + +TEST_CASE("Test browser doesn't crash when accessing sites with specified port number that is not responsive.") { + Browser b{}; + bool res = b.goToSite("gemini://laack.co:3847"); + REQUIRE(!res); + b.goForward(); + b.goBack(); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); +} + + +TEST_CASE("Test browser can access sites with specified (default) port number.") { + Browser b{}; + b.goToSite("gemini://laack.co:1965"); + REQUIRE(b.getCurrentSite()->getMeta() == "text/gemini;lang=en-US"); +} + + +// TODO: Test invalid meta lines (like the case where weird stuff is sent from server) +TEST_CASE("Test browser returns proper meta for visited sites.") { + Browser b{}; + b.goToSite("gemini://laack.co"); + // meta strips status. + REQUIRE(b.getCurrentSite()->getMeta() == "text/gemini;lang=en-US"); +} + + TEST_CASE("Test browser doesn't crash on invalid link number following, excessive back, and excessive forwards.") { Browser b{}; @@ -92,6 +135,35 @@ TEST_CASE("Test page downloading.") { REQUIRE(std::filesystem::remove(destination)); } + +TEST_CASE("Test downloading to specific location") { + std::filesystem::path cwd = std::filesystem::current_path(); + + Browser b{}; + b.goToSite("gemini://blog.laack.co"); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://blog.laack.co"); + std::string destination = b.tryDownloadPage(cwd / "out.gmi"); + std::string out = readFileToString(destination); + REQUIRE(out == b.getCurrentSite()->getBody()); + REQUIRE(destination == cwd / "out.gmi"); + REQUIRE(std::filesystem::remove(destination)); +} + + +TEST_CASE("Test going forwards doesn't break when accessing an invalid site prior.") { + Browser b{}; + bool res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie"); + REQUIRE(!res); + b.goForward(); +} + +TEST_CASE("Test going backwards doesn't break when accessing an invalid site prior.") { + Browser b{}; + bool res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie"); + REQUIRE(!res); + b.goBack(); +} + TEST_CASE("Test encoding allows relative linking with : in parameter") { Browser b{}; b.goToSite("gemini://tlgs.one/search"); @@ -103,7 +175,11 @@ TEST_CASE("Test encoding allows relative linking with : in parameter") { TEST_CASE("Sanitize characters to draw tests") { Browser b {}; bool res = b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/line-return.gmi"); + REQUIRE(res); + REQUIRE(b.getCurrentSite()->getStatusCode() >= 20); + REQUIRE(b.getCurrentSite()->getStatusCode() <= 29); + auto ls = b.renderSite(); sanitizeCharactersToDraw(ls); for(auto& line : ls) { diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -137,6 +137,26 @@ TEST_CASE("Test user input handling for destinations") { } + +TEST_CASE("Test width == 0 doesn't crash program") { + std::vector<std::pair<std::string, TextRender>> strLs {}; + for(int i = 0; i < 10; ++i) { + std::pair<std::string, TextRender> current {"this is a simple test line", TextRender{10,false}}; + strLs.push_back(current); + } + REQUIRE_NOTHROW(breakLines(strLs, 0,80)); +} + +TEST_CASE("Don't fold doesn't fold lines") { + std::vector<std::pair<std::string, TextRender>> strLs {}; + for(int i = 0; i < 10; ++i) { + std::pair<std::string, TextRender> current {"this is a simple test line", TextRender{10,true,false}}; + strLs.push_back(current); + } + auto result = breakLines(strLs, 10,80); + REQUIRE(result.size() == strLs.size()); +} + TEST_CASE("Test trivial line breaking") { std::vector<std::pair<std::string, TextRender>> strLs {}; for(int i = 0; i < 10; ++i) { @@ -405,3 +425,17 @@ TEST_CASE("Crash test random line strings") { delete ln; }); } + +TEST_CASE("Test cache never exceeds CACHE_SIZE") { + const int size = CACHE_SIZE; + Cache c{}; + + for (int i = 0; i < 10000; ++i) { + c.addSite("gemini://" + std::to_string(i), Site("header", "body")); + if (i >= size) { + REQUIRE(c.getSite("gemini://" + std::to_string(i - size)) == std::nullopt); + } + int oldest = std::max(0, i - size + 1); + REQUIRE(c.getSite("gemini://" + std::to_string(oldest)) != std::nullopt); + } +} diff --git a/tests/sites/all_line_types.gmi b/tests/sites/all_line_types.gmi @@ -0,0 +1,40 @@ +# H1 +## H1 +## H1 + +``` +tthis is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text.his is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text. +this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text. +this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text. +this is some preformatted text.this is some preformatted text.this is some preformatted text.this is some preformatted text. +``` + +=> basic.gmi this is a link)(*&)($*7230947123087)$(* & #)(#@$&)(#@*&$ )987(@0348794) + +> This is a quote + + +tthis is a normal line.this is a normal line.this is a normal line.this is a normal line.this is a normal line.this is a normal line.this is a normal line.this is a normal line.this is a normal line.this is a normal line.his is a normal line. + +this is a normal line. +this is a normal line. +this is a normal line. + +* list item 1 +* list item 2 +* list item 3 +* list item 4 +* + +=> +* +- +> +# +## +### + + + +``` + diff --git a/tests/sites/line-return.gmi b/tests/sites/line-return.gmi @@ -44,3 +44,4 @@ I'll go through them and add ones I like to the database of question/answer pair ---------------- => mailto:abraxas@hidden.nexus Contact => /about.gmi ℹ️ About + + \ No newline at end of file