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 { AlertManagerDefinition, RuleGroupsNamespace, Workspace } from "@alchemy.run/aws/AMP"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as amp from "@distilled.cloud/aws/amp"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | const RULES_V1 = `groups: |
| 13 | - name: example |
| 14 | rules: |
| 15 | - record: metric:requests:rate5m |
| 16 | expr: rate(http_requests_total[5m])`; |
| 17 | |
| 18 | const RULES_V2 = `groups: |
| 19 | - name: example |
| 20 | rules: |
| 21 | - record: metric:requests:rate10m |
| 22 | expr: rate(http_requests_total[10m])`; |
| 23 | |
| 24 | const ALERTS = `alertmanager_config: | |
| 25 | route: |
| 26 | receiver: default |
| 27 | receivers: |
| 28 | - name: default`; |
| 29 | |
| 30 | const decode = (data: Uint8Array) => new TextDecoder().decode(data); |
| 31 | |
| 32 | class WorkspaceStillExists extends Data.TaggedError("WorkspaceStillExists")<{ |
| 33 | readonly workspaceId: string; |
| 34 | }> {} |
| 35 | |
| 36 | const assertWorkspaceDeleted = (workspaceId: string) => |
| 37 | amp.describeWorkspace({ workspaceId }).pipe( |
| 38 | Effect.flatMap((r) => |
| 39 | r.workspace.status.statusCode === "DELETING" |
| 40 | ? Effect.fail(new WorkspaceStillExists({ workspaceId })) |
| 41 | : Effect.succeed(undefined), |
| 42 | ), |
| 43 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 44 | Effect.retry({ |
| 45 | while: (e) => e._tag === "WorkspaceStillExists", |
| 46 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | // Ungated typed-error probe: proves the distilled error union carries the |
| 51 | // not-found tag the read/delete paths depend on. |
| 52 | test.provider( |
| 53 | "describeWorkspace on a nonexistent id fails with ResourceNotFoundException", |
| 54 | () => |
| 55 | Effect.gen(function* () { |
| 56 | const error = yield* Effect.flip( |
| 57 | amp.describeWorkspace({ workspaceId: "ws-00000000-0000-0000-0000-000000000000" }), |
| 58 | ); |
| 59 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 60 | }), |
| 61 | { tags: ["provider:aws", "provider:aws:amp", "live"] }, |
| 62 | ); |
| 63 | |
| 64 | // AMP workspaces + rule/alert sub-resources are cheap and quick to |
| 65 | // provision, so the full lifecycle runs ungated. |
| 66 | test.provider( |
| 67 | "create, update, and destroy a workspace with rules and alerts", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | yield* stack.destroy(); |
| 71 | |
| 72 | // Create the workspace + a rule groups namespace + alert manager def. |
| 73 | const created = yield* stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | const workspace = yield* Workspace("Metrics", { |
| 76 | alias: "alchemy-test-amp", |
| 77 | tags: { Environment: "test" }, |
| 78 | }); |
| 79 | const rules = yield* RuleGroupsNamespace("Rules", { |
| 80 | workspaceId: workspace.workspaceId, |
| 81 | name: "alchemy-test-rules", |
| 82 | definition: RULES_V1, |
| 83 | tags: { Environment: "test" }, |
| 84 | }); |
| 85 | const alerts = yield* AlertManagerDefinition("Alerts", { |
| 86 | workspaceId: workspace.workspaceId, |
| 87 | definition: ALERTS, |
| 88 | }); |
| 89 | return { workspace, rules, alerts }; |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | const workspaceId = created.workspace.workspaceId; |
| 94 | expect(workspaceId).toMatch(/^ws-/); |
| 95 | expect(created.workspace.workspaceArn).toContain(":workspace/"); |
| 96 | expect(created.workspace.status).toBe("ACTIVE"); |
| 97 | expect(created.workspace.prometheusEndpoint).toContain("aps-workspaces"); |
| 98 | expect(created.rules.ruleGroupsNamespaceArn).toContain(":rulegroupsnamespace/"); |
| 99 | expect(created.alerts.workspaceId).toBe(workspaceId); |
| 100 | |
| 101 | // Out-of-band verification via distilled. |
| 102 | const describedWs = yield* amp.describeWorkspace({ workspaceId }); |
| 103 | expect(describedWs.workspace.alias).toBe("alchemy-test-amp"); |
| 104 | expect(describedWs.workspace.tags?.["alchemy::id"]).toBe("Metrics"); |
| 105 | |
| 106 | const describedRules = yield* amp.describeRuleGroupsNamespace({ |
| 107 | workspaceId, |
| 108 | name: "alchemy-test-rules", |
| 109 | }); |
| 110 | expect(describedRules.ruleGroupsNamespace.data).toBeDefined(); |
| 111 | expect(decode(describedRules.ruleGroupsNamespace.data!)).toBe(RULES_V1); |
| 112 | expect(describedRules.ruleGroupsNamespace.tags?.["alchemy::id"]).toBe("Rules"); |
| 113 | |
| 114 | // The workspace Alertmanager provisions asynchronously (a few |
| 115 | // minutes); the definition exists immediately but its `data` blob is |
| 116 | // only returned once ACTIVE. |
| 117 | const describedAlerts = yield* amp.describeAlertManagerDefinition({ workspaceId }); |
| 118 | expect( |
| 119 | ["ACTIVE", "CREATING"].includes(describedAlerts.alertManagerDefinition.status.statusCode), |
| 120 | ).toBe(true); |
| 121 | if (describedAlerts.alertManagerDefinition.data !== undefined) { |
| 122 | expect(decode(describedAlerts.alertManagerDefinition.data)).toBe(ALERTS); |
| 123 | } |
| 124 | |
| 125 | // Update: change the alias and the rules definition in place. |
| 126 | const updated = yield* stack.deploy( |
| 127 | Effect.gen(function* () { |
| 128 | const workspace = yield* Workspace("Metrics", { |
| 129 | alias: "alchemy-test-amp-v2", |
| 130 | tags: { Environment: "test", Extra: "yes" }, |
| 131 | }); |
| 132 | const rules = yield* RuleGroupsNamespace("Rules", { |
| 133 | workspaceId: workspace.workspaceId, |
| 134 | name: "alchemy-test-rules", |
| 135 | definition: RULES_V2, |
| 136 | tags: { Environment: "test" }, |
| 137 | }); |
| 138 | const alerts = yield* AlertManagerDefinition("Alerts", { |
| 139 | workspaceId: workspace.workspaceId, |
| 140 | definition: ALERTS, |
| 141 | }); |
| 142 | return { workspace, rules, alerts }; |
| 143 | }), |
| 144 | ); |
| 145 | |
| 146 | // Stable identifiers survive the in-place update. |
| 147 | expect(updated.workspace.workspaceId).toBe(workspaceId); |
| 148 | expect(updated.workspace.workspaceArn).toBe(created.workspace.workspaceArn); |
| 149 | expect(updated.rules.ruleGroupsNamespaceArn).toBe(created.rules.ruleGroupsNamespaceArn); |
| 150 | |
| 151 | const afterWs = yield* amp.describeWorkspace({ workspaceId }); |
| 152 | expect(afterWs.workspace.alias).toBe("alchemy-test-amp-v2"); |
| 153 | expect(afterWs.workspace.tags?.Extra).toBe("yes"); |
| 154 | |
| 155 | const afterRules = yield* amp.describeRuleGroupsNamespace({ |
| 156 | workspaceId, |
| 157 | name: "alchemy-test-rules", |
| 158 | }); |
| 159 | expect(afterRules.ruleGroupsNamespace.data).toBeDefined(); |
| 160 | expect(decode(afterRules.ruleGroupsNamespace.data!)).toBe(RULES_V2); |
| 161 | |
| 162 | yield* stack.destroy(); |
| 163 | yield* assertWorkspaceDeleted(workspaceId); |
| 164 | }), |
| 165 | { tags: ["provider:aws", "provider:aws:amp", "live"], timeout: 240_000 }, |
| 166 | ); |