@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 211 lines 8.12 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Role } from "@alchemy.run/aws/IAM/Role";
3 import { Bot, BotAlias, BotLocale, Intent, SlotType } from "@alchemy.run/aws/LexV2";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as lexm from "@distilled.cloud/aws/lex-models-v2";
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 // Ungated typed-error probe: prove the distilled error union carries the
13 // not-found tag the provider's read/observe paths depend on. Runs in every
14 // CI pass at near-zero cost.
15 test.provider(
16 "describeBot on a nonexistent bot fails with ResourceNotFoundException",
17 () =>
18 Effect.gen(function* () {
19 const error = yield* Effect.flip(lexm.describeBot({ botId: "BOGUSBOT01" }));
20 expect(error._tag).toBe("ResourceNotFoundException");
21 }),
22 { tags: ["provider:aws", "provider:aws:lexv2", "live"] },
23 );
24
25 const lexBotRole = Role("LexBotRole", {
26 assumeRolePolicyDocument: {
27 Version: "2012-10-17",
28 Statement: [
29 {
30 Effect: "Allow",
31 Principal: { Service: "lexv2.amazonaws.com" },
32 Action: ["sts:AssumeRole"],
33 },
34 ],
35 },
36 });
37
38 /** Deletion is asynchronous — verify the bot is fully gone out-of-band. */
39 const assertBotGone = (botId: string) =>
40 Effect.gen(function* () {
41 const status = yield* lexm.describeBot({ botId }).pipe(
42 Effect.map((bot) => bot.botStatus ?? "unknown"),
43 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
44 );
45 if (status !== "gone") {
46 return yield* Effect.fail(new Error(`bot '${botId}' still exists (status: ${status})`));
47 }
48 }).pipe(
49 Effect.retry({ schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]) }),
50 );
51
52 test.provider(
53 "bot + locale + slot type + intent + alias lifecycle (create, update, destroy)",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 // ---- create ----
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const role = yield* lexBotRole;
62 const bot = yield* Bot("TestBot", {
63 roleArn: role.roleArn,
64 description: "alchemy lex test bot",
65 idleSessionTTL: "300 seconds",
66 tags: { fixture: "lexv2-bot" },
67 });
68 const locale = yield* BotLocale("En", {
69 botId: bot.botId,
70 localeId: "en_US",
71 nluIntentConfidenceThreshold: 0.4,
72 });
73 const size = yield* SlotType("Size", {
74 botId: locale.botId,
75 localeId: locale.localeId,
76 slotTypeValues: [
77 { value: "small", synonyms: ["tiny"] },
78 { value: "large", synonyms: ["big"] },
79 ],
80 resolutionStrategy: "TopResolution",
81 });
82 const greet = yield* Intent("Greet", {
83 botId: size.botId,
84 localeId: size.localeId,
85 sampleUtterances: ["hello", "hi"],
86 description: "greets the user",
87 });
88 const alias = yield* BotAlias("Staging", {
89 botId: greet.botId,
90 description: "unassociated staging alias",
91 tags: { fixture: "lexv2-alias" },
92 });
93 return { bot, locale, size, greet, alias };
94 }),
95 );
96
97 expect(created.bot.botId).toBeDefined();
98 expect(created.bot.botArn).toContain(":bot/");
99 expect(created.bot.botStatus).toBe("Available");
100 expect(created.locale.localeId).toBe("en_US");
101 expect(created.locale.botVersion).toBe("DRAFT");
102 expect(created.size.slotTypeId).toBeDefined();
103 expect(created.greet.intentId).toBeDefined();
104 expect(created.alias.botAliasId).toBeDefined();
105 expect(created.alias.botAliasArn).toContain(":bot-alias/");
106 expect(created.alias.botAliasStatus).toBe("Available");
107
108 // Out-of-band verification via distilled.
109 const bot = yield* lexm.describeBot({ botId: created.bot.botId });
110 expect(bot.botStatus).toBe("Available");
111 expect(bot.description).toBe("alchemy lex test bot");
112 const intent = yield* lexm.describeIntent({
113 botId: created.bot.botId,
114 botVersion: "DRAFT",
115 localeId: "en_US",
116 intentId: created.greet.intentId,
117 });
118 expect((intent.sampleUtterances ?? []).map((u) => u.utterance).sort()).toEqual([
119 "hello",
120 "hi",
121 ]);
122 const slotType = yield* lexm.describeSlotType({
123 botId: created.bot.botId,
124 botVersion: "DRAFT",
125 localeId: "en_US",
126 slotTypeId: created.size.slotTypeId,
127 });
128 expect(slotType.valueSelectionSetting?.resolutionStrategy).toBe("TopResolution");
129 const tags = yield* lexm.listTagsForResource({ resourceARN: created.bot.botArn });
130 expect(tags.tags?.fixture).toBe("lexv2-bot");
131 expect(tags.tags?.["alchemy::id"]).toBe("TestBot");
132
133 // ---- update in place (same identities) ----
134 const updated = yield* stack.deploy(
135 Effect.gen(function* () {
136 const role = yield* lexBotRole;
137 const bot = yield* Bot("TestBot", {
138 roleArn: role.roleArn,
139 description: "alchemy lex test bot (updated)",
140 idleSessionTTL: "10 minutes",
141 tags: { fixture: "lexv2-bot", pass: "two" },
142 });
143 const locale = yield* BotLocale("En", {
144 botId: bot.botId,
145 localeId: "en_US",
146 nluIntentConfidenceThreshold: 0.5,
147 });
148 const size = yield* SlotType("Size", {
149 botId: locale.botId,
150 localeId: locale.localeId,
151 slotTypeValues: [
152 { value: "small", synonyms: ["tiny"] },
153 { value: "medium" },
154 { value: "large", synonyms: ["big"] },
155 ],
156 resolutionStrategy: "TopResolution",
157 });
158 const greet = yield* Intent("Greet", {
159 botId: size.botId,
160 localeId: size.localeId,
161 sampleUtterances: ["hello", "hi", "good morning"],
162 description: "greets the user politely",
163 dialogCodeHook: true,
164 fulfillmentCodeHook: true,
165 });
166 const alias = yield* BotAlias("Staging", {
167 botId: greet.botId,
168 description: "still unassociated",
169 tags: { fixture: "lexv2-alias" },
170 });
171 return { bot, locale, size, greet, alias };
172 }),
173 );
174
175 // Identities are stable across the update.
176 expect(updated.bot.botId).toBe(created.bot.botId);
177 expect(updated.greet.intentId).toBe(created.greet.intentId);
178 expect(updated.size.slotTypeId).toBe(created.size.slotTypeId);
179 expect(updated.alias.botAliasId).toBe(created.alias.botAliasId);
180
181 const updatedBot = yield* lexm.describeBot({ botId: updated.bot.botId });
182 expect(updatedBot.description).toBe("alchemy lex test bot (updated)");
183 expect(updatedBot.idleSessionTTLInSeconds).toBe(600);
184 const updatedLocale = yield* lexm.describeBotLocale({
185 botId: updated.bot.botId,
186 botVersion: "DRAFT",
187 localeId: "en_US",
188 });
189 expect(updatedLocale.nluIntentConfidenceThreshold).toBe(0.5);
190 const updatedIntent = yield* lexm.describeIntent({
191 botId: updated.bot.botId,
192 botVersion: "DRAFT",
193 localeId: "en_US",
194 intentId: updated.greet.intentId,
195 });
196 expect((updatedIntent.sampleUtterances ?? []).map((u) => u.utterance)).toContain(
197 "good morning",
198 );
199 // Code hook flags synced onto the DRAFT intent by the update.
200 expect(updatedIntent.dialogCodeHook?.enabled).toBe(true);
201 expect(updatedIntent.fulfillmentCodeHook?.enabled).toBe(true);
202 const updatedTags = yield* lexm.listTagsForResource({ resourceARN: updated.bot.botArn });
203 expect(updatedTags.tags?.pass).toBe("two");
204
205 // ---- destroy + typed wait-until-gone ----
206 yield* stack.destroy();
207 yield* assertBotGone(created.bot.botId);
208 }),
209 // Observed ~15s live; headroom for slow deletes and the wait-until-gone.
210 { tags: ["provider:aws", "provider:aws:iam", "provider:aws:lexv2", "live"], timeout: 180_000 },
211 );