|
8 | 8 | * query-param decoding, route pattern matching. |
9 | 9 | * 2. Live-socket integration tests against the full UI server |
10 | 10 | * (http_server.c) on an ephemeral port: routing, CORS policy, |
11 | | - * RPC dispatch, transport limits, receive deadline, clean shutdown. |
| 11 | + * native read APIs, transport limits, receive deadline, clean shutdown. |
12 | 12 | */ |
13 | 13 | #include "../src/foundation/compat.h" |
14 | 14 | #if defined(__APPLE__) |
|
28 | 28 | #include "ui/http_server.h" |
29 | 29 | #include <store/store.h> |
30 | 30 | #include <watcher/watcher.h> |
| 31 | +#include <yyjson/yyjson.h> |
31 | 32 |
|
32 | 33 | #include <stdio.h> |
33 | 34 | #include <stdatomic.h> |
@@ -862,6 +863,156 @@ static bool ui_adr_equals(const ui_delete_fixture_t *fx, const char *project, |
862 | 863 | return equal; |
863 | 864 | } |
864 | 865 |
|
| 866 | +/* Real project stores, source files, and HTTP sockets: native routes must |
| 867 | + * preserve the operation payloads and their continuation metadata. */ |
| 868 | +TEST(ui_server_native_read_endpoints) { |
| 869 | + ui_delete_fixture_t fx; |
| 870 | + ASSERT_EQ(ui_delete_fixture_init(&fx), 0); |
| 871 | + char path[1024]; |
| 872 | + snprintf(path, sizeof(path), "%s/code.c", fx.root_dir); |
| 873 | + ASSERT_EQ(th_write_file(path, "first\nsecond\nthird\nfourth\n"), 0); |
| 874 | + for (int i = 0; i < 2; i++) { |
| 875 | + const char *project = i == 0 ? "alpha" : "beta"; |
| 876 | + ui_delete_db_path(&fx, project, path, sizeof(path)); |
| 877 | + cbm_store_t *store = cbm_store_open_path(path); |
| 878 | + ASSERT_NOT_NULL(store); |
| 879 | + ASSERT_EQ(cbm_store_upsert_project(store, project, fx.root_dir), CBM_STORE_OK); |
| 880 | + cbm_node_t node = {.project = project, |
| 881 | + .label = "Function", |
| 882 | + .name = "demo", |
| 883 | + .qualified_name = "app::demo & more", |
| 884 | + .file_path = "code.c", |
| 885 | + .start_line = 1, |
| 886 | + .end_line = 4}; |
| 887 | + int64_t function = cbm_store_upsert_node(store, &node); |
| 888 | + ASSERT_GT(function, 0); |
| 889 | + node.label = "Class"; |
| 890 | + node.name = "Container"; |
| 891 | + node.qualified_name = "app::Container"; |
| 892 | + int64_t container = cbm_store_upsert_node(store, &node); |
| 893 | + ASSERT_GT(container, 0); |
| 894 | + cbm_edge_t edge = { |
| 895 | + .project = project, .source_id = function, .target_id = container, .type = "CALLS"}; |
| 896 | + ASSERT_GT(cbm_store_insert_edge(store, &edge), 0); |
| 897 | + cbm_store_close(store); |
| 898 | + } |
| 899 | + th_server_t ts; |
| 900 | + ASSERT_EQ(th_server_start(&ts), 0); |
| 901 | + int port = cbm_http_server_port(ts.srv); |
| 902 | + bool projects_ok = true, schema_ok = true, snippet_ok = true, errors_ok = true; |
| 903 | + for (int kind = 0; kind < 3; kind++) { |
| 904 | + int offset = 0; |
| 905 | + int pages = kind == 1 ? 3 : 2; |
| 906 | + for (int page = 0; page < pages; page++) { |
| 907 | + char request[512], response[16384], pool[32768]; |
| 908 | + if (kind == 0) { |
| 909 | + snprintf(request, sizeof(request), |
| 910 | + "GET /api/projects?limit=1&offset=%d HTTP/1.1\r\n\r\n", offset); |
| 911 | + } else if (kind == 1) { |
| 912 | + snprintf(request, sizeof(request), |
| 913 | + "GET /api/schema?project=alpha&limit=1&offset=%d HTTP/1.1\r\n\r\n", |
| 914 | + offset); |
| 915 | + } else { |
| 916 | + snprintf( |
| 917 | + request, sizeof(request), |
| 918 | + "GET /api/snippet?project=alpha&qualified_name=app%%3A%%3Ademo%%20%%26%%20more" |
| 919 | + "&source_mode=full&max_lines=2&start_line=%d HTTP/1.1\r\n\r\n", |
| 920 | + offset + 1); |
| 921 | + } |
| 922 | + int n = th_http(port, request, response, sizeof(response)); |
| 923 | + char *body = n > 0 ? strstr(response, "\r\n\r\n") : NULL; |
| 924 | + body = body ? body + 4 : NULL; |
| 925 | + yyjson_alc alc; |
| 926 | + yyjson_alc_pool_init(&alc, pool, sizeof(pool)); |
| 927 | + yyjson_doc *doc = body ? yyjson_read_opts(body, strlen(body), 0, &alc, NULL) : NULL; |
| 928 | + yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL; |
| 929 | + bool ok = n > 0 && th_status(response) == 200 && yyjson_is_obj(root) && |
| 930 | + strstr(response, "Content-Type: application/json") != NULL; |
| 931 | + bool more = page + 1 < pages; |
| 932 | + if (kind == 0) { |
| 933 | + yyjson_val *rows = yyjson_obj_get(root, "projects"); |
| 934 | + const char *name = yyjson_get_str(yyjson_obj_get(yyjson_arr_get(rows, 0), "name")); |
| 935 | + ok = ok && yyjson_get_int(yyjson_obj_get(root, "total")) == 2 && |
| 936 | + yyjson_arr_size(rows) == 1 && name && |
| 937 | + strcmp(name, page == 0 ? "alpha" : "beta") == 0; |
| 938 | + } else if (kind == 1) { |
| 939 | + yyjson_val *rows = yyjson_obj_get(root, page < 2 ? "node_labels" : "edge_types"); |
| 940 | + const char *name = yyjson_get_str( |
| 941 | + yyjson_obj_get(yyjson_arr_get(rows, 0), page < 2 ? "label" : "type")); |
| 942 | + const char *expected = page == 0 ? "Class" : page == 1 ? "Function" : "CALLS"; |
| 943 | + ok = ok && name && strcmp(name, expected) == 0 && |
| 944 | + yyjson_get_int(yyjson_obj_get(root, "total")) == 3 && |
| 945 | + yyjson_get_int(yyjson_obj_get(root, "returned")) == 1 && |
| 946 | + yyjson_arr_size(yyjson_obj_get(root, "node_labels")) + |
| 947 | + yyjson_arr_size(yyjson_obj_get(root, "edge_types")) == |
| 948 | + 1; |
| 949 | + } else { |
| 950 | + const char *source = yyjson_get_str(yyjson_obj_get(root, "source")); |
| 951 | + ok = ok && source && |
| 952 | + strcmp(source, page == 0 ? "first\nsecond\n" : "third\nfourth\n") == 0 && |
| 953 | + yyjson_get_int(yyjson_obj_get(root, "start_line")) == offset + 1; |
| 954 | + } |
| 955 | + const char *more_key = kind == 2 ? "source_truncated" : "has_more"; |
| 956 | + const char *next_key = kind == 2 ? "next_start_line" : "next_offset"; |
| 957 | + ok = ok && yyjson_get_bool(yyjson_obj_get(root, more_key)) == more; |
| 958 | + if (more) { |
| 959 | + int next = (int)yyjson_get_int(yyjson_obj_get(root, next_key)); |
| 960 | + ok = ok && next == (kind == 2 ? 3 : offset + 1); |
| 961 | + offset = kind == 2 ? next - 1 : next; |
| 962 | + } |
| 963 | + if (kind == 0) |
| 964 | + projects_ok = projects_ok && ok; |
| 965 | + else if (kind == 1) |
| 966 | + schema_ok = schema_ok && ok; |
| 967 | + else |
| 968 | + snippet_ok = snippet_ok && ok; |
| 969 | + if (doc) |
| 970 | + yyjson_doc_free(doc); |
| 971 | + } |
| 972 | + } |
| 973 | + static const char *const invalid[] = { |
| 974 | + "/api/projects?limit=no", |
| 975 | + "/api/projects?offset=-1", |
| 976 | + "/api/projects?limit=2147483648", |
| 977 | + "/api/projects?limit=1&limit=2", |
| 978 | + "/api/schema", |
| 979 | + "/api/schema?project=missing", |
| 980 | + "/api/schema?project=alpha&offset=%XX", |
| 981 | + "/api/snippet?project=alpha", |
| 982 | + "/api/snippet?project=alpha&qualified_name=missing", |
| 983 | + "/api/snippet?project=alpha&qualified_name=demo&include_neighbors=yes", |
| 984 | + }; |
| 985 | + for (size_t i = 0; i < sizeof(invalid) / sizeof(invalid[0]); i++) { |
| 986 | + char request[512], response[4096]; |
| 987 | + snprintf(request, sizeof(request), "GET %s HTTP/1.1\r\n\r\n", invalid[i]); |
| 988 | + int n = th_http(port, request, response, sizeof(response)); |
| 989 | + errors_ok = errors_ok && n > 0 && th_status(response) == 400 && |
| 990 | + strstr(response, "\"error\"") != NULL; |
| 991 | + } |
| 992 | + static const char *const routes[] = {"projects", "schema", "snippet"}; |
| 993 | + for (size_t i = 0; i < sizeof(routes) / sizeof(routes[0]); i++) { |
| 994 | + char request[512], response[4096]; |
| 995 | + snprintf(request, sizeof(request), |
| 996 | + "GET /api/%s HTTP/1.1\r\nOrigin: https://foreign.invalid\r\n\r\n", routes[i]); |
| 997 | + int n = th_http(port, request, response, sizeof(response)); |
| 998 | + errors_ok = errors_ok && n > 0 && th_status(response) == 403 && |
| 999 | + strstr(response, "Access-Control-Allow-Origin: *") == NULL; |
| 1000 | + snprintf(request, sizeof(request), |
| 1001 | + "POST /api/%s HTTP/1.1\r\nContent-Type: application/json\r\n" |
| 1002 | + "Content-Length: 2\r\n\r\n{}", |
| 1003 | + routes[i]); |
| 1004 | + n = th_http(port, request, response, sizeof(response)); |
| 1005 | + errors_ok = errors_ok && n > 0 && th_status(response) == 404; |
| 1006 | + } |
| 1007 | + th_server_stop(&ts); |
| 1008 | + ui_delete_fixture_cleanup(&fx); |
| 1009 | + ASSERT_TRUE(projects_ok); |
| 1010 | + ASSERT_TRUE(schema_ok); |
| 1011 | + ASSERT_TRUE(snippet_ok); |
| 1012 | + ASSERT_TRUE(errors_ok); |
| 1013 | + PASS(); |
| 1014 | +} |
| 1015 | + |
865 | 1016 | TEST(ui_server_readiness_proof_is_exact_and_generation_bound) { |
866 | 1017 | static const char challenge[] = |
867 | 1018 | "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"; |
@@ -2378,6 +2529,7 @@ SUITE(httpd) { |
2378 | 2529 | /* Full UI server */ |
2379 | 2530 | RUN_TEST(ui_server_readiness_proof_is_exact_and_generation_bound); |
2380 | 2531 | RUN_TEST(ui_server_rejects_non_loopback_host); |
| 2532 | + RUN_TEST(ui_server_native_read_endpoints); |
2381 | 2533 | RUN_TEST(ui_server_unknown_path_404); |
2382 | 2534 | RUN_TEST(ui_server_process_kill_route_is_unavailable); |
2383 | 2535 | RUN_TEST(ui_server_routes_indexing_through_joinable_daemon_executor); |
|
0 commit comments