|
| 1 | +import { afterEach, beforeEach, describe, it, vi } from "vitest"; |
| 2 | +import { validateAccountId } from "../src/account-id"; |
| 3 | +import { getCloudflareAccountIdFromEnv } from "../src/env-vars"; |
| 4 | + |
| 5 | +describe("validateAccountId", () => { |
| 6 | + it("accepts alphanumeric characters, hyphens, and underscores", ({ |
| 7 | + expect, |
| 8 | + }) => { |
| 9 | + for (const accountId of [ |
| 10 | + "a4b3c2d1e0f9a8b7c6d5e4f3a2b1c0d9", |
| 11 | + "ACCOUNT-ID", |
| 12 | + "account_id", |
| 13 | + "0", |
| 14 | + ]) { |
| 15 | + expect(validateAccountId(accountId, "in the test")).toBe(accountId); |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + it("rejects values that cannot be used in an API URL", ({ expect }) => { |
| 20 | + for (const accountId of [ |
| 21 | + "ваш-идентификатор-аккаунта", |
| 22 | + "account id", |
| 23 | + "\naccount-id", |
| 24 | + "account/id", |
| 25 | + "account:id", |
| 26 | + "", |
| 27 | + ]) { |
| 28 | + expect(() => validateAccountId(accountId, "in the test")).toThrow( |
| 29 | + "Account IDs may only contain alphanumeric characters, hyphens, and underscores." |
| 30 | + ); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + it("names the source of the invalid value", ({ expect }) => { |
| 35 | + expect(() => |
| 36 | + validateAccountId("oh no", "set in the `SOME_VARIABLE` variable") |
| 37 | + ).toThrowErrorMatchingInlineSnapshot( |
| 38 | + `[Error: Invalid account ID "oh no" set in the \`SOME_VARIABLE\` variable. Account IDs may only contain alphanumeric characters, hyphens, and underscores.]` |
| 39 | + ); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("getCloudflareAccountIdFromEnv", () => { |
| 44 | + beforeEach(() => { |
| 45 | + vi.unstubAllEnvs(); |
| 46 | + }); |
| 47 | + afterEach(() => { |
| 48 | + vi.unstubAllEnvs(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns the value of CLOUDFLARE_ACCOUNT_ID", ({ expect }) => { |
| 52 | + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "some-account-id"); |
| 53 | + expect(getCloudflareAccountIdFromEnv()).toBe("some-account-id"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("treats an empty value as unset", ({ expect }) => { |
| 57 | + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", ""); |
| 58 | + expect(getCloudflareAccountIdFromEnv()).toBeUndefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("throws for a value that cannot be used in an API URL", ({ expect }) => { |
| 62 | + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "ваш-идентификатор-аккаунта"); |
| 63 | + expect(getCloudflareAccountIdFromEnv).toThrowErrorMatchingInlineSnapshot( |
| 64 | + `[Error: Invalid account ID "ваш-идентификатор-аккаунта" set in the \`CLOUDFLARE_ACCOUNT_ID\` environment variable. Account IDs may only contain alphanumeric characters, hyphens, and underscores.]` |
| 65 | + ); |
| 66 | + }); |
| 67 | +}); |
0 commit comments