1// Alchemy modifications are licensed under Apache-2.0.
2// This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
3import { mkdtempSync } from "node:fs";
4import { writeFile } from "node:fs/promises";
5import { tmpdir } from "node:os";
6import { join } from "node:path";
7import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
8import * as NodePath from "@effect/platform-node/NodePath";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import { describe, it } from "vitest";
12import { createAssetsIgnoreFunction, getContentType } from "../helpers.ts";
13
14describe("assets", () => {
15 const tmpDir = mkdtempSync(join(tmpdir(), "wrangler-tests"));
16 const platform = Layer.merge(NodeFileSystem.layer, NodePath.layer);
17 const makeAssetsIgnore = createAssetsIgnoreFunction(tmpDir).pipe(
18 Effect.provide(platform),
19 );
20
21 describe(".assetsignore", () => {
22 it("should ignore metafiles by default", async ({ expect }) => {
23 const { assetsIgnoreFunction } =
24 await Effect.runPromise(makeAssetsIgnore);
25
26 expect(assetsIgnoreFunction(".assetsignore")).toBeTruthy();
27 expect(assetsIgnoreFunction("_redirects")).toBeTruthy();
28 expect(assetsIgnoreFunction("_headers")).toBeTruthy();
29
30 // don't ignore metafiles in child directories
31 expect(assetsIgnoreFunction(join("child", ".assetsignore"))).toBeFalsy();
32 expect(assetsIgnoreFunction(join("child", "_redirects"))).toBeFalsy();
33 expect(assetsIgnoreFunction(join("child", "_headers"))).toBeFalsy();
34 });
35
36 it("should allow users to force opt-in metafiles", async ({ expect }) => {
37 await writeFile(
38 join(tmpDir, "./.assetsignore"),
39 "!.assetsignore\n!_redirects\n!_headers",
40 );
41 const { assetsIgnoreFunction } =
42 await Effect.runPromise(makeAssetsIgnore);
43
44 expect(assetsIgnoreFunction(".assetsignore")).toBeFalsy();
45 expect(assetsIgnoreFunction("_redirects")).toBeFalsy();
46 expect(assetsIgnoreFunction("_headers")).toBeFalsy();
47 });
48
49 it("should allow users to ignore files", async ({ expect }) => {
50 await writeFile(
51 join(tmpDir, "./.assetsignore"),
52 "logo.png\nchild/**/*.svg\n!child/nope.svg\n/*.js",
53 );
54 const { assetsIgnoreFunction } =
55 await Effect.runPromise(makeAssetsIgnore);
56
57 expect(assetsIgnoreFunction("abc")).toBeFalsy();
58 expect(assetsIgnoreFunction("logo.png")).toBeTruthy();
59 expect(assetsIgnoreFunction(join("child", "logo.png"))).toBeTruthy();
60 expect(assetsIgnoreFunction("foo.js")).toBeTruthy();
61 expect(assetsIgnoreFunction(join("child", "foo.js"))).toBeFalsy();
62 expect(assetsIgnoreFunction(join("child", "yup.svg"))).toBeTruthy();
63 expect(
64 assetsIgnoreFunction(join("child", "a", "b", "c", "yup.svg")),
65 ).toBeTruthy();
66 expect(assetsIgnoreFunction(join("child", "nope.svg"))).toBeFalsy();
67 });
68 });
69});
70
71describe("getContentType", () => {
72 it("should return 'text/javascript", ({ expect }) => {
73 const contentType = getContentType("/_astro/sponsors.CIiPz7eJ.js");
74 expect(contentType).toBe("text/javascript; charset=utf-8");
75 });
76});