@@ -4,7 +4,7 @@ import { stableStringify } from "../stableStringify";
44describe ( "stableStringify" , ( ) => {
55 test ( "should stringify json" , ( ) => {
66 expect ( stableStringify ( { a : 1 , b : 2 , c : 3 } ) ) . toBe (
7- '{"a":1,"b":2,"c":3}' ,
7+ JSON . stringify ( { a : 1 , b : 2 , c : 3 } ) ,
88 ) ;
99 } ) ;
1010
@@ -14,4 +14,101 @@ describe("stableStringify", () => {
1414
1515 expect ( stableStringify ( obj1 ) ) . toBe ( stableStringify ( obj2 ) ) ;
1616 } ) ;
17+
18+ test ( "should stringify nested objects with stable key order" , ( ) => {
19+ const obj1 = { a : 1 , nested : { x : 10 , y : 20 } } ;
20+ const obj2 = { nested : { y : 20 , x : 10 } , a : 1 } ;
21+
22+ expect ( stableStringify ( obj1 ) ) . toBe ( stableStringify ( obj2 ) ) ;
23+ } ) ;
24+
25+ test ( "should stringify deeply nested objects with stable key order" , ( ) => {
26+ const obj1 = {
27+ level1 : {
28+ level2 : {
29+ c : 3 ,
30+ b : 2 ,
31+ a : 1 ,
32+ } ,
33+ } ,
34+ } ;
35+ const obj2 = {
36+ level1 : {
37+ level2 : {
38+ a : 1 ,
39+ b : 2 ,
40+ c : 3 ,
41+ } ,
42+ } ,
43+ } ;
44+
45+ expect ( stableStringify ( obj1 ) ) . toBe ( stableStringify ( obj2 ) ) ;
46+ } ) ;
47+
48+ test ( "should stringify arrays containing objects with stable key order" , ( ) => {
49+ const obj1 = {
50+ items : [
51+ { z : 3 , y : 2 , x : 1 } ,
52+ { c : "c" , b : "b" , a : "a" } ,
53+ ] ,
54+ } ;
55+ const obj2 = {
56+ items : [
57+ { x : 1 , y : 2 , z : 3 } ,
58+ { a : "a" , b : "b" , c : "c" } ,
59+ ] ,
60+ } ;
61+
62+ expect ( stableStringify ( obj1 ) ) . toBe ( stableStringify ( obj2 ) ) ;
63+ } ) ;
64+
65+ test ( "should handle null and primitive values correctly" , ( ) => {
66+ const obj1 = { b : null , a : 1 , c : "string" , d : true } ;
67+ const obj2 = { d : true , c : "string" , a : 1 , b : null } ;
68+
69+ expect ( stableStringify ( obj1 ) ) . toBe ( stableStringify ( obj2 ) ) ;
70+ } ) ;
71+
72+ test ( "should produce deterministic output for canonical spec hashing" , ( ) => {
73+ const spec1 = {
74+ operationId : "getUser" ,
75+ responses : {
76+ "200" : {
77+ schema : {
78+ type : "object" ,
79+ properties : { name : { } , id : { } } ,
80+ } ,
81+ } ,
82+ } ,
83+ parameters : [ { name : "id" , in : "path" , required : true } ] ,
84+ } ;
85+ const spec2 = {
86+ parameters : [ { required : true , in : "path" , name : "id" } ] ,
87+ responses : {
88+ "200" : {
89+ schema : {
90+ properties : { id : { } , name : { } } ,
91+ type : "object" ,
92+ } ,
93+ } ,
94+ } ,
95+ operationId : "getUser" ,
96+ } ;
97+
98+ expect ( stableStringify ( spec1 ) ) . toBe ( stableStringify ( spec2 ) ) ;
99+ } ) ;
100+
101+ test ( "should output nested object keys in sorted order" , ( ) => {
102+ const obj = { b : 2 , a : { z : 1 , y : 2 } } ;
103+ const result = stableStringify ( obj ) ;
104+
105+ expect ( result ) . toBe ( JSON . stringify ( { a : { y : 2 , z : 1 } , b : 2 } ) ) ;
106+ } ) ;
107+
108+ test ( "should sort keys in arrays of objects" , ( ) => {
109+ const obj = { items : [ { b : 1 , a : 2 } ] } ;
110+ const result = stableStringify ( obj ) ;
111+
112+ expect ( result ) . toBe ( JSON . stringify ( { items : [ { a : 2 , b : 1 } ] } ) ) ;
113+ } ) ;
17114} ) ;
0 commit comments