| 1 | import { |
| 2 | mergeAssetsConfigFiles, |
| 3 | readAssetsConfigFiles, |
| 4 | } from "@/Cloudflare/Workers/Assets.ts"; |
| 5 | import { PutDispatchNamespaceScriptRequest } from "@distilled.cloud/cloudflare/workers-for-platforms"; |
| 6 | import { buildRequest } from "@distilled.cloud/core/protocol-http"; |
| 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 8 | import { expect, layer } from "alchemy-test"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as pathe from "pathe"; |
| 11 | |
| 12 | const directory = pathe.resolve(import.meta.dirname, "fixtures/assets-config"); |
| 13 | |
| 14 | layer(NodeServices.layer)( |
| 15 | "dispatch asset configuration serialization", |
| 16 | (it) => { |
| 17 | for (const keepAssets of [false, true]) { |
| 18 | it.effect(`preserves file rules with keepAssets=${keepAssets}`, () => |
| 19 | Effect.gen(function* () { |
| 20 | const files = yield* readAssetsConfigFiles(directory); |
| 21 | const config = mergeAssetsConfigFiles(undefined, files); |
| 22 | const input: PutDispatchNamespaceScriptRequest = { |
| 23 | accountId: "account", |
| 24 | dispatchNamespace: "namespace", |
| 25 | scriptName: "script", |
| 26 | metadata: { |
| 27 | mainModule: "index.js", |
| 28 | keepAssets, |
| 29 | assets: { |
| 30 | ...(!keepAssets ? { jwt: "upload-token" } : {}), |
| 31 | config, |
| 32 | }, |
| 33 | }, |
| 34 | }; |
| 35 | const request = buildRequest({ |
| 36 | input, |
| 37 | inputAst: PutDispatchNamespaceScriptRequest.ast, |
| 38 | baseUrl: "https://api.cloudflare.com/client/v4", |
| 39 | }); |
| 40 | const form = (request.body as { formData: FormData }).formData; |
| 41 | expect(JSON.parse(form.get("metadata") as string)).toEqual({ |
| 42 | main_module: "index.js", |
| 43 | keep_assets: keepAssets, |
| 44 | assets: { |
| 45 | ...(!keepAssets ? { jwt: "upload-token" } : {}), |
| 46 | config: { |
| 47 | _headers: "/*\n Cache-Control: public, max-age=3600\n", |
| 48 | _redirects: "/old-path /index.html 301\n", |
| 49 | }, |
| 50 | }, |
| 51 | }); |
| 52 | }), |
| 53 | ); |
| 54 | } |
| 55 | }, |
| 56 | ); |