The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { mergeWithObservedConfig } from "@alchemy.run/aws/CloudFront/Distribution"; |
| 2 | import { describe, expect, test } from "@alchemy.run/test"; |
| 3 | import type * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 4 | |
| 5 | /** |
| 6 | * A live distribution config as returned by `GetDistributionConfig` for a |
| 7 | * distribution CloudFront has normalized — every member present, including |
| 8 | * the ones `DistributionProps` cannot express (per-origin `CustomHeaders`, |
| 9 | * per-behavior `TrustedSigners`, `GrpcConfig`, …). `UpdateDistribution` |
| 10 | * rejects requests missing any of these with `IllegalUpdate`. |
| 11 | */ |
| 12 | const observed: cloudfront.DistributionConfig = { |
| 13 | CallerReference: "caller-ref", |
| 14 | Aliases: { Quantity: 0 }, |
| 15 | DefaultRootObject: "", |
| 16 | Origins: { |
| 17 | Quantity: 1, |
| 18 | Items: [ |
| 19 | { |
| 20 | Id: "s3", |
| 21 | DomainName: "bucket.s3.us-east-1.amazonaws.com", |
| 22 | OriginPath: "", |
| 23 | CustomHeaders: { Quantity: 0 }, |
| 24 | S3OriginConfig: { OriginAccessIdentity: "", OriginReadTimeout: 30 }, |
| 25 | ConnectionAttempts: 3, |
| 26 | ConnectionTimeout: 10, |
| 27 | OriginShield: { Enabled: false }, |
| 28 | OriginAccessControlId: "OAC_OBSERVED", |
| 29 | }, |
| 30 | ], |
| 31 | }, |
| 32 | OriginGroups: { Quantity: 0 }, |
| 33 | DefaultCacheBehavior: { |
| 34 | TargetOriginId: "s3", |
| 35 | TrustedSigners: { Enabled: false, Quantity: 0 }, |
| 36 | TrustedKeyGroups: { Enabled: false, Quantity: 0 }, |
| 37 | ViewerProtocolPolicy: "redirect-to-https", |
| 38 | AllowedMethods: { |
| 39 | Quantity: 2, |
| 40 | Items: ["HEAD", "GET"], |
| 41 | CachedMethods: { Quantity: 2, Items: ["HEAD", "GET"] }, |
| 42 | }, |
| 43 | SmoothStreaming: false, |
| 44 | Compress: true, |
| 45 | LambdaFunctionAssociations: { Quantity: 0 }, |
| 46 | FunctionAssociations: { Quantity: 0 }, |
| 47 | FieldLevelEncryptionId: "", |
| 48 | CachePolicyId: "cache-policy-id", |
| 49 | GrpcConfig: { Enabled: false }, |
| 50 | }, |
| 51 | CacheBehaviors: { |
| 52 | Quantity: 1, |
| 53 | Items: [ |
| 54 | { |
| 55 | PathPattern: "*.webp", |
| 56 | TargetOriginId: "s3", |
| 57 | TrustedSigners: { Enabled: false, Quantity: 0 }, |
| 58 | TrustedKeyGroups: { Enabled: false, Quantity: 0 }, |
| 59 | ViewerProtocolPolicy: "redirect-to-https", |
| 60 | AllowedMethods: { |
| 61 | Quantity: 2, |
| 62 | Items: ["HEAD", "GET"], |
| 63 | CachedMethods: { Quantity: 2, Items: ["HEAD", "GET"] }, |
| 64 | }, |
| 65 | SmoothStreaming: false, |
| 66 | Compress: true, |
| 67 | LambdaFunctionAssociations: { Quantity: 0 }, |
| 68 | FunctionAssociations: { Quantity: 0 }, |
| 69 | FieldLevelEncryptionId: "", |
| 70 | CachePolicyId: "cache-policy-id", |
| 71 | GrpcConfig: { Enabled: false }, |
| 72 | }, |
| 73 | ], |
| 74 | }, |
| 75 | CustomErrorResponses: { Quantity: 0 }, |
| 76 | Comment: "", |
| 77 | Logging: { Enabled: false, IncludeCookies: false, Bucket: "", Prefix: "" }, |
| 78 | PriceClass: "PriceClass_100", |
| 79 | Enabled: true, |
| 80 | ViewerCertificate: { |
| 81 | CloudFrontDefaultCertificate: true, |
| 82 | SSLSupportMethod: "vip", |
| 83 | MinimumProtocolVersion: "TLSv1", |
| 84 | }, |
| 85 | Restrictions: { GeoRestriction: { RestrictionType: "none", Quantity: 0 } }, |
| 86 | WebACLId: "", |
| 87 | HttpVersion: "http2", |
| 88 | IsIPV6Enabled: false, |
| 89 | ContinuousDeploymentPolicyId: "", |
| 90 | Staging: false, |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * The sparse config `toConfig` builds from props — only what the props |
| 95 | * express, everything else `undefined` (i.e. omitted from the request). |
| 96 | */ |
| 97 | const desired: cloudfront.DistributionConfig = { |
| 98 | CallerReference: "caller-ref", |
| 99 | Aliases: { Quantity: 0, Items: [] }, |
| 100 | Origins: { |
| 101 | Quantity: 1, |
| 102 | Items: [ |
| 103 | { |
| 104 | Id: "s3", |
| 105 | DomainName: "bucket.s3.us-east-1.amazonaws.com", |
| 106 | OriginAccessControlId: "OAC_DESIRED", |
| 107 | S3OriginConfig: { OriginAccessIdentity: "" }, |
| 108 | }, |
| 109 | ], |
| 110 | }, |
| 111 | DefaultCacheBehavior: { |
| 112 | TargetOriginId: "s3", |
| 113 | ViewerProtocolPolicy: "redirect-to-https", |
| 114 | AllowedMethods: { |
| 115 | Quantity: 2, |
| 116 | Items: ["GET", "HEAD"], |
| 117 | CachedMethods: { Quantity: 2, Items: ["GET", "HEAD"] }, |
| 118 | }, |
| 119 | Compress: true, |
| 120 | CachePolicyId: "cache-policy-id", |
| 121 | FunctionAssociations: { |
| 122 | Quantity: 1, |
| 123 | Items: [ |
| 124 | { FunctionARN: "arn:aws:cloudfront::123:function/deny-all", EventType: "viewer-request" }, |
| 125 | ], |
| 126 | }, |
| 127 | }, |
| 128 | CacheBehaviors: { |
| 129 | Quantity: 1, |
| 130 | Items: [ |
| 131 | { |
| 132 | PathPattern: "*.webp", |
| 133 | TargetOriginId: "s3", |
| 134 | ViewerProtocolPolicy: "redirect-to-https", |
| 135 | AllowedMethods: { |
| 136 | Quantity: 2, |
| 137 | Items: ["GET", "HEAD"], |
| 138 | CachedMethods: { Quantity: 2, Items: ["GET", "HEAD"] }, |
| 139 | }, |
| 140 | Compress: true, |
| 141 | CachePolicyId: "cache-policy-id", |
| 142 | }, |
| 143 | ], |
| 144 | }, |
| 145 | Comment: "", |
| 146 | Enabled: false, |
| 147 | Restrictions: { GeoRestriction: { RestrictionType: "none", Quantity: 0 } }, |
| 148 | IsIPV6Enabled: true, |
| 149 | } as cloudfront.DistributionConfig; |
| 150 | |
| 151 | describe( |
| 152 | "mergeWithObservedConfig", |
| 153 | { tags: ["unit", "provider:aws", "provider:aws:cloudfront", "local"] }, |
| 154 | () => { |
| 155 | const merged = mergeWithObservedConfig(desired, observed); |
| 156 | |
| 157 | test("desired values win over observed ones", () => { |
| 158 | expect(merged.Enabled).toBe(false); |
| 159 | expect(merged.IsIPV6Enabled).toBe(true); |
| 160 | expect(merged.Origins?.Items?.[0]?.OriginAccessControlId).toBe("OAC_DESIRED"); |
| 161 | expect(merged.DefaultCacheBehavior?.FunctionAssociations?.Quantity).toBe(1); |
| 162 | }); |
| 163 | |
| 164 | test("top-level members the desired config omits are filled from observed", () => { |
| 165 | expect(merged.DefaultRootObject).toBe(""); |
| 166 | expect(merged.WebACLId).toBe(""); |
| 167 | expect(merged.ContinuousDeploymentPolicyId).toBe(""); |
| 168 | expect(merged.Staging).toBe(false); |
| 169 | expect(merged.PriceClass).toBe("PriceClass_100"); |
| 170 | expect(merged.Logging).toEqual(observed.Logging); |
| 171 | expect(merged.OriginGroups).toEqual({ Quantity: 0 }); |
| 172 | expect(merged.CustomErrorResponses).toEqual({ Quantity: 0 }); |
| 173 | }); |
| 174 | |
| 175 | test("nested members inside origin items are filled by Id", () => { |
| 176 | const origin = merged.Origins?.Items?.[0]; |
| 177 | expect(origin?.CustomHeaders).toEqual({ Quantity: 0 }); |
| 178 | expect(origin?.OriginPath).toBe(""); |
| 179 | expect(origin?.ConnectionAttempts).toBe(3); |
| 180 | expect(origin?.ConnectionTimeout).toBe(10); |
| 181 | expect(origin?.OriginShield).toEqual({ Enabled: false }); |
| 182 | expect(origin?.S3OriginConfig?.OriginReadTimeout).toBe(30); |
| 183 | }); |
| 184 | |
| 185 | test("nested members inside behaviors are filled (default + by PathPattern)", () => { |
| 186 | for (const behavior of [merged.DefaultCacheBehavior, merged.CacheBehaviors?.Items?.[0]]) { |
| 187 | expect(behavior?.TrustedSigners).toEqual({ Enabled: false, Quantity: 0 }); |
| 188 | expect(behavior?.TrustedKeyGroups).toEqual({ Enabled: false, Quantity: 0 }); |
| 189 | expect(behavior?.SmoothStreaming).toBe(false); |
| 190 | expect(behavior?.FieldLevelEncryptionId).toBe(""); |
| 191 | expect(behavior?.GrpcConfig).toEqual({ Enabled: false }); |
| 192 | expect(behavior?.LambdaFunctionAssociations).toEqual({ Quantity: 0 }); |
| 193 | } |
| 194 | }); |
| 195 | |
| 196 | test("partial nested objects are completed member-wise, not replaced", () => { |
| 197 | // Desired sets CloudFrontDefaultCertificate only; the observed |
| 198 | // SSLSupportMethod / MinimumProtocolVersion carry over. |
| 199 | const mergedWithCert = mergeWithObservedConfig( |
| 200 | { ...desired, ViewerCertificate: { CloudFrontDefaultCertificate: true } }, |
| 201 | observed, |
| 202 | ); |
| 203 | expect(mergedWithCert.ViewerCertificate).toEqual({ |
| 204 | CloudFrontDefaultCertificate: true, |
| 205 | SSLSupportMethod: "vip", |
| 206 | MinimumProtocolVersion: "TLSv1", |
| 207 | }); |
| 208 | }); |
| 209 | |
| 210 | test("desired arrays are never extended by observed items", () => { |
| 211 | // Observed has one origin; a desired config that removes it must win. |
| 212 | const withoutOrigins = mergeWithObservedConfig( |
| 213 | { ...desired, Origins: { Quantity: 0, Items: [] } }, |
| 214 | observed, |
| 215 | ); |
| 216 | expect(withoutOrigins.Origins).toEqual({ Quantity: 0, Items: [] }); |
| 217 | }); |
| 218 | |
| 219 | test("a Quantity-only desired list never resurrects observed Items", () => { |
| 220 | // Dropping a geo whitelist produces `{ RestrictionType: "none", |
| 221 | // Quantity: 0 }` with no `Items`; filling `Items` from the observed |
| 222 | // whitelist would desynchronize Quantity/Items and CloudFront rejects |
| 223 | // the update with `InconsistentQuantities`. |
| 224 | const withWhitelist = { |
| 225 | ...observed, |
| 226 | Restrictions: { |
| 227 | GeoRestriction: { RestrictionType: "whitelist", Quantity: 2, Items: ["US", "GB"] }, |
| 228 | }, |
| 229 | } as cloudfront.DistributionConfig; |
| 230 | const dropped = mergeWithObservedConfig( |
| 231 | { |
| 232 | ...desired, |
| 233 | Restrictions: { GeoRestriction: { RestrictionType: "none", Quantity: 0 } }, |
| 234 | } as cloudfront.DistributionConfig, |
| 235 | withWhitelist, |
| 236 | ); |
| 237 | expect(dropped.Restrictions).toEqual({ |
| 238 | GeoRestriction: { RestrictionType: "none", Quantity: 0 }, |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | test("an origin absent from observed is passed through unchanged", () => { |
| 243 | const withNewOrigin = mergeWithObservedConfig( |
| 244 | { |
| 245 | ...desired, |
| 246 | Origins: { |
| 247 | Quantity: 1, |
| 248 | Items: [{ Id: "brand-new", DomainName: "new.example.com" } as cloudfront.Origin], |
| 249 | }, |
| 250 | }, |
| 251 | observed, |
| 252 | ); |
| 253 | expect(withNewOrigin.Origins?.Items?.[0]).toEqual({ |
| 254 | Id: "brand-new", |
| 255 | DomainName: "new.example.com", |
| 256 | }); |
| 257 | }); |
| 258 | |
| 259 | // A site switching between a server and S3 keeps the origin id but |
| 260 | // changes its kind. The old kind's config and OAC must not carry over |
| 261 | // (CloudFront rejects both kinds at once); shared members still fill. |
| 262 | test("an origin that changes kind keeps only the members both kinds share", () => { |
| 263 | const custom = mergeWithObservedConfig( |
| 264 | { |
| 265 | ...desired, |
| 266 | Origins: { |
| 267 | Quantity: 1, |
| 268 | Items: [ |
| 269 | { |
| 270 | Id: "s3", |
| 271 | DomainName: "server.example.com", |
| 272 | CustomOriginConfig: { |
| 273 | HTTPPort: 80, |
| 274 | HTTPSPort: 443, |
| 275 | OriginProtocolPolicy: "https-only", |
| 276 | }, |
| 277 | } as cloudfront.Origin, |
| 278 | ], |
| 279 | }, |
| 280 | }, |
| 281 | observed, |
| 282 | ).Origins?.Items?.[0]; |
| 283 | expect(custom?.S3OriginConfig).toBeUndefined(); |
| 284 | expect(custom?.OriginAccessControlId).toBeUndefined(); |
| 285 | expect(custom?.CustomOriginConfig?.OriginProtocolPolicy).toBe("https-only"); |
| 286 | expect(custom?.CustomHeaders).toEqual({ Quantity: 0 }); |
| 287 | expect(custom?.ConnectionAttempts).toBe(3); |
| 288 | }); |
| 289 | }, |
| 290 | ); |