From 30e027092bc98d998e5f1de5218d0d8d1b7f2757 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 2 Nov 2024 15:43:03 -0400 Subject: [PATCH] test: Cover more ways to request apps and files. --- apps/test.json | 4 ++++ apps/test/app.js | 1 + apps/test/hello.txt | 1 + src/httpd.js.c | 3 ++- src/ssb.db.c | 4 ++++ src/tests.c | 32 +++++++++++++++++++++++++++++++- 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 apps/test.json create mode 100644 apps/test/app.js create mode 100644 apps/test/hello.txt diff --git a/apps/test.json b/apps/test.json new file mode 100644 index 00000000..400f9c97 --- /dev/null +++ b/apps/test.json @@ -0,0 +1,4 @@ +{ + "type": "tildefriends-app", + "emoji": "📦" +} diff --git a/apps/test/app.js b/apps/test/app.js new file mode 100644 index 00000000..7533113b --- /dev/null +++ b/apps/test/app.js @@ -0,0 +1 @@ +app.setDocument('

Maybe one day this app will run tests, but for now there is nothing to see here.

'); \ No newline at end of file diff --git a/apps/test/hello.txt b/apps/test/hello.txt new file mode 100644 index 00000000..5dd01c17 --- /dev/null +++ b/apps/test/hello.txt @@ -0,0 +1 @@ +Hello, world! \ No newline at end of file diff --git a/src/httpd.js.c b/src/httpd.js.c index c824fc3f..98e4d97b 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -1077,7 +1077,8 @@ static void _httpd_endpoint_view_work(tf_ssb_t* ssb, void* user_data) } else { - tf_ssb_db_blob_get(ssb, blob_id, (uint8_t**)&view->data, &view->size); + bool result = tf_ssb_db_blob_get(ssb, blob_id, (uint8_t**)&view->data, &view->size); + tf_printf("ssb=%p result=%d blob_id=[%s] data=%p size=%zd\n", ssb, result, blob_id, view->data, view->size); } } } diff --git a/src/ssb.db.c b/src/ssb.db.c index 0dad2091..c359d6de 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -638,6 +638,10 @@ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_ } result = true; } + else + { + tf_printf("OOPS %s\n", sqlite3_errmsg(db)); + } sqlite3_finalize(statement); } tf_ssb_release_db_reader(ssb, db); diff --git a/src/tests.c b/src/tests.c index 085a5d3c..cddc18a1 100644 --- a/src/tests.c +++ b/src/tests.c @@ -4,6 +4,8 @@ #include "http.h" #include "log.h" #include "mem.h" +#include "ssb.h" +#include "ssb.db.h" #include "ssb.tests.h" #include "util.js.h" @@ -828,6 +830,10 @@ static void _http_check_body_contains(const char* url, const char* expected) tf_printf("%s => found: \"%s\"\n", url, expected); } } + if (!found) + { + tf_printf("Didn't find \"%s\" in %s.\n", expected, url); + } assert(found); assert(file); int status = pclose(file); @@ -844,23 +850,35 @@ static void _test_httpd(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run -b 0 --db-path=out/test_db0.sqlite" TEST_ARGS, options->exe_path); + uv_stdio_container_t stdio[] = + { + [STDIN_FILENO] = { .flags = UV_IGNORE }, + [STDOUT_FILENO] = { .flags = UV_INHERIT_FD }, + [STDERR_FILENO] = { .flags = UV_INHERIT_FD }, + }; uv_process_t process = { 0 }; uv_spawn(&loop, &process, &(uv_process_options_t) { .file = options->exe_path, .args = (char*[]) { (char*)options->exe_path, "run", "-b0", "--db-path=out/test_db0.sqlite", "--http-port=8080", "--https-port=0", NULL }, + .stdio_count = sizeof(stdio) / sizeof(*stdio), + .stdio = stdio, }); for (int i = 0; i < 100; i++) { - if (_http_get_status_code("http://localhost:8080/mem") == 200) + if (_http_get_status_code("http://localhost:8080/debug") == 200) { break; } uv_sleep(1000); } + tf_ssb_t* ssb = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL); + const char* app_id = tf_ssb_db_get_property(ssb, "core", "path:test"); + tf_ssb_destroy(ssb); + _http_check_status_code("http://localhost:8080/404", 404); _http_check_status_code("http://localhost:8080/", 303); _http_check_status_code("http://localhost:8080/~core/apps/", 200); @@ -868,6 +886,18 @@ static void _test_httpd(const tf_test_options_t* options) _http_check_status_code("http://localhost:8080/~core/apps/view", 200); _http_check_body_contains("http://localhost:8080/~core/apps/", "Tilde Friends"); _http_check_body_contains("http://localhost:8080/~core/apps/view", "\"type\":\"tildefriends-app\""); + _http_check_body_contains("http://localhost:8080/~core/test/hello.txt", "Hello, world!"); + _http_check_status_code("http://localhost:8080/~core/test/nonexistent.txt", 404); + _http_check_body_contains("http://localhost:8080/&MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=.sha256/view", "Hello, world!"); + + char url[1024]; + snprintf(url, sizeof(url), "http://localhost:8080/%s/", app_id); + _http_check_body_contains(url, "Tilde Friends"); + snprintf(url, sizeof(url), "http://localhost:8080/%s/view", app_id); + _http_check_body_contains(url, "\"type\":\"tildefriends-app\""); + snprintf(url, sizeof(url), "http://localhost:8080/%s/hello.txt", app_id); + _http_check_body_contains(url, "Hello, world!"); + tf_free((void*)app_id); uv_process_kill(&process, SIGTERM); uv_close((uv_handle_t*)&process, NULL);