@@ -4,7 +4,6 @@ import { resolveVersion, restoreVpCache, saveVpCache } from "./cache-vp.js";
44import { State } from "./types.js" ;
55import { restoreCache , saveCache } from "@actions/cache" ;
66import { saveState , getState , warning } from "@actions/core" ;
7- import { existsSync , symlinkSync , mkdirSync } from "node:fs" ;
87
98// Mock @actions /cache
109vi . mock ( "@actions/cache" , ( ) => ( {
@@ -21,18 +20,6 @@ vi.mock("@actions/core", () => ({
2120 getState : vi . fn ( ) ,
2221} ) ) ;
2322
24- // Mock node:fs
25- vi . mock ( "node:fs" , async ( ) => {
26- const actual = await vi . importActual < typeof import ( "node:fs" ) > ( "node:fs" ) ;
27- return {
28- ...actual ,
29- existsSync : vi . fn ( ) ,
30- symlinkSync : vi . fn ( ) ,
31- mkdirSync : vi . fn ( ) ,
32- rmSync : vi . fn ( ) ,
33- } ;
34- } ) ;
35-
3623describe ( "resolveVersion" , ( ) => {
3724 afterEach ( ( ) => {
3825 vi . restoreAllMocks ( ) ;
@@ -94,51 +81,51 @@ describe("restoreVpCache", () => {
9481 vi . resetAllMocks ( ) ;
9582 } ) ;
9683
97- it ( "should return true on cache hit and create symlinks " , async ( ) => {
98- vi . mocked ( restoreCache ) . mockResolvedValue ( `setup-vp-Linux-${ arch ( ) } -0.1.8` ) ;
99- vi . mocked ( existsSync ) . mockReturnValue ( false ) ;
84+ it ( "should return true on cache hit" , async ( ) => {
85+ const expectedKey = `setup-vp-Linux-${ arch ( ) } -0.1.8-node20` ;
86+ vi . mocked ( restoreCache ) . mockResolvedValue ( expectedKey ) ;
10087
101- const result = await restoreVpCache ( "0.1.8" ) ;
88+ const result = await restoreVpCache ( "0.1.8" , "20" ) ;
10289
10390 expect ( result ) . toBe ( true ) ;
104- expect ( saveState ) . toHaveBeenCalledWith (
105- State . VpCachePrimaryKey ,
106- `setup-vp-Linux-${ arch ( ) } -0.1.8` ,
107- ) ;
108- expect ( saveState ) . toHaveBeenCalledWith (
109- State . VpCacheMatchedKey ,
110- `setup-vp-Linux-${ arch ( ) } -0.1.8` ,
91+ expect ( saveState ) . toHaveBeenCalledWith ( State . VpCachePrimaryKey , expectedKey ) ;
92+ expect ( saveState ) . toHaveBeenCalledWith ( State . VpCacheMatchedKey , expectedKey ) ;
93+ } ) ;
94+
95+ it ( "should include node version in cache key" , async ( ) => {
96+ vi . mocked ( restoreCache ) . mockResolvedValue ( undefined ) ;
97+
98+ await restoreVpCache ( "0.1.8" , "22" ) ;
99+
100+ expect ( restoreCache ) . toHaveBeenCalledWith (
101+ [ "/home/runner/.vite-plus" ] ,
102+ `setup-vp-Linux-${ arch ( ) } -0.1.8-node22` ,
111103 ) ;
112- expect ( saveState ) . toHaveBeenCalledWith ( State . VpCacheVersion , "0.1.8" ) ;
113- // Verify symlinks were created
114- expect ( symlinkSync ) . toHaveBeenCalledTimes ( 2 ) ;
115- expect ( mkdirSync ) . toHaveBeenCalled ( ) ;
116104 } ) ;
117105
118- it ( "should cache the version-specific directory, not the whole home " , async ( ) => {
106+ it ( "should handle empty node version " , async ( ) => {
119107 vi . mocked ( restoreCache ) . mockResolvedValue ( undefined ) ;
120108
121- await restoreVpCache ( "0.1.8" ) ;
109+ await restoreVpCache ( "0.1.8" , "" ) ;
122110
123111 expect ( restoreCache ) . toHaveBeenCalledWith (
124- [ "/home/runner/.vite-plus/0.1.8 " ] ,
125- `setup-vp-Linux-${ arch ( ) } -0.1.8` ,
112+ [ "/home/runner/.vite-plus" ] ,
113+ `setup-vp-Linux-${ arch ( ) } -0.1.8-node ` ,
126114 ) ;
127115 } ) ;
128116
129117 it ( "should return false on cache miss" , async ( ) => {
130118 vi . mocked ( restoreCache ) . mockResolvedValue ( undefined ) ;
131119
132- const result = await restoreVpCache ( "0.1.8" ) ;
120+ const result = await restoreVpCache ( "0.1.8" , "20" ) ;
133121
134122 expect ( result ) . toBe ( false ) ;
135- expect ( symlinkSync ) . not . toHaveBeenCalled ( ) ;
136123 } ) ;
137124
138125 it ( "should return false and warn on cache restore error" , async ( ) => {
139126 vi . mocked ( restoreCache ) . mockRejectedValue ( new Error ( "cache error" ) ) ;
140127
141- const result = await restoreVpCache ( "0.1.8" ) ;
128+ const result = await restoreVpCache ( "0.1.8" , "20" ) ;
142129
143130 expect ( result ) . toBe ( false ) ;
144131 expect ( warning ) . toHaveBeenCalled ( ) ;
@@ -164,10 +151,10 @@ describe("saveVpCache", () => {
164151 } ) ;
165152
166153 it ( "should skip when primary key matches matched key" , async ( ) => {
167- vi . mocked ( getState ) . mockImplementation ( ( key : string ) => {
168- if ( key === State . VpCachePrimaryKey ) return `setup-vp-Linux- ${ arch ( ) } -0.1.8` ;
169- if ( key === State . VpCacheMatchedKey ) return `setup-vp-Linux- ${ arch ( ) } -0.1.8` ;
170- if ( key === State . VpCacheVersion ) return "0.1.8" ;
154+ const key = `setup-vp-Linux- ${ arch ( ) } -0.1.8-node20` ;
155+ vi . mocked ( getState ) . mockImplementation ( ( k : string ) => {
156+ if ( k === State . VpCachePrimaryKey ) return key ;
157+ if ( k === State . VpCacheMatchedKey ) return key ;
171158 return "" ;
172159 } ) ;
173160
@@ -176,26 +163,22 @@ describe("saveVpCache", () => {
176163 expect ( saveCache ) . not . toHaveBeenCalled ( ) ;
177164 } ) ;
178165
179- it ( "should save only the version-specific directory on cache miss" , async ( ) => {
180- vi . mocked ( getState ) . mockImplementation ( ( key : string ) => {
181- if ( key === State . VpCachePrimaryKey ) return `setup-vp-Linux- ${ arch ( ) } -0.1.8` ;
182- if ( key === State . VpCacheVersion ) return "0.1.8" ;
166+ it ( "should save cache on cache miss" , async ( ) => {
167+ const key = `setup-vp-Linux- ${ arch ( ) } -0.1.8-node20` ;
168+ vi . mocked ( getState ) . mockImplementation ( ( k : string ) => {
169+ if ( k === State . VpCachePrimaryKey ) return key ;
183170 return "" ;
184171 } ) ;
185172 vi . mocked ( saveCache ) . mockResolvedValue ( 12345 ) ;
186173
187174 await saveVpCache ( ) ;
188175
189- expect ( saveCache ) . toHaveBeenCalledWith (
190- [ "/home/runner/.vite-plus/0.1.8" ] ,
191- `setup-vp-Linux-${ arch ( ) } -0.1.8` ,
192- ) ;
176+ expect ( saveCache ) . toHaveBeenCalledWith ( [ "/home/runner/.vite-plus" ] , key ) ;
193177 } ) ;
194178
195179 it ( "should handle save errors gracefully" , async ( ) => {
196- vi . mocked ( getState ) . mockImplementation ( ( key : string ) => {
197- if ( key === State . VpCachePrimaryKey ) return `setup-vp-Linux-${ arch ( ) } -0.1.8` ;
198- if ( key === State . VpCacheVersion ) return "0.1.8" ;
180+ vi . mocked ( getState ) . mockImplementation ( ( k : string ) => {
181+ if ( k === State . VpCachePrimaryKey ) return `setup-vp-Linux-${ arch ( ) } -0.1.8-node20` ;
199182 return "" ;
200183 } ) ;
201184 vi . mocked ( saveCache ) . mockRejectedValue ( new Error ( "ReserveCacheError" ) ) ;
0 commit comments