@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 152 lines 5.86 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ServiceLevelObjective } from "@alchemy.run/aws/ApplicationSignals";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as appsignals from "@distilled.cloud/aws/application-signals";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 // Ungated typed-error probe: prove the distilled error union carries the
12 // not-found tag this provider's read/delete paths depend on. Runs in every
13 // CI pass at near-zero cost.
14 test.provider(
15 "getServiceLevelObjective on a bogus id fails with ResourceNotFoundException",
16 () =>
17 Effect.gen(function* () {
18 const error = yield* Effect.flip(
19 appsignals.getServiceLevelObjective({ Id: "alchemy-nonexistent-slo-probe" }),
20 );
21 expect(error._tag).toBe("ResourceNotFoundException");
22 }),
23 { tags: ["provider:aws", "provider:aws:applicationsignals", "live"] },
24 );
25
26 // A period-based SLI over an arbitrary CloudWatch metric (the metric does
27 // not need to exist for the SLO to be valid).
28 const sliConfig = (threshold: number): appsignals.ServiceLevelIndicatorConfig => ({
29 SliMetricConfig: {
30 MetricDataQueries: [
31 {
32 Id: "m1",
33 MetricStat: {
34 Metric: {
35 Namespace: "Alchemy/Test",
36 MetricName: "AppSignalsSloLatency",
37 Dimensions: [{ Name: "Fixture", Value: "slo" }],
38 },
39 Period: 60,
40 Stat: "Average",
41 },
42 ReturnData: true,
43 },
44 ],
45 },
46 MetricThreshold: threshold,
47 ComparisonOperator: "LessThanOrEqualTo",
48 });
49
50 const goal = (attainmentGoal: number): appsignals.Goal => ({
51 Interval: { RollingInterval: { DurationUnit: "DAY", Duration: 7 } },
52 AttainmentGoal: attainmentGoal,
53 WarningThreshold: 50,
54 });
55
56 // Deletion is synchronous but allow brief eventual consistency.
57 const assertSloGone = (id: string) =>
58 appsignals.getServiceLevelObjective({ Id: id }).pipe(
59 Effect.flatMap((r) => Effect.fail(new Error(`slo '${r.Slo.Name}' still exists`))),
60 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
61 Effect.retry({ schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]) }),
62 );
63
64 test.provider(
65 "create, update in place, replace on rename, destroy SLO",
66 (stack) =>
67 Effect.gen(function* () {
68 yield* stack.destroy();
69
70 // CREATE — engine-generated deterministic name.
71 const { slo } = yield* stack.deploy(
72 Effect.gen(function* () {
73 const slo = yield* ServiceLevelObjective("Slo", {
74 description: "alchemy application-signals test slo",
75 sliConfig: sliConfig(2000),
76 goal: goal(99),
77 tags: { fixture: "application-signals-slo" },
78 });
79 return { slo };
80 }),
81 );
82
83 expect(slo.sloName).toBeDefined();
84 expect(slo.sloArn).toContain(":slo/");
85 expect(slo.evaluationType).toBe("PeriodBased");
86
87 // Out-of-band verification via distilled.
88 const observed = yield* appsignals
89 .getServiceLevelObjective({ Id: slo.sloName })
90 .pipe(Effect.map((r) => r.Slo));
91 expect(observed.Arn).toBe(slo.sloArn);
92 expect(observed.Description).toBe("alchemy application-signals test slo");
93 expect(observed.Goal.AttainmentGoal).toBe(99);
94 expect(observed.Sli?.MetricThreshold).toBe(2000);
95
96 // Tags: user tag + internal Alchemy branding.
97 const tags = yield* appsignals
98 .listTagsForResource({ ResourceArn: slo.sloArn })
99 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
100 expect(tags.fixture).toBe("application-signals-slo");
101 expect(tags["alchemy::id"]).toBe("Slo");
102
103 // UPDATE IN PLACE — new description, threshold, attainment, and tag.
104 const { slo: updated } = yield* stack.deploy(
105 Effect.gen(function* () {
106 const slo = yield* ServiceLevelObjective("Slo", {
107 description: "alchemy application-signals test slo (updated)",
108 sliConfig: sliConfig(3000),
109 goal: goal(99.5),
110 tags: { fixture: "application-signals-slo", updated: "true" },
111 });
112 return { slo };
113 }),
114 );
115
116 expect(updated.sloArn).toBe(slo.sloArn);
117 const observedUpdated = yield* appsignals
118 .getServiceLevelObjective({ Id: updated.sloName })
119 .pipe(Effect.map((r) => r.Slo));
120 expect(observedUpdated.Description).toBe("alchemy application-signals test slo (updated)");
121 expect(observedUpdated.Goal.AttainmentGoal).toBe(99.5);
122 expect(observedUpdated.Sli?.MetricThreshold).toBe(3000);
123 const updatedTags = yield* appsignals
124 .listTagsForResource({ ResourceArn: updated.sloArn })
125 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
126 expect(updatedTags.updated).toBe("true");
127
128 // REPLACE — an explicit name change replaces the SLO (no rename API).
129 const { slo: replaced } = yield* stack.deploy(
130 Effect.gen(function* () {
131 const slo = yield* ServiceLevelObjective("Slo", {
132 sloName: "alchemy-appsignals-slo-replaced",
133 description: "alchemy application-signals test slo (replaced)",
134 sliConfig: sliConfig(3000),
135 goal: goal(99.5),
136 tags: { fixture: "application-signals-slo" },
137 });
138 return { slo };
139 }),
140 );
141
142 expect(replaced.sloName).toBe("alchemy-appsignals-slo-replaced");
143 expect(replaced.sloArn).not.toBe(slo.sloArn);
144 // The old SLO is deleted by the replacement.
145 yield* assertSloGone(slo.sloName);
146
147 // DESTROY — and verify the SLO is gone out-of-band.
148 yield* stack.destroy();
149 yield* assertSloGone(replaced.sloName);
150 }),
151 { tags: ["provider:aws", "provider:aws:applicationsignals", "live"], timeout: 120_000 },
152 );