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 * as AWS from "@alchemy.run/aws"; |
| 2 | import { KeyValueStore, KvEntries, KvRoutesUpdate } from "@alchemy.run/aws/CloudFront"; |
| 3 | import { extractValue, withKvsRegion } from "@alchemy.run/aws/CloudFront/common"; |
| 4 | import { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 7 | import * as kvs from "@distilled.cloud/aws/cloudfront-keyvaluestore"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { driftActions, repairDrift, waitForDrift } from "../drift.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | describe( |
| 16 | "AWS.CloudFront.KvEntries", |
| 17 | { tags: ["provider:aws", "provider:aws:cloudfront", "live"] }, |
| 18 | () => { |
| 19 | test.provider("list returns [] for the non-listable KvEntries resource", () => |
| 20 | Effect.gen(function* () { |
| 21 | // KvEntries is keyed entirely by a parent store ARN + namespace and |
| 22 | // represents managed data, so it has no enumeration API → list() is []. |
| 23 | const provider = yield* Provider.findProvider(KvEntries); |
| 24 | expect(yield* provider.list()).toEqual([]); |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "create, update, and delete namespaced entries", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | const initialEntries = { |
| 35 | "/": "/index.html", |
| 36 | "/about": "/about.html", |
| 37 | "/old": "/old.html", |
| 38 | }; |
| 39 | |
| 40 | const deployed = yield* stack.deploy( |
| 41 | Effect.gen(function* () { |
| 42 | const store = yield* KeyValueStore("EntriesStore", { |
| 43 | comment: "kv-entries lifecycle", |
| 44 | }); |
| 45 | const entries = yield* KvEntries("Routes", { |
| 46 | store: store.keyValueStoreArn, |
| 47 | namespace: "routes", |
| 48 | entries: initialEntries, |
| 49 | purge: true, |
| 50 | }); |
| 51 | return { store, entries }; |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | expect(deployed.entries.store).toBe(deployed.store.keyValueStoreArn); |
| 56 | expect(deployed.entries.namespace).toBe("routes"); |
| 57 | expect(deployed.entries.entries).toEqual(initialEntries); |
| 58 | |
| 59 | const described = yield* withKvsRegion( |
| 60 | kvs.describeKeyValueStore({ KvsARN: deployed.store.keyValueStoreArn }), |
| 61 | ); |
| 62 | expect(described.KvsARN).toBe(deployed.store.keyValueStoreArn); |
| 63 | expect(described.ItemCount).toBeGreaterThanOrEqual(3); |
| 64 | |
| 65 | yield* assertEntries(deployed.store.keyValueStoreArn, "routes", { |
| 66 | "/": "/index.html", |
| 67 | "/about": "/about.html", |
| 68 | "/old": "/old.html", |
| 69 | }); |
| 70 | |
| 71 | const updatedEntries = { |
| 72 | "/": "/index.html", |
| 73 | "/about": "/about-v2.html", |
| 74 | "/contact": "/contact.html", |
| 75 | }; |
| 76 | |
| 77 | const updated = yield* stack.deploy( |
| 78 | Effect.gen(function* () { |
| 79 | const store = yield* KeyValueStore("EntriesStore", { |
| 80 | comment: "kv-entries lifecycle", |
| 81 | }); |
| 82 | const entries = yield* KvEntries("Routes", { |
| 83 | store: store.keyValueStoreArn, |
| 84 | namespace: "routes", |
| 85 | entries: updatedEntries, |
| 86 | purge: true, |
| 87 | }); |
| 88 | return { store, entries }; |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(updated.store.keyValueStoreArn).toBe(deployed.store.keyValueStoreArn); |
| 93 | expect(updated.entries.entries).toEqual(updatedEntries); |
| 94 | |
| 95 | yield* assertEntries(updated.store.keyValueStoreArn, "routes", { |
| 96 | "/": "/index.html", |
| 97 | "/about": "/about-v2.html", |
| 98 | "/contact": "/contact.html", |
| 99 | }); |
| 100 | |
| 101 | yield* stack.destroy(); |
| 102 | yield* assertKeyValueStoreDeleted(deployed.store.keyValueStoreName); |
| 103 | }), |
| 104 | { timeout: 120_000 }, |
| 105 | ); |
| 106 | |
| 107 | test.provider( |
| 108 | "delete without purge leaves keys it does not own", |
| 109 | (stack) => |
| 110 | Effect.gen(function* () { |
| 111 | yield* stack.destroy(); |
| 112 | |
| 113 | // A Router's layout: the `routes` table and its inline route |
| 114 | // metadata share the router namespace. |
| 115 | const router = (withMetadata: boolean) => |
| 116 | Effect.gen(function* () { |
| 117 | const store = yield* KeyValueStore("SharedStore", { |
| 118 | comment: "kv-entries shared namespace", |
| 119 | }); |
| 120 | yield* KvRoutesUpdate("Routes", { |
| 121 | store: store.keyValueStoreArn, |
| 122 | namespace: "router", |
| 123 | key: "routes", |
| 124 | entry: "url,router:29e7,,/api", |
| 125 | }); |
| 126 | if (withMetadata) { |
| 127 | yield* KvEntries("Metadata", { |
| 128 | store: store.keyValueStoreArn, |
| 129 | namespace: "router", |
| 130 | entries: { "29e7:metadata": "{}" }, |
| 131 | }); |
| 132 | } |
| 133 | return { store }; |
| 134 | }); |
| 135 | |
| 136 | const deployed = yield* stack.deploy(router(true)); |
| 137 | const store = deployed.store.keyValueStoreArn; |
| 138 | yield* assertEntries(store, "router", { |
| 139 | routes: JSON.stringify(["url,router:29e7,,/api"]), |
| 140 | "29e7:metadata": "{}", |
| 141 | }); |
| 142 | |
| 143 | // Removing the last inline route deletes the KvEntries; the |
| 144 | // routes table must survive. |
| 145 | yield* stack.deploy(router(false)); |
| 146 | yield* assertEntries(store, "router", { |
| 147 | routes: JSON.stringify(["url,router:29e7,,/api"]), |
| 148 | }); |
| 149 | |
| 150 | yield* stack.destroy(); |
| 151 | yield* assertKeyValueStoreDeleted(deployed.store.keyValueStoreName); |
| 152 | }), |
| 153 | { timeout: 120_000 }, |
| 154 | ); |
| 155 | }, |
| 156 | ); |
| 157 | |
| 158 | const listNamespacedEntries = (store: string, namespace: string) => |
| 159 | withKvsRegion( |
| 160 | Effect.gen(function* () { |
| 161 | const prefix = `${namespace}:`; |
| 162 | const out: Record<string, string> = {}; |
| 163 | let nextToken: string | undefined; |
| 164 | do { |
| 165 | const resp = yield* kvs.listKeys({ KvsARN: store, NextToken: nextToken }); |
| 166 | for (const item of resp.Items ?? []) { |
| 167 | if (!item.Key.startsWith(prefix)) continue; |
| 168 | out[item.Key.slice(prefix.length)] = extractValue(item.Value); |
| 169 | } |
| 170 | nextToken = resp.NextToken; |
| 171 | } while (nextToken); |
| 172 | return out; |
| 173 | }), |
| 174 | ); |
| 175 | |
| 176 | const getNamespacedEntry = (store: string, namespace: string, key: string) => |
| 177 | withKvsRegion(kvs.getKey({ KvsARN: store, Key: `${namespace}:${key}` })).pipe( |
| 178 | Effect.map((resp) => extractValue(resp.Value)), |
| 179 | ); |
| 180 | |
| 181 | const entriesMatch = (got: Record<string, string>, expected: Record<string, string>) => { |
| 182 | const gotKeys = Object.keys(got).sort(); |
| 183 | const expectedKeys = Object.keys(expected).sort(); |
| 184 | return ( |
| 185 | gotKeys.length === expectedKeys.length && |
| 186 | expectedKeys.every((key) => got[key] === expected[key]) |
| 187 | ); |
| 188 | }; |
| 189 | |
| 190 | // #2688: KvEntries and KvRoutesUpdate returned their persisted output from |
| 191 | // `read`, so edited, deleted or extra keys read as `unchanged`. |
| 192 | test.provider( |
| 193 | "drift detects and repairs out-of-band key value store changes", |
| 194 | (stack) => |
| 195 | Effect.gen(function* () { |
| 196 | yield* stack.destroy(); |
| 197 | |
| 198 | const { store } = yield* stack.deploy( |
| 199 | Effect.gen(function* () { |
| 200 | const store = yield* KeyValueStore("DriftStore", { comment: "kv drift" }); |
| 201 | yield* KvEntries("DriftEntries", { |
| 202 | store: store.keyValueStoreArn, |
| 203 | namespace: "routes", |
| 204 | entries: { "/": "/index.html", "/about": "/about.html" }, |
| 205 | purge: true, |
| 206 | }); |
| 207 | yield* KvRoutesUpdate("DriftRoute", { |
| 208 | store: store.keyValueStoreArn, |
| 209 | namespace: "app", |
| 210 | key: "routes", |
| 211 | entry: "site,app,*,/", |
| 212 | }); |
| 213 | return { store }; |
| 214 | }), |
| 215 | ); |
| 216 | const arn = store.keyValueStoreArn; |
| 217 | const allUnchanged = (actions: Record<string, string | undefined>) => |
| 218 | Object.values(actions).every((action) => action === "unchanged"); |
| 219 | expect(allUnchanged(yield* driftActions(stack))).toBe(true); |
| 220 | |
| 221 | const write = (puts: kvs.PutKeyRequestListItem[], deletes: kvs.DeleteKeyRequestListItem[]) => |
| 222 | withKvsRegion( |
| 223 | Effect.gen(function* () { |
| 224 | const { ETag } = yield* kvs.describeKeyValueStore({ KvsARN: arn }); |
| 225 | yield* kvs.updateKeys({ KvsARN: arn, IfMatch: ETag, Puts: puts, Deletes: deletes }); |
| 226 | }), |
| 227 | ); |
| 228 | yield* write( |
| 229 | [ |
| 230 | { Key: "routes:/", Value: "/out-of-band.html" }, |
| 231 | { Key: "routes:/stray", Value: "/stray.html" }, |
| 232 | { Key: "app:routes", Value: "[]" }, |
| 233 | ], |
| 234 | [{ Key: "routes:/about" }], |
| 235 | ); |
| 236 | |
| 237 | const drifted = yield* waitForDrift(stack, "DriftEntries", "drifted"); |
| 238 | expect(drifted?.attr?.entries).toEqual({ "/": "/out-of-band.html", "/stray": "/stray.html" }); |
| 239 | expect((yield* waitForDrift(stack, "DriftRoute", "missing"))?.action).toBe("missing"); |
| 240 | |
| 241 | yield* repairDrift(stack); |
| 242 | expect(allUnchanged(yield* driftActions(stack))).toBe(true); |
| 243 | yield* assertEntries(arn, "routes", { "/": "/index.html", "/about": "/about.html" }); |
| 244 | |
| 245 | yield* stack.destroy(); |
| 246 | }), |
| 247 | { tags: ["provider:aws", "provider:aws:cloudfront", "live"], timeout: 240_000 }, |
| 248 | ); |
| 249 | |
| 250 | const assertEntries = (store: string, namespace: string, expected: Record<string, string>) => |
| 251 | Effect.gen(function* () { |
| 252 | const listed = yield* listNamespacedEntries(store, namespace).pipe( |
| 253 | Effect.repeat({ |
| 254 | schedule: Schedule.spaced("200 millis"), |
| 255 | until: (got) => entriesMatch(got, expected), |
| 256 | times: 15, |
| 257 | }), |
| 258 | ); |
| 259 | expect(listed).toEqual(expected); |
| 260 | |
| 261 | for (const [key, value] of Object.entries(expected)) { |
| 262 | expect(yield* getNamespacedEntry(store, namespace, key)).toBe(value); |
| 263 | } |
| 264 | }); |
| 265 | |
| 266 | const assertKeyValueStoreDeleted = (name: string) => |
| 267 | cloudfront.describeKeyValueStore({ Name: name }).pipe( |
| 268 | Effect.flatMap(() => Effect.fail(new Error("KeyValueStoreStillExists"))), |
| 269 | Effect.catchTag("EntityNotFound", () => Effect.void), |
| 270 | Effect.retry({ |
| 271 | while: (error) => error instanceof Error && error.message === "KeyValueStoreStillExists", |
| 272 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]), |
| 273 | }), |
| 274 | ); |