foreseen

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

commit 4d10e1e5d43e84be093bdc9ff3f04a6c047587d8
parent 4228c2f922a980c015332e4cc96fc70d3540a1ac
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 28 Sep 2026 17:28:14 -0500

Download path stuff + tests

Diffstat:
Minclude/browser.hpp | 2+-
Msrc/browser.cpp | 15+++++++++++++--
Msrc/main.cpp | 3++-
Mtests/browser-test.cpp | 32++++++++++++++++++++++++++------
Mtests/mem-leak-test.cpp | 10+++++-----
5 files changed, 47 insertions(+), 15 deletions(-)

diff --git a/include/browser.hpp b/include/browser.hpp @@ -37,7 +37,7 @@ class Browser { void setDone(int threadIdx); void refresh(); Site* getCurrentSite(); - std::string downloadPage(); + std::string tryDownloadPage(std::string destinationPath = "") noexcept; Link* getCurrentLink(); std::optional<uri> getPriorUri(); std::vector<std::pair<std::string, TextRender>> renderSite(); diff --git a/src/browser.cpp b/src/browser.cpp @@ -16,12 +16,23 @@ // TODO: Wrap this because it could probably throw. -std::string Browser::downloadPage() { +std::string Browser::tryDownloadPage(std::string downloadPath) noexcept { std::string body = currentSite->getBody(); Link* current = getCurrentLink(); assert(current != nullptr); // calling download page should always happen from a page... + std::string destination = encodeAsFilename(current->getLinkDestination()); - writeStringToFile(body, destination); + + if(downloadPath != "") { + destination = downloadPath; + } + + try { + writeStringToFile(body, destination); + } catch (...) { + return ""; + } + return destination; } diff --git a/src/main.cpp b/src/main.cpp @@ -376,7 +376,8 @@ int main(int argc, char** argv) { b.goForward(); ds.y = 0; // todo: make this part of state somewhere. } else if(input == 'd') { - b.downloadPage(); + // TODO: Handle outLocation == "" meaning failed + std::string outLocation = b.tryDownloadPage(); } else if(input == 'b') { b.goBack(); ds.y = 0; // todo: make this part of state somewhere. diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp @@ -1,4 +1,5 @@ #include <catch2/catch_test_macros.hpp> +#include <experimental/filesystem> #include <rapidcheck.h> #include <cstdlib> #include <ctime> @@ -10,6 +11,22 @@ #include "../include/utils.hpp" +std::string genRandom(const int len) { + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + std::string tmp_s; + tmp_s.reserve(len); + + for (int i = 0; i < len; ++i) { + tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + return tmp_s; +} + + TEST_CASE( "Basic navigation" ) { Browser b{}; b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/basic.gmi", true); @@ -69,7 +86,7 @@ TEST_CASE("Test page downloading.") { Browser b{}; b.goToSite("gemini://blog.laack.co"); REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://blog.laack.co"); - std::string destination = b.downloadPage(); + std::string destination = b.tryDownloadPage(); std::string out = readFileToString(destination); REQUIRE(out == b.getCurrentSite()->getBody()); REQUIRE(std::filesystem::remove(destination)); @@ -95,11 +112,14 @@ TEST_CASE("Sanitize characters to draw tests") { } TEST_CASE("Never crash from weird user inputs") { - rc::check("Never crash from weird inputs", [] (std::vector<std::string> const ls) { + for(int i = 0; i < 5; ++i) { Browser b {}; - if(ls.size() > 20) { - return; + + std::vector<std::string> ls {}; + for(int x = 0; x < 10; ++x) { + ls.push_back(genRandom(rand() % 5000)); } + for(auto& str : ls) { auto res = handleDestinationResolution(str, false); if(res.t == STRING_DESTINATION) { @@ -109,7 +129,7 @@ TEST_CASE("Never crash from weird user inputs") { } b.refresh(); b.goBack(); - RC_ASSERT(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); } - }); + } } diff --git a/tests/mem-leak-test.cpp b/tests/mem-leak-test.cpp @@ -7,7 +7,7 @@ #include <ctime> #include <unistd.h> -std::string gen_random(const int len) { +std::string genRandom(const int len) { static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -47,18 +47,18 @@ void traversal() { b.goBack(); } else if (rand() % 5 == 0){ if(rand() % 5 == 0) { - std::string dst = std::string{"gemini://"} + gen_random(rand() % 5000) + ".com"; + std::string dst = std::string{"gemini://"} + genRandom(rand() % 5000) + ".com"; std::cout << "TRAVELLING TO: " << dst << std::endl; b.goToSite(dst); } else { - std::string dst = std::string{"gemini://"} + gen_random(rand() % 15) + ".com"; + std::string dst = std::string{"gemini://"} + genRandom(rand() % 15) + ".com"; std::cout << "TRAVELLING TO: " << dst << std::endl; b.goToSite(dst); } } else if (rand() % 20 == 0) { - Destination destination = handleDestinationResolution(gen_random(rand() % 50), false); + Destination destination = handleDestinationResolution(genRandom(rand() % 50), false); if(rand() % 2 == 0) { - destination = handleDestinationResolution(gen_random(rand() % 5), false); + destination = handleDestinationResolution(genRandom(rand() % 5), false); } switch (destination.t) { case NO_DESTINATION: