|
12 | 12 | #include "test_helpers.h" |
13 | 13 | #include "test_operation_host.h" |
14 | 14 | #include "operations/output_budget.h" |
| 15 | +#include "operations/result_wire.h" |
| 16 | +#include "cbm.h" |
15 | 17 | #include <yyjson/yyjson.h> |
16 | 18 | #include <store/store.h> |
17 | 19 | #include <pipeline/pipeline.h> |
@@ -106,6 +108,14 @@ static int create_test_project(void) { |
106 | 108 | fprintf(f, " budgetMarker%d();\n", i); |
107 | 109 | } |
108 | 110 | fprintf(f, "}\n"); |
| 111 | + fprintf(f, "\nfunction budgetSnippet() {\n"); |
| 112 | + for (int i = 0; i < 40; i++) { |
| 113 | + fprintf(f, |
| 114 | + " // line %02d: a long source line to exercise whole-line output budgeting " |
| 115 | + "abcdefghijklmnopqrstuvwxyz\n", |
| 116 | + i); |
| 117 | + } |
| 118 | + fprintf(f, "}\n"); |
109 | 119 | fclose(f); |
110 | 120 |
|
111 | 121 | return 0; |
@@ -544,6 +554,137 @@ TEST(integ_mcp_search_code_match_limit_and_budget) { |
544 | 554 | PASS(); |
545 | 555 | } |
546 | 556 |
|
| 557 | +TEST(integ_js_path_module_default_client) { |
| 558 | + char tmp[] = "/tmp/cbm_js_module_XXXXXX"; |
| 559 | + ASSERT_NOT_NULL(cbm_mkdtemp(tmp)); |
| 560 | + const char *paths[] = {".eslintrc.js", ".storybook/main.ts", "(group)/page.tsx"}; |
| 561 | + const CBMLanguage langs[] = {CBM_LANG_JAVASCRIPT, CBM_LANG_TYPESCRIPT, CBM_LANG_TSX}; |
| 562 | + const char *text = "import axios from 'axios';\n" |
| 563 | + "function helper() { return 1; }\n" |
| 564 | + "export default axios.create({baseURL: 'https://example.test/api'});\n"; |
| 565 | + bool ok = true; |
| 566 | + cbm_init(); |
| 567 | + for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { |
| 568 | + const char *path = TH_PATH(tmp, paths[i]); |
| 569 | + if (th_write_file(path, text) != 0) { |
| 570 | + ok = false; |
| 571 | + break; |
| 572 | + } |
| 573 | + char source[512]; |
| 574 | + FILE *f = fopen(path, "rb"); |
| 575 | + if (!f) { |
| 576 | + ok = false; |
| 577 | + break; |
| 578 | + } |
| 579 | + size_t len = fread(source, 1, sizeof(source) - 1, f); |
| 580 | + fclose(f); |
| 581 | + source[len] = '\0'; |
| 582 | + CBMFileResult *r = |
| 583 | + cbm_extract_file(source, (int)len, langs[i], "path-module", paths[i], 0, NULL, NULL); |
| 584 | + if (!r || r->defs.count < 2) { |
| 585 | + ok = false; |
| 586 | + } else { |
| 587 | + CBMDefinition *module = &r->defs.items[0]; |
| 588 | + ok = ok && strcmp(module->label, "Module") == 0 && |
| 589 | + strcmp(module->name, paths[i]) == 0 && module->http_client && |
| 590 | + strcmp(module->http_client, "axios") == 0 && module->http_base_url && |
| 591 | + strcmp(module->http_base_url, "https://example.test/api") == 0; |
| 592 | + for (int j = 1; j < r->defs.count; j++) { |
| 593 | + ok = ok && !r->defs.items[j].http_client && !r->defs.items[j].http_base_url; |
| 594 | + } |
| 595 | + } |
| 596 | + cbm_free_result(r); |
| 597 | + } |
| 598 | + th_rmtree(tmp); |
| 599 | + ASSERT_TRUE(ok); |
| 600 | + PASS(); |
| 601 | +} |
| 602 | + |
| 603 | +TEST(integ_mcp_snippet_line_and_output_budget) { |
| 604 | + char args[1024]; |
| 605 | + snprintf(args, sizeof(args), |
| 606 | + "{\"project\":\"%s\",\"qualified_name\":\"%s.app.budgetSnippet\"," |
| 607 | + "\"max_lines\":24}", |
| 608 | + g_project, g_project); |
| 609 | + char *base = cbm_test_operation_execute(g_srv, "get_code_snippet", args); |
| 610 | + ASSERT_NOT_NULL(base); |
| 611 | + yyjson_doc *base_doc = yyjson_read(base, strlen(base), 0); |
| 612 | + ASSERT_NOT_NULL(base_doc); |
| 613 | + yyjson_val *base_root = yyjson_doc_get_root(base_doc); |
| 614 | + const char *source = yyjson_get_str(yyjson_obj_get(base_root, "source")); |
| 615 | + ASSERT_NOT_NULL(source); |
| 616 | + int start = (int)yyjson_get_int(yyjson_obj_get(base_root, "start_line")); |
| 617 | + int original_end = (int)yyjson_get_int(yyjson_obj_get(base_root, "original_end_line")); |
| 618 | + ASSERT_TRUE(original_end > start + 23); |
| 619 | + /* Sweep boundaries: both successful and failed final binary-search probes, |
| 620 | + * including metadata growth, must keep the largest complete prefix. */ |
| 621 | + for (int tokens = 200; tokens <= 600; tokens += 7) { |
| 622 | + snprintf(args, sizeof(args), |
| 623 | + "{\"project\":\"%s\",\"qualified_name\":\"%s.app.budgetSnippet\"," |
| 624 | + "\"max_lines\":24,\"max_output_tokens\":%d}", |
| 625 | + g_project, g_project, tokens); |
| 626 | + char *reply = cbm_test_operation_execute(g_srv, "get_code_snippet", args); |
| 627 | + ASSERT_NOT_NULL(reply); |
| 628 | + ASSERT_TRUE(strlen(reply) <= (size_t)tokens * CBM_OUTPUT_BYTES_PER_TOKEN); |
| 629 | + yyjson_doc *doc = yyjson_read(reply, strlen(reply), 0); |
| 630 | + ASSERT_NOT_NULL(doc); |
| 631 | + yyjson_val *root = yyjson_doc_get_root(doc); |
| 632 | + const char *prefix = yyjson_get_str(yyjson_obj_get(root, "source")); |
| 633 | + ASSERT_NOT_NULL(prefix); |
| 634 | + ASSERT_TRUE(strncmp(source, prefix, strlen(prefix)) == 0); |
| 635 | + int lines = 0; |
| 636 | + for (const char *q = prefix; *q; q++) { |
| 637 | + if (*q == '\n') |
| 638 | + lines++; |
| 639 | + } |
| 640 | + ASSERT_TRUE(lines > 0 && lines < 24); |
| 641 | + ASSERT_EQ((int)yyjson_get_int(yyjson_obj_get(root, "end_line")), start + lines - 1); |
| 642 | + ASSERT_EQ((int)yyjson_get_int(yyjson_obj_get(root, "next_start_line")), start + lines); |
| 643 | + ASSERT_EQ((int)yyjson_get_int(yyjson_obj_get(root, "original_end_line")), original_end); |
| 644 | + ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(root, "source_truncated"))); |
| 645 | + /* Parseable JSON alone permits duplicate keys. Check every root key. */ |
| 646 | + size_t idx, max; |
| 647 | + yyjson_val *key, *val; |
| 648 | + const char *keys[] = {"source", "source_truncated", "source_clipped", |
| 649 | + "next_start_line", "original_end_line", "end_line"}; |
| 650 | + for (size_t k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) { |
| 651 | + int occurrences = 0; |
| 652 | + yyjson_obj_foreach(root, idx, max, key, val) { |
| 653 | + (void)val; |
| 654 | + if (strcmp(yyjson_get_str(key), keys[k]) == 0) |
| 655 | + occurrences++; |
| 656 | + } |
| 657 | + ASSERT_EQ(occurrences, 1); |
| 658 | + } |
| 659 | + /* One more whole line with its final metadata must exceed the ceiling. */ |
| 660 | + size_t keep = strlen(prefix); |
| 661 | + const char *next_end = strchr(source + keep, '\n'); |
| 662 | + ASSERT_NOT_NULL(next_end); |
| 663 | + yyjson_mut_doc *probe = yyjson_mut_doc_new(NULL); |
| 664 | + ASSERT_NOT_NULL(probe); |
| 665 | + yyjson_mut_val *candidate = yyjson_val_mut_copy(probe, root); |
| 666 | + ASSERT_NOT_NULL(candidate); |
| 667 | + yyjson_mut_doc_set_root(probe, candidate); |
| 668 | + (void)yyjson_mut_obj_remove_key(candidate, "source"); |
| 669 | + yyjson_mut_obj_add_val(probe, candidate, "source", |
| 670 | + yyjson_mut_strncpy(probe, source, (size_t)(next_end - source + 1))); |
| 671 | + (void)yyjson_mut_obj_remove_key(candidate, "end_line"); |
| 672 | + yyjson_mut_obj_add_int(probe, candidate, "end_line", start + lines); |
| 673 | + (void)yyjson_mut_obj_remove_key(candidate, "next_start_line"); |
| 674 | + yyjson_mut_obj_add_int(probe, candidate, "next_start_line", start + lines + 1); |
| 675 | + char *larger = cbm_operation_json_write(probe); |
| 676 | + ASSERT_NOT_NULL(larger); |
| 677 | + ASSERT_TRUE(strlen(larger) > (size_t)tokens * CBM_OUTPUT_BYTES_PER_TOKEN); |
| 678 | + free(larger); |
| 679 | + yyjson_mut_doc_free(probe); |
| 680 | + yyjson_doc_free(doc); |
| 681 | + free(reply); |
| 682 | + } |
| 683 | + yyjson_doc_free(base_doc); |
| 684 | + free(base); |
| 685 | + PASS(); |
| 686 | +} |
| 687 | + |
547 | 688 | TEST(integ_mcp_trace_path_output_budget) { |
548 | 689 | char args[512]; |
549 | 690 |
|
@@ -1203,6 +1344,8 @@ SUITE(integration) { |
1203 | 1344 | RUN_TEST(integ_mcp_trace_path_cross_service); |
1204 | 1345 | RUN_TEST(integ_mcp_search_code_match_limit_and_budget); |
1205 | 1346 | RUN_TEST(integ_mcp_trace_path_output_budget); |
| 1347 | + RUN_TEST(integ_mcp_snippet_line_and_output_budget); |
| 1348 | + RUN_TEST(integ_js_path_module_default_client); |
1206 | 1349 | RUN_TEST(integ_mcp_index_status); |
1207 | 1350 | RUN_TEST(integ_mcp_adr_outline_fence_status); |
1208 | 1351 |
|
|
0 commit comments