Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Deferred from "effect/Deferred";
4import * as Fiber from "effect/Fiber";
5import { Deadline } from "../vendor/vite-plus-remote-cache-cloudflare/src/streams.ts";
6import * as Layer from "effect/Layer";
7import { vi } from "vite-plus/test";
8import { Observations } from "../vendor/vite-plus-remote-cache-cloudflare/src/observations.ts";
9import * as Admission from "./Admission.ts";
10import * as Cache from "./Cache.ts";
11import * as Savings from "./Savings.ts";
12
13vi.mock("../vendor/vite-plus-remote-cache-cloudflare/src/database.ts", () => ({
14 getScope: async () => ({}),
15 selectBlob: async () => ({ blob_object: "artifact", blob_size: 4 }),
16 cleanup: async () => {},
17}));
18
19it.effect("includes successful blob downloads in exported R2 usage", () =>
20 Effect.gen(function* () {
21 const get = vi.fn(async () => ({ size: 4, body: new Blob(["data"]).stream() }));
22 const bindings = {
23 developerKeys: JSON.stringify({
24 julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
25 }),
26 ARTIFACTS: { get },
27 readLimit: { limit: async () => ({ success: true }) },
28 } as unknown as Cache.Bindings["Service"];
29 using observations = vi.spyOn(Observations.prototype, "toJSON");
30 const response = yield* Effect.gen(function* () {
31 const cache = yield* Cache.Cache;
32 return yield* cache.handle(
33 new Request(
34 "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
35 {
36 headers: {
37 authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
38 },
39 },
40 ),
41 { waitUntil: () => {} },
42 );
43 }).pipe(
44 Effect.provide(
45 Cache.layer.pipe(
46 Layer.provide([
47 Layer.succeed(Cache.Bindings, bindings),
48 Admission.layer,
49 Layer.succeed(Savings.Savings, {
50 record: () => Effect.void,
51 emitTotals: () => Effect.void,
52 cleanup: Effect.void,
53 }),
54 ]),
55 ),
56 ),
57 );
58 expect(response.status).toBe(200);
59 expect(yield* Effect.promise(() => response.text())).toBe("data");
60 expect(get).toHaveBeenCalledWith("artifact");
61 expect(observations.mock.results.at(-1)?.value.r2_operations).toBe(1);
62 }),
63);
64
65it.effect("aborts the upstream deadline before disposing it on interruption", () =>
66 Effect.gen(function* () {
67 const entered = yield* Deferred.make<void>();
68 const cancelled: boolean[] = [];
69 const originalDispose = Deadline.prototype.dispose;
70 using _dispose = vi.spyOn(Deadline.prototype, "dispose").mockImplementation(function (
71 this: Deadline,
72 ) {
73 try {
74 this.check();
75 cancelled.push(false);
76 } catch {
77 cancelled.push(true);
78 }
79 originalDispose.call(this);
80 });
81 const bindings = {
82 developerKeys: JSON.stringify({
83 julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
84 }),
85 ARTIFACTS: {
86 get: () => {
87 Deferred.doneUnsafe(entered, Effect.void);
88 return new Promise(() => {});
89 },
90 },
91 readLimit: { limit: async () => ({ success: true }) },
92 } as unknown as Cache.Bindings["Service"];
93 yield* Effect.gen(function* () {
94 const cache = yield* Cache.Cache;
95 const fiber = yield* cache
96 .handle(
97 new Request(
98 "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
99 {
100 headers: {
101 authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
102 },
103 },
104 ),
105 { waitUntil: () => {} },
106 )
107 .pipe(Effect.forkChild);
108 yield* Deferred.await(entered);
109 yield* Fiber.interrupt(fiber);
110 expect(cancelled).toEqual([true]);
111 }).pipe(
112 Effect.provide(
113 Cache.layer.pipe(
114 Layer.provide([
115 Layer.succeed(Cache.Bindings, bindings),
116 Admission.layer,
117 Layer.succeed(Savings.Savings, {
118 record: () => Effect.void,
119 emitTotals: () => Effect.void,
120 cleanup: Effect.void,
121 }),
122 ]),
123 ),
124 ),
125 );
126 }),
127);
128
129it.effect("does not charge authenticated budgets for anonymous or malformed requests", () => {
130 const limit = vi.fn(async (_options: { key: string }) => ({ success: true }));
131 return Effect.gen(function* () {
132 const cache = yield* Cache.Cache;
133 for (const operation of [
134 "fetch",
135 "store",
136 "telemetry",
137 "blob/12345678-1234-1234-1234-123456789abc",
138 ]) {
139 for (const method of ["GET", "POST"]) {
140 yield* cache.handle(
141 new Request(`https://cache.example/projects/t3code/${operation}`, { method }),
142 { waitUntil: () => {} },
143 );
144 }
145 }
146 expect(limit).not.toHaveBeenCalled();
147 const headers = {
148 authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
149 };
150 yield* cache.handle(
151 new Request("https://cache.example/projects/t3code/telemetry", {
152 method: "POST",
153 headers,
154 body: "{}",
155 }),
156 { waitUntil: () => {} },
157 );
158 yield* cache.handle(
159 new Request(
160 "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
161 { headers },
162 ),
163 { waitUntil: () => {} },
164 );
165 expect(limit.mock.calls).toEqual([[{ key: "t3code:telemetry" }], [{ key: "t3code:blob" }]]);
166 }).pipe(
167 Effect.provide(
168 Cache.layer.pipe(
169 Layer.provide([
170 Layer.succeed(Cache.Bindings, {
171 developerKeys: JSON.stringify({
172 julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
173 }),
174 readerKeys: "{}",
175 readLimit: { limit },
176 storeLimit: { limit },
177 ARTIFACTS: { get: async () => ({ size: 4, body: new Blob(["data"]).stream() }) },
178 } as unknown as Cache.Bindings["Service"]),
179 Admission.layer,
180 Layer.succeed(Savings.Savings, {
181 record: () => Effect.void,
182 emitTotals: () => Effect.void,
183 cleanup: Effect.void,
184 }),
185 ]),
186 ),
187 ),
188 );
189});
190
191it.effect("authenticates setup probes without recording empty savings", () => {
192 const record = vi.fn(() => Effect.void);
193 const emitTotals = vi.fn(() => Effect.void);
194 return Effect.gen(function* () {
195 const cache = yield* Cache.Cache;
196 const response = yield* cache.handle(
197 new Request("https://cache.example/projects/t3code/telemetry", {
198 method: "POST",
199 headers: {
200 authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
201 },
202 body: JSON.stringify({
203 id: "a".repeat(64),
204 recordedAt: 0,
205 host: "setup-host",
206 platform: "linux",
207 arch: "x64",
208 ci: false,
209 runId: "setup",
210 tasks: [],
211 }),
212 }),
213 { waitUntil: () => {} },
214 );
215 expect(response.status).toBe(204);
216 expect(record).not.toHaveBeenCalled();
217 expect(emitTotals).not.toHaveBeenCalled();
218 }).pipe(
219 Effect.provide(
220 Cache.layer.pipe(
221 Layer.provide([
222 Layer.succeed(Cache.Bindings, {
223 developerKeys: JSON.stringify({
224 julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
225 }),
226 readLimit: { limit: async () => ({ success: true }) },
227 } as unknown as Cache.Bindings["Service"]),
228 Admission.layer,
229 Layer.succeed(Savings.Savings, { record, emitTotals, cleanup: Effect.void }),
230 ]),
231 ),
232 ),
233 );
234});
235
236it.effect("keeps artifacts private and limits CI reader credentials to reads", () =>
237 Effect.gen(function* () {
238 const cache = yield* Cache.Cache;
239 const context = { waitUntil: () => {} };
240 const blob = "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc";
241 for (const url of [blob, "https://cache.example/projects/t3code/fetch"]) {
242 const method = url === blob ? "GET" : "POST";
243 expect((yield* cache.handle(new Request(url, { method }), context)).status).toBe(401);
244 expect(
245 (yield* cache.handle(
246 new Request(url, {
247 method,
248 headers: { authorization: `Basic ${btoa("ci-reader:wrong")}` },
249 }),
250 context,
251 )).status,
252 ).toBe(403);
253 }
254 const headers = {
255 authorization: `Basic ${btoa("ci-reader:fixture-token-012345678901234567890123456789")}`,
256 };
257 expect((yield* cache.handle(new Request(blob, { headers }), context)).status).toBe(200);
258 expect(
259 (yield* cache.handle(
260 new Request("https://cache.example/projects/t3code/store", { method: "POST", headers }),
261 context,
262 )).status,
263 ).toBe(403);
264 }).pipe(
265 Effect.provide(
266 Cache.layer.pipe(
267 Layer.provide([
268 Layer.succeed(Cache.Bindings, {
269 developerKeys: "{}",
270 readerKeys: JSON.stringify({
271 "ci-reader": "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
272 }),
273 ARTIFACTS: { get: vi.fn(async () => ({ size: 4, body: new Blob(["data"]).stream() })) },
274 readLimit: { limit: async () => ({ success: true }) },
275 storeLimit: { limit: async () => ({ success: true }) },
276 } as unknown as Cache.Bindings["Service"]),
277 Admission.layer,
278 Layer.succeed(Savings.Savings, {
279 record: () => Effect.void,
280 emitTotals: () => Effect.void,
281 cleanup: Effect.void,
282 }),
283 ]),
284 ),
285 ),
286 ),
287);
288
289it.effect("rejects anonymous and invalid Basic requests before admission", () =>
290 Effect.gen(function* () {
291 const cache = yield* Cache.Cache;
292 for (const operation of ["store", "telemetry", "fetch"]) {
293 for (const authorization of [undefined, `Basic ${btoa("attacker:invalid")}`]) {
294 const response = yield* cache.handle(
295 new Request(`https://cache.example/projects/t3code/${operation}`, {
296 method: "POST",
297 headers: authorization ? { authorization } : {},
298 }),
299 { waitUntil: () => {} },
300 );
301 expect(response.status).toBe(authorization ? 403 : 401);
302 }
303 }
304 }).pipe(
305 Effect.provide(
306 Cache.layer.pipe(
307 Layer.provide([
308 Layer.succeed(Cache.Bindings, {
309 developerKeys: "{}",
310 readerKeys: "{}",
311 } as Cache.Bindings["Service"]),
312 Layer.succeed(Admission.Admission, {
313 withPermit: () => Effect.die("Unauthenticated request reached admission"),
314 }),
315 Layer.succeed(Savings.Savings, {
316 record: () => Effect.void,
317 emitTotals: () => Effect.void,
318 cleanup: Effect.void,
319 }),
320 ]),
321 ),
322 ),
323 ),
324);