The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { CachePolicy } from "@alchemy.run/aws/CloudFront"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | describe( |
| 13 | "AWS.CloudFront.CachePolicy", |
| 14 | { tags: ["provider:aws", "provider:aws:cloudfront", "live"] }, |
| 15 | () => { |
| 16 | test.provider( |
| 17 | "create, update, and delete a cache policy", |
| 18 | (stack) => |
| 19 | Effect.gen(function* () { |
| 20 | yield* stack.destroy(); |
| 21 | |
| 22 | const created = yield* stack.deploy( |
| 23 | Effect.gen(function* () { |
| 24 | return yield* CachePolicy("ApiCachePolicy", { |
| 25 | comment: "initial", |
| 26 | minTTL: 0, |
| 27 | defaultTTL: "60 seconds", |
| 28 | maxTTL: "1 hour", |
| 29 | parametersInCacheKeyAndForwardedToOrigin: { |
| 30 | EnableAcceptEncodingGzip: true, |
| 31 | EnableAcceptEncodingBrotli: true, |
| 32 | HeadersConfig: { |
| 33 | HeaderBehavior: "whitelist", |
| 34 | Headers: { Quantity: 1, Items: ["Authorization"] }, |
| 35 | }, |
| 36 | CookiesConfig: { CookieBehavior: "none" }, |
| 37 | QueryStringsConfig: { QueryStringBehavior: "all" }, |
| 38 | }, |
| 39 | }); |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | const initial = yield* cloudfront.getCachePolicy({ Id: created.cachePolicyId }); |
| 44 | expect(initial.CachePolicy?.Id).toEqual(created.cachePolicyId); |
| 45 | expect(initial.CachePolicy?.CachePolicyConfig?.Comment).toEqual("initial"); |
| 46 | expect(initial.CachePolicy?.CachePolicyConfig?.DefaultTTL).toEqual(60); |
| 47 | |
| 48 | const updated = yield* stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | return yield* CachePolicy("ApiCachePolicy", { |
| 51 | comment: "updated", |
| 52 | minTTL: 0, |
| 53 | defaultTTL: "120 seconds", |
| 54 | maxTTL: "1 day", |
| 55 | parametersInCacheKeyAndForwardedToOrigin: { |
| 56 | EnableAcceptEncodingGzip: true, |
| 57 | EnableAcceptEncodingBrotli: true, |
| 58 | HeadersConfig: { HeaderBehavior: "none" }, |
| 59 | CookiesConfig: { CookieBehavior: "none" }, |
| 60 | QueryStringsConfig: { QueryStringBehavior: "none" }, |
| 61 | }, |
| 62 | }); |
| 63 | }), |
| 64 | ); |
| 65 | |
| 66 | expect(updated.cachePolicyId).toEqual(created.cachePolicyId); |
| 67 | |
| 68 | // Control-plane reads are eventually consistent — poll until the |
| 69 | // update is visible, then assert the exact wire values. |
| 70 | const after = yield* cloudfront.getCachePolicy({ Id: updated.cachePolicyId }).pipe( |
| 71 | Effect.repeat({ |
| 72 | schedule: Schedule.fixed("2 seconds"), |
| 73 | until: (r) => r.CachePolicy?.CachePolicyConfig?.Comment === "updated", |
| 74 | times: 15, |
| 75 | }), |
| 76 | ); |
| 77 | expect(after.CachePolicy?.CachePolicyConfig?.Comment).toEqual("updated"); |
| 78 | expect(after.CachePolicy?.CachePolicyConfig?.DefaultTTL).toEqual(120); |
| 79 | expect(after.CachePolicy?.CachePolicyConfig?.MaxTTL).toEqual(86400); |
| 80 | |
| 81 | yield* stack.destroy(); |
| 82 | yield* assertCachePolicyDeleted(updated.cachePolicyId); |
| 83 | }), |
| 84 | { timeout: 300_000 }, |
| 85 | ); |
| 86 | |
| 87 | test.provider( |
| 88 | "list enumerates the deployed cache policy", |
| 89 | (stack) => |
| 90 | Effect.gen(function* () { |
| 91 | yield* stack.destroy(); |
| 92 | |
| 93 | const deployed = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | return yield* CachePolicy("ListCachePolicy", { |
| 96 | comment: "list", |
| 97 | minTTL: 0, |
| 98 | defaultTTL: "60 seconds", |
| 99 | maxTTL: "1 hour", |
| 100 | parametersInCacheKeyAndForwardedToOrigin: { |
| 101 | EnableAcceptEncodingGzip: true, |
| 102 | EnableAcceptEncodingBrotli: true, |
| 103 | HeadersConfig: { HeaderBehavior: "none" }, |
| 104 | CookiesConfig: { CookieBehavior: "none" }, |
| 105 | QueryStringsConfig: { QueryStringBehavior: "none" }, |
| 106 | }, |
| 107 | }); |
| 108 | }), |
| 109 | ); |
| 110 | |
| 111 | const provider = yield* Provider.findProvider(CachePolicy); |
| 112 | const all = yield* provider.list(); |
| 113 | |
| 114 | expect(all.some((p) => p.cachePolicyId === deployed.cachePolicyId)).toBe(true); |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | yield* assertCachePolicyDeleted(deployed.cachePolicyId); |
| 118 | }), |
| 119 | { timeout: 300_000 }, |
| 120 | ); |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | const assertCachePolicyDeleted = (id: string) => |
| 125 | cloudfront.getCachePolicy({ Id: id }).pipe( |
| 126 | Effect.flatMap(() => Effect.fail(new Error("CachePolicyStillExists"))), |
| 127 | Effect.catchTag("NoSuchCachePolicy", () => Effect.void), |
| 128 | Effect.retry({ |
| 129 | while: (error) => error instanceof Error && error.message === "CachePolicyStillExists", |
| 130 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]), |
| 131 | }), |
| 132 | ); |