Skip to content

Commit efdbdd1

Browse files
committed
test: cover UTF-8 search output paths
Signed-off-by: anfedoro <anfedoro@gmail.com>
1 parent a513934 commit efdbdd1

2 files changed

Lines changed: 183 additions & 5 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8683,16 +8683,17 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matc
86838683
build_dedup_files_array(doc, sr, output_count, raw, raw_count));
86848684
} else {
86858685
/* json-stringified tree: cols + column-ordered row arrays. FULL mode
8686-
* appends a per-row object cell with the (guarded, windowed) source —
8687-
* attach_result_source semantics unchanged. */
8686+
* appends a per-row object cell with the (guarded, windowed) source;
8687+
* context requests append the corresponding context object. */
8688+
bool attach_context = context_lines > 0 && mode != MODE_FULL;
86888689
yyjson_mut_val *jcols = yyjson_mut_arr(doc);
86898690
static const char *const sc_cols[] = {"qn", "label", "file", "lines",
86908691
"matches", "in", "out"};
86918692
for (size_t ci = 0; ci < sizeof(sc_cols) / sizeof(sc_cols[0]); ci++) {
86928693
yyjson_mut_arr_add_str(doc, jcols, sc_cols[ci]);
86938694
}
8694-
if (mode == MODE_FULL) {
8695-
yyjson_mut_arr_add_str(doc, jcols, "source");
8695+
if (mode == MODE_FULL || attach_context) {
8696+
yyjson_mut_arr_add_str(doc, jcols, mode == MODE_FULL ? "source" : "context");
86968697
}
86978698
yyjson_mut_obj_add_val(doc, root_obj, "cols", jcols);
86988699

@@ -8718,7 +8719,7 @@ static char *assemble_search_output(search_result_t *sr, int sr_count, grep_matc
87188719
yyjson_mut_arr_add_val(row, ml);
87198720
yyjson_mut_arr_add_int(doc, row, r->in_degree);
87208721
yyjson_mut_arr_add_int(doc, row, r->out_degree);
8721-
if (mode == MODE_FULL) {
8722+
if (mode == MODE_FULL || attach_context) {
87228723
yyjson_mut_val *src = yyjson_mut_obj(doc);
87238724
attach_result_source(doc, src, r, mode, context_lines, root_path);
87248725
yyjson_mut_arr_add_val(row, src);

‎tests/test_mcp.c‎

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3655,6 +3655,8 @@ TEST(search_code_multi_word) {
36553655
}
36563656

36573657
/* Regression guard: search_code full results must preserve valid UTF-8 source. */
3658+
static bool is_valid_json_response(const char *json);
3659+
36583660
TEST(search_code_full_preserves_utf8_source) {
36593661
char tmp[512];
36603662
snprintf(tmp, sizeof(tmp), "/tmp/cbm_srch_utf8_XXXXXX");
@@ -3724,6 +3726,178 @@ TEST(search_code_full_preserves_utf8_source) {
37243726
PASS();
37253727
}
37263728

3729+
TEST(search_code_raw_match_preserves_utf8_content) {
3730+
char tmp[256];
3731+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
3732+
ASSERT_NOT_NULL(srv);
3733+
3734+
char raw_path[512];
3735+
snprintf(raw_path, sizeof(raw_path), "%s/project/raw.md", tmp);
3736+
const char raw_source[] = "header\nraw-Русский content\n";
3737+
FILE *fp = cbm_fopen(raw_path, "wb");
3738+
ASSERT_NOT_NULL(fp);
3739+
ASSERT_EQ(fwrite(raw_source, 1, sizeof(raw_source) - SKIP_ONE, fp),
3740+
sizeof(raw_source) - SKIP_ONE);
3741+
ASSERT_EQ(fclose(fp), 0);
3742+
3743+
cbm_store_t *st = cbm_mcp_server_store(srv);
3744+
ASSERT_NOT_NULL(st);
3745+
cbm_node_t node = {.project = "test-project",
3746+
.label = "Section",
3747+
.name = "raw",
3748+
.qualified_name = "test-project.raw",
3749+
.file_path = "raw.md",
3750+
.start_line = 1,
3751+
.end_line = 1};
3752+
ASSERT_GT(cbm_store_upsert_node(st, &node), 0);
3753+
3754+
char *resp = cbm_mcp_server_handle(
3755+
srv, "{\"jsonrpc\":\"2.0\",\"id\":98,\"method\":\"tools/call\","
3756+
"\"params\":{\"name\":\"search_code\",\"arguments\":{"
3757+
"\"project\":\"test-project\",\"pattern\":\"raw-\","
3758+
"\"file_pattern\":\"*.md\",\"format\":\"json\",\"limit\":5}}}");
3759+
ASSERT_NOT_NULL(resp);
3760+
char *inner = extract_text_content(resp);
3761+
ASSERT_NOT_NULL(inner);
3762+
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
3763+
ASSERT_NOT_NULL(doc);
3764+
yyjson_val *raw_obj = yyjson_obj_get(yyjson_doc_get_root(doc), "raw_matches");
3765+
ASSERT_NOT_NULL(raw_obj);
3766+
yyjson_val *raw_rows = yyjson_obj_get(raw_obj, "rows");
3767+
ASSERT_NOT_NULL(raw_rows);
3768+
ASSERT_TRUE(yyjson_arr_size(raw_rows) > 0);
3769+
yyjson_val *raw_row = yyjson_arr_get(raw_rows, 0);
3770+
ASSERT_STR_EQ(yyjson_get_str(yyjson_arr_get(raw_row, 2)), "raw-Русский content");
3771+
yyjson_doc_free(doc);
3772+
3773+
free(inner);
3774+
free(resp);
3775+
cbm_mcp_server_free(srv);
3776+
cleanup_snippet_dir(tmp);
3777+
PASS();
3778+
}
3779+
3780+
TEST(search_code_context_preserves_utf8_context) {
3781+
char tmp[256];
3782+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
3783+
ASSERT_NOT_NULL(srv);
3784+
3785+
char context_path[512];
3786+
snprintf(context_path, sizeof(context_path), "%s/project/context.md", tmp);
3787+
const char context_source[] = "before-до\ncontext-needle\nпосле-после\n";
3788+
FILE *fp = cbm_fopen(context_path, "wb");
3789+
ASSERT_NOT_NULL(fp);
3790+
ASSERT_EQ(fwrite(context_source, 1, sizeof(context_source) - SKIP_ONE, fp),
3791+
sizeof(context_source) - SKIP_ONE);
3792+
ASSERT_EQ(fclose(fp), 0);
3793+
3794+
cbm_store_t *st = cbm_mcp_server_store(srv);
3795+
ASSERT_NOT_NULL(st);
3796+
cbm_node_t node = {.project = "test-project",
3797+
.label = "Section",
3798+
.name = "context",
3799+
.qualified_name = "test-project.context",
3800+
.file_path = "context.md",
3801+
.start_line = 1,
3802+
.end_line = 3};
3803+
ASSERT_GT(cbm_store_upsert_node(st, &node), 0);
3804+
3805+
char *resp = cbm_mcp_server_handle(
3806+
srv, "{\"jsonrpc\":\"2.0\",\"id\":99,\"method\":\"tools/call\","
3807+
"\"params\":{\"name\":\"search_code\",\"arguments\":{"
3808+
"\"project\":\"test-project\",\"pattern\":\"context-needle\","
3809+
"\"format\":\"json\",\"context\":1,\"limit\":5}}}");
3810+
ASSERT_NOT_NULL(resp);
3811+
char *inner = extract_text_content(resp);
3812+
ASSERT_NOT_NULL(inner);
3813+
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
3814+
ASSERT_NOT_NULL(doc);
3815+
yyjson_val *root = yyjson_doc_get_root(doc);
3816+
yyjson_val *cols = yyjson_obj_get(root, "cols");
3817+
yyjson_val *rows = yyjson_obj_get(root, "rows");
3818+
ASSERT_NOT_NULL(cols);
3819+
ASSERT_NOT_NULL(rows);
3820+
ASSERT_TRUE(yyjson_arr_size(rows) > 0);
3821+
3822+
size_t context_index = SIZE_MAX;
3823+
size_t col_count = yyjson_arr_size(cols);
3824+
for (size_t i = 0; i < col_count; i++) {
3825+
const char *col = yyjson_get_str(yyjson_arr_get(cols, i));
3826+
if (col && strcmp(col, "context") == 0) {
3827+
context_index = i;
3828+
break;
3829+
}
3830+
}
3831+
ASSERT_TRUE(context_index != SIZE_MAX);
3832+
yyjson_val *context_obj = yyjson_arr_get(yyjson_arr_get(rows, 0), context_index);
3833+
ASSERT_NOT_NULL(context_obj);
3834+
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(context_obj, "context")), context_source);
3835+
ASSERT_EQ(yyjson_get_int(yyjson_obj_get(context_obj, "context_start")), 1);
3836+
yyjson_doc_free(doc);
3837+
3838+
free(inner);
3839+
free(resp);
3840+
cbm_mcp_server_free(srv);
3841+
cleanup_snippet_dir(tmp);
3842+
PASS();
3843+
}
3844+
3845+
TEST(search_code_invalid_utf8_still_returns_valid_json) {
3846+
char tmp[256];
3847+
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
3848+
ASSERT_NOT_NULL(srv);
3849+
3850+
char invalid_path[512];
3851+
snprintf(invalid_path, sizeof(invalid_path), "%s/project/invalid.md", tmp);
3852+
static const unsigned char invalid_source[] = {
3853+
'i', 'n', 'v', 'a', 'l', 'i', 'd', '-', 'n', 'e', 'e', 'd', 'l', 'e', ' ',
3854+
0xFF, '\n',
3855+
};
3856+
FILE *fp = cbm_fopen(invalid_path, "wb");
3857+
ASSERT_NOT_NULL(fp);
3858+
ASSERT_EQ(fwrite(invalid_source, 1, sizeof(invalid_source), fp), sizeof(invalid_source));
3859+
ASSERT_EQ(fclose(fp), 0);
3860+
3861+
cbm_store_t *st = cbm_mcp_server_store(srv);
3862+
ASSERT_NOT_NULL(st);
3863+
cbm_node_t node = {.project = "test-project",
3864+
.label = "Section",
3865+
.name = "invalid",
3866+
.qualified_name = "test-project.invalid",
3867+
.file_path = "invalid.md",
3868+
.start_line = 1,
3869+
.end_line = 1};
3870+
ASSERT_GT(cbm_store_upsert_node(st, &node), 0);
3871+
3872+
char *resp = cbm_mcp_server_handle(
3873+
srv, "{\"jsonrpc\":\"2.0\",\"id\":100,\"method\":\"tools/call\","
3874+
"\"params\":{\"name\":\"search_code\",\"arguments\":{"
3875+
"\"project\":\"test-project\",\"pattern\":\"invalid-needle\","
3876+
"\"mode\":\"full\",\"format\":\"json\",\"limit\":5}}}");
3877+
ASSERT_NOT_NULL(resp);
3878+
char *inner = extract_text_content(resp);
3879+
ASSERT_NOT_NULL(inner);
3880+
ASSERT_TRUE(is_valid_json_response(inner));
3881+
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
3882+
ASSERT_NOT_NULL(doc);
3883+
yyjson_val *rows = yyjson_obj_get(yyjson_doc_get_root(doc), "rows");
3884+
ASSERT_NOT_NULL(rows);
3885+
ASSERT_TRUE(yyjson_arr_size(rows) > 0);
3886+
yyjson_val *source_obj = yyjson_arr_get(yyjson_arr_get(rows, 0), 7);
3887+
ASSERT_NOT_NULL(source_obj);
3888+
const char *safe_source = yyjson_get_str(yyjson_obj_get(source_obj, "source"));
3889+
ASSERT_NOT_NULL(safe_source);
3890+
ASSERT_NOT_NULL(strstr(safe_source, "\xEF\xBF\xBD"));
3891+
ASSERT_NULL(memchr(safe_source, 0xFF, strlen(safe_source)));
3892+
yyjson_doc_free(doc);
3893+
3894+
free(inner);
3895+
free(resp);
3896+
cbm_mcp_server_free(srv);
3897+
cleanup_snippet_dir(tmp);
3898+
PASS();
3899+
}
3900+
37273901
/* Reproduce-first (#687): scoped content search over a repo whose ROOT PATH
37283902
* contains a space. write_scoped_filelist emits "<root>/<file>" records that the
37293903
* Unix pipeline pipes to grep via xargs. With plain `xargs` (newline-split) the
@@ -9693,6 +9867,9 @@ SUITE(mcp) {
96939867
RUN_TEST(tool_search_code_no_project);
96949868
RUN_TEST(search_code_multi_word);
96959869
RUN_TEST(search_code_full_preserves_utf8_source);
9870+
RUN_TEST(search_code_raw_match_preserves_utf8_content);
9871+
RUN_TEST(search_code_context_preserves_utf8_context);
9872+
RUN_TEST(search_code_invalid_utf8_still_returns_valid_json);
96969873
RUN_TEST(search_code_scoped_path_with_spaces_issue687);
96979874
#ifdef _WIN32
96989875
RUN_TEST(search_code_scoped_path_with_cjk_root_issue903);

0 commit comments

Comments
 (0)