Open History to see the last commit
1import * as Cloudflare from "@alchemy.run/cloudflare";
2import { expect } from "@alchemy.run/test";
3import * as Test from "@alchemy.run/test/Alchemy";
4import * as Effect from "effect/Effect";
5import { MinimumLogLevel } from "effect/References";
6import * as pathe from "pathe";
7import { expectUrlContains, expectUrlHeader, expectUrlRedirect } from "../Utils/Http.ts";
8
9const { test } = Test.make({
10 providers: Cloudflare.providers(),
11 dev: true,
12});
13
14const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures/assets-config");
17
18/**
19 * Regression for https://github.com/alchemy-run/alchemy/pull/1418:
20 * local asset serving used to crash with `TypeError: undefined is not an
21 * object` the moment the assets directory contained a `_headers` or
22 * `_redirects` file, because `constructHeaders` / `constructRedirects`
23 * called `logger.log` with no logger. After the crash every subsequent
24 * request hung. Those helpers now log through Effect.log.
25 *
26 * This is the local counterpart of `AssetsConfigFiles.test.ts`: the
27 * fixture's rules must reach the local assets worker and actually apply.
28 */
29test.provider(
30 "dev assets _headers and _redirects apply without crashing the assets worker",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const deployed = yield* stack.deploy(
36 Effect.gen(function* () {
37 const worker = yield* Cloudflare.Worker("assets-config-local", {
38 assets: { directory: fixtureDir },
39 });
40 return { worker };
41 }),
42 );
43
44 expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/);
45
46 const url = deployed.worker.url;
47 yield* expectUrlContains(`${url}/`, "alchemy-assets-config-index");
48 yield* expectUrlHeader(`${url}/`, "x-alchemy-test", "assets-config-header", {
49 label: "local _headers",
50 });
51 yield* expectUrlRedirect(`${url}/old-path`, "/index.html", {
52 status: 301,
53 label: "local _redirects",
54 });
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 {
59 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
60 timeout: 120_000,
61 },
62);