|
1 | | -import { describe, expect, test, vi } from "vitest"; |
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
2 | 2 |
|
3 | 3 | import { CURRENT_VERSION_ID } from "../templates/skew-protection.js"; |
4 | | -import { listWorkerVersions, updateDeploymentMapping } from "./skew-protection.js"; |
| 4 | +import { getDeploymentMapping, listWorkerVersions, updateDeploymentMapping } from "./skew-protection.js"; |
| 5 | + |
| 6 | +const { MockCloudflare } = vi.hoisted(() => { |
| 7 | + class MockCloudflare { |
| 8 | + workers = { |
| 9 | + scripts: { |
| 10 | + versions: { |
| 11 | + list: vi.fn(() => []), |
| 12 | + }, |
| 13 | + }, |
| 14 | + }; |
| 15 | + } |
| 16 | + |
| 17 | + return { MockCloudflare: vi.fn(MockCloudflare) }; |
| 18 | +}); |
| 19 | + |
| 20 | +vi.mock("@opennextjs/aws/adapters/config/util.js", () => ({ |
| 21 | + loadConfig: vi.fn(() => ({ deploymentId: "deployment-id" })), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("cloudflare", async (importOriginal) => { |
| 25 | + const original = await importOriginal<typeof import("cloudflare")>(); |
| 26 | + return { ...original, Cloudflare: MockCloudflare }; |
| 27 | +}); |
5 | 28 |
|
6 | 29 | describe("skew protection", () => { |
| 30 | + describe("getDeploymentMapping", () => { |
| 31 | + beforeEach(() => { |
| 32 | + vi.stubEnv("CF_WORKER_NAME", "worker-name"); |
| 33 | + vi.stubEnv("CF_PREVIEW_DOMAIN", "example.workers.dev"); |
| 34 | + vi.stubEnv("CF_WORKERS_SCRIPTS_API_TOKEN", "test-token"); |
| 35 | + vi.stubEnv("CF_ACCOUNT_ID", "test-account-id"); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + vi.unstubAllEnvs(); |
| 40 | + }); |
| 41 | + |
| 42 | + test("disables response compression for worker version requests", async () => { |
| 43 | + await expect( |
| 44 | + getDeploymentMapping( |
| 45 | + { appBuildOutputPath: "/tmp/app" } as never, |
| 46 | + { cloudflare: { skewProtection: { enabled: true } } }, |
| 47 | + {} |
| 48 | + ) |
| 49 | + ).resolves.toEqual({ "deployment-id": CURRENT_VERSION_ID }); |
| 50 | + |
| 51 | + expect(MockCloudflare).toHaveBeenCalledWith({ |
| 52 | + apiToken: "test-token", |
| 53 | + defaultHeaders: { "Accept-Encoding": "identity" }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
7 | 58 | describe("listWorkerVersions", () => { |
8 | 59 | test("listWorkerVersions return versions ordered by time DESC", async () => { |
9 | 60 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
|
0 commit comments