@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 164 lines 6.48 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { KeyGroup, PublicKey } 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 const PRIMARY_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
13 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvTkfqkMHU8HMmIRKJaMl
14 IoD691g60aS15QlaP/DVkpuoeEp8JA8YDs5vQFu6HSIYCTQ7WwFx9oRvN08i7yXB
15 EHt3x7uZVpdkp6JBbjR9BGNsAVri6DZ0TJQ11zWeN3keqhnUdFhQjPwT+u4r6oKk
16 kNvkl7eU2nFK+UIaPlD+rA+AlYT0m7gSVcd9KaLf/UzBrtSy1dbXYDT4dHChMUVy
17 4gDsQ6D4u6lRAHY9jcKxlgEIM+O8ODKyzlbergv2EwhANG4P27DBeDhA/off3upM
18 TTVTGKZeoABtqM0ZiYq0cDgf8KUn9NPxSdnJ4+cbigLjJBPS93VYWzWX0HXlZpQ3
19 HQIDAQAB
20 -----END PUBLIC KEY-----
21 `;
22
23 const SECONDARY_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
24 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu6QT6PtJ4O4Xcy6C8BkI
25 NHDRF8TnPbWZlVFYXr4R9miLMlORiH34sUtyYAwYlvHyYlrL76Z4A8HaLTMDfCf5
26 eswQr0r7qVOstRJrEW8ycTXJw/8Mj+1KKSrs8QopVZBMEnOfiUsentnWhQgKSpbI
27 mA4sLTg45jBoaZi9c8M40psgAp/1vPn5zeloAjoku5ax18FM32yRi9C21J84BWCd
28 vp6E7NtzJh9XlH22xOPGmbCh1r3uAtiujpXj1UNw/QEMooiEsJAeoROpvHZkh6/m
29 VZUU1njrXaTMl4OUd+I+Y96eWWo539vU0dD5XnUUNuYybwI6FeVmrouHR9wG4RJL
30 JQIDAQAB
31 -----END PUBLIC KEY-----
32 `;
33
34 describe(
35 "AWS.CloudFront.KeyGroup",
36 { tags: ["provider:aws", "provider:aws:cloudfront", "live"] },
37 () => {
38 test.provider(
39 "create, update items, and delete a key group",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const created = yield* stack.deploy(
45 Effect.gen(function* () {
46 const primary = yield* PublicKey("PrimarySigningKey", {
47 encodedKey: PRIMARY_PUBLIC_KEY,
48 comment: "primary",
49 });
50 const secondary = yield* PublicKey("SecondarySigningKey", {
51 encodedKey: SECONDARY_PUBLIC_KEY,
52 comment: "secondary",
53 });
54 const group = yield* KeyGroup("SignedUrlKeys", {
55 comment: "initial",
56 items: [primary.publicKeyId],
57 });
58 return { primary, secondary, group };
59 }),
60 );
61
62 const initial = yield* cloudfront.getKeyGroup({ Id: created.group.keyGroupId });
63 expect(initial.KeyGroup?.Id).toEqual(created.group.keyGroupId);
64 expect(initial.KeyGroup?.KeyGroupConfig?.Comment).toEqual("initial");
65 expect(initial.KeyGroup?.KeyGroupConfig?.Items).toEqual([created.primary.publicKeyId]);
66
67 const updated = yield* stack.deploy(
68 Effect.gen(function* () {
69 const primary = yield* PublicKey("PrimarySigningKey", {
70 encodedKey: PRIMARY_PUBLIC_KEY,
71 comment: "primary",
72 });
73 const secondary = yield* PublicKey("SecondarySigningKey", {
74 encodedKey: SECONDARY_PUBLIC_KEY,
75 comment: "secondary",
76 });
77 const group = yield* KeyGroup("SignedUrlKeys", {
78 comment: "updated",
79 items: [primary.publicKeyId, secondary.publicKeyId],
80 });
81 return { primary, secondary, group };
82 }),
83 );
84
85 expect(updated.group.keyGroupId).toEqual(created.group.keyGroupId);
86
87 // `getKeyGroup` right after `updateKeyGroup` can serve the
88 // pre-update config (control-plane reads are eventually
89 // consistent) — poll until the update is visible, then assert.
90 const after = yield* cloudfront.getKeyGroup({ Id: updated.group.keyGroupId }).pipe(
91 Effect.repeat({
92 schedule: Schedule.fixed("2 seconds"),
93 until: (response) => response.KeyGroup?.KeyGroupConfig?.Items?.length === 2,
94 times: 15,
95 }),
96 );
97 expect(after.KeyGroup?.KeyGroupConfig?.Comment).toEqual("updated");
98 // CloudFront does not preserve the order of key-group items —
99 // compare as sets.
100 expect([...(after.KeyGroup?.KeyGroupConfig?.Items ?? [])].sort()).toEqual(
101 [updated.primary.publicKeyId, updated.secondary.publicKeyId].sort(),
102 );
103
104 yield* stack.destroy();
105 yield* assertKeyGroupDeleted(updated.group.keyGroupId);
106 yield* assertPublicKeyDeleted(updated.primary.publicKeyId);
107 yield* assertPublicKeyDeleted(updated.secondary.publicKeyId);
108 }),
109 { timeout: 300_000 },
110 );
111
112 test.provider(
113 "list enumerates the deployed key group",
114 (stack) =>
115 Effect.gen(function* () {
116 yield* stack.destroy();
117
118 const deployed = yield* stack.deploy(
119 Effect.gen(function* () {
120 const primary = yield* PublicKey("PrimarySigningKey", {
121 encodedKey: PRIMARY_PUBLIC_KEY,
122 comment: "primary",
123 });
124 const group = yield* KeyGroup("ListKeyGroup", {
125 comment: "list",
126 items: [primary.publicKeyId],
127 });
128 return { primary, group };
129 }),
130 );
131
132 const provider = yield* Provider.findProvider(KeyGroup);
133 const all = yield* provider.list();
134
135 expect(all.some((g) => g.keyGroupId === deployed.group.keyGroupId)).toBe(true);
136
137 yield* stack.destroy();
138 yield* assertKeyGroupDeleted(deployed.group.keyGroupId);
139 yield* assertPublicKeyDeleted(deployed.primary.publicKeyId);
140 }),
141 { timeout: 300_000 },
142 );
143 },
144 );
145
146 const assertKeyGroupDeleted = (id: string) =>
147 cloudfront.getKeyGroup({ Id: id }).pipe(
148 Effect.flatMap(() => Effect.fail(new Error("KeyGroupStillExists"))),
149 Effect.catchTag("NoSuchResource", () => Effect.void),
150 Effect.retry({
151 while: (error) => error instanceof Error && error.message === "KeyGroupStillExists",
152 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]),
153 }),
154 );
155
156 const assertPublicKeyDeleted = (id: string) =>
157 cloudfront.getPublicKey({ Id: id }).pipe(
158 Effect.flatMap(() => Effect.fail(new Error("PublicKeyStillExists"))),
159 Effect.catchTag("NoSuchPublicKey", () => Effect.void),
160 Effect.retry({
161 while: (error) => error instanceof Error && error.message === "PublicKeyStillExists",
162 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]),
163 }),
164 );