@@ -642,6 +642,162 @@ TEST(layout_json_memory_core_ownership) {
642642 PASS ();
643643}
644644
645+ /* handle_layout re-reads this payload with yyjson_read(..., 0). Malformed
646+ * UTF-8 has to come back as U+FFFD (EF BF BD); valid text has to decode to
647+ * the original string. Stack strings are not owned by the result. */
648+ TEST (layout_json_invalid_utf8_roundtrip ) {
649+ ASSERT_NULL (cbm_layout_to_json (NULL ));
650+
651+ cbm_layout_result_t empty = {0 };
652+ char * json = cbm_layout_to_json (& empty );
653+ ASSERT_NOT_NULL (json );
654+ yyjson_doc * doc = yyjson_read (json , strlen (json ), 0 );
655+ ASSERT_NOT_NULL (doc );
656+ yyjson_val * root = yyjson_doc_get_root (doc );
657+ ASSERT_EQ ((int )yyjson_arr_size (yyjson_obj_get (root , "nodes" )), 0 );
658+ ASSERT_EQ ((int )yyjson_arr_size (yyjson_obj_get (root , "edges" )), 0 );
659+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (root , "total_nodes" )), 0 );
660+ yyjson_doc_free (doc );
661+ cbm_layout_free_json (json );
662+
663+ cbm_layout_node_t nodes [4 ] = {
664+ {
665+ .id = 11 ,
666+ .x = 1.5f ,
667+ .y = -2.0f ,
668+ .z = 3.25f ,
669+ .label = "Section\x80" , /* isolated continuation */
670+ .name = "Head\xC3" , /* truncated 2-byte lead */
671+ .qualified_name = "doc.md::H\xE4\xB8" , /* truncated 3-byte sequence */
672+ .file_path = "docs/\xFFreadme.md" , /* invalid leading byte */
673+ .start_line = 4 ,
674+ .end_line = 9 ,
675+ .size = 2.5f ,
676+ .color = 0x336699 ,
677+ .in_calls = 1 ,
678+ .out_calls = 2 ,
679+ .status = "structural" ,
680+ },
681+ {
682+ .id = 12 ,
683+ .label = "Section" ,
684+ .name = "q\"\x80" , /* quote, then a continuation byte */
685+ .qualified_name = "bs\\\xFF" , /* backslash, then an invalid lead */
686+ .file_path = "nl\n\xC2" , /* newline, then a truncated lead */
687+ .size = 1.0f ,
688+ },
689+ {
690+ .id = 13 ,
691+ .label = "Section" ,
692+ .name = "caf\xC3\xA9" , /* U+00E9 */
693+ .qualified_name = "\xE7\xAB\xA0\xE8\x8A\x82" , /* U+7AE0 U+8282 */
694+ .file_path = "docs/\xF0\x9F\x98\x80.md" , /* U+1F600 */
695+ .size = 1.0f ,
696+ .status = "structural" ,
697+ },
698+ {
699+ .id = 14 ,
700+ .x = NAN ,
701+ .y = INFINITY ,
702+ .z = - INFINITY ,
703+ .size = NAN ,
704+ },
705+ };
706+ cbm_layout_edge_t edges [2 ] = {
707+ {.source = 11 , .target = 12 , .type = "MENTIONS\x80" },
708+ {.source = 12 , .target = 13 , .type = NULL },
709+ };
710+ cbm_layout_result_t layout = {
711+ .nodes = nodes ,
712+ .node_count = 4 ,
713+ .edges = edges ,
714+ .edge_count = 2 ,
715+ .total_nodes = 4 ,
716+ };
717+
718+ json = cbm_layout_to_json (& layout );
719+ ASSERT_NOT_NULL (json );
720+ doc = yyjson_read (json , strlen (json ), 0 );
721+ ASSERT_NOT_NULL (doc );
722+ for (size_t i = 0 ; json [i ] != '\0' ; i ++ ) {
723+ ASSERT ((unsigned char )json [i ] < 0x80 );
724+ }
725+ ASSERT_NOT_NULL (strstr (json , "\"Section\"" ));
726+ ASSERT_NOT_NULL (strstr (json , "\\u00E9" ));
727+ ASSERT_NOT_NULL (strstr (json , "\\u7AE0\\u8282" ));
728+ ASSERT_NOT_NULL (strstr (json , "\\uD83D\\uDE00" ));
729+ ASSERT_NOT_NULL (strstr (json , "\\uFFFD" ));
730+ ASSERT_NOT_NULL (strstr (json , "q\\\"\\uFFFD" ));
731+ ASSERT_NOT_NULL (strstr (json , "bs\\\\\\uFFFD" ));
732+ ASSERT_NOT_NULL (strstr (json , "nl\\n\\uFFFD" ));
733+
734+ root = yyjson_doc_get_root (doc );
735+ yyjson_val * nodes_json = yyjson_obj_get (root , "nodes" );
736+ yyjson_val * edges_json = yyjson_obj_get (root , "edges" );
737+ ASSERT_EQ ((int )yyjson_arr_size (nodes_json ), 4 );
738+ ASSERT_EQ ((int )yyjson_arr_size (edges_json ), 2 );
739+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (root , "total_nodes" )), 4 );
740+
741+ yyjson_val * n0 = yyjson_arr_get (nodes_json , 0 );
742+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n0 , "id" )), 11 );
743+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n0 , "label" )), "Section\xEF\xBF\xBD" );
744+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n0 , "name" )), "Head\xEF\xBF\xBD" );
745+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n0 , "qualified_name" )),
746+ "doc.md::H\xEF\xBF\xBD\xEF\xBF\xBD" );
747+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n0 , "file_path" )), "docs/\xEF\xBF\xBDreadme.md" );
748+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n0 , "status" )), "structural" );
749+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n0 , "start_line" )), 4 );
750+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n0 , "end_line" )), 9 );
751+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n0 , "in_calls" )), 1 );
752+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n0 , "out_calls" )), 2 );
753+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n0 , "x" )), 1.5 , 1e-6 );
754+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n0 , "y" )), -2.0 , 1e-6 );
755+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n0 , "z" )), 3.25 , 1e-6 );
756+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n0 , "size" )), 2.5 , 1e-6 );
757+
758+ yyjson_val * n1 = yyjson_arr_get (nodes_json , 1 );
759+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n1 , "id" )), 12 );
760+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n1 , "label" )), "Section" );
761+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n1 , "name" )), "q\"\xEF\xBF\xBD" );
762+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n1 , "qualified_name" )), "bs\\\xEF\xBF\xBD" );
763+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n1 , "file_path" )), "nl\n\xEF\xBF\xBD" );
764+ ASSERT_NULL (yyjson_obj_get (n1 , "status" ));
765+
766+ yyjson_val * n2 = yyjson_arr_get (nodes_json , 2 );
767+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n2 , "id" )), 13 );
768+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n2 , "label" )), "Section" );
769+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n2 , "name" )), "caf\xC3\xA9" );
770+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n2 , "qualified_name" )), "\xE7\xAB\xA0\xE8\x8A\x82" );
771+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (n2 , "file_path" )), "docs/\xF0\x9F\x98\x80.md" );
772+
773+ yyjson_val * n3 = yyjson_arr_get (nodes_json , 3 );
774+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (n3 , "id" )), 14 );
775+ ASSERT_NULL (yyjson_obj_get (n3 , "label" ));
776+ ASSERT_NULL (yyjson_obj_get (n3 , "name" ));
777+ ASSERT_NULL (yyjson_obj_get (n3 , "qualified_name" ));
778+ ASSERT_NULL (yyjson_obj_get (n3 , "file_path" ));
779+ ASSERT_NULL (yyjson_obj_get (n3 , "status" ));
780+ ASSERT_NULL (yyjson_obj_get (n3 , "start_line" ));
781+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n3 , "x" )), 0.0 , 1e-6 );
782+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n3 , "y" )), 0.0 , 1e-6 );
783+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n3 , "z" )), 0.0 , 1e-6 );
784+ ASSERT_FLOAT_EQ (yyjson_get_real (yyjson_obj_get (n3 , "size" )), 1.0 , 1e-6 );
785+
786+ yyjson_val * e0 = yyjson_arr_get (edges_json , 0 );
787+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (e0 , "source" )), 11 );
788+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (e0 , "target" )), 12 );
789+ ASSERT_STR_EQ (yyjson_get_str (yyjson_obj_get (e0 , "type" )), "MENTIONS\xEF\xBF\xBD" );
790+
791+ yyjson_val * e1 = yyjson_arr_get (edges_json , 1 );
792+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (e1 , "source" )), 12 );
793+ ASSERT_EQ ((int )yyjson_get_int (yyjson_obj_get (e1 , "target" )), 13 );
794+ ASSERT_NULL (yyjson_obj_get (e1 , "type" ));
795+
796+ yyjson_doc_free (doc );
797+ cbm_layout_free_json (json );
798+ PASS ();
799+ }
800+
645801TEST (layout_null_inputs ) {
646802 /* NULL store → NULL result */
647803 cbm_layout_result_t * r = cbm_layout_compute (NULL , "test" , CBM_LAYOUT_OVERVIEW , NULL , 0 , 100 );
@@ -2830,6 +2986,7 @@ SUITE(ui) {
28302986 RUN_TEST (layout_deterministic );
28312987 RUN_TEST (layout_to_json );
28322988 RUN_TEST (layout_json_memory_core_ownership );
2989+ RUN_TEST (layout_json_invalid_utf8_roundtrip );
28332990 RUN_TEST (layout_null_inputs );
28342991 RUN_TEST (layout_dead_code_classification );
28352992 RUN_TEST (layout_coincident_nodes_bounded );
0 commit comments