@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 150 lines 5.6 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { KeyValueStore, 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
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 describe(
15 "AWS.CloudFront.KvRoutesUpdate",
16 { tags: ["provider:aws", "provider:aws:cloudfront", "live"] },
17 () => {
18 test.provider(
19 "destroy succeeds after the store is deleted out of band",
20 (stack) =>
21 Effect.gen(function* () {
22 yield* stack.destroy();
23 const store = yield* stack.deploy(
24 Effect.gen(function* () {
25 const store = yield* KeyValueStore("Store", {});
26 yield* KvRoutesUpdate("Route", {
27 store: store.keyValueStoreArn,
28 namespace: "app",
29 key: "routes",
30 entry: "site,app,*,/",
31 });
32 return store;
33 }),
34 );
35 const current = yield* cloudfront.describeKeyValueStore({
36 Name: store.keyValueStoreName,
37 });
38 yield* cloudfront.deleteKeyValueStore({
39 Name: store.keyValueStoreName,
40 IfMatch: current.ETag!,
41 });
42 yield* assertKeyValueStoreDeleted(store.keyValueStoreName);
43 yield* stack.destroy();
44 yield* stack.destroy();
45 }),
46 { timeout: 120_000 },
47 );
48
49 // KvRoutesUpdate is an update operation that manages a single route entry
50 // inside a JSON array stored at a KV store key. It is keyed entirely by
51 // {store, namespace, key, entry} and has no enumeration API, so list() is
52 // non-listable and returns [] cleanly.
53 test.provider("list returns [] (non-listable)", () =>
54 Effect.gen(function* () {
55 const provider = yield* Provider.findProvider(KvRoutesUpdate);
56 const all = yield* provider.list();
57 expect(all).toEqual([]);
58 }),
59 );
60
61 test.provider(
62 "create, update, and delete a routes document entry",
63 (stack) =>
64 Effect.gen(function* () {
65 yield* stack.destroy();
66
67 const initialEntry = "site,mysite,*,/";
68
69 const deployed = yield* stack.deploy(
70 Effect.gen(function* () {
71 const store = yield* KeyValueStore("RoutesStore", { comment: "kv-routes lifecycle" });
72 const route = yield* KvRoutesUpdate("HomeRoute", {
73 store: store.keyValueStoreArn,
74 namespace: "app",
75 key: "routes",
76 entry: initialEntry,
77 });
78 return { store, route };
79 }),
80 );
81
82 expect(deployed.route.store).toBe(deployed.store.keyValueStoreArn);
83 expect(deployed.route.namespace).toBe("app");
84 expect(deployed.route.key).toBe("routes");
85 expect(deployed.route.entry).toBe(initialEntry);
86
87 const described = yield* withKvsRegion(
88 kvs.describeKeyValueStore({ KvsARN: deployed.store.keyValueStoreArn }),
89 );
90 expect(described.KvsARN).toBe(deployed.store.keyValueStoreArn);
91
92 yield* assertRoutes(deployed.store.keyValueStoreArn, "app:routes", [initialEntry]);
93
94 const updatedEntry = "site,mysite,*,/app";
95
96 const updated = yield* stack.deploy(
97 Effect.gen(function* () {
98 const store = yield* KeyValueStore("RoutesStore", { comment: "kv-routes lifecycle" });
99 const route = yield* KvRoutesUpdate("HomeRoute", {
100 store: store.keyValueStoreArn,
101 namespace: "app",
102 key: "routes",
103 entry: updatedEntry,
104 });
105 return { store, route };
106 }),
107 );
108
109 expect(updated.store.keyValueStoreArn).toBe(deployed.store.keyValueStoreArn);
110 expect(updated.route.entry).toBe(updatedEntry);
111
112 yield* assertRoutes(updated.store.keyValueStoreArn, "app:routes", [updatedEntry]);
113
114 yield* stack.destroy();
115 yield* assertKeyValueStoreDeleted(deployed.store.keyValueStoreName);
116 }),
117 { timeout: 120_000 },
118 );
119 },
120 );
121
122 const getRoutesDocument = (store: string, fullKey: string) =>
123 withKvsRegion(kvs.getKey({ KvsARN: store, Key: fullKey })).pipe(
124 Effect.map((resp) => JSON.parse(extractValue(resp.Value)) as string[]),
125 );
126
127 const routesMatch = (got: string[], expected: string[]) =>
128 got.length === expected.length && expected.every((entry) => got.includes(entry));
129
130 const assertRoutes = (store: string, fullKey: string, expected: string[]) =>
131 Effect.gen(function* () {
132 const routes = yield* getRoutesDocument(store, fullKey).pipe(
133 Effect.repeat({
134 schedule: Schedule.spaced("200 millis"),
135 until: (got) => routesMatch(got, expected),
136 times: 15,
137 }),
138 );
139 expect(routes).toEqual(expected);
140 });
141
142 const assertKeyValueStoreDeleted = (name: string) =>
143 cloudfront.describeKeyValueStore({ Name: name }).pipe(
144 Effect.flatMap(() => Effect.fail(new Error("KeyValueStoreStillExists"))),
145 Effect.catchTag("EntityNotFound", () => Effect.void),
146 Effect.retry({
147 while: (error) => error instanceof Error && error.message === "KeyValueStoreStillExists",
148 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]),
149 }),
150 );