@@ -63,4 +63,67 @@ describe("convertBigIntToString", () => {
6363 const result = convertBigIntToString ( 123 )
6464 expect ( result ) . toBe ( 123 )
6565 } )
66+
67+ it ( "should replace circular references with [Circular]" , ( ) => {
68+ const o : Record < string , unknown > = { }
69+ o . self = o
70+ const result = convertBigIntToString ( o )
71+ expect ( result ) . toEqual ( { self : "[Circular]" } )
72+ } )
73+
74+ it ( "should replace circular reference at depth 1 with [Circular]" , ( ) => {
75+ const root : Record < string , unknown > = { a : 1 }
76+ root . nested = { b : 2 , back : root }
77+ const result = convertBigIntToString ( root )
78+ expect ( result ) . toEqual ( {
79+ a : 1 ,
80+ nested : { b : 2 , back : "[Circular]" } ,
81+ } )
82+ } )
83+
84+ it ( "should not replace shared (non-circular) references with [Circular]" , ( ) => {
85+ const shared = { x : 1 , y : 2 }
86+ const root = { a : shared , b : shared }
87+ const result = convertBigIntToString ( root )
88+ expect ( result ) . toEqual ( {
89+ a : { x : 1 , y : 2 } ,
90+ b : { x : 1 , y : 2 } ,
91+ } )
92+ } )
93+
94+ it ( "should replace deep acyclic structure beyond maxDepth with [Max depth reached]" , ( ) => {
95+ let deep : Record < string , unknown > = { }
96+ const root = deep
97+ for ( let i = 0 ; i < 60 ; i ++ ) {
98+ deep . next = { }
99+ deep = deep . next as Record < string , unknown >
100+ }
101+ const result = convertBigIntToString ( root )
102+ let current : unknown = result
103+ let depth = 0
104+ while ( current !== null && typeof current === "object" && "next" in current ) {
105+ current = ( current as Record < string , unknown > ) . next
106+ depth ++
107+ }
108+ expect ( depth ) . toBe ( 50 )
109+ expect ( current ) . toBe ( "[Max depth reached]" )
110+ } )
111+
112+ it ( "should traverse deeper when maxDepth option is increased" , ( ) => {
113+ let deep : Record < string , unknown > = { }
114+ const root = deep
115+ for ( let i = 0 ; i < 60 ; i ++ ) {
116+ deep . next = { }
117+ deep = deep . next as Record < string , unknown >
118+ }
119+ const result = convertBigIntToString ( root , { maxDepth : 100 } )
120+ let current : unknown = result
121+ let depth = 0
122+ while ( current !== null && typeof current === "object" && "next" in current ) {
123+ current = ( current as Record < string , unknown > ) . next
124+ depth ++
125+ }
126+ expect ( depth ) . toBe ( 60 )
127+ expect ( current ) . toEqual ( { } )
128+ } )
66129} )
0 commit comments