@@ -2,8 +2,11 @@ import crypto from 'node:crypto'
22import fs from 'node:fs/promises'
33import path from 'node:path'
44
5+ import { Readable } from 'node:stream'
56import { restoreCache , saveCache } from '@actions/cache'
67import { afterAll , beforeAll , describe , expect , test } from 'vitest'
8+ import { useStorageAdapter } from '~/lib/storage'
9+ import { getCacheFileName } from '~/lib/utils'
710
811const TEST_TEMP_DIR = path . join ( import . meta. dirname , 'temp' )
912await fs . mkdir ( TEST_TEMP_DIR , { recursive : true } )
@@ -44,3 +47,69 @@ for (const version of versions) {
4447 } )
4548 } )
4649}
50+
51+ test (
52+ 'pruning cache' ,
53+ {
54+ timeout : 60_000 ,
55+ } ,
56+ async ( ) => {
57+ const storage = await useStorageAdapter ( )
58+
59+ const { cacheId } = await storage . reserveCache ( {
60+ key : 'cache-a' ,
61+ version : '1' ,
62+ } )
63+ if ( ! cacheId ) throw new Error ( 'Failed to reserve cache' )
64+
65+ // random 100MB ReadableStream
66+ const stream = new ReadableStream < Buffer > ( {
67+ start ( controller ) {
68+ const chunkSize = 1024 * 1024 // 1MB
69+ for ( let i = 0 ; i < 100 ; i ++ ) {
70+ const chunk = Buffer . alloc ( chunkSize )
71+ controller . enqueue ( chunk )
72+ }
73+ controller . close ( )
74+ } ,
75+ } )
76+ await storage . uploadChunk ( {
77+ uploadId : cacheId ,
78+ chunkIndex : 0 ,
79+ chunkStart : 0 ,
80+ chunkStream : stream ,
81+ } )
82+ await storage . commitCache ( cacheId )
83+
84+ // exists
85+ expect (
86+ await storage . getCacheEntry ( {
87+ keys : [ 'cache-a' ] ,
88+ version : '1' ,
89+ } ) ,
90+ ) . toStrictEqual ( {
91+ archiveLocation : expect . stringMatching (
92+ new RegExp (
93+ `http:\/\/localhost:3000\/download\/[^\/]+\/${ getCacheFileName ( 'cache-a' , '1' ) } ` ,
94+ ) ,
95+ ) ,
96+ cacheKey : 'cache-a' ,
97+ } )
98+ expect (
99+ await storage . driver . createReadStream ( getCacheFileName ( 'cache-a' , '1' ) ) . catch ( ( ) => null ) ,
100+ ) . toBeInstanceOf ( Readable )
101+
102+ await storage . pruneCaches ( )
103+
104+ // doesn't exist
105+ expect (
106+ await storage . getCacheEntry ( {
107+ keys : [ 'cache-a' ] ,
108+ version : '1' ,
109+ } ) ,
110+ ) . toBeNull ( )
111+ expect (
112+ await storage . driver . createReadStream ( getCacheFileName ( 'cache-a' , '1' ) ) . catch ( ( ) => null ) ,
113+ ) . toBe ( null )
114+ } ,
115+ )
0 commit comments