The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { ReportDefinition } from "@alchemy.run/aws/CostAndUsageReport"; |
| 3 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 4 | import { Bucket } from "@alchemy.run/aws/S3"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as cur from "@distilled.cloud/aws/cost-and-usage-report-service"; |
| 8 | import { Region } from "@distilled.cloud/aws/Region"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // The CUR control plane is only hosted in us-east-1 — pin every out-of-band |
| 16 | // distilled call there, exactly like the provider does. |
| 17 | const pin = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 18 | effect.pipe(Effect.provideService(Region, Effect.succeed("us-east-1"))); |
| 19 | |
| 20 | const findReport = (name: string) => |
| 21 | pin(cur.describeReportDefinitions.pages({}).pipe(Stream.runCollect)).pipe( |
| 22 | Effect.map((pages) => |
| 23 | Array.from(pages) |
| 24 | .flatMap((page) => page.ReportDefinitions ?? []) |
| 25 | .find((r) => r.ReportName === name), |
| 26 | ), |
| 27 | ); |
| 28 | |
| 29 | const assertReportGone = (name: string) => |
| 30 | Effect.gen(function* () { |
| 31 | const found = yield* findReport(name); |
| 32 | if (found) { |
| 33 | return yield* Effect.fail(new Error(`report definition '${name}' still exists`)); |
| 34 | } |
| 35 | }).pipe( |
| 36 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]) }), |
| 37 | ); |
| 38 | |
| 39 | // Ungated probe: proves auth + response schema against the live us-east-1 |
| 40 | // CUR endpoint on every CI pass at near-zero cost. |
| 41 | test.provider( |
| 42 | "describeReportDefinitions lists report definitions (auth + schema probe)", |
| 43 | () => |
| 44 | Effect.gen(function* () { |
| 45 | const response = yield* pin(cur.describeReportDefinitions({})); |
| 46 | expect(Array.isArray(response.ReportDefinitions ?? [])).toBe(true); |
| 47 | }), |
| 48 | { tags: ["provider:aws", "provider:aws:costandusagereport", "live"] }, |
| 49 | ); |
| 50 | |
| 51 | const REPORT_NAME = "alchemy-test-cur-report"; |
| 52 | const REPLACED_REPORT_NAME = "alchemy-test-cur-report-v2"; |
| 53 | const BUCKET_NAME = "alchemy-test-cur-reports"; |
| 54 | |
| 55 | test.provider( |
| 56 | "create, update, replace, delete report definition", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const { accountId } = yield* AWSEnvironment.current; |
| 62 | |
| 63 | // The CUR bucket policy AWS requires before it will deliver reports. |
| 64 | const curBucketPolicy = [ |
| 65 | { |
| 66 | Effect: "Allow" as const, |
| 67 | Principal: { Service: "billingreports.amazonaws.com" }, |
| 68 | Action: ["s3:GetBucketAcl", "s3:GetBucketPolicy"], |
| 69 | Resource: `arn:aws:s3:::${BUCKET_NAME}`, |
| 70 | Condition: { |
| 71 | StringEquals: { |
| 72 | "aws:SourceAccount": accountId, |
| 73 | "aws:SourceArn": `arn:aws:cur:us-east-1:${accountId}:definition/*`, |
| 74 | }, |
| 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | Effect: "Allow" as const, |
| 79 | Principal: { Service: "billingreports.amazonaws.com" }, |
| 80 | Action: ["s3:PutObject"], |
| 81 | Resource: `arn:aws:s3:::${BUCKET_NAME}/*`, |
| 82 | Condition: { |
| 83 | StringEquals: { |
| 84 | "aws:SourceAccount": accountId, |
| 85 | "aws:SourceArn": `arn:aws:cur:us-east-1:${accountId}:definition/*`, |
| 86 | }, |
| 87 | }, |
| 88 | }, |
| 89 | ]; |
| 90 | |
| 91 | const program = (opts: { |
| 92 | reportName: string; |
| 93 | timeUnit: "DAILY" | "HOURLY"; |
| 94 | compression: "GZIP" | "ZIP"; |
| 95 | }) => |
| 96 | Effect.gen(function* () { |
| 97 | const bucket = yield* Bucket("ReportBucket", { |
| 98 | bucketName: BUCKET_NAME, |
| 99 | forceDestroy: true, |
| 100 | policy: curBucketPolicy, |
| 101 | }); |
| 102 | const report = yield* ReportDefinition("Report", { |
| 103 | reportName: opts.reportName, |
| 104 | timeUnit: opts.timeUnit, |
| 105 | format: "textORcsv", |
| 106 | compression: opts.compression, |
| 107 | additionalSchemaElements: ["RESOURCES"], |
| 108 | s3Bucket: bucket.bucketName, |
| 109 | s3Prefix: "cur", |
| 110 | s3Region: bucket.region, |
| 111 | tags: { fixture: "cur-report" }, |
| 112 | }); |
| 113 | return { bucket, report }; |
| 114 | }); |
| 115 | |
| 116 | // CREATE |
| 117 | const created = yield* stack.deploy( |
| 118 | program({ reportName: REPORT_NAME, timeUnit: "DAILY", compression: "GZIP" }), |
| 119 | ); |
| 120 | expect(created.report.reportName).toBe(REPORT_NAME); |
| 121 | expect(created.report.reportArn).toBe( |
| 122 | `arn:aws:cur:us-east-1:${accountId}:definition/${REPORT_NAME}`, |
| 123 | ); |
| 124 | expect(created.report.s3Bucket).toBe(BUCKET_NAME); |
| 125 | expect(created.report.s3Prefix).toBe("cur"); |
| 126 | expect(created.report.timeUnit).toBe("DAILY"); |
| 127 | |
| 128 | // Out-of-band verification via distilled. |
| 129 | const observed = yield* findReport(REPORT_NAME); |
| 130 | expect(observed?.TimeUnit).toBe("DAILY"); |
| 131 | expect(observed?.Format).toBe("textORcsv"); |
| 132 | expect(observed?.Compression).toBe("GZIP"); |
| 133 | expect(observed?.S3Bucket).toBe(BUCKET_NAME); |
| 134 | expect(observed?.AdditionalSchemaElements).toContain("RESOURCES"); |
| 135 | |
| 136 | // Tags: user tag + alchemy internal tags. |
| 137 | const tags = yield* pin(cur.listTagsForResource({ ReportName: REPORT_NAME })); |
| 138 | expect(tags.Tags).toContainEqual({ Key: "fixture", Value: "cur-report" }); |
| 139 | expect(tags.Tags?.some((t) => t.Key.startsWith("alchemy:"))).toBe(true); |
| 140 | |
| 141 | // Typed duplicate probe: a second putReportDefinition with the same |
| 142 | // name must surface the typed DuplicateReportNameException tag. |
| 143 | const duplicate = yield* Effect.flip( |
| 144 | pin( |
| 145 | cur.putReportDefinition({ |
| 146 | ReportDefinition: { |
| 147 | ReportName: REPORT_NAME, |
| 148 | TimeUnit: "DAILY", |
| 149 | Format: "textORcsv", |
| 150 | Compression: "GZIP", |
| 151 | AdditionalSchemaElements: ["RESOURCES"], |
| 152 | S3Bucket: BUCKET_NAME, |
| 153 | S3Prefix: "cur", |
| 154 | S3Region: observed!.S3Region, |
| 155 | }, |
| 156 | }), |
| 157 | ), |
| 158 | ); |
| 159 | expect(duplicate._tag).toBe("DuplicateReportNameException"); |
| 160 | |
| 161 | // UPDATE in place (timeUnit + compression via modifyReportDefinition). |
| 162 | const updated = yield* stack.deploy( |
| 163 | program({ reportName: REPORT_NAME, timeUnit: "HOURLY", compression: "ZIP" }), |
| 164 | ); |
| 165 | expect(updated.report.reportName).toBe(REPORT_NAME); |
| 166 | const modified = yield* findReport(REPORT_NAME); |
| 167 | expect(modified?.TimeUnit).toBe("HOURLY"); |
| 168 | expect(modified?.Compression).toBe("ZIP"); |
| 169 | |
| 170 | // REPLACE on reportName change: new report created, old one deleted. |
| 171 | const replaced = yield* stack.deploy( |
| 172 | program({ reportName: REPLACED_REPORT_NAME, timeUnit: "HOURLY", compression: "ZIP" }), |
| 173 | ); |
| 174 | expect(replaced.report.reportName).toBe(REPLACED_REPORT_NAME); |
| 175 | const replacement = yield* findReport(REPLACED_REPORT_NAME); |
| 176 | expect(replacement?.TimeUnit).toBe("HOURLY"); |
| 177 | yield* assertReportGone(REPORT_NAME); |
| 178 | |
| 179 | // DELETE |
| 180 | yield* stack.destroy(); |
| 181 | yield* assertReportGone(REPLACED_REPORT_NAME); |
| 182 | }), |
| 183 | { |
| 184 | tags: ["provider:aws", "provider:aws:costandusagereport", "provider:aws:s3", "live"], |
| 185 | timeout: 240_000, |
| 186 | }, |
| 187 | ); |