|
10 | 10 | #include "../src/foundation/log.h" |
11 | 11 | #include "../src/foundation/platform.h" /* cbm_file_size */ |
12 | 12 | #include "../src/foundation/mem.h" /* cbm_mem_set_budget_for_tests — over-budget seam */ |
| 13 | +#include "../src/foundation/str_util.h" |
13 | 14 | #include "../src/foundation/subprocess.h" |
14 | 15 | #include "../src/foundation/workspace.h" |
15 | 16 | #include "../src/mcp/compact_out.h" |
@@ -18334,6 +18335,186 @@ static bool issue704_make_db(const char *dir, const char *filename, const char * |
18334 | 18335 | return ok; |
18335 | 18336 | } |
18336 | 18337 |
|
| 18338 | +/* Cache entry classification must use exact internal names. CI workspace |
| 18339 | + * paths can produce real project names beginning with one or more underscores. */ |
| 18340 | +TEST(tool_underscore_projects_visible_in_cache_scans) { |
| 18341 | + char cache[CBM_SZ_512]; |
| 18342 | + int path_len = snprintf(cache, sizeof(cache), "%s/cbm-uscore-cache-XXXXXX", cbm_tmpdir()); |
| 18343 | + ASSERT_TRUE(path_len > 0 && (size_t)path_len < sizeof(cache)); |
| 18344 | + ASSERT_NOT_NULL(cbm_mkdtemp(cache)); |
| 18345 | + const char *saved = getenv("CBM_CACHE_DIR"); |
| 18346 | + char *saved_copy = saved ? cbm_strdup(saved) : NULL; |
| 18347 | + bool setup = (!saved || saved_copy) && cbm_setenv("CBM_CACHE_DIR", cache, 1) == 0; |
| 18348 | + bool seeded = setup && |
| 18349 | + issue704_make_db(cache, "__w-org-uscorerepo.db", "__w-org-uscorerepo", |
| 18350 | + "uscore_target_fn") && |
| 18351 | + issue704_make_db(cache, "_renamed.db", "uscore-drift", "uscore_drift_fn") && |
| 18352 | + issue704_make_db(cache, "_config-extra.db", "_config-extra", "uscore_extra_fn") && |
| 18353 | + issue704_make_db(cache, CBM_CONFIG_DB_FILENAME, "hidden-config", "hidden_fn") && |
| 18354 | + issue704_make_db(cache, CBM_CROSS_REPO_DB_FILENAME, "hidden-cross", "hidden_fn"); |
| 18355 | + cbm_mcp_server_t *srv = seeded ? cbm_mcp_server_new(NULL) : NULL; |
| 18356 | + bool server_created = srv != NULL; |
| 18357 | + bool listed = false; |
| 18358 | + bool queried[3] = {false}; |
| 18359 | + bool hinted = false; |
| 18360 | + bool hidden = false; |
| 18361 | + if (srv) { |
| 18362 | + char *response = cbm_mcp_handle_tool(srv, "list_projects", "{\"format\":\"json\"}"); |
| 18363 | + char *inner = response ? extract_text_content(response) : NULL; |
| 18364 | + yyjson_doc *doc = inner ? yyjson_read(inner, strlen(inner), 0) : NULL; |
| 18365 | + if (doc) { |
| 18366 | + yyjson_val *root = yyjson_doc_get_root(doc); |
| 18367 | + yyjson_val *projects = yyjson_obj_get(root, "projects"); |
| 18368 | + bool found[3] = {false}; |
| 18369 | + const char *expected[] = {"__w-org-uscorerepo", "uscore-drift", "_config-extra"}; |
| 18370 | + for (size_t i = 0; i < yyjson_arr_size(projects); i++) { |
| 18371 | + const char *name = |
| 18372 | + yyjson_get_str(yyjson_obj_get(yyjson_arr_get(projects, i), "name")); |
| 18373 | + for (size_t j = 0; j < 3; j++) { |
| 18374 | + found[j] = found[j] || (name && strcmp(name, expected[j]) == 0); |
| 18375 | + } |
| 18376 | + } |
| 18377 | + listed = yyjson_arr_size(projects) == 3 && |
| 18378 | + yyjson_get_int(yyjson_obj_get(root, "total")) == 3 && found[0] && found[1] && |
| 18379 | + found[2]; |
| 18380 | + yyjson_doc_free(doc); |
| 18381 | + } |
| 18382 | + free(inner); |
| 18383 | + free(response); |
| 18384 | + const char *args[] = { |
| 18385 | + "{\"project\":\"__w-org-uscorerepo\",\"name_pattern\":\"uscore_target_fn\"}", |
| 18386 | + "{\"project\":\"uscorerepo\",\"name_pattern\":\"uscore_target_fn\"}", |
| 18387 | + "{\"project\":\"uscore-drift\",\"name_pattern\":\"uscore_drift_fn\"}"}; |
| 18388 | + const char *targets[] = {"uscore_target_fn", "uscore_target_fn", "uscore_drift_fn"}; |
| 18389 | + for (size_t i = 0; i < 3; i++) { |
| 18390 | + response = cbm_mcp_handle_tool(srv, "search_graph", args[i]); |
| 18391 | + queried[i] = response && !strstr(response, "\"isError\":true") && |
| 18392 | + !strstr(response, "project not found") && strstr(response, targets[i]); |
| 18393 | + free(response); |
| 18394 | + } |
| 18395 | + response = |
| 18396 | + cbm_mcp_handle_tool(srv, "search_graph", |
| 18397 | + "{\"project\":\"unknown-uscore-project\",\"name_pattern\":\".*\"}"); |
| 18398 | + hinted = response && strstr(response, "project not found") && |
| 18399 | + strstr(response, "__w-org-uscorerepo") && strstr(response, "uscore-drift") && |
| 18400 | + strstr(response, "_config-extra") && !strstr(response, "hidden-config") && |
| 18401 | + !strstr(response, "hidden-cross"); |
| 18402 | + free(response); |
| 18403 | + response = cbm_mcp_handle_tool( |
| 18404 | + srv, "search_graph", "{\"project\":\"hidden-config\",\"name_pattern\":\"hidden_fn\"}"); |
| 18405 | + hidden = response && strstr(response, "project not found"); |
| 18406 | + free(response); |
| 18407 | + } |
| 18408 | + cbm_mcp_server_free(srv); |
| 18409 | + bool restored = !setup || (saved_copy ? cbm_setenv("CBM_CACHE_DIR", saved_copy, 1) |
| 18410 | + : cbm_unsetenv("CBM_CACHE_DIR")) == 0; |
| 18411 | + free(saved_copy); |
| 18412 | + bool removed = th_rmtree(cache) == 0; |
| 18413 | + ASSERT_TRUE(setup && seeded && server_created); |
| 18414 | + ASSERT_TRUE(restored && removed); |
| 18415 | + ASSERT_TRUE(listed); |
| 18416 | + ASSERT_TRUE(queried[0] && queried[1] && queried[2]); |
| 18417 | + ASSERT_TRUE(hinted && hidden); |
| 18418 | + PASS(); |
| 18419 | +} |
| 18420 | + |
| 18421 | +/* The fifth cache scan adopts an existing root owner before reindexing. |
| 18422 | + * Ignoring its underscore name would create a second path-derived project. */ |
| 18423 | +TEST(tool_reindex_reuses_underscore_root_owner) { |
| 18424 | + char repo[CBM_SZ_4K]; |
| 18425 | + char cache[CBM_SZ_4K]; |
| 18426 | + int repo_len = snprintf(repo, sizeof(repo), "%s/cbm-uscore-repo-XXXXXX", cbm_tmpdir()); |
| 18427 | + int cache_len = snprintf(cache, sizeof(cache), "%s/cbm-uscore-root-cache-XXXXXX", cbm_tmpdir()); |
| 18428 | + ASSERT_TRUE(repo_len > 0 && (size_t)repo_len < sizeof(repo)); |
| 18429 | + ASSERT_TRUE(cache_len > 0 && (size_t)cache_len < sizeof(cache)); |
| 18430 | + bool repo_created = cbm_mkdtemp(repo) != NULL; |
| 18431 | + bool cache_created = repo_created && cbm_mkdtemp(cache) != NULL; |
| 18432 | + char canonical[CBM_SZ_4K]; |
| 18433 | + bool setup = cache_created && cbm_canonical_path(repo, canonical, sizeof(canonical)); |
| 18434 | + if (setup) { |
| 18435 | + cbm_normalize_path_sep(canonical); |
| 18436 | + } |
| 18437 | + const char *keys[] = {"CBM_CACHE_DIR", "CBM_INDEX_SUPERVISOR"}; |
| 18438 | + char *saved[2] = {NULL}; |
| 18439 | + bool present[2] = {false}; |
| 18440 | + for (size_t i = 0; i < 2; i++) { |
| 18441 | + const char *value = getenv(keys[i]); |
| 18442 | + present[i] = value != NULL; |
| 18443 | + saved[i] = value ? cbm_strdup(value) : NULL; |
| 18444 | + setup = setup && (!value || saved[i]); |
| 18445 | + } |
| 18446 | + bool env_changed = setup; |
| 18447 | + if (setup) { |
| 18448 | + setup = cbm_setenv(keys[0], cache, 1) == 0 && cbm_setenv(keys[1], "0", 1) == 0; |
| 18449 | + } |
| 18450 | + char path[CBM_SZ_4K]; |
| 18451 | + int length = setup ? snprintf(path, sizeof(path), "%s/mod.py", repo) : -1; |
| 18452 | + FILE *source = length > 0 && (size_t)length < sizeof(path) ? cbm_fopen(path, "w") : NULL; |
| 18453 | + bool written = source && fputs("def underscore_root_target():\n return 1\n", source) >= 0; |
| 18454 | + if (source) { |
| 18455 | + written = fclose(source) == 0 && written; |
| 18456 | + } |
| 18457 | + length = setup ? snprintf(path, sizeof(path), "%s/_root-owner.db", cache) : -1; |
| 18458 | + cbm_store_t *store = |
| 18459 | + written && length > 0 && (size_t)length < sizeof(path) ? cbm_store_open_path(path) : NULL; |
| 18460 | + bool seeded = |
| 18461 | + store && cbm_store_upsert_project(store, "_root-owner", canonical) == CBM_STORE_OK; |
| 18462 | + if (store) { |
| 18463 | + cbm_store_close(store); |
| 18464 | + } |
| 18465 | + cbm_mcp_server_t *srv = seeded ? cbm_mcp_server_new(NULL) : NULL; |
| 18466 | + bool server_created = srv != NULL; |
| 18467 | + bool session_bound = srv && cbm_mcp_server_set_session_context(srv, canonical, canonical); |
| 18468 | + bool indexed = false; |
| 18469 | + bool resolved = false; |
| 18470 | + bool no_duplicate = false; |
| 18471 | + if (session_bound) { |
| 18472 | + yyjson_mut_doc *args = yyjson_mut_doc_new(NULL); |
| 18473 | + yyjson_mut_val *object = args ? yyjson_mut_obj(args) : NULL; |
| 18474 | + char *serialized = NULL; |
| 18475 | + if (object) { |
| 18476 | + yyjson_mut_doc_set_root(args, object); |
| 18477 | + if (yyjson_mut_obj_add_str(args, object, "repo_path", canonical)) { |
| 18478 | + serialized = yyjson_mut_write(args, 0, NULL); |
| 18479 | + } |
| 18480 | + } |
| 18481 | + char *response = |
| 18482 | + serialized ? cbm_mcp_handle_tool(srv, "index_repository", serialized) : NULL; |
| 18483 | + indexed = |
| 18484 | + response && !strstr(response, "\"isError\":true") && strstr(response, "_root-owner"); |
| 18485 | + free(response); |
| 18486 | + free(serialized); |
| 18487 | + yyjson_mut_doc_free(args); |
| 18488 | + response = cbm_mcp_handle_tool( |
| 18489 | + srv, "search_graph", |
| 18490 | + "{\"project\":\"_root-owner\",\"name_pattern\":\"underscore_root_target\"}"); |
| 18491 | + resolved = response && !strstr(response, "\"isError\":true") && |
| 18492 | + strstr(response, "underscore_root_target") && |
| 18493 | + !strstr(response, "project not found"); |
| 18494 | + free(response); |
| 18495 | + char *derived = cbm_project_name_from_path(canonical); |
| 18496 | + length = derived ? snprintf(path, sizeof(path), "%s/%s.db", cache, derived) : -1; |
| 18497 | + no_duplicate = derived && strcmp(derived, "_root-owner") != 0 && length > 0 && |
| 18498 | + (size_t)length < sizeof(path) && !cbm_file_exists(path); |
| 18499 | + free(derived); |
| 18500 | + } |
| 18501 | + cbm_mcp_server_free(srv); |
| 18502 | + bool restored = true; |
| 18503 | + for (size_t i = 0; i < 2; i++) { |
| 18504 | + if (env_changed) { |
| 18505 | + int rc = present[i] ? cbm_setenv(keys[i], saved[i], 1) : cbm_unsetenv(keys[i]); |
| 18506 | + restored = rc == 0 && restored; |
| 18507 | + } |
| 18508 | + free(saved[i]); |
| 18509 | + } |
| 18510 | + bool repo_removed = !repo_created || th_rmtree(repo) == 0; |
| 18511 | + bool cache_removed = !cache_created || th_rmtree(cache) == 0; |
| 18512 | + ASSERT_TRUE(setup && written && seeded && server_created && session_bound); |
| 18513 | + ASSERT_TRUE(restored && repo_removed && cache_removed); |
| 18514 | + ASSERT_TRUE(indexed && resolved && no_duplicate); |
| 18515 | + PASS(); |
| 18516 | +} |
| 18517 | + |
18337 | 18518 | TEST(tool_resolve_store_by_internal_name_issue704) { |
18338 | 18519 | char cache[256]; |
18339 | 18520 | snprintf(cache, sizeof(cache), "/tmp/cbm-issue704-XXXXXX"); |
@@ -21998,6 +22179,8 @@ SUITE(mcp) { |
21998 | 22179 | RUN_TEST(snippet_source_invalid_utf8); |
21999 | 22180 | RUN_TEST(tool_bad_project_name_no_overflow_issue235); |
22000 | 22181 | RUN_TEST(tool_bad_project_error_valid_json_issue235); |
| 22182 | + RUN_TEST(tool_underscore_projects_visible_in_cache_scans); |
| 22183 | + RUN_TEST(tool_reindex_reuses_underscore_root_owner); |
22001 | 22184 | RUN_TEST(tool_resolve_store_by_internal_name_issue704); |
22002 | 22185 | RUN_TEST(tool_list_projects_ignores_missed_shadow_issue1044); |
22003 | 22186 |
|
|
0 commit comments