@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 205 lines 7.98 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Export } from "@alchemy.run/aws/BCMDataExports/Export";
3 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
4 import { Bucket } from "@alchemy.run/aws/S3/Bucket";
5 import { expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as bcm from "@distilled.cloud/aws/bcm-data-exports";
8 import * as Provider from "alchemy/Provider";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Ungated typed-error probe: prove the distilled error union carries the
15 // not-found tag this provider's read/delete paths depend on. Runs in every
16 // CI pass at near-zero cost.
17 test.provider(
18 "getExport on a nonexistent export ARN fails with ResourceNotFoundException",
19 () =>
20 Effect.gen(function* () {
21 const { accountId } = yield* AWSEnvironment.current;
22 const error = yield* Effect.flip(
23 bcm.getExport({
24 ExportArn: `arn:aws:bcm-data-exports:us-east-1:${accountId}:export/alchemy-nonexistent-probe/00000000-0000-0000-0000-000000000000`,
25 }),
26 );
27 expect(error._tag).toBe("ResourceNotFoundException");
28 }),
29 { tags: ["provider:aws", "provider:aws:bcmdataexports", "live"] },
30 );
31
32 const bucketName = "alchemy-test-bcm-export-dest";
33 const exportName = "alchemy-test-bcm-export";
34 const renamedExportName = "alchemy-test-bcm-export-renamed";
35
36 const queryStatement =
37 "SELECT identity_line_item_id, identity_time_interval, line_item_unblended_cost FROM COST_AND_USAGE_REPORT";
38 const tableConfigurations = {
39 COST_AND_USAGE_REPORT: {
40 TIME_GRANULARITY: "HOURLY",
41 INCLUDE_RESOURCES: "FALSE",
42 INCLUDE_MANUAL_DISCOUNT_COMPATIBILITY: "FALSE",
43 INCLUDE_SPLIT_COST_ALLOCATION_DATA: "FALSE",
44 },
45 };
46
47 // The destination bucket must let the Data Exports service principals write
48 // to it and read its policy.
49 const destinationBucket = (accountId: string) =>
50 Bucket("ExportDest", {
51 bucketName,
52 forceDestroy: true,
53 policy: [
54 {
55 Sid: "EnableAWSDataExportsToWriteToS3AndCheckPolicy",
56 Effect: "Allow",
57 Principal: { Service: ["billingreports.amazonaws.com", "bcm-data-exports.amazonaws.com"] },
58 Action: ["s3:PutObject", "s3:GetBucketPolicy"],
59 Resource: [`arn:aws:s3:::${bucketName}`, `arn:aws:s3:::${bucketName}/*`],
60 Condition: {
61 StringLike: {
62 "aws:SourceAccount": accountId,
63 "aws:SourceArn": [
64 `arn:aws:cur:us-east-1:${accountId}:definition/*`,
65 `arn:aws:bcm-data-exports:us-east-1:${accountId}:export/*`,
66 ],
67 },
68 },
69 },
70 ],
71 });
72
73 const getExportByArn = (arn: string) =>
74 bcm.getExport({ ExportArn: arn }).pipe(
75 Effect.map((r) => r.Export),
76 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
77 );
78
79 // Typed wait-until-gone: bounded, declarative.
80 const assertExportGone = (arn: string) =>
81 Effect.gen(function* () {
82 const live = yield* getExportByArn(arn);
83 if (live !== undefined) {
84 return yield* Effect.fail(new Error(`export '${arn}' still exists after destroy`));
85 }
86 }).pipe(
87 Effect.retry({ schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]) }),
88 );
89
90 test.provider(
91 "lifecycle: create CUR 2.0 export, update in place, replace on rename, destroy",
92 (stack) =>
93 Effect.gen(function* () {
94 yield* stack.destroy();
95
96 const { accountId, region } = yield* AWSEnvironment.current;
97
98 // CREATE — bucket (with Data Exports write policy) + export.
99 const deployed = yield* stack.deploy(
100 Effect.gen(function* () {
101 const bucket = yield* destinationBucket(accountId);
102 const exp = yield* Export("CurExport", {
103 exportName,
104 description: "alchemy bcm-data-exports test",
105 dataQuery: { queryStatement, tableConfigurations },
106 s3Destination: { s3Bucket: bucket.bucketName, s3Prefix: "cur2", s3Region: region },
107 tags: { fixture: "bcm-data-exports" },
108 });
109 return { exportArn: exp.exportArn, exportName: exp.exportName };
110 }),
111 );
112
113 expect(deployed.exportName).toBe(exportName);
114 expect(deployed.exportArn).toContain(":bcm-data-exports:");
115 expect(deployed.exportArn).toContain(`:export/`);
116
117 // Out-of-band verification via distilled.
118 const created = yield* getExportByArn(deployed.exportArn);
119 expect(created?.Name).toBe(exportName);
120 expect(created?.Description).toBe("alchemy bcm-data-exports test");
121 expect(created?.DataQuery.QueryStatement).toBe(queryStatement);
122 expect(created?.DestinationConfigurations.S3Destination.S3Bucket).toBe(bucketName);
123 expect(created?.DestinationConfigurations.S3Destination.S3Prefix).toBe("cur2");
124 expect(created?.RefreshCadence.Frequency).toBe("SYNCHRONOUS");
125
126 // Tags: internal branding + user tag attached at create.
127 const tags = yield* bcm
128 .listTagsForResource({ ResourceArn: deployed.exportArn })
129 .pipe(
130 Effect.map((r) =>
131 Object.fromEntries((r.ResourceTags ?? []).map((t) => [t.Key, t.Value])),
132 ),
133 );
134 expect(tags.fixture).toBe("bcm-data-exports");
135 expect(tags["alchemy::id"]).toBe("CurExport");
136
137 // Canonical list() coverage.
138 const provider = yield* Provider.findProvider(Export);
139 const all = yield* provider.list();
140 expect(all.some((e) => e.exportName === exportName)).toBe(true);
141
142 // UPDATE in place — new description + prefix, tag change. Same ARN.
143 const updated = yield* stack.deploy(
144 Effect.gen(function* () {
145 const bucket = yield* destinationBucket(accountId);
146 const exp = yield* Export("CurExport", {
147 exportName,
148 description: "alchemy bcm-data-exports test (updated)",
149 dataQuery: { queryStatement, tableConfigurations },
150 s3Destination: {
151 s3Bucket: bucket.bucketName,
152 s3Prefix: "cur2-updated",
153 s3Region: region,
154 },
155 tags: { fixture: "bcm-data-exports", phase: "two" },
156 });
157 return { exportArn: exp.exportArn };
158 }),
159 );
160 expect(updated.exportArn).toBe(deployed.exportArn);
161
162 const afterUpdate = yield* getExportByArn(deployed.exportArn);
163 expect(afterUpdate?.Description).toBe("alchemy bcm-data-exports test (updated)");
164 expect(afterUpdate?.DestinationConfigurations.S3Destination.S3Prefix).toBe("cur2-updated");
165
166 const tagsAfterUpdate = yield* bcm
167 .listTagsForResource({ ResourceArn: deployed.exportArn })
168 .pipe(
169 Effect.map((r) =>
170 Object.fromEntries((r.ResourceTags ?? []).map((t) => [t.Key, t.Value])),
171 ),
172 );
173 expect(tagsAfterUpdate.phase).toBe("two");
174
175 // REPLACE — renaming the export replaces it (new ARN, old one gone).
176 const renamed = yield* stack.deploy(
177 Effect.gen(function* () {
178 const bucket = yield* destinationBucket(accountId);
179 const exp = yield* Export("CurExport", {
180 exportName: renamedExportName,
181 description: "alchemy bcm-data-exports test (updated)",
182 dataQuery: { queryStatement, tableConfigurations },
183 s3Destination: {
184 s3Bucket: bucket.bucketName,
185 s3Prefix: "cur2-updated",
186 s3Region: region,
187 },
188 tags: { fixture: "bcm-data-exports", phase: "two" },
189 });
190 return { exportArn: exp.exportArn, exportName: exp.exportName };
191 }),
192 );
193 expect(renamed.exportName).toBe(renamedExportName);
194 expect(renamed.exportArn).not.toBe(deployed.exportArn);
195 yield* assertExportGone(deployed.exportArn);
196
197 // DESTROY — export and bucket removed; verify out-of-band.
198 yield* stack.destroy();
199 yield* assertExportGone(renamed.exportArn);
200 }),
201 {
202 tags: ["provider:aws", "provider:aws:bcmdataexports", "provider:aws:s3", "live"],
203 timeout: 300_000,
204 },
205 );