@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 209 lines 8.42 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { 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 { isResourceState, State, type ResourceState } from "alchemy/State";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const TEST_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
15 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvTkfqkMHU8HMmIRKJaMl
16 IoD691g60aS15QlaP/DVkpuoeEp8JA8YDs5vQFu6HSIYCTQ7WwFx9oRvN08i7yXB
17 EHt3x7uZVpdkp6JBbjR9BGNsAVri6DZ0TJQ11zWeN3keqhnUdFhQjPwT+u4r6oKk
18 kNvkl7eU2nFK+UIaPlD+rA+AlYT0m7gSVcd9KaLf/UzBrtSy1dbXYDT4dHChMUVy
19 4gDsQ6D4u6lRAHY9jcKxlgEIM+O8ODKyzlbergv2EwhANG4P27DBeDhA/off3upM
20 TTVTGKZeoABtqM0ZiYq0cDgf8KUn9NPxSdnJ4+cbigLjJBPS93VYWzWX0HXlZpQ3
21 HQIDAQAB
22 -----END PUBLIC KEY-----
23 `;
24
25 describe(
26 "AWS.CloudFront.PublicKey",
27 { tags: ["provider:aws", "provider:aws:cloudfront", "live"] },
28 () => {
29 test.provider(
30 "create, update comment, and delete a public key",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const created = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* PublicKey("SignedUrlKey", {
38 encodedKey: TEST_PUBLIC_KEY,
39 comment: "initial",
40 });
41 }),
42 );
43
44 const initial = yield* cloudfront.getPublicKey({ Id: created.publicKeyId });
45 expect(initial.PublicKey?.Id).toEqual(created.publicKeyId);
46 expect(initial.PublicKey?.PublicKeyConfig?.Comment).toEqual("initial");
47 // CloudFront normalizes the stored key (trailing newline stripped).
48 expect(initial.PublicKey?.PublicKeyConfig?.EncodedKey?.trim()).toEqual(
49 TEST_PUBLIC_KEY.trim(),
50 );
51
52 // Update with the SAME key wrapped in Redacted — the provider's
53 // `extractValue` must unwrap it so diff sees no key change (update in
54 // place, not replacement) and the wire value round-trips unredacted.
55 const updated = yield* stack.deploy(
56 Effect.gen(function* () {
57 return yield* PublicKey("SignedUrlKey", {
58 encodedKey: Redacted.make(TEST_PUBLIC_KEY),
59 comment: "updated",
60 });
61 }),
62 );
63
64 expect(updated.publicKeyId).toEqual(created.publicKeyId);
65 expect(updated.callerReference).toEqual(created.callerReference);
66
67 // Control-plane reads are eventually consistent — poll until the
68 // update is visible, then assert.
69 const after = yield* cloudfront.getPublicKey({ Id: updated.publicKeyId }).pipe(
70 Effect.repeat({
71 schedule: Schedule.fixed("2 seconds"),
72 until: (r) => r.PublicKey?.PublicKeyConfig?.Comment === "updated",
73 times: 15,
74 }),
75 );
76 expect(after.PublicKey?.PublicKeyConfig?.Comment).toEqual("updated");
77 // The Redacted-wrapped key reached the wire as the plain PEM.
78 expect(after.PublicKey?.PublicKeyConfig?.EncodedKey?.trim()).toEqual(
79 TEST_PUBLIC_KEY.trim(),
80 );
81
82 yield* stack.destroy();
83 yield* assertPublicKeyDeleted(updated.publicKeyId);
84 }),
85 { timeout: 300_000 },
86 );
87
88 test.provider(
89 "list enumerates the deployed public key",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* stack.destroy();
93
94 const deployed = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* PublicKey("ListResource", {
97 encodedKey: TEST_PUBLIC_KEY,
98 comment: "list test",
99 });
100 }),
101 );
102
103 const provider = yield* Provider.findProvider(PublicKey);
104 const all = yield* provider.list();
105
106 expect(all.some((x) => x.publicKeyId === deployed.publicKeyId)).toBe(true);
107
108 yield* stack.destroy();
109 yield* assertPublicKeyDeleted(deployed.publicKeyId);
110 }),
111 { timeout: 300_000 },
112 );
113
114 test.provider(
115 "recovers a wedged creating-state row that lost its Output-valued encodedKey (#736)",
116 (stack) =>
117 Effect.gen(function* () {
118 yield* stack.destroy();
119
120 const deployKey = () =>
121 stack.deploy(
122 Effect.gen(function* () {
123 return yield* PublicKey("WedgedKey", {
124 encodedKey: TEST_PUBLIC_KEY,
125 comment: "wedged recovery test",
126 });
127 }),
128 );
129
130 const created = yield* deployKey();
131
132 // Rewrite the persisted row into the wedged shape an interrupted
133 // deploy leaves behind: `creating`, no attributes, and the
134 // Output-valued `encodedKey` lost in the round-trip (#736).
135 const state = yield* yield* State;
136 const stage = stack.stage;
137 const fqns = yield* state.list({ stack: stack.name, stage });
138 const rows = yield* Effect.forEach(fqns, (fqn) =>
139 state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))),
140 );
141 const wedged = rows.find(
142 (r): r is { fqn: string; row: ResourceState } =>
143 isResourceState(r.row) && r.row.resourceType === "AWS.CloudFront.PublicKey",
144 );
145 if (!wedged) {
146 return yield* Effect.die(
147 new Error("no AWS.CloudFront.PublicKey state row found after deploy"),
148 );
149 }
150 // Delete the public key out-of-band FIRST — while the state row is
151 // still intact — so the recovery `read` misses and the engine falls
152 // through to `diff` with the junk olds. Ordering matters for
153 // orphan-safety: CloudFront assigns the key Id (no user-controlled
154 // name), so if this test were killed after wedging the state row
155 // (attr: undefined) but before this delete, the ensuring
156 // stack.destroy could no longer identify the live key and it would
157 // orphan forever. Deleting before the state rewrite means every
158 // interruption point leaves either an intact row (destroy reclaims
159 // it) or an already-deleted key.
160 const current = yield* cloudfront.getPublicKey({ Id: created.publicKeyId });
161 yield* cloudfront
162 .deletePublicKey({ Id: created.publicKeyId, IfMatch: current.ETag })
163 .pipe(Effect.catchTag("NoSuchPublicKey", () => Effect.void));
164 yield* assertPublicKeyDeleted(created.publicKeyId);
165
166 // Now rewrite the persisted row into the wedged shape.
167 yield* state.set({
168 stack: stack.name,
169 stage,
170 fqn: wedged.fqn,
171 value: {
172 ...wedged.row,
173 status: "creating",
174 attr: undefined,
175 props: { ...wedged.row.props, encodedKey: undefined },
176 },
177 });
178
179 // Before the fix this crashed in `diff`: `extractValue(undefined)`
180 // called `Redacted.value(undefined)` on the lost `olds.encodedKey`.
181 // After the fix, diff skips the comparison and the engine recreates
182 // the key cleanly.
183 const recovered = yield* deployKey();
184 expect(recovered.publicKeyId).not.toEqual(created.publicKeyId);
185
186 const after = yield* cloudfront.getPublicKey({ Id: recovered.publicKeyId });
187 expect(after.PublicKey?.Id).toEqual(recovered.publicKeyId);
188 // CloudFront normalizes the stored key (trailing newline stripped).
189 expect(after.PublicKey?.PublicKeyConfig?.EncodedKey?.trim()).toEqual(
190 TEST_PUBLIC_KEY.trim(),
191 );
192
193 yield* stack.destroy();
194 yield* assertPublicKeyDeleted(recovered.publicKeyId);
195 }),
196 { timeout: 240_000 },
197 );
198 },
199 );
200
201 const assertPublicKeyDeleted = (id: string) =>
202 cloudfront.getPublicKey({ Id: id }).pipe(
203 Effect.flatMap(() => Effect.fail(new Error("PublicKeyStillExists"))),
204 Effect.catchTag("NoSuchPublicKey", () => Effect.void),
205 Effect.retry({
206 while: (error) => error instanceof Error && error.message === "PublicKeyStillExists",
207 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]),
208 }),
209 );