@@ -3178,6 +3178,58 @@ func TestFlatten(t *testing.T) {
31783178 require .Equal (t , map [string ]interface {}{"nested11" : map [string ]interface {}{"nested12" : "abc" }}, flattened ["nested0.nested1.nested2.nested3.nested4.nested5.nested6.nested7.nested8.nested9.nested10" ])
31793179 require .Equal (t , "def" , flattened ["other.nested1.nested2" ])
31803180 })
3181+
3182+ t .Run ("Flattens multiple nested branches consistently" , func (t * testing.T ) {
3183+ // This test would have caught the currentDepth bug that was fixed in PR #156.
3184+ // The bug caused currentDepth to accumulate across sibling branches, leading to
3185+ // inconsistent flattening behavior where later branches hit maxDepth prematurely.
3186+ // We create deep nesting (8 levels) so the bug causes second branch to exceed maxDepth.
3187+ obj := map [string ]interface {}{
3188+ "a" : map [string ]interface {}{
3189+ "l1" : map [string ]interface {}{
3190+ "l2" : map [string ]interface {}{
3191+ "l3" : map [string ]interface {}{
3192+ "l4" : map [string ]interface {}{
3193+ "l5" : map [string ]interface {}{
3194+ "l6" : map [string ]interface {}{
3195+ "l7" : map [string ]interface {}{
3196+ "value" : "deep_value_1" ,
3197+ },
3198+ },
3199+ },
3200+ },
3201+ },
3202+ },
3203+ },
3204+ },
3205+ "b" : map [string ]interface {}{
3206+ "l1" : map [string ]interface {}{
3207+ "l2" : map [string ]interface {}{
3208+ "l3" : map [string ]interface {}{
3209+ "l4" : map [string ]interface {}{
3210+ "l5" : map [string ]interface {}{
3211+ "l6" : map [string ]interface {}{
3212+ "l7" : map [string ]interface {}{
3213+ "value" : "deep_value_2" ,
3214+ },
3215+ },
3216+ },
3217+ },
3218+ },
3219+ },
3220+ },
3221+ },
3222+ }
3223+
3224+ flattened := flatten (obj )
3225+
3226+ // Both branches should be flattened consistently to the same depth
3227+ require .Equal (t , "deep_value_1" , flattened ["a.l1.l2.l3.l4.l5.l6.l7.value" ])
3228+ require .Equal (t , "deep_value_2" , flattened ["b.l1.l2.l3.l4.l5.l6.l7.value" ])
3229+
3230+ // Ensure we have exactly 2 flattened fields (not partially flattened objects)
3231+ require .Len (t , flattened , 2 )
3232+ })
31813233}
31823234
31833235func TestTrimEdges (t * testing.T ) {
0 commit comments