@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 139 lines 5.29 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 Application,
4 AttributeGroup,
5 AttributeGroupAssociation,
6 ResourceAssociation,
7 } from "@alchemy.run/aws/AppRegistry";
8 import { Stack } from "@alchemy.run/aws/CloudFormation";
9 import { expect } from "@alchemy.run/test";
10 import * as Test from "@alchemy.run/test/Alchemy";
11 import * as appregistry from "@distilled.cloud/aws/service-catalog-appregistry";
12 import * as Data from "effect/Data";
13 import * as Effect from "effect/Effect";
14 import * as Schedule from "effect/Schedule";
15 import * as Stream from "effect/Stream";
16 import { makeAppRegistryTestLease } from "./TestLease.ts";
17
18 const { test, beforeAll, afterAll } = Test.make({ providers: AWS.providers() });
19 const serviceLease = makeAppRegistryTestLease();
20
21 beforeAll(serviceLease.acquire, { timeout: 3_600_000 });
22 afterAll(serviceLease.release);
23
24 class ApplicationStillExists extends Data.TaggedError("ApplicationStillExists")<{
25 specifier: string;
26 }> {}
27
28 const assertApplicationGone = (specifier: string) =>
29 appregistry.getApplication({ application: specifier }).pipe(
30 Effect.flatMap(() => Effect.fail(new ApplicationStillExists({ specifier }))),
31 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
32 Effect.retry({
33 while: (e) => e._tag === "ApplicationStillExists",
34 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
35 }),
36 );
37
38 const listAssociatedGroupIds = (application: string) =>
39 appregistry.listAssociatedAttributeGroups.pages({ application }).pipe(
40 Stream.runCollect,
41 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.attributeGroups ?? [])),
42 );
43
44 // A free, instantly-creatable no-op stack to associate with the application.
45 const NOOP_TEMPLATE = JSON.stringify({
46 Resources: {
47 Noop: { Type: "AWS::CloudFormation::WaitConditionHandle" },
48 },
49 });
50
51 // AppRegistry is in maintenance mode (see Application.test.ts) — lifecycle
52 // tests only run on accounts that retain access via AWS_TEST_APPREGISTRY=1.
53 test.provider.skipIf(!process.env.AWS_TEST_APPREGISTRY)(
54 "associates an attribute group and a CloudFormation stack with an application",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const app = yield* Application("AssocApp", {
62 description: "association lifecycle test",
63 });
64 const group = yield* AttributeGroup("AssocGroup", {
65 attributes: { owner: "alchemy-test" },
66 });
67 const cfn = yield* Stack("AssocStack", {
68 templateBody: NOOP_TEMPLATE,
69 });
70 const groupAssociation = yield* AttributeGroupAssociation("GroupAssoc", {
71 application: app.applicationId,
72 attributeGroup: group.attributeGroupId,
73 });
74 const resourceAssociation = yield* ResourceAssociation("StackAssoc", {
75 application: app.applicationId,
76 resourceType: "CFN_STACK",
77 resource: cfn.stackName,
78 // Skip the awsApplication tag so association does not round-trip
79 // a CloudFormation stack update.
80 options: ["SKIP_APPLICATION_TAG"],
81 });
82 return {
83 applicationId: app.applicationId,
84 attributeGroupId: group.attributeGroupId,
85 stackName: cfn.stackName,
86 associatedGroupId: groupAssociation.attributeGroupId,
87 associatedResourceArn: resourceAssociation.resourceArn,
88 };
89 }),
90 );
91
92 expect(created.associatedGroupId).toBe(created.attributeGroupId);
93 expect(created.associatedResourceArn).toContain(":cloudformation:");
94
95 // out-of-band verify via distilled
96 const groupIds = yield* listAssociatedGroupIds(created.applicationId);
97 expect(groupIds).toContain(created.attributeGroupId);
98 const associated = yield* appregistry.getAssociatedResource({
99 application: created.applicationId,
100 resourceType: "CFN_STACK",
101 resource: created.stackName,
102 });
103 expect(associated.resource?.name).toBe(created.stackName);
104
105 // idempotent re-deploy converges without error
106 yield* stack.deploy(
107 Effect.gen(function* () {
108 const app = yield* Application("AssocApp", {
109 description: "association lifecycle test",
110 });
111 const group = yield* AttributeGroup("AssocGroup", {
112 attributes: { owner: "alchemy-test" },
113 });
114 const cfn = yield* Stack("AssocStack", {
115 templateBody: NOOP_TEMPLATE,
116 });
117 yield* AttributeGroupAssociation("GroupAssoc", {
118 application: app.applicationId,
119 attributeGroup: group.attributeGroupId,
120 });
121 yield* ResourceAssociation("StackAssoc", {
122 application: app.applicationId,
123 resourceType: "CFN_STACK",
124 resource: cfn.stackName,
125 options: ["SKIP_APPLICATION_TAG"],
126 });
127 }),
128 );
129
130 // destroy disassociates before deleting the application (AppRegistry
131 // rejects deleting an application with live associations).
132 yield* stack.destroy();
133 yield* assertApplicationGone(created.applicationId);
134 }),
135 {
136 tags: ["provider:aws", "provider:aws:appregistry", "provider:aws:cloudformation", "live"],
137 timeout: 300_000,
138 },
139 );