@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, 04:45 UTC

main
ts 134 lines 4.85 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { DataSource } from "@alchemy.run/aws/QuickSight";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as quicksight from "@distilled.cloud/aws/quicksight";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // The testing account has no QuickSight subscription, so the full lifecycle
14 // is gated behind AWS_TEST_QUICKSIGHT=1 (run on an entitled account). The
15 // ungated probe below proves both the distilled patch and the gating.
16 const SUBSCRIBED = !!process.env.AWS_TEST_QUICKSIGHT;
17
18 /**
19 * Ungated probe: without an account subscription, QuickSight rejects
20 * `CreateDataSource` with `ResourceNotFoundException` whose message is
21 * "Directory information for account ... is not found." The distilled patch
22 * specializes that into the typed `QuickSightSubscriptionRequired` tag. This
23 * test asserts the tag is what surfaces (or, on an entitled account, that the
24 * probe data source was created and is cleaned up).
25 */
26 test.provider(
27 "createDataSource surfaces typed QuickSightSubscriptionRequired without a subscription",
28 () =>
29 Effect.gen(function* () {
30 const { accountId } = yield* AWSEnvironment.current;
31 const dataSourceId = "alchemy-quicksight-subscription-probe";
32
33 const outcome = yield* quicksight
34 .createDataSource({
35 AwsAccountId: accountId,
36 DataSourceId: dataSourceId,
37 Name: "AlchemyQuickSightSubscriptionProbe",
38 Type: "ATHENA",
39 DataSourceParameters: {
40 AthenaParameters: { WorkGroup: "primary" },
41 },
42 })
43 .pipe(
44 Effect.as("created" as const),
45 Effect.catchTag("QuickSightSubscriptionRequired", (e) => Effect.succeed(e._tag)),
46 // Tolerate a leftover probe from a prior entitled run.
47 Effect.catchTag("ResourceExistsException", () => Effect.succeed("created" as const)),
48 );
49
50 // Clean up if the account turned out to be subscribed.
51 if (outcome === "created") {
52 yield* quicksight
53 .deleteDataSource({
54 AwsAccountId: accountId,
55 DataSourceId: dataSourceId,
56 })
57 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
58 }
59
60 expect(["QuickSightSubscriptionRequired", "created"]).toContain(outcome);
61 }),
62 {
63 tags: ["provider:aws", "provider:aws:quicksight", "live"],
64 timeout: 60_000,
65 },
66 );
67
68 class DataSourceStillExists extends Data.TaggedError("DataSourceStillExists")<{
69 readonly dataSourceId: string;
70 }> {}
71
72 const findDataSource = (accountId: string, dataSourceId: string) =>
73 quicksight.describeDataSource({ AwsAccountId: accountId, DataSourceId: dataSourceId }).pipe(
74 Effect.map((r) => r.DataSource),
75 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
76 );
77
78 // Gated lifecycle: requires an entitled QuickSight account.
79 test.provider.skipIf(!SUBSCRIBED)(
80 "create Athena data source, verify out-of-band, delete",
81 (stack) =>
82 Effect.gen(function* () {
83 const { accountId } = yield* AWSEnvironment.current;
84 yield* stack.destroy();
85
86 const { source } = yield* stack.deploy(
87 Effect.gen(function* () {
88 const source = yield* DataSource("AthenaSource", {
89 name: "Alchemy Athena Source",
90 type: "ATHENA",
91 dataSourceParameters: {
92 AthenaParameters: { WorkGroup: "primary" },
93 },
94 });
95 return { source };
96 }),
97 );
98
99 expect(source.dataSourceId).toBeDefined();
100 expect(source.arn).toContain(":datasource/");
101
102 const observed = yield* findDataSource(accountId, source.dataSourceId);
103 expect(observed?.Arn).toBe(source.arn);
104 expect(observed?.Type).toBe("ATHENA");
105
106 // internal ownership tags applied
107 const tags = yield* quicksight
108 .listTagsForResource({ ResourceArn: source.arn })
109 .pipe(Effect.map((r) => r.Tags ?? []));
110 expect(tags.find((t) => t.Key === "alchemy::id")?.Value).toBe("AthenaSource");
111
112 yield* stack.destroy();
113
114 yield* findDataSource(accountId, source.dataSourceId).pipe(
115 Effect.flatMap((ds) =>
116 ds === undefined || ds.Status === "DELETED"
117 ? Effect.void
118 : Effect.fail(
119 new DataSourceStillExists({
120 dataSourceId: source.dataSourceId,
121 }),
122 ),
123 ),
124 Effect.retry({
125 while: (e) => e._tag === "DataSourceStillExists",
126 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
127 }),
128 );
129 }),
130 {
131 tags: ["provider:aws", "provider:aws:quicksight", "live"],
132 timeout: 180_000,
133 },
134 );