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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { |
| 3 | AnomalyDetector, |
| 4 | LoggingConfiguration, |
| 5 | QueryLoggingConfiguration, |
| 6 | ResourcePolicy, |
| 7 | Workspace, |
| 8 | } from "@alchemy.run/aws/AMP"; |
| 9 | import * as Logs from "@alchemy.run/aws/Logs"; |
| 10 | import { expect } from "@alchemy.run/test"; |
| 11 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 12 | import * as amp from "@distilled.cloud/aws/amp"; |
| 13 | import * as sts from "@distilled.cloud/aws/sts"; |
| 14 | import * as Output from "alchemy/Output"; |
| 15 | import * as Data from "effect/Data"; |
| 16 | import type * as Duration from "effect/Duration"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as Schedule from "effect/Schedule"; |
| 19 | |
| 20 | const { test } = Test.make({ providers: AWS.providers() }); |
| 21 | |
| 22 | class WorkspaceStillExists extends Data.TaggedError("WorkspaceStillExists")<{ |
| 23 | readonly workspaceId: string; |
| 24 | }> {} |
| 25 | |
| 26 | /** Poll the workspace configuration until the retention lands (bounded). */ |
| 27 | const pollRetention = (workspaceId: string, expected: number) => |
| 28 | amp.describeWorkspaceConfiguration({ workspaceId }).pipe( |
| 29 | Effect.map((r) => r.workspaceConfiguration.retentionPeriodInDays), |
| 30 | Effect.repeat({ |
| 31 | schedule: Schedule.spaced("4 seconds"), |
| 32 | until: (days): boolean => days === expected, |
| 33 | times: 25, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | const assertWorkspaceDeleted = (workspaceId: string) => |
| 38 | amp.describeWorkspace({ workspaceId }).pipe( |
| 39 | Effect.flatMap((r) => |
| 40 | r.workspace.status.statusCode === "DELETING" |
| 41 | ? Effect.fail(new WorkspaceStillExists({ workspaceId })) |
| 42 | : Effect.succeed(undefined), |
| 43 | ), |
| 44 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 45 | Effect.retry({ |
| 46 | while: (e) => e._tag === "WorkspaceStillExists", |
| 47 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]), |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | // Full lifecycle for the workspace-configuration surface: retention period |
| 52 | // (workspace configuration), rules/alerting logging, query logging, and the |
| 53 | // workspace resource-based policy. |
| 54 | test.provider( |
| 55 | "workspace configuration, logging, query logging, and resource policy", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const { Account } = yield* sts.getCallerIdentity({}); |
| 61 | const policyFor = (workspaceArn: string, action: string) => |
| 62 | JSON.stringify({ |
| 63 | Version: "2012-10-17", |
| 64 | Statement: [ |
| 65 | { |
| 66 | Effect: "Allow", |
| 67 | Principal: { AWS: `arn:aws:iam::${Account}:root` }, |
| 68 | Action: [action], |
| 69 | Resource: workspaceArn, |
| 70 | }, |
| 71 | ], |
| 72 | }); |
| 73 | |
| 74 | const program = (retention: Duration.Input, qspThreshold: number) => |
| 75 | Effect.gen(function* () { |
| 76 | const workspace = yield* Workspace("Metrics", { |
| 77 | alias: "alchemy-test-amp-config", |
| 78 | retentionPeriod: retention, |
| 79 | tags: { Environment: "test" }, |
| 80 | }); |
| 81 | const ruleLogs = yield* Logs.LogGroup("RuleLogs", { |
| 82 | logGroupName: "/aws/vendedlogs/prometheus/alchemy-test-amp-config", |
| 83 | retention: "1 day", |
| 84 | }); |
| 85 | const queryLogs = yield* Logs.LogGroup("QueryLogs", { |
| 86 | logGroupName: "/aws/vendedlogs/prometheus/alchemy-test-amp-config-queries", |
| 87 | retention: "1 day", |
| 88 | }); |
| 89 | const logging = yield* LoggingConfiguration("Logging", { |
| 90 | workspaceId: workspace.workspaceId, |
| 91 | logGroupArn: ruleLogs.logGroupArn, |
| 92 | }); |
| 93 | const queryLogging = yield* QueryLoggingConfiguration("QueryLogging", { |
| 94 | workspaceId: workspace.workspaceId, |
| 95 | destinations: [{ logGroupArn: queryLogs.logGroupArn, qspThreshold }], |
| 96 | }); |
| 97 | const policy = yield* ResourcePolicy("Policy", { |
| 98 | workspaceId: workspace.workspaceId, |
| 99 | policyDocument: Output.map(workspace.workspaceArn, (arn) => |
| 100 | policyFor(arn, "aps:QueryMetrics"), |
| 101 | ), |
| 102 | }); |
| 103 | return { workspace, ruleLogs, logging, queryLogging, policy }; |
| 104 | }); |
| 105 | |
| 106 | // Create. |
| 107 | const created = yield* stack.deploy(program("30 days", 0)); |
| 108 | const workspaceId = created.workspace.workspaceId; |
| 109 | expect(workspaceId).toMatch(/^ws-/); |
| 110 | expect(created.logging.workspaceId).toBe(workspaceId); |
| 111 | expect(created.logging.logGroupArn).toContain(":log-group:"); |
| 112 | expect(created.queryLogging.destinations).toHaveLength(1); |
| 113 | expect(created.queryLogging.destinations[0].qspThreshold).toBe(0); |
| 114 | expect(created.policy.policyStatus).toBeTruthy(); |
| 115 | const firstRevision = created.policy.revisionId; |
| 116 | |
| 117 | // Out-of-band verification via distilled. The retention update |
| 118 | // applies asynchronously — poll until it lands. |
| 119 | const retentionAfterCreate = yield* pollRetention(workspaceId, 30); |
| 120 | expect(retentionAfterCreate).toBe(30); |
| 121 | |
| 122 | const logging = yield* amp.describeLoggingConfiguration({ workspaceId }); |
| 123 | expect(logging.loggingConfiguration.logGroupArn).toContain("alchemy-test-amp-config"); |
| 124 | |
| 125 | const queryLogging = yield* amp.describeQueryLoggingConfiguration({ |
| 126 | workspaceId, |
| 127 | }); |
| 128 | expect(queryLogging.queryLoggingConfiguration.destinations).toHaveLength(1); |
| 129 | expect(queryLogging.queryLoggingConfiguration.destinations[0].filters.qspThreshold).toBe(0); |
| 130 | |
| 131 | const policy = yield* amp.describeResourcePolicy({ workspaceId }); |
| 132 | expect(policy.policyDocument).toContain("aps:QueryMetrics"); |
| 133 | |
| 134 | // Update: retention 45 days, qsp threshold 1000. The policy document |
| 135 | // is unchanged — its revision must be stable (no-op skipped the put). |
| 136 | const updated = yield* stack.deploy(program("45 days", 1000)); |
| 137 | expect(updated.workspace.workspaceId).toBe(workspaceId); |
| 138 | expect(updated.policy.revisionId).toBe(firstRevision); |
| 139 | expect(updated.queryLogging.destinations[0].qspThreshold).toBe(1000); |
| 140 | |
| 141 | const retentionAfterUpdate = yield* pollRetention(workspaceId, 45); |
| 142 | expect(retentionAfterUpdate).toBe(45); |
| 143 | const queryLoggingAfter = yield* amp.describeQueryLoggingConfiguration({ |
| 144 | workspaceId, |
| 145 | }); |
| 146 | expect(queryLoggingAfter.queryLoggingConfiguration.destinations[0].filters.qspThreshold).toBe( |
| 147 | 1000, |
| 148 | ); |
| 149 | |
| 150 | yield* stack.destroy(); |
| 151 | yield* assertWorkspaceDeleted(workspaceId); |
| 152 | |
| 153 | // The sub-configurations died with the workspace; a fresh describe on |
| 154 | // the deleted workspace id is a typed not-found. |
| 155 | const error = yield* Effect.flip(amp.describeLoggingConfiguration({ workspaceId })); |
| 156 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 157 | }), |
| 158 | { |
| 159 | tags: ["provider:aws", "provider:aws:amp", "provider:aws:logs", "live"], |
| 160 | timeout: 300_000, |
| 161 | }, |
| 162 | ); |
| 163 | |
| 164 | // Anomaly detectors train asynchronously (backfill against workspace data), |
| 165 | // so the lifecycle asserts identity + configuration and lets the provider's |
| 166 | // bounded wait tolerate a still-training detector. |
| 167 | test.provider( |
| 168 | "anomaly detector lifecycle", |
| 169 | (stack) => |
| 170 | Effect.gen(function* () { |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | const program = (interval: Duration.Input) => |
| 174 | Effect.gen(function* () { |
| 175 | const workspace = yield* Workspace("DetectorWorkspace", { |
| 176 | alias: "alchemy-test-amp-anomaly", |
| 177 | tags: { Environment: "test" }, |
| 178 | }); |
| 179 | const detector = yield* AnomalyDetector("Detector", { |
| 180 | workspaceId: workspace.workspaceId, |
| 181 | alias: "alchemy-test-detector", |
| 182 | query: "avg(rate(alchemy_amp_anomaly_test_total[5m]))", |
| 183 | evaluationInterval: interval, |
| 184 | missingDataAction: { skip: true }, |
| 185 | tags: { Environment: "test" }, |
| 186 | }); |
| 187 | return { workspace, detector }; |
| 188 | }); |
| 189 | |
| 190 | const created = yield* stack.deploy(program("1 minute")); |
| 191 | const workspaceId = created.workspace.workspaceId; |
| 192 | const detectorId = created.detector.anomalyDetectorId; |
| 193 | expect(detectorId).toBeTruthy(); |
| 194 | expect(created.detector.anomalyDetectorArn).toContain("arn:aws:aps:"); |
| 195 | expect(created.detector.alias).toBe("alchemy-test-detector"); |
| 196 | |
| 197 | // Out-of-band verification via distilled. |
| 198 | const described = yield* amp.describeAnomalyDetector({ |
| 199 | workspaceId, |
| 200 | anomalyDetectorId: detectorId, |
| 201 | }); |
| 202 | expect(described.anomalyDetector.evaluationIntervalInSeconds).toBe(60); |
| 203 | expect(described.anomalyDetector.tags?.["alchemy::id"]).toBe("Detector"); |
| 204 | |
| 205 | // Update the evaluation interval in place (same id survives). |
| 206 | const updated = yield* stack.deploy(program("2 minutes")); |
| 207 | expect(updated.detector.anomalyDetectorId).toBe(detectorId); |
| 208 | const describedAfter = yield* amp.describeAnomalyDetector({ |
| 209 | workspaceId, |
| 210 | anomalyDetectorId: detectorId, |
| 211 | }); |
| 212 | expect(describedAfter.anomalyDetector.evaluationIntervalInSeconds).toBe(120); |
| 213 | |
| 214 | yield* stack.destroy(); |
| 215 | yield* assertWorkspaceDeleted(workspaceId); |
| 216 | }), |
| 217 | { tags: ["provider:aws", "provider:aws:amp", "live"], timeout: 300_000 }, |
| 218 | ); |