@@ -35,6 +35,10 @@ function ignoreNode14<T>(callback: () => T) {
3535 return callback ( ) ;
3636}
3737
38+ const anyMetadata = expect . objectContaining ( {
39+ createdTime : expect . any ( Number ) ,
40+ } satisfies CacheMetadata ) ;
41+
3842let currentTime = 0 ;
3943beforeEach ( ( ) => {
4044 currentTime = 0 ;
@@ -1423,7 +1427,7 @@ describe('cachified', () => {
14231427
14241428 expect ( values ) . toEqual ( [ 'value-1' , 'YOLO!' , 'value-3' ] ) ;
14251429 expect ( getValues ) . toHaveBeenCalledTimes ( 1 ) ;
1426- expect ( getValues ) . toHaveBeenCalledWith ( [ 1 , 3 ] ) ;
1430+ expect ( getValues ) . toHaveBeenCalledWith ( [ 1 , 3 ] , [ anyMetadata , anyMetadata ] ) ;
14271431 } ) ;
14281432
14291433 it ( 'rejects all values when batch get fails' , async ( ) => {
@@ -1469,7 +1473,10 @@ describe('cachified', () => {
14691473
14701474 expect ( await valuesP ) . toEqual ( [ 'value-1' , 'value-seven' ] ) ;
14711475 expect ( getValues ) . toHaveBeenCalledTimes ( 1 ) ;
1472- expect ( getValues ) . toHaveBeenCalledWith ( [ 1 , 'seven' ] ) ;
1476+ expect ( getValues ) . toHaveBeenCalledWith (
1477+ [ 1 , 'seven' ] ,
1478+ [ anyMetadata , anyMetadata ] ,
1479+ ) ;
14731480 } ) ;
14741481
14751482 it ( 'can edit metadata for single batch values' , async ( ) => {
@@ -1670,6 +1677,65 @@ describe('cachified', () => {
16701677
16711678 expect ( value ) . toBe ( 'ONE' ) ;
16721679 } ) ;
1680+
1681+ it ( 'supports trace ids' , async ( ) => {
1682+ expect . assertions ( 6 ) ;
1683+
1684+ const cache = new Map < string , CacheEntry > ( ) ;
1685+ const traceId1 = Symbol ( ) ;
1686+ const d = new Deferred < string > ( ) ;
1687+
1688+ const value = cachified ( {
1689+ cache,
1690+ key : 'test-1' ,
1691+ ttl : 200 ,
1692+ traceId : traceId1 ,
1693+ async getFreshValue ( { metadata : { traceId } } ) {
1694+ // in getFreshValue
1695+ expect ( traceId ) . toBe ( traceId1 ) ;
1696+ return d . promise ;
1697+ } ,
1698+ } ) ;
1699+ await delay ( 0 ) ;
1700+
1701+ d . resolve ( 'ONE' ) ;
1702+ expect ( await value ) . toBe ( 'ONE' ) ;
1703+
1704+ // on cache entry
1705+ expect ( cache . get ( 'test-1' ) ?. metadata . traceId ) . toBe ( traceId1 ) ;
1706+
1707+ const traceId2 = 'some-string-id' ;
1708+
1709+ // in batch getFreshValues
1710+ const batch = createBatch ( ( freshIndexes , metadata ) => {
1711+ expect ( metadata [ 0 ] . traceId ) . toBe ( traceId2 ) ;
1712+ return freshIndexes . map ( ( i ) => `value-${ i } ` ) ;
1713+ } ) ;
1714+
1715+ const createReporter = jest . fn ( ( ) => ( ) => { } ) ;
1716+
1717+ await cachified (
1718+ {
1719+ cache,
1720+ key : 'test-2' ,
1721+ ttl : 200 ,
1722+ traceId : traceId2 ,
1723+ getFreshValue : batch . add ( 1 ) ,
1724+ } ,
1725+ createReporter ,
1726+ ) ;
1727+
1728+ expect ( cache . get ( 'test-2' ) ?. metadata . traceId ) . toBe ( traceId2 ) ;
1729+
1730+ expect ( createReporter ) . toHaveBeenCalledWith (
1731+ expect . objectContaining ( {
1732+ traceId : traceId2 ,
1733+ metadata : expect . objectContaining ( {
1734+ traceId : traceId2 ,
1735+ } ) ,
1736+ } ) ,
1737+ ) ;
1738+ } ) ;
16731739} ) ;
16741740
16751741function createReporter ( ) {
0 commit comments