commit 11f5baa1493d561a505d9018268f219323b0532f
parent 3059dbcdbaca5b46fc57faf2471a71ce2155dc0d
Author: Andrew Laack <andrew@laack.co>
Date: Sun, 27 Sep 2026 15:11:32 -0500
Bunch more tests, refactoring, and fixed a bug with segfaults for page load failures.
Diffstat:
7 files changed, 118 insertions(+), 61 deletions(-)
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -33,7 +33,7 @@ class Browser {
Browser();
void setLinksOfCurrentLines();
~Browser();
- void goToSite(std::string url, bool addToHistory = true, bool refresh = false);
+ bool goToSite(std::string url, bool addToHistory = true, bool refresh = false);
void setDone(int threadIdx);
void refresh();
Site* getCurrentSite();
diff --git a/include/utils.hpp b/include/utils.hpp
@@ -27,4 +27,17 @@ std::vector<std::pair<std::string, TextRender>> breakLines(std::vector<std::pair
void writeStringToFile(std::string toWrite, std::string filePath);
std::string encodeAsFilename(uri link);
std::filesystem::path getHome();
-std::optional<std::string> handleDestinationResolution(std::string destination, bool isCli);
+
+enum ReturnType {
+ NUMBER_DESTINATION,
+ STRING_DESTINATION,
+ NO_DESTINATION
+};
+
+struct Destination {
+ int linkNumber;
+ std::string destination;
+ ReturnType t;
+};
+
+Destination handleDestinationResolution(std::string destination, bool isCli);
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -1,5 +1,6 @@
#include "../include/browser.hpp"
#include <cassert>
+#include <iostream>
#include <string>
#include <thread>
#include <unistd.h>
@@ -78,7 +79,7 @@ void Browser::refresh() {
goToSite(getPriorUri().value().to_string(), false, true);
}
-void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
+bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
Link* prior = nullptr;
@@ -108,7 +109,7 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
openThread = std::thread(openUrl, urlString);
delete destination;
- return;
+ return true;
}
@@ -135,10 +136,8 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
if(site != nullptr) {
delete site;
}
- previousIdx += 1;
delete destination;
- goBack();
- return;
+ return false;
}
if(addToHistory) {
@@ -174,6 +173,7 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
}
tryCacheTargets();
+ return true;
}
Identity Browser::getIdentity(uri uriInput) {
@@ -336,6 +336,8 @@ void Browser::goBack() {
// but only in cases where they are right next to each other without any other 2X status code sites
// between them.
+ Link* cur = getCurrentLink();
+
std::string starting = getCurrentLink()->getLinkDestination().to_string();
previousIdx -= 1;
@@ -354,7 +356,9 @@ void Browser::goBack() {
return; // safely fail with rollback
}
}
- goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false);
+ if(!goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false)) {
+ previousIdx = original;
+ }
} else {
previousIdx = original;
@@ -364,6 +368,9 @@ void Browser::goBack() {
void Browser::goForward() {
int original = previousIdx;
+
+ Link* cur = getCurrentLink();
+
std::string starting = getCurrentLink()->getLinkDestination().to_string();
previousIdx += 1;
@@ -384,7 +391,9 @@ void Browser::goForward() {
}
}
- goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false);
+ if(!goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false)) {
+ previousIdx = original;
+ }
} else {
previousIdx = original;
}
diff --git a/src/main.cpp b/src/main.cpp
@@ -4,6 +4,7 @@
#include <cassert>
#include <cstddef>
#include <filesystem>
+#include <iostream>
#include <ncurses.h>
#include <optional>
#include <string>
@@ -336,20 +337,20 @@ int main(int argc, char** argv) {
b.goToSite("about://newtab"); // baseline site so we can guarantee we have a starting point that's working.
-
std::string destination = "";
if(argc > 1) {
destination = argv[1];
}
- std::optional<std::string> cliDestination = handleDestinationResolution(destination, true);
+ Destination cliDestination = handleDestinationResolution(destination, true);
- if(cliDestination != std::nullopt) {
- b.goToSite(cliDestination.value());
+ // TODO: Refactor to switch statement
+ if(cliDestination.t != NO_DESTINATION) {
+ assert(cliDestination.linkNumber = 0);
+ b.goToSite(cliDestination.destination);
}
-
int input = 0;
// this is the main loop.
@@ -381,33 +382,19 @@ int main(int argc, char** argv) {
} else if(input == 'o') {
std::string locationToGo = openPageHandler(ds);
-
- // TODO: Check if this is an int and if it is try to use that link.
- // if it's not, then try to go to domain (should consider having a default search engine too.)
-
- try {
- b.followLinkNumber(std::stoi(locationToGo));
- } catch (...) {
- if(locationToGo != "") {
- if(locationToGo.find(":") == std::string::npos) { // TODO: Is this how we which scheme was specified?
-
- // This still feels weak, but my urlencode(locationToGo) != locationToGo was worse
- // because that removed stuff like tlgs.one/robots.txt and stuff like that.
- //
- // TODO: Make this a pure function and then write tests for it to define expected behavior.
-
- if(locationToGo.find('.') != std::string::npos && locationToGo.find(' ') == std::string::npos) {
- locationToGo = "gemini://" + locationToGo;
- } else {
- locationToGo = DEFAULT_SEARCH_ENGINE + urlEncode(locationToGo);
- }
- }
- b.goToSite(locationToGo);
- ds.y = 0; // todo: make this part of state somewhere.
-
- }
+ Destination destination = handleDestinationResolution(locationToGo, false);
+ switch(destination.t) {
+ case NUMBER_DESTINATION:
+ b.followLinkNumber(destination.linkNumber);
+ ds.y = 0;
+ break;
+ case STRING_DESTINATION:
+ b.goToSite(destination.destination);
+ ds.y = 0;
+ break;
+ case NO_DESTINATION:
+ break;
}
-
}
// this is the loop where we deal with redirects and stuff like that.
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -3,6 +3,7 @@
#include "../include/quote.hpp"
#include "../include/heading.hpp"
#include "../include/link.hpp"
+#include "../include/shared.hpp"
#include "../include/format-switch.hpp"
#include "../include/errors.hpp"
#include "../include/plaintext.hpp"
@@ -16,6 +17,7 @@
#include <iostream>
#include <regex>
#include <sstream>
+#include <stdexcept>
#include <string>
#include <utility>
#include <filesystem>
@@ -265,34 +267,64 @@ std::filesystem::path getHome()
return home;
}
-std::optional<std::string> handleDestinationResolution(std::string destination, bool isCli) {
+Destination handleDestinationResolution(std::string destination, bool isCli) {
+
+ Destination ret {};
+
+ if(destination == "") {
+ ret.t = NO_DESTINATION;
+ return ret;
+ }
if(isCli) {
- if(destination != "") {
- if(std::filesystem::exists(destination)) {
- std::string path = "file://" + std::filesystem::current_path().string() + "/" + destination;
+ if(std::filesystem::exists(destination)) {
+ std::string path = "file://" + std::filesystem::current_path().string() + "/" + destination;
- if(destination.find('/') == std::size_t(0)) {
- path = "file://" + destination;
- }
+ if(destination.find('/') == std::size_t(0)) {
+ path = "file://" + destination;
+ }
+
+ ret.destination = path;
+ ret.t = STRING_DESTINATION;
+ return ret;
+
+ } else {
+ std::string inputString = destination;
+ if(inputString.find("gemini://") == 0) {
+ ret.destination = destination;
+ ret.t = STRING_DESTINATION;
+ return ret;
+ } else if (inputString.find(':') == std::string::npos){
+ ret.destination = std::string {"gemini://"} + destination;
+ ret.t = STRING_DESTINATION;
+ return ret;
- return path;
+ } else {
+ ret.destination = destination;
+ ret.t = STRING_DESTINATION;
+ return ret;
- } else {
- std::string inputString = destination;
- if(inputString.find("gemini://") == 0) {
- return destination;
- } else if (inputString.find(':') == std::string::npos){
- return std::string {"gemini://"} + destination;
+ }
+ }
+ } else {
+ try {
+ int dest = std::stoi(destination);
+ ret.linkNumber = dest;
+ ret.t = NUMBER_DESTINATION;
+ return ret;
+ } catch (...) {
+ if(destination.find(":") == std::string::npos) {
+ if(destination.find('.') != std::string::npos && destination.find(' ') == std::string::npos) {
+ destination = "gemini://" + destination;
} else {
- return destination;
+ destination = DEFAULT_SEARCH_ENGINE + urlEncode(destination);
}
}
-
- } else {
- return std::nullopt;
+ ret.destination = destination;
+ ret.t = STRING_DESTINATION;
+ return ret;
}
- } else {
- throw NotImplemented();
+
}
+ throw std::logic_error("Unexpected input.");
}
diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp
@@ -25,3 +25,9 @@ TEST_CASE( "Local filesystem relative navigation" ) {
REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic_2.gmi"));
}
+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");
+}
diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp
@@ -1,5 +1,6 @@
// Testing pure functions
#include <catch2/catch_test_macros.hpp>
+#include <climits>
#include <cstdlib>
#include <ctime>
#include <filesystem>
@@ -34,11 +35,20 @@ TEST_CASE("Test cli input handling") {
expectations["blog.laack.co/pygame-vs-raylib.gmi"] = "gemini://blog.laack.co/pygame-vs-raylib.gmi";
for(auto& expect : expectations) {
- REQUIRE(handleDestinationResolution(expect.first, true) == expect.second);
+ REQUIRE(handleDestinationResolution(expect.first, true).destination == expect.second);
}
}
+TEST_CASE("Test user input handling for destinations") {
+
+ for(int i = 0; i < 10000; ++i) {
+ REQUIRE(handleDestinationResolution(std::to_string(i), false).linkNumber == i);
+ REQUIRE(handleDestinationResolution(std::to_string(i), false).t == NUMBER_DESTINATION);
+ }
+
+}
+
TEST_CASE("Test trivial line breaking") {
std::vector<std::pair<std::string, TextRender>> strLs {};
for(int i = 0; i < 10; ++i) {