foreseen

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

commit 72eaeb4ce0073bac51e477b88339857cf6f7fca6
parent f523778763f2a93208018cdd378fa7c32911c4a1
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 27 Sep 2026 16:18:33 -0500

Refactor so browser defaults to about page instead of requiring invocations of that sort for invariants

Diffstat:
Minclude/browser.hpp | 2+-
Minclude/errors.hpp | 5+++++
Msrc/browser.cpp | 13++++++++++---
Msrc/main.cpp | 3++-
Mtests/browser-test.cpp | 35++++++++++++++++++++++++++++++++++-
5 files changed, 52 insertions(+), 6 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(); - void downloadPage(); + std::string downloadPage(); Link* getCurrentLink(); std::optional<uri> getPriorUri(); std::vector<std::pair<std::string, TextRender>> renderSite(); diff --git a/include/errors.hpp b/include/errors.hpp @@ -9,6 +9,11 @@ public: }; +class BrowserStartError : public std::logic_error { + public: + BrowserStartError() : std::logic_error("An issue occurred while creating the browser") { }; +}; + class FileReadError : public std::invalid_argument { public: diff --git a/src/browser.cpp b/src/browser.cpp @@ -5,6 +5,7 @@ #include <thread> #include <unistd.h> #include "../include/site.hpp" +#include "../include/errors.hpp" #include "../include/gemini-client.hpp" #include "../include/utils.hpp" #include "../include/identity-manager.hpp" @@ -15,14 +16,15 @@ #include <vector> -void Browser::downloadPage() { +std::string Browser::downloadPage() { std::string body = currentSite->getBody(); Link* current = getCurrentLink(); assert(current != nullptr); // calling download page should always happen from a page... - writeStringToFile(body, encodeAsFilename(current->getLinkDestination())); + std::string destination = encodeAsFilename(current->getLinkDestination()); + writeStringToFile(body, destination); + return destination; } - void dispatch(std::vector<Link>* targets, Browser& b, int threadIdx) { std::vector<Link>& refT = *targets; for(int i = 0 ; i < (int)refT.size() && i < SITE_CACHE_LIMIT; ++i) { @@ -246,6 +248,11 @@ Browser::Browser() : threads(THREAD_NUM), done(THREAD_NUM){ d = true; } + // this ensures some nice invariants about the browser, like always having at least one valid page. + bool start = goToSite("about://newtab"); + if(!start) { + throw BrowserStartError{}; + } } // TODO: SHould add more stuff here too, like the links stuff. diff --git a/src/main.cpp b/src/main.cpp @@ -335,7 +335,8 @@ int main(int argc, char** argv) { initColors(); - b.goToSite("about://newtab"); // baseline site so we can guarantee we have a starting point that's working. + // TODO: Would it be possible to have this done by default so our tests can be more consistent with actual usage + // where we expect this to default happen? std::string destination = ""; diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp @@ -2,6 +2,7 @@ #include <cstdlib> #include <ctime> #include <iostream> +#include <filesystem> #include <utility> #include "../include/browser.hpp" #include "../include/utils.hpp" @@ -27,7 +28,39 @@ TEST_CASE( "Local filesystem relative navigation" ) { TEST_CASE("Test browser doesn't crash on invalid sites.") { Browser b{}; - b.goToSite("about://newtab"); b.goToSite("gemini://this_site_doesn_t_exsist"); REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); + b.goBack(); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); +} + +TEST_CASE("Test browser doesn't crash on invalid link number following, excessive back, and excessive forwards.") { + + Browser b{}; + b.goToSite("gemini://blog.laack.co/feed.xml"); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://blog.laack.co/feed.xml"); + for(int i = 0; i < 1000; ++i) { + b.followLinkNumber(i); + } + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://blog.laack.co/feed.xml"); + b.goBack(); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); + b.goForward(); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://blog.laack.co/feed.xml"); + b.goBack(); + b.goBack(); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); + b.goForward(); + b.goForward(); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://blog.laack.co/feed.xml"); +} + +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 out = readFileToString(destination); + REQUIRE(out == b.getCurrentSite()->getBody()); + REQUIRE(std::filesystem::remove(destination)); }