@@ -3,7 +3,7 @@ import Testing
33@testable import Kaset
44
55/// Tests for APICache.
6- @Suite ( . serialized)
6+ @Suite ( " APICache " , . serialized, . tags ( . service ) )
77@MainActor
88struct APICacheTests {
99 var cache : APICache
@@ -16,99 +16,99 @@ struct APICacheTests {
1616 @Test ( " Cache set and get " )
1717 func cacheSetAndGet( ) {
1818 let data : [ String : Any ] = [ " key " : " value " , " number " : 42 ]
19- cache. set ( key: " test_key " , data: data, ttl: 60 )
19+ self . cache. set ( key: " test_key " , data: data, ttl: 60 )
2020
21- let retrieved = cache. get ( key: " test_key " )
21+ let retrieved = self . cache. get ( key: " test_key " )
2222 #expect( retrieved != nil )
2323 #expect( retrieved ? [ " key " ] as? String == " value " )
2424 #expect( retrieved ? [ " number " ] as? Int == 42 )
2525 }
2626
2727 @Test ( " Cache get nonexistent returns nil " )
2828 func cacheGetNonexistent( ) {
29- let retrieved = cache. get ( key: " nonexistent_key " )
29+ let retrieved = self . cache. get ( key: " nonexistent_key " )
3030 #expect( retrieved == nil )
3131 }
3232
3333 @Test ( " Cache invalidate all " )
3434 func cacheInvalidateAll( ) {
35- cache. set ( key: " key1 " , data: [ " a " : 1 ] , ttl: 60 )
36- cache. set ( key: " key2 " , data: [ " b " : 2 ] , ttl: 60 )
35+ self . cache. set ( key: " key1 " , data: [ " a " : 1 ] , ttl: 60 )
36+ self . cache. set ( key: " key2 " , data: [ " b " : 2 ] , ttl: 60 )
3737
38- #expect( cache. get ( key: " key1 " ) != nil )
39- #expect( cache. get ( key: " key2 " ) != nil )
38+ #expect( self . cache. get ( key: " key1 " ) != nil )
39+ #expect( self . cache. get ( key: " key2 " ) != nil )
4040
41- cache. invalidateAll ( )
41+ self . cache. invalidateAll ( )
4242
43- #expect( cache. get ( key: " key1 " ) == nil )
44- #expect( cache. get ( key: " key2 " ) == nil )
43+ #expect( self . cache. get ( key: " key1 " ) == nil )
44+ #expect( self . cache. get ( key: " key2 " ) == nil )
4545 }
4646
4747 @Test ( " Cache invalidate matching prefix " )
4848 func cacheInvalidateMatchingPrefix( ) {
49- cache. set ( key: " home_section1 " , data: [ " a " : 1 ] , ttl: 60 )
50- cache. set ( key: " home_section2 " , data: [ " b " : 2 ] , ttl: 60 )
51- cache. set ( key: " search_results " , data: [ " c " : 3 ] , ttl: 60 )
49+ self . cache. set ( key: " home_section1 " , data: [ " a " : 1 ] , ttl: 60 )
50+ self . cache. set ( key: " home_section2 " , data: [ " b " : 2 ] , ttl: 60 )
51+ self . cache. set ( key: " search_results " , data: [ " c " : 3 ] , ttl: 60 )
5252
53- cache. invalidate ( matching: " home_ " )
53+ self . cache. invalidate ( matching: " home_ " )
5454
55- #expect( cache. get ( key: " home_section1 " ) == nil )
56- #expect( cache. get ( key: " home_section2 " ) == nil )
57- #expect( cache. get ( key: " search_results " ) != nil )
55+ #expect( self . cache. get ( key: " home_section1 " ) == nil )
56+ #expect( self . cache. get ( key: " home_section2 " ) == nil )
57+ #expect( self . cache. get ( key: " search_results " ) != nil )
5858 }
5959
6060 @Test ( " Cache entry expiration " )
6161 func cacheEntryExpiration( ) async throws {
62- cache. set ( key: " short_lived " , data: [ " test " : true ] , ttl: 0.1 )
62+ self . cache. set ( key: " short_lived " , data: [ " test " : true ] , ttl: 0.1 )
6363
64- #expect( cache. get ( key: " short_lived " ) != nil )
64+ #expect( self . cache. get ( key: " short_lived " ) != nil )
6565
6666 try await Task . sleep ( for: . milliseconds( 150 ) )
6767
68- #expect( cache. get ( key: " short_lived " ) == nil )
68+ #expect( self . cache. get ( key: " short_lived " ) == nil )
6969 }
7070
7171 @Test ( " Cache overwrite " )
7272 func cacheOverwrite( ) {
73- cache. set ( key: " key " , data: [ " value " : 1 ] , ttl: 60 )
74- #expect( cache. get ( key: " key " ) ? [ " value " ] as? Int == 1 )
73+ self . cache. set ( key: " key " , data: [ " value " : 1 ] , ttl: 60 )
74+ #expect( self . cache. get ( key: " key " ) ? [ " value " ] as? Int == 1 )
7575
76- cache. set ( key: " key " , data: [ " value " : 2 ] , ttl: 60 )
77- #expect( cache. get ( key: " key " ) ? [ " value " ] as? Int == 2 )
76+ self . cache. set ( key: " key " , data: [ " value " : 2 ] , ttl: 60 )
77+ #expect( self . cache. get ( key: " key " ) ? [ " value " ] as? Int == 2 )
7878 }
7979
8080 @Test ( " Cache TTL constants are correct " )
8181 func cacheTTLConstants( ) {
82- #expect( APICache . TTL. home == 5 * 60 ) // 5 minutes
83- #expect( APICache . TTL. playlist == 30 * 60 ) // 30 minutes
84- #expect( APICache . TTL. artist == 60 * 60 ) // 1 hour
85- #expect( APICache . TTL. search == 2 * 60 ) // 2 minutes
86- #expect( APICache . TTL. library == 5 * 60 ) // 5 minutes
87- #expect( APICache . TTL. lyrics == 24 * 60 * 60 ) // 24 hours
88- #expect( APICache . TTL. songMetadata == 30 * 60 ) // 30 minutes
82+ #expect( APICache . TTL. home == 5 * 60 ) // 5 minutes
83+ #expect( APICache . TTL. playlist == 30 * 60 ) // 30 minutes
84+ #expect( APICache . TTL. artist == 60 * 60 ) // 1 hour
85+ #expect( APICache . TTL. search == 2 * 60 ) // 2 minutes
86+ #expect( APICache . TTL. library == 5 * 60 ) // 5 minutes
87+ #expect( APICache . TTL. lyrics == 24 * 60 * 60 ) // 24 hours
88+ #expect( APICache . TTL. songMetadata == 30 * 60 ) // 30 minutes
8989 }
9090
9191 @Test ( " Lyrics cache not invalidated by mutations " )
9292 func lyricsCacheNotInvalidatedByMutations( ) {
93- cache. set ( key: " browse:lyrics_abc123 " , data: [ " text " : " lyrics content " ] , ttl: APICache . TTL. lyrics)
94- cache. set ( key: " next:song_abc123 " , data: [ " title " : " song " ] , ttl: APICache . TTL. songMetadata)
93+ self . cache. set ( key: " browse:lyrics_abc123 " , data: [ " text " : " lyrics content " ] , ttl: APICache . TTL. lyrics)
94+ self . cache. set ( key: " next:song_abc123 " , data: [ " title " : " song " ] , ttl: APICache . TTL. songMetadata)
9595
96- cache. invalidate ( matching: " next: " )
96+ self . cache. invalidate ( matching: " next: " )
9797
98- #expect( cache. get ( key: " browse:lyrics_abc123 " ) != nil )
99- #expect( cache. get ( key: " next:song_abc123 " ) == nil )
98+ #expect( self . cache. get ( key: " browse:lyrics_abc123 " ) != nil )
99+ #expect( self . cache. get ( key: " next:song_abc123 " ) == nil )
100100 }
101101
102102 @Test ( " Song metadata cache invalidated by mutations " )
103103 func songMetadataCacheInvalidatedByMutations( ) {
104- cache. set ( key: " next:song_abc123 " , data: [ " title " : " song " ] , ttl: APICache . TTL. songMetadata)
105- cache. set ( key: " browse:home_section " , data: [ " section " : " home " ] , ttl: APICache . TTL. home)
104+ self . cache. set ( key: " next:song_abc123 " , data: [ " title " : " song " ] , ttl: APICache . TTL. songMetadata)
105+ self . cache. set ( key: " browse:home_section " , data: [ " section " : " home " ] , ttl: APICache . TTL. home)
106106
107- cache. invalidate ( matching: " browse: " )
108- cache. invalidate ( matching: " next: " )
107+ self . cache. invalidate ( matching: " browse: " )
108+ self . cache. invalidate ( matching: " next: " )
109109
110- #expect( cache. get ( key: " next:song_abc123 " ) == nil )
111- #expect( cache. get ( key: " browse:home_section " ) == nil )
110+ #expect( self . cache. get ( key: " next:song_abc123 " ) == nil )
111+ #expect( self . cache. get ( key: " browse:home_section " ) == nil )
112112 }
113113
114114 @Test ( " Cache entry isExpired property " )
0 commit comments