@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 5.88 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { deleteRestApiAndWait } from "@alchemy.run/aws/ApiGateway/common";
3 import { expect } from "@alchemy.run/test";
4 import * as ag from "@distilled.cloud/aws/api-gateway";
5 import * as Provider from "alchemy/Provider";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import { assertRestApiDeleted } from "./assertions.ts";
9 import * as Test from "./Test.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // `deleteRestApi` is governed by a hard account-wide throttle (~1 request per
14 // 30s). During a fleet-wide test wave the teardown of this suite can race
15 // other suites' REST API deletes; if the vitest process is killed before the
16 // provider's throttle retry wins, the REST API (and its ~21 auto-created
17 // GatewayResponses) is stranded. The physical name is deterministic
18 // (`{stack}-{logicalId}-{stage}-{suffix}`), so reap out-of-band by the
19 // `-{logicalId}-test-` marker: run as a pre-clean before deploy (covers
20 // strays from a previous killed run even when the state store was wiped) and
21 // as an `Effect.ensuring` finalizer (covers mid-test failures).
22 const reapRestApis = (logicalId: string) =>
23 ag.getRestApis.pages({}).pipe(
24 Stream.runCollect,
25 Effect.map((chunk) =>
26 Array.from(chunk).flatMap((page) =>
27 (page.items ?? []).filter(
28 (api): api is ag.RestApi & { id: string } =>
29 api.id != null && (api.name?.includes(`-${logicalId}-test-`) ?? false),
30 ),
31 ),
32 ),
33 Effect.flatMap(Effect.forEach((api) => deleteRestApiAndWait(api.id))),
34 Effect.asVoid,
35 // Finalizer contract: the error channel must be `never`. Any unexpected
36 // error here is a genuinely stuck delete and should surface as a defect.
37 Effect.orDie,
38 );
39
40 test.provider.skipIf(!!process.env.FAST)(
41 "create and delete deployment",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45 yield* reapRestApis("AgDepApi");
46
47 const { api, deployment } = yield* stack.deploy(
48 Effect.gen(function* () {
49 const api = yield* AWS.ApiGateway.RestApi("AgDepApi", {
50 endpointConfiguration: { types: ["REGIONAL"] },
51 });
52 yield* AWS.ApiGateway.Method("AgDepMock", {
53 restApi: api,
54 httpMethod: "GET",
55 authorizationType: "NONE",
56 integration: { type: "MOCK" },
57 });
58 const deployment = yield* AWS.ApiGateway.Deployment("AgDep", {
59 restApi: api,
60 description: "alchemy-test-deployment",
61 });
62 return { api, deployment };
63 }),
64 );
65
66 expect(deployment.deploymentId).toBeDefined();
67
68 yield* stack.destroy();
69 yield* assertRestApiDeleted(api.restApiId);
70 }).pipe(Effect.ensuring(reapRestApis("AgDepApi"))),
71 { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
72 );
73
74 test.provider.skipIf(!!process.env.FAST)(
75 "deployment trigger change creates new deployment",
76 (stack) =>
77 Effect.gen(function* () {
78 yield* stack.destroy();
79 yield* reapRestApis("AgTrigApi");
80
81 const { api, d1 } = yield* stack.deploy(
82 Effect.gen(function* () {
83 const api = yield* AWS.ApiGateway.RestApi("AgTrigApi", {
84 endpointConfiguration: { types: ["REGIONAL"] },
85 });
86 yield* AWS.ApiGateway.Method("AgTrigMock", {
87 restApi: api,
88 httpMethod: "GET",
89 authorizationType: "NONE",
90 integration: { type: "MOCK" },
91 });
92 const deployment = yield* AWS.ApiGateway.Deployment("AgTrigDep", {
93 restApi: api,
94 description: "v1",
95 triggers: { t: "a" },
96 });
97 return { api, d1: deployment };
98 }),
99 );
100
101 const { d2 } = yield* stack.deploy(
102 Effect.gen(function* () {
103 const api = yield* AWS.ApiGateway.RestApi("AgTrigApi", {
104 endpointConfiguration: { types: ["REGIONAL"] },
105 });
106 yield* AWS.ApiGateway.Method("AgTrigMock", {
107 restApi: api,
108 httpMethod: "GET",
109 authorizationType: "NONE",
110 integration: { type: "MOCK" },
111 });
112 const deployment = yield* AWS.ApiGateway.Deployment("AgTrigDep", {
113 restApi: api,
114 description: "v1",
115 triggers: { t: "b" },
116 });
117 return { d2: deployment };
118 }),
119 );
120
121 expect(d2.deploymentId).not.toEqual(d1.deploymentId);
122
123 yield* stack.destroy();
124 yield* assertRestApiDeleted(api.restApiId);
125 }).pipe(Effect.ensuring(reapRestApis("AgTrigApi"))),
126 { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
127 );
128
129 test.provider.skipIf(!!process.env.FAST)(
130 "list enumerates the deployed deployment",
131 (stack) =>
132 Effect.gen(function* () {
133 yield* stack.destroy();
134 yield* reapRestApis("AgListApi");
135
136 const { api, deployment } = yield* stack.deploy(
137 Effect.gen(function* () {
138 const api = yield* AWS.ApiGateway.RestApi("AgListApi", {
139 endpointConfiguration: { types: ["REGIONAL"] },
140 });
141 yield* AWS.ApiGateway.Method("AgListMock", {
142 restApi: api,
143 httpMethod: "GET",
144 authorizationType: "NONE",
145 integration: { type: "MOCK" },
146 });
147 const deployment = yield* AWS.ApiGateway.Deployment("AgListDep", {
148 restApi: api,
149 description: "alchemy-test-list-deployment",
150 });
151 return { api, deployment };
152 }),
153 );
154
155 const provider = yield* Provider.findProvider(AWS.ApiGateway.DeploymentResource);
156 const all = yield* provider.list();
157
158 expect(all.some((d) => d.deploymentId === deployment.deploymentId)).toBe(true);
159
160 yield* stack.destroy();
161 yield* assertRestApiDeleted(api.restApiId);
162 }).pipe(Effect.ensuring(reapRestApis("AgListApi"))),
163 { tags: ["provider:aws", "provider:aws:apigateway", "live"] },
164 );