1- import { describe , expect , it } from "vitest" ;
1+ import { afterEach , describe , expect , it , vi } from "vitest" ;
22import { MAX_TIMER_TIMEOUT_SECONDS } from "../../shared/number-coercion.js" ;
3- import { resolvePositiveTimeoutSeconds , resolveTimeoutSeconds } from "./web-shared.js" ;
3+ import {
4+ readCache ,
5+ resolvePositiveTimeoutSeconds ,
6+ resolveTimeoutSeconds ,
7+ writeCache ,
8+ type CacheEntry ,
9+ } from "./web-shared.js" ;
10+
11+ afterEach ( ( ) => {
12+ vi . restoreAllMocks ( ) ;
13+ } ) ;
414
515describe ( "web shared timeout seconds" , ( ) => {
616 it ( "caps timeoutSeconds at the shared timer-safe ceiling" , ( ) => {
@@ -16,4 +26,41 @@ describe("web shared timeout seconds", () => {
1626 expect ( resolvePositiveTimeoutSeconds ( 0 , 30 ) ) . toBe ( 30 ) ;
1727 expect ( resolvePositiveTimeoutSeconds ( 1.9 , 30 ) ) . toBe ( 1 ) ;
1828 } ) ;
29+
30+ it ( "drops cached values while the process clock is invalid" , ( ) => {
31+ const cache = new Map < string , CacheEntry < string > > ( ) ;
32+ writeCache ( cache , "key" , "old" , 60_000 ) ;
33+ expect ( readCache ( cache , "key" ) ?. value ) . toBe ( "old" ) ;
34+
35+ vi . spyOn ( Date , "now" ) . mockReturnValue ( 8_640_000_000_000_001 ) ;
36+ expect ( readCache ( cache , "key" ) ) . toBeNull ( ) ;
37+
38+ vi . mocked ( Date . now ) . mockReturnValue ( 1_000 ) ;
39+ expect ( readCache ( cache , "key" ) ) . toBeNull ( ) ;
40+ } ) ;
41+
42+ it ( "does not write cache values when expiry would exceed the Date range" , ( ) => {
43+ const cache = new Map < string , CacheEntry < string > > ( ) ;
44+ vi . spyOn ( Date , "now" ) . mockReturnValue ( 8_640_000_000_000_000 ) ;
45+
46+ writeCache ( cache , "key" , "value" , 60_000 ) ;
47+
48+ expect ( cache . size ) . toBe ( 0 ) ;
49+ expect ( readCache ( cache , "key" ) ) . toBeNull ( ) ;
50+ } ) ;
51+
52+ it ( "does not evict valid entries when an invalid expiry cannot be cached" , ( ) => {
53+ const cache = new Map < string , CacheEntry < string > > ( ) ;
54+ for ( let index = 0 ; index < 100 ; index += 1 ) {
55+ writeCache ( cache , `key-${ index } ` , `value-${ index } ` , 60_000 ) ;
56+ }
57+ expect ( cache . get ( "key-0" ) ?. value ) . toBe ( "value-0" ) ;
58+
59+ vi . spyOn ( Date , "now" ) . mockReturnValue ( 8_640_000_000_000_000 ) ;
60+ writeCache ( cache , "invalid" , "value" , 60_000 ) ;
61+
62+ expect ( cache . size ) . toBe ( 100 ) ;
63+ expect ( cache . get ( "key-0" ) ?. value ) . toBe ( "value-0" ) ;
64+ expect ( cache . has ( "invalid" ) ) . toBe ( false ) ;
65+ } ) ;
1966} ) ;
0 commit comments