Skip to content

Commit bc960d8

Browse files
committed
test(cli): pin JSON array literal handling for array-typed arguments
`codebase-memory-mcp cli <tool> --flag value` builds the tool's JSON arguments from typed flags. Array-typed parameters such as `semantic_query` used to accept only repeated flags; the JSON spelling shown in the MCP schema (`--semantic-query '["a","b"]'`) was stored as a single opaque string element (DeusData#2150). `cli_add_typed` now expands such a literal; this pins the edge cases of that contract so it cannot regress: - a well-formed array of strings contributes its elements - an explicit empty array `[]` contributes nothing (not the keyword "[]") - a malformed literal stays one literal string, without an error - non-string items are copied as typed values, not stringified - literals and repeated flags mix, accumulating in argument order - repeated literals accumulate Signed-off-by: Hubert Tarnacki <hubert.tarnacki@gmail.com>
1 parent 64c23fa commit bc960d8

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

‎tests/test_cli.c‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15426,6 +15426,81 @@ TEST(cli_build_args_json_repeated_array_issue680) {
1542615426
PASS();
1542715427
}
1542815428

15429+
/* An array-typed flag also accepts the JSON-array spelling shown by the MCP
15430+
* schemas, expanding it into individual items instead of one opaque keyword. */
15431+
TEST(cli_build_args_json_array_literal) {
15432+
char *err = NULL;
15433+
char *argv[] = {"--semantic-query", "[\"create\",\"billing\",\"account\"]"};
15434+
char *json = cbm_cli_build_args_json("search_graph", 2, argv, &err);
15435+
ASSERT_NOT_NULL(json);
15436+
ASSERT(strstr(json, "\"semantic_query\":[\"create\",\"billing\",\"account\"]") != NULL);
15437+
free(json);
15438+
PASS();
15439+
}
15440+
15441+
/* An explicit empty JSON array contributes no items; it must not turn into
15442+
* the literal keyword "[]". */
15443+
TEST(cli_build_args_json_array_literal_empty) {
15444+
char *err = NULL;
15445+
char *argv[] = {"--semantic-query", "[]"};
15446+
char *json = cbm_cli_build_args_json("search_graph", 2, argv, &err);
15447+
ASSERT_NOT_NULL(json);
15448+
ASSERT(strstr(json, "\"semantic_query\":[]") != NULL);
15449+
ASSERT(strstr(json, "\"[]\"") == NULL);
15450+
free(json);
15451+
PASS();
15452+
}
15453+
15454+
/* Malformed JSON keeps the pre-existing behaviour: the value stays one
15455+
* literal string and no error is raised. */
15456+
TEST(cli_build_args_json_array_literal_malformed) {
15457+
char *err = NULL;
15458+
char *argv[] = {"--semantic-query", "[\"create\","};
15459+
char *json = cbm_cli_build_args_json("search_graph", 2, argv, &err);
15460+
ASSERT_NOT_NULL(json);
15461+
ASSERT_NULL(err);
15462+
ASSERT(strstr(json, "\"semantic_query\":[\"[\\\"create\\\",\"]") != NULL);
15463+
free(json);
15464+
PASS();
15465+
}
15466+
15467+
/* Non-string items of a well-formed JSON array are copied as typed values,
15468+
* not stringified and not collapsed into one literal element. */
15469+
TEST(cli_build_args_json_array_literal_non_string_items) {
15470+
char *err = NULL;
15471+
char *argv[] = {"--semantic-query", "[1,2]"};
15472+
char *json = cbm_cli_build_args_json("search_graph", 2, argv, &err);
15473+
ASSERT_NOT_NULL(json);
15474+
ASSERT(strstr(json, "\"semantic_query\":[1,2]") != NULL);
15475+
ASSERT(strstr(json, "\"[1,2]\"") == NULL);
15476+
free(json);
15477+
PASS();
15478+
}
15479+
15480+
/* Repeated flags and array literals may be mixed; items accumulate in
15481+
* argument order. */
15482+
TEST(cli_build_args_json_array_literal_mixed_with_repeated) {
15483+
char *err = NULL;
15484+
char *argv[] = {"--semantic-query", "send", "--semantic-query", "[\"publish\",\"emit\"]",
15485+
"--semantic-query", "notify"};
15486+
char *json = cbm_cli_build_args_json("search_graph", 6, argv, &err);
15487+
ASSERT_NOT_NULL(json);
15488+
ASSERT(strstr(json, "\"semantic_query\":[\"send\",\"publish\",\"emit\",\"notify\"]") != NULL);
15489+
free(json);
15490+
PASS();
15491+
}
15492+
15493+
/* A repeated array literal accumulates into one array. */
15494+
TEST(cli_build_args_json_array_literal_repeated) {
15495+
char *err = NULL;
15496+
char *argv[] = {"--semantic-query", "[\"send\"]", "--semantic-query", "[\"publish\"]"};
15497+
char *json = cbm_cli_build_args_json("search_graph", 4, argv, &err);
15498+
ASSERT_NOT_NULL(json);
15499+
ASSERT(strstr(json, "\"semantic_query\":[\"send\",\"publish\"]") != NULL);
15500+
free(json);
15501+
PASS();
15502+
}
15503+
1542915504
/* kebab-case flag names map to snake_case JSON keys. */
1543015505
TEST(cli_build_args_json_kebab_to_snake_issue680) {
1543115506
char *err = NULL;
@@ -16560,6 +16635,12 @@ SUITE(cli) {
1656016635
RUN_TEST(cli_build_args_json_array_flag_accepts_json_literal);
1656116636
RUN_TEST(cli_build_args_json_unknown_flag_rejected);
1656216637
RUN_TEST(cli_build_args_json_repeated_array_issue680);
16638+
RUN_TEST(cli_build_args_json_array_literal);
16639+
RUN_TEST(cli_build_args_json_array_literal_empty);
16640+
RUN_TEST(cli_build_args_json_array_literal_malformed);
16641+
RUN_TEST(cli_build_args_json_array_literal_non_string_items);
16642+
RUN_TEST(cli_build_args_json_array_literal_mixed_with_repeated);
16643+
RUN_TEST(cli_build_args_json_array_literal_repeated);
1656316644
RUN_TEST(cli_build_args_json_kebab_to_snake_issue680);
1656416645
RUN_TEST(cli_build_args_json_key_equals_value_issue680);
1656516646
RUN_TEST(cli_build_args_json_bad_positional_errors_issue680);

0 commit comments

Comments
 (0)