@@ -19,3 +19,46 @@ it('cache', async () => {
1919 cache . clear ( )
2020 expect ( cache . cached < number > ( 'fn2' , [ 3 , 4 ] ) ) . toBeUndefined ( )
2121} )
22+
23+ it ( 'serves falsy cached values via `has` (presence, not truthiness)' , ( ) => {
24+ const cache = new RpcCacheManager ( { functions : [ 'fn' ] } )
25+
26+ // absent key: `has` reports false, distinct from a stored falsy value
27+ expect ( cache . has ( 'fn' , [ 'zero' ] ) ) . toBe ( false )
28+
29+ cache . apply ( { m : 'fn' , a : [ 'zero' ] } , 0 )
30+ cache . apply ( { m : 'fn' , a : [ 'false' ] } , false )
31+ cache . apply ( { m : 'fn' , a : [ 'empty' ] } , '' )
32+ cache . apply ( { m : 'fn' , a : [ 'null' ] } , null )
33+
34+ expect ( cache . has ( 'fn' , [ 'zero' ] ) ) . toBe ( true )
35+ expect ( cache . has ( 'fn' , [ 'false' ] ) ) . toBe ( true )
36+ expect ( cache . has ( 'fn' , [ 'empty' ] ) ) . toBe ( true )
37+ expect ( cache . has ( 'fn' , [ 'null' ] ) ) . toBe ( true )
38+
39+ expect ( cache . cached ( 'fn' , [ 'zero' ] ) ) . toBe ( 0 )
40+ expect ( cache . cached ( 'fn' , [ 'false' ] ) ) . toBe ( false )
41+ expect ( cache . cached ( 'fn' , [ 'empty' ] ) ) . toBe ( '' )
42+ expect ( cache . cached ( 'fn' , [ 'null' ] ) ) . toBe ( null )
43+ } )
44+
45+ it ( 'the client cache path (has-then-cached) serves a falsy value without a second fetch' , async ( ) => {
46+ const cache = new RpcCacheManager ( { functions : [ 'fn' ] } )
47+ let nextCalls = 0
48+ const next = async ( req : { m : string , a : unknown [ ] } ) => {
49+ nextCalls ++
50+ cache . apply ( req , 0 )
51+ return 0
52+ }
53+
54+ // mirrors the onRequest cache path in client/rpc.ts
55+ async function callThroughCache ( req : { m : string , a : unknown [ ] } ) {
56+ if ( cache . has ( req . m , req . a ) )
57+ return cache . cached ( req . m , req . a )
58+ return next ( req )
59+ }
60+
61+ expect ( await callThroughCache ( { m : 'fn' , a : [ ] } ) ) . toBe ( 0 )
62+ expect ( await callThroughCache ( { m : 'fn' , a : [ ] } ) ) . toBe ( 0 )
63+ expect ( nextCalls ) . toBe ( 1 ) // second call served from cache, not re-fetched
64+ } )
0 commit comments