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
| 1 | import * as Chatbot from "@alchemy.run/aws/Chatbot"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import path from "pathe"; |
| 8 | |
| 9 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 10 | |
| 11 | // Well-formed-but-nonexistent identifiers — drive the typed error paths of |
| 12 | // the identity offboarding bindings without touching any real workspace. |
| 13 | const NONEXISTENT_SLACK_TEAM = "T0000000000"; |
| 14 | const NONEXISTENT_SLACK_USER = "U0000000000"; |
| 15 | const NONEXISTENT_TEAMS_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"; |
| 16 | |
| 17 | // The delete-identity operations validate the configuration ARN against the |
| 18 | // caller's account, so the test passes its account id as `?account=` and the |
| 19 | // fixture builds well-formed same-account (but nonexistent) ARNs. |
| 20 | const nonexistentSlackConfigurationArn = (account: string) => |
| 21 | `arn:aws:chatbot::${account}:chat-configuration/slack-channel/alchemy-probe-nonexistent`; |
| 22 | const nonexistentTeamsConfigurationArn = (account: string) => |
| 23 | `arn:aws:chatbot::${account}:chat-configuration/microsoft-teams-channel/alchemy-probe-nonexistent`; |
| 24 | |
| 25 | export class ChatbotTestFunction extends Lambda.Function<Lambda.Function>()( |
| 26 | "ChatbotTestFunction", |
| 27 | ) {} |
| 28 | |
| 29 | /** |
| 30 | * Account-level Chatbot bindings fixture. |
| 31 | * |
| 32 | * The testing account has no Slack workspace or Microsoft Teams team |
| 33 | * onboarded (onboarding requires the console OAuth flow), so the reads |
| 34 | * answer real empty lists — a REAL data-plane success through each |
| 35 | * binding's IAM grant — and the identity deletes answer the typed error |
| 36 | * tags for nonexistent identities, proving the wiring and typed error |
| 37 | * union for each capability. |
| 38 | */ |
| 39 | export default ChatbotTestFunction.make( |
| 40 | { |
| 41 | main, |
| 42 | functionUrl: true, |
| 43 | }, |
| 44 | Effect.gen(function* () { |
| 45 | const getAccountPreferences = yield* Chatbot.GetAccountPreferences(); |
| 46 | const updateAccountPreferences = yield* Chatbot.UpdateAccountPreferences(); |
| 47 | const describeSlackWorkspaces = yield* Chatbot.DescribeSlackWorkspaces(); |
| 48 | const describeSlackUserIdentities = yield* Chatbot.DescribeSlackUserIdentities(); |
| 49 | const deleteSlackUserIdentity = yield* Chatbot.DeleteSlackUserIdentity(); |
| 50 | const deleteSlackWorkspaceAuthorization = yield* Chatbot.DeleteSlackWorkspaceAuthorization(); |
| 51 | const listMicrosoftTeamsConfiguredTeams = yield* Chatbot.ListMicrosoftTeamsConfiguredTeams(); |
| 52 | const listMicrosoftTeamsUserIdentities = yield* Chatbot.ListMicrosoftTeamsUserIdentities(); |
| 53 | const deleteMicrosoftTeamsUserIdentity = yield* Chatbot.DeleteMicrosoftTeamsUserIdentity(); |
| 54 | const deleteMicrosoftTeamsConfiguredTeam = yield* Chatbot.DeleteMicrosoftTeamsConfiguredTeam(); |
| 55 | |
| 56 | const bound = { |
| 57 | getAccountPreferences, |
| 58 | updateAccountPreferences, |
| 59 | describeSlackWorkspaces, |
| 60 | describeSlackUserIdentities, |
| 61 | deleteSlackUserIdentity, |
| 62 | deleteSlackWorkspaceAuthorization, |
| 63 | listMicrosoftTeamsConfiguredTeams, |
| 64 | listMicrosoftTeamsUserIdentities, |
| 65 | deleteMicrosoftTeamsUserIdentity, |
| 66 | deleteMicrosoftTeamsConfiguredTeam, |
| 67 | }; |
| 68 | |
| 69 | return { |
| 70 | fetch: Effect.gen(function* () { |
| 71 | const request = yield* HttpServerRequest; |
| 72 | const url = new URL(request.originalUrl); |
| 73 | const pathname = url.pathname; |
| 74 | |
| 75 | if (request.method === "GET" && pathname === "/bindings") { |
| 76 | return yield* HttpServerResponse.json({ |
| 77 | bound: Object.keys(bound), |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | if (request.method === "GET" && pathname === "/account-preferences") { |
| 82 | // Succeeds on every account — a REAL success through the |
| 83 | // binding's IAM grant. |
| 84 | const result = yield* getAccountPreferences(); |
| 85 | return yield* HttpServerResponse.json({ |
| 86 | ok: true, |
| 87 | hasPreferences: result.AccountPreferences !== undefined, |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | if (request.method === "POST" && pathname === "/account-preferences-roundtrip") { |
| 92 | // Read the current preferences, then write the identical values |
| 93 | // back — a safe no-op write proving the update binding. |
| 94 | const current = yield* getAccountPreferences(); |
| 95 | const result = yield* updateAccountPreferences({ |
| 96 | UserAuthorizationRequired: |
| 97 | current.AccountPreferences?.UserAuthorizationRequired ?? false, |
| 98 | TrainingDataCollectionEnabled: |
| 99 | current.AccountPreferences?.TrainingDataCollectionEnabled ?? false, |
| 100 | }).pipe( |
| 101 | Effect.map((r) => ({ |
| 102 | ok: true as const, |
| 103 | hasPreferences: r.AccountPreferences !== undefined, |
| 104 | })), |
| 105 | Effect.catchTag(["InvalidParameterException", "InvalidRequestException"], (e) => |
| 106 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 107 | ), |
| 108 | ); |
| 109 | return yield* HttpServerResponse.json(result); |
| 110 | } |
| 111 | |
| 112 | if (request.method === "GET" && pathname === "/slack-workspaces") { |
| 113 | const result = yield* describeSlackWorkspaces(); |
| 114 | return yield* HttpServerResponse.json({ |
| 115 | ok: true, |
| 116 | count: (result.SlackWorkspaces ?? []).length, |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | if (request.method === "GET" && pathname === "/slack-user-identities") { |
| 121 | const result = yield* describeSlackUserIdentities(); |
| 122 | return yield* HttpServerResponse.json({ |
| 123 | ok: true, |
| 124 | count: (result.SlackUserIdentities ?? []).length, |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | if (request.method === "GET" && pathname === "/teams-configured-teams") { |
| 129 | const result = yield* listMicrosoftTeamsConfiguredTeams(); |
| 130 | return yield* HttpServerResponse.json({ |
| 131 | ok: true, |
| 132 | count: (result.ConfiguredTeams ?? []).length, |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | if (request.method === "GET" && pathname === "/teams-user-identities") { |
| 137 | const result = yield* listMicrosoftTeamsUserIdentities(); |
| 138 | return yield* HttpServerResponse.json({ |
| 139 | ok: true, |
| 140 | count: (result.TeamsUserIdentities ?? []).length, |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | if (request.method === "POST" && pathname === "/delete-slack-user-identity") { |
| 145 | const result = yield* deleteSlackUserIdentity({ |
| 146 | ChatConfigurationArn: nonexistentSlackConfigurationArn( |
| 147 | url.searchParams.get("account") ?? "", |
| 148 | ), |
| 149 | SlackTeamId: NONEXISTENT_SLACK_TEAM, |
| 150 | SlackUserId: NONEXISTENT_SLACK_USER, |
| 151 | }).pipe( |
| 152 | Effect.map(() => ({ ok: true as const })), |
| 153 | Effect.catchTag( |
| 154 | [ |
| 155 | "DeleteSlackUserIdentityException", |
| 156 | "InvalidParameterException", |
| 157 | "ResourceNotFoundException", |
| 158 | ], |
| 159 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 160 | ), |
| 161 | ); |
| 162 | return yield* HttpServerResponse.json(result); |
| 163 | } |
| 164 | |
| 165 | if (request.method === "POST" && pathname === "/delete-slack-workspace-authorization") { |
| 166 | const result = yield* deleteSlackWorkspaceAuthorization({ |
| 167 | SlackTeamId: NONEXISTENT_SLACK_TEAM, |
| 168 | }).pipe( |
| 169 | Effect.map(() => ({ ok: true as const })), |
| 170 | Effect.catchTag( |
| 171 | ["DeleteSlackWorkspaceAuthorizationFault", "InvalidParameterException"], |
| 172 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 173 | ), |
| 174 | ); |
| 175 | return yield* HttpServerResponse.json(result); |
| 176 | } |
| 177 | |
| 178 | if (request.method === "POST" && pathname === "/delete-teams-user-identity") { |
| 179 | const result = yield* deleteMicrosoftTeamsUserIdentity({ |
| 180 | ChatConfigurationArn: nonexistentTeamsConfigurationArn( |
| 181 | url.searchParams.get("account") ?? "", |
| 182 | ), |
| 183 | UserId: NONEXISTENT_TEAMS_ID, |
| 184 | }).pipe( |
| 185 | Effect.map(() => ({ ok: true as const })), |
| 186 | Effect.catchTag( |
| 187 | [ |
| 188 | "DeleteMicrosoftTeamsUserIdentityException", |
| 189 | "InvalidParameterException", |
| 190 | "ResourceNotFoundException", |
| 191 | ], |
| 192 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 193 | ), |
| 194 | ); |
| 195 | return yield* HttpServerResponse.json(result); |
| 196 | } |
| 197 | |
| 198 | if (request.method === "POST" && pathname === "/delete-teams-configured-team") { |
| 199 | const result = yield* deleteMicrosoftTeamsConfiguredTeam({ |
| 200 | TeamId: NONEXISTENT_TEAMS_ID, |
| 201 | }).pipe( |
| 202 | Effect.map(() => ({ ok: true as const })), |
| 203 | // ResourceNotFoundException ("No Team found for the team id") |
| 204 | // is outside the Smithy model's union for this operation and is |
| 205 | // added via patches/chatbot.json. |
| 206 | Effect.catchTag( |
| 207 | [ |
| 208 | "DeleteTeamsConfiguredTeamException", |
| 209 | "InvalidParameterException", |
| 210 | "ResourceNotFoundException", |
| 211 | ], |
| 212 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 213 | ), |
| 214 | ); |
| 215 | return yield* HttpServerResponse.json(result); |
| 216 | } |
| 217 | |
| 218 | return yield* HttpServerResponse.json( |
| 219 | { error: "Not found", method: request.method, pathname }, |
| 220 | { status: 404 }, |
| 221 | ); |
| 222 | }).pipe(Effect.orDie), |
| 223 | }; |
| 224 | }).pipe( |
| 225 | Effect.provide( |
| 226 | Layer.mergeAll( |
| 227 | Chatbot.GetAccountPreferencesHttp, |
| 228 | Chatbot.UpdateAccountPreferencesHttp, |
| 229 | Chatbot.DescribeSlackWorkspacesHttp, |
| 230 | Chatbot.DescribeSlackUserIdentitiesHttp, |
| 231 | Chatbot.DeleteSlackUserIdentityHttp, |
| 232 | Chatbot.DeleteSlackWorkspaceAuthorizationHttp, |
| 233 | Chatbot.ListMicrosoftTeamsConfiguredTeamsHttp, |
| 234 | Chatbot.ListMicrosoftTeamsUserIdentitiesHttp, |
| 235 | Chatbot.DeleteMicrosoftTeamsUserIdentityHttp, |
| 236 | Chatbot.DeleteMicrosoftTeamsConfiguredTeamHttp, |
| 237 | ), |
| 238 | ), |
| 239 | ), |
| 240 | ); |