1
+ // @vitest -environment nuxt
2
+ import { describe , it , expect , beforeEach } from 'vitest'
3
+
4
+ describe ( 'Metrics Cache Key Generation' , ( ) => {
5
+ it ( 'should create unique cache keys for different query parameters' , ( ) => {
6
+ // Test the cache key logic that was implemented
7
+ const createCacheKey = ( path : string , query : Record < string , string > ) => {
8
+ const queryString = new URLSearchParams ( query ) . toString ( )
9
+ return queryString ? `${ path } ?${ queryString } ` : path
10
+ }
11
+
12
+ const path = '/api/metrics'
13
+
14
+ // Different date ranges should create different cache keys
15
+ const query1 = { since : '2024-01-01' , until : '2024-01-31' , scope : 'organization' , githubOrg : 'test-org' }
16
+ const query2 = { since : '2024-02-01' , until : '2024-02-28' , scope : 'organization' , githubOrg : 'test-org' }
17
+ const query3 = { since : '2024-01-01' , until : '2024-01-31' , scope : 'organization' , githubOrg : 'test-org' }
18
+
19
+ const key1 = createCacheKey ( path , query1 )
20
+ const key2 = createCacheKey ( path , query2 )
21
+ const key3 = createCacheKey ( path , query3 )
22
+
23
+ // Different date ranges should have different keys
24
+ expect ( key1 ) . not . toBe ( key2 )
25
+
26
+ // Same parameters should have same key
27
+ expect ( key1 ) . toBe ( key3 )
28
+
29
+ // Keys should include query parameters
30
+ expect ( key1 ) . toContain ( 'since=2024-01-01' )
31
+ expect ( key1 ) . toContain ( 'until=2024-01-31' )
32
+ expect ( key2 ) . toContain ( 'since=2024-02-01' )
33
+ expect ( key2 ) . toContain ( 'until=2024-02-28' )
34
+ } )
35
+
36
+ it ( 'should handle empty query parameters' , ( ) => {
37
+ const createCacheKey = ( path : string , query : Record < string , string > ) => {
38
+ const queryString = new URLSearchParams ( query ) . toString ( )
39
+ return queryString ? `${ path } ?${ queryString } ` : path
40
+ }
41
+
42
+ const path = '/api/metrics'
43
+ const emptyQuery = { }
44
+
45
+ const key = createCacheKey ( path , emptyQuery )
46
+ expect ( key ) . toBe ( path )
47
+ } )
48
+
49
+ it ( 'should handle undefined query values' , ( ) => {
50
+ const createCacheKey = ( path : string , query : Record < string , any > ) => {
51
+ // Filter out undefined values before creating query string
52
+ const filteredQuery = Object . fromEntries (
53
+ Object . entries ( query ) . filter ( ( [ _ , value ] ) => value !== undefined )
54
+ )
55
+ const queryString = new URLSearchParams ( filteredQuery ) . toString ( )
56
+ return queryString ? `${ path } ?${ queryString } ` : path
57
+ }
58
+
59
+ const path = '/api/metrics'
60
+ const queryWithUndefined = { since : '2024-01-01' , until : undefined , scope : 'organization' }
61
+
62
+ const key = createCacheKey ( path , queryWithUndefined )
63
+ expect ( key ) . toContain ( 'since=2024-01-01' )
64
+ expect ( key ) . toContain ( 'scope=organization' )
65
+ expect ( key ) . not . toContain ( 'until=' )
66
+ } )
67
+ } )
0 commit comments