-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcache-vp.test.ts
More file actions
207 lines (165 loc) · 6.05 KB
/
cache-vp.test.ts
File metadata and controls
207 lines (165 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { describe, it, expect, beforeEach, afterEach, vi } from "vite-plus/test";
import { arch } from "node:os";
import { resolveVersion, restoreVpCache, saveVpCache } from "./cache-vp.js";
import { State } from "./types.js";
import { restoreCache, saveCache } from "@actions/cache";
import { saveState, getState, warning } from "@actions/core";
import { existsSync, symlinkSync, mkdirSync } from "node:fs";
// Mock @actions/cache
vi.mock("@actions/cache", () => ({
restoreCache: vi.fn(),
saveCache: vi.fn(),
}));
// Mock @actions/core
vi.mock("@actions/core", () => ({
info: vi.fn(),
debug: vi.fn(),
warning: vi.fn(),
saveState: vi.fn(),
getState: vi.fn(),
}));
// Mock node:fs
vi.mock("node:fs", async () => {
const actual = await vi.importActual<typeof import("node:fs")>("node:fs");
return {
...actual,
existsSync: vi.fn(),
symlinkSync: vi.fn(),
mkdirSync: vi.fn(),
rmSync: vi.fn(),
};
});
describe("resolveVersion", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("should return explicit version as-is", async () => {
const result = await resolveVersion("0.1.8");
expect(result).toBe("0.1.8");
});
it("should return explicit semver-like versions as-is", async () => {
const result = await resolveVersion("1.0.0-beta.1");
expect(result).toBe("1.0.0-beta.1");
});
it("should resolve 'latest' from npm registry", async () => {
const fetchSpy = vi
.spyOn(globalThis, "fetch")
.mockResolvedValue(new Response(JSON.stringify({ version: "0.2.0" }), { status: 200 }));
const result = await resolveVersion("latest");
expect(result).toBe("0.2.0");
expect(fetchSpy).toHaveBeenCalledWith(
"https://registry.npmjs.org/vite-plus/latest",
expect.objectContaining({ signal: expect.any(AbortSignal) }),
);
});
it("should return undefined when fetch fails", async () => {
vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("network error"));
const result = await resolveVersion("latest");
expect(result).toBeUndefined();
});
it("should return undefined when fetch returns non-ok status", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("Not Found", { status: 404 }));
const result = await resolveVersion("latest");
expect(result).toBeUndefined();
});
it("should return undefined for empty string input", async () => {
vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("should not be called"));
const result = await resolveVersion("");
expect(result).toBeUndefined();
});
});
describe("restoreVpCache", () => {
beforeEach(() => {
vi.stubEnv("RUNNER_OS", "Linux");
vi.stubEnv("HOME", "/home/runner");
});
afterEach(() => {
vi.unstubAllEnvs();
vi.resetAllMocks();
});
it("should return true on cache hit and create symlinks", async () => {
vi.mocked(restoreCache).mockResolvedValue(`setup-vp-Linux-${arch()}-0.1.8`);
vi.mocked(existsSync).mockReturnValue(false);
const result = await restoreVpCache("0.1.8");
expect(result).toBe(true);
expect(saveState).toHaveBeenCalledWith(
State.VpCachePrimaryKey,
`setup-vp-Linux-${arch()}-0.1.8`,
);
expect(saveState).toHaveBeenCalledWith(
State.VpCacheMatchedKey,
`setup-vp-Linux-${arch()}-0.1.8`,
);
expect(saveState).toHaveBeenCalledWith(State.VpCacheVersion, "0.1.8");
// Verify symlinks were created
expect(symlinkSync).toHaveBeenCalledTimes(2);
expect(mkdirSync).toHaveBeenCalled();
});
it("should cache the version-specific directory, not the whole home", async () => {
vi.mocked(restoreCache).mockResolvedValue(undefined);
await restoreVpCache("0.1.8");
expect(restoreCache).toHaveBeenCalledWith(
["/home/runner/.vite-plus/0.1.8"],
`setup-vp-Linux-${arch()}-0.1.8`,
);
});
it("should return false on cache miss", async () => {
vi.mocked(restoreCache).mockResolvedValue(undefined);
const result = await restoreVpCache("0.1.8");
expect(result).toBe(false);
expect(symlinkSync).not.toHaveBeenCalled();
});
it("should return false and warn on cache restore error", async () => {
vi.mocked(restoreCache).mockRejectedValue(new Error("cache error"));
const result = await restoreVpCache("0.1.8");
expect(result).toBe(false);
expect(warning).toHaveBeenCalled();
});
});
describe("saveVpCache", () => {
beforeEach(() => {
vi.stubEnv("HOME", "/home/runner");
});
afterEach(() => {
vi.unstubAllEnvs();
vi.resetAllMocks();
});
it("should skip when no primary key", async () => {
vi.mocked(getState).mockReturnValue("");
await saveVpCache();
expect(saveCache).not.toHaveBeenCalled();
});
it("should skip when primary key matches matched key", async () => {
vi.mocked(getState).mockImplementation((key: string) => {
if (key === State.VpCachePrimaryKey) return `setup-vp-Linux-${arch()}-0.1.8`;
if (key === State.VpCacheMatchedKey) return `setup-vp-Linux-${arch()}-0.1.8`;
if (key === State.VpCacheVersion) return "0.1.8";
return "";
});
await saveVpCache();
expect(saveCache).not.toHaveBeenCalled();
});
it("should save only the version-specific directory on cache miss", async () => {
vi.mocked(getState).mockImplementation((key: string) => {
if (key === State.VpCachePrimaryKey) return `setup-vp-Linux-${arch()}-0.1.8`;
if (key === State.VpCacheVersion) return "0.1.8";
return "";
});
vi.mocked(saveCache).mockResolvedValue(12345);
await saveVpCache();
expect(saveCache).toHaveBeenCalledWith(
["/home/runner/.vite-plus/0.1.8"],
`setup-vp-Linux-${arch()}-0.1.8`,
);
});
it("should handle save errors gracefully", async () => {
vi.mocked(getState).mockImplementation((key: string) => {
if (key === State.VpCachePrimaryKey) return `setup-vp-Linux-${arch()}-0.1.8`;
if (key === State.VpCacheVersion) return "0.1.8";
return "";
});
vi.mocked(saveCache).mockRejectedValue(new Error("ReserveCacheError"));
await saveVpCache();
expect(warning).toHaveBeenCalled();
});
});