@@ -1800,6 +1800,10 @@ describe('entity', () => {
18001800 const key = urlSafeKey . legacyDecode ( encodedKey ) ;
18011801 assert . strictEqual ( key . namespace , 'NS' ) ;
18021802 assert . deepStrictEqual ( key . path , [ 'Task' , 'sampletask1' ] ) ;
1803+ assert . strictEqual (
1804+ urlSafeKey . legacyEncode ( PROJECT_ID , key , LOCATION_PREFIX ) ,
1805+ encodedKey ,
1806+ ) ;
18031807 } ) ;
18041808
18051809 it ( 'should decode key with single path element string type' , ( ) => {
@@ -1808,6 +1812,10 @@ describe('entity', () => {
18081812 const key = urlSafeKey . legacyDecode ( encodedKey ) ;
18091813 assert . strictEqual ( key . namespace , undefined ) ;
18101814 assert . deepStrictEqual ( key . path , [ 'Task' , 'sampletask1' ] ) ;
1815+ assert . strictEqual (
1816+ urlSafeKey . legacyEncode ( PROJECT_ID , key ) ,
1817+ encodedKey ,
1818+ ) ;
18111819 } ) ;
18121820
18131821 it ( 'should decode key with single path element long int type' , ( ) => {
@@ -1816,6 +1824,10 @@ describe('entity', () => {
18161824 const key = urlSafeKey . legacyDecode ( encodedKey ) ;
18171825 assert . strictEqual ( key . namespace , undefined ) ;
18181826 assert . deepStrictEqual ( key . path , [ 'Task' , '5754248394440704' ] ) ;
1827+ assert . strictEqual (
1828+ urlSafeKey . legacyEncode ( PROJECT_ID , key , LOCATION_PREFIX ) ,
1829+ encodedKey ,
1830+ ) ;
18191831 } ) ;
18201832
18211833 it ( 'should decode key with parent path' , ( ) => {
@@ -1831,6 +1843,69 @@ describe('entity', () => {
18311843 ] ) ;
18321844 assert . strictEqual ( key . parent ! . name , 'sampletask1' ) ;
18331845 assert . deepStrictEqual ( key . parent ! . path , [ 'Task' , 'sampletask1' ] ) ;
1846+ assert . strictEqual (
1847+ urlSafeKey . legacyEncode ( PROJECT_ID , key , LOCATION_PREFIX ) ,
1848+ encodedKey ,
1849+ ) ;
1850+ } ) ;
1851+
1852+ describe ( 'should ensure that decode inverses encode and decoding is correct' , ( ) => {
1853+ const TEST_PROJECT = 'test-project' ;
1854+ const testCases = [
1855+ { name : 'numeric ID' , path : [ 'Kind' , '123' ] } ,
1856+ { name : 'string name with spaces' , path : [ 'Kind' , 'name with spaces' ] } ,
1857+ { name : 'special characters' , path : [ 'Kind' , 'special!@#$%^&*()' ] } ,
1858+ {
1859+ name : '3-level parent' ,
1860+ path : [ 'Grandparent' , '1' , 'Parent' , 'p1' , 'Child' , '2' ] ,
1861+ } ,
1862+ {
1863+ name : 'namespace and numeric ID' ,
1864+ path : [ 'Kind' , '456' ] ,
1865+ namespace : 'MyNS' ,
1866+ } ,
1867+ {
1868+ name : 'namespace and parent' ,
1869+ path : [ 'Parent' , '1' , 'Child' , 'c1' ] ,
1870+ namespace : 'MyNS' ,
1871+ } ,
1872+ {
1873+ name : 'long integer ID as string' ,
1874+ path : [ 'Kind' , '9223372036854775807' ] ,
1875+ } ,
1876+ { name : 'kind with hyphens' , path : [ 'My-Kind' , '1' ] } ,
1877+ {
1878+ name : 'different kinds in path' ,
1879+ path : [ 'User' , 'user1' , 'Post' , '100' , 'Comment' , 'c1' ] ,
1880+ } ,
1881+ {
1882+ name : 'same kinds in path' ,
1883+ path : [ 'Node' , '1' , 'Node' , '2' , 'Node' , '3' ] ,
1884+ } ,
1885+ ] ;
1886+
1887+ testCases . forEach ( tc => {
1888+ it ( `should decode and re-encode ${ tc . name } correctly` , ( ) => {
1889+ const key = new testEntity . Key ( {
1890+ path : tc . path ,
1891+ namespace : tc . namespace ,
1892+ } ) ;
1893+ const encoded = urlSafeKey . legacyEncode (
1894+ TEST_PROJECT ,
1895+ key ,
1896+ LOCATION_PREFIX ,
1897+ ) ;
1898+ const decoded = urlSafeKey . legacyDecode ( encoded ) ;
1899+ assert . strictEqual ( decoded . namespace , tc . namespace ) ;
1900+ assert . deepStrictEqual ( decoded . path , tc . path ) ;
1901+ const reEncoded = urlSafeKey . legacyEncode (
1902+ TEST_PROJECT ,
1903+ decoded ,
1904+ LOCATION_PREFIX ,
1905+ ) ;
1906+ assert . strictEqual ( reEncoded , encoded ) ;
1907+ } ) ;
1908+ } ) ;
18341909 } ) ;
18351910 } ) ;
18361911 } ) ;
0 commit comments