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 Detective from "@alchemy.run/aws/Detective"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | export class DetectiveTestFunction extends Lambda.Function<Lambda.Function>()( |
| 13 | "DetectiveTestFunction", |
| 14 | ) {} |
| 15 | |
| 16 | /** |
| 17 | * Routes answer `{ …fields }` on success or `{ errorTag }` when the |
| 18 | * operation fails with a TYPED error — the test asserts the tag is in a |
| 19 | * route-specific allowlist, which proves both the binding wiring and the |
| 20 | * IAM grant. An untyped error crashes into a 500 instead. |
| 21 | */ |
| 22 | const errorTagged = <A, E extends { _tag: string }, R>( |
| 23 | effect: Effect.Effect<A, E, R>, |
| 24 | ): Effect.Effect<A | { errorTag: string; errorMessage?: string }, never, R> => |
| 25 | effect.pipe( |
| 26 | Effect.map((a): A | { errorTag: string; errorMessage?: string } => a), |
| 27 | Effect.catch((e) => |
| 28 | Effect.succeed({ |
| 29 | errorTag: e._tag, |
| 30 | errorMessage: (e as { Message?: string }).Message ?? (e as { message?: string }).message, |
| 31 | }), |
| 32 | ), |
| 33 | ); |
| 34 | |
| 35 | // One binding per Detective capability. Write/member/org-admin operations |
| 36 | // with irreversible or cross-account side effects (UpdateDatasourcePackages |
| 37 | // enables billed ingest; CreateMembers invites foreign accounts; |
| 38 | // Enable/DisableOrganizationAdminAccount require the org management |
| 39 | // account) are bound — proving init + IAM deploy — but not exercised |
| 40 | // against live state. |
| 41 | export default DetectiveTestFunction.make( |
| 42 | { |
| 43 | main, |
| 44 | functionUrl: true, |
| 45 | timeout: Duration.seconds(30), |
| 46 | }, |
| 47 | Effect.gen(function* () { |
| 48 | // The behavior graph every graph-scoped binding is bound to. Detective |
| 49 | // allows one graph per account/region — the test account keeps none |
| 50 | // deployed between runs (Graph.test.ts is capture-and-restore safe). |
| 51 | const graph = yield* Detective.Graph("BindingsGraph", { |
| 52 | tags: { fixture: "detective-bindings" }, |
| 53 | }); |
| 54 | |
| 55 | // Investigations |
| 56 | const startInvestigation = yield* Detective.StartInvestigation(graph); |
| 57 | const getInvestigation = yield* Detective.GetInvestigation(graph); |
| 58 | const listInvestigations = yield* Detective.ListInvestigations(graph); |
| 59 | const listIndicators = yield* Detective.ListIndicators(graph); |
| 60 | const updateInvestigationState = yield* Detective.UpdateInvestigationState(graph); |
| 61 | |
| 62 | // Data source packages |
| 63 | const listDatasourcePackages = yield* Detective.ListDatasourcePackages(graph); |
| 64 | const updateDatasourcePackages = yield* Detective.UpdateDatasourcePackages(graph); |
| 65 | const batchGetGraphMemberDatasources = yield* Detective.BatchGetGraphMemberDatasources(graph); |
| 66 | |
| 67 | // Member administration |
| 68 | const listMembers = yield* Detective.ListMembers(graph); |
| 69 | const getMembers = yield* Detective.GetMembers(graph); |
| 70 | const createMembers = yield* Detective.CreateMembers(graph); |
| 71 | const deleteMembers = yield* Detective.DeleteMembers(graph); |
| 72 | const startMonitoringMember = yield* Detective.StartMonitoringMember(graph); |
| 73 | |
| 74 | // Member-account invitation flow (account-level) |
| 75 | const listInvitations = yield* Detective.ListInvitations(); |
| 76 | const acceptInvitation = yield* Detective.AcceptInvitation(); |
| 77 | const rejectInvitation = yield* Detective.RejectInvitation(); |
| 78 | const disassociateMembership = yield* Detective.DisassociateMembership(); |
| 79 | const batchGetMembershipDatasources = yield* Detective.BatchGetMembershipDatasources(); |
| 80 | |
| 81 | // Organization administration |
| 82 | const describeOrganizationConfiguration = |
| 83 | yield* Detective.DescribeOrganizationConfiguration(graph); |
| 84 | const updateOrganizationConfiguration = yield* Detective.UpdateOrganizationConfiguration(graph); |
| 85 | const listOrganizationAdminAccounts = yield* Detective.ListOrganizationAdminAccounts(); |
| 86 | const enableOrganizationAdminAccount = yield* Detective.EnableOrganizationAdminAccount(); |
| 87 | const disableOrganizationAdminAccount = yield* Detective.DisableOrganizationAdminAccount(); |
| 88 | |
| 89 | const bound = { |
| 90 | startInvestigation, |
| 91 | getInvestigation, |
| 92 | listInvestigations, |
| 93 | listIndicators, |
| 94 | updateInvestigationState, |
| 95 | listDatasourcePackages, |
| 96 | updateDatasourcePackages, |
| 97 | batchGetGraphMemberDatasources, |
| 98 | listMembers, |
| 99 | getMembers, |
| 100 | createMembers, |
| 101 | deleteMembers, |
| 102 | startMonitoringMember, |
| 103 | listInvitations, |
| 104 | acceptInvitation, |
| 105 | rejectInvitation, |
| 106 | disassociateMembership, |
| 107 | batchGetMembershipDatasources, |
| 108 | describeOrganizationConfiguration, |
| 109 | updateOrganizationConfiguration, |
| 110 | listOrganizationAdminAccounts, |
| 111 | enableOrganizationAdminAccount, |
| 112 | disableOrganizationAdminAccount, |
| 113 | }; |
| 114 | |
| 115 | return { |
| 116 | fetch: Effect.gen(function* () { |
| 117 | const request = yield* HttpServerRequest; |
| 118 | const url = new URL(request.originalUrl); |
| 119 | const pathname = url.pathname; |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/bindings") { |
| 122 | return yield* HttpServerResponse.json({ |
| 123 | bound: Object.keys(bound), |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | // Graph-scoped reads — the GraphArn is injected from the binding. |
| 128 | if (request.method === "GET" && pathname === "/members") { |
| 129 | const { MemberDetails } = yield* listMembers(); |
| 130 | return yield* HttpServerResponse.json({ |
| 131 | count: (MemberDetails ?? []).length, |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | if (request.method === "GET" && pathname === "/member-status") { |
| 136 | // A well-formed account id that is not a member: Detective |
| 137 | // answers via UnprocessedAccounts (no side effects). |
| 138 | const { MemberDetails, UnprocessedAccounts } = yield* getMembers({ |
| 139 | AccountIds: [url.searchParams.get("account") ?? "123456789012"], |
| 140 | }); |
| 141 | return yield* HttpServerResponse.json({ |
| 142 | members: (MemberDetails ?? []).length, |
| 143 | unprocessed: (UnprocessedAccounts ?? []).length, |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | if (request.method === "GET" && pathname === "/datasources") { |
| 148 | const { DatasourcePackages } = yield* listDatasourcePackages(); |
| 149 | return yield* HttpServerResponse.json({ |
| 150 | packages: Object.keys(DatasourcePackages ?? {}), |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | if (request.method === "GET" && pathname === "/graph-member-datasources") { |
| 155 | const result = yield* errorTagged( |
| 156 | batchGetGraphMemberDatasources({ |
| 157 | AccountIds: [url.searchParams.get("account") ?? "123456789012"], |
| 158 | }).pipe( |
| 159 | Effect.map((r) => ({ |
| 160 | memberDatasources: (r.MemberDatasources ?? []).length, |
| 161 | unprocessed: (r.UnprocessedAccounts ?? []).length, |
| 162 | })), |
| 163 | ), |
| 164 | ); |
| 165 | return yield* HttpServerResponse.json(result); |
| 166 | } |
| 167 | |
| 168 | if (request.method === "GET" && pathname === "/investigations") { |
| 169 | const { InvestigationDetails } = yield* listInvestigations(); |
| 170 | return yield* HttpServerResponse.json({ |
| 171 | count: (InvestigationDetails ?? []).length, |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | // Investigation write: a brand-new graph has no ingested data yet, |
| 176 | // so Detective typically rejects the entity with a typed |
| 177 | // ValidationException — either outcome proves the binding. |
| 178 | if (request.method === "POST" && pathname === "/investigate") { |
| 179 | const entity = url.searchParams.get("entity"); |
| 180 | if (!entity) { |
| 181 | return yield* HttpServerResponse.json({ error: "missing entity" }, { status: 400 }); |
| 182 | } |
| 183 | const result = yield* errorTagged( |
| 184 | startInvestigation({ |
| 185 | EntityArn: entity, |
| 186 | ScopeStartTime: new Date(Date.now() - 24 * 60 * 60 * 1000), |
| 187 | ScopeEndTime: new Date(), |
| 188 | }).pipe(Effect.map((r) => ({ investigationId: r.InvestigationId }))), |
| 189 | ); |
| 190 | return yield* HttpServerResponse.json(result); |
| 191 | } |
| 192 | |
| 193 | // Account-level reads (member-account side of the handshake). |
| 194 | if (request.method === "GET" && pathname === "/invitations") { |
| 195 | const { Invitations } = yield* listInvitations(); |
| 196 | return yield* HttpServerResponse.json({ |
| 197 | count: (Invitations ?? []).length, |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | if (request.method === "GET" && pathname === "/membership-datasources") { |
| 202 | const graphArn = url.searchParams.get("graphArn"); |
| 203 | if (!graphArn) { |
| 204 | return yield* HttpServerResponse.json({ error: "missing graphArn" }, { status: 400 }); |
| 205 | } |
| 206 | const result = yield* errorTagged( |
| 207 | batchGetMembershipDatasources({ GraphArns: [graphArn] }).pipe( |
| 208 | Effect.map((r) => ({ |
| 209 | membershipDatasources: (r.MembershipDatasources ?? []).length, |
| 210 | unprocessedGraphs: (r.UnprocessedGraphs ?? []).length, |
| 211 | })), |
| 212 | ), |
| 213 | ); |
| 214 | return yield* HttpServerResponse.json(result); |
| 215 | } |
| 216 | |
| 217 | // Org administration: this account is a standalone (non-delegated) |
| 218 | // account, so Detective answers with a typed error — asserting the |
| 219 | // tag proves the binding + IAM wiring end-to-end. |
| 220 | if (request.method === "GET" && pathname === "/org-config") { |
| 221 | const result = yield* errorTagged( |
| 222 | describeOrganizationConfiguration().pipe( |
| 223 | Effect.map((r) => ({ autoEnable: r.AutoEnable ?? false })), |
| 224 | ), |
| 225 | ); |
| 226 | return yield* HttpServerResponse.json(result); |
| 227 | } |
| 228 | |
| 229 | if (request.method === "GET" && pathname === "/org-admins") { |
| 230 | const result = yield* errorTagged( |
| 231 | listOrganizationAdminAccounts().pipe( |
| 232 | Effect.map((r) => ({ |
| 233 | administrators: (r.Administrators ?? []).length, |
| 234 | })), |
| 235 | ), |
| 236 | ); |
| 237 | return yield* HttpServerResponse.json(result); |
| 238 | } |
| 239 | |
| 240 | return yield* HttpServerResponse.json( |
| 241 | { error: "Not found", method: request.method, pathname }, |
| 242 | { status: 404 }, |
| 243 | ); |
| 244 | }).pipe(Effect.orDie), |
| 245 | }; |
| 246 | }).pipe( |
| 247 | Effect.provide( |
| 248 | Layer.mergeAll( |
| 249 | Detective.AcceptInvitationHttp, |
| 250 | Detective.BatchGetGraphMemberDatasourcesHttp, |
| 251 | Detective.BatchGetMembershipDatasourcesHttp, |
| 252 | Detective.CreateMembersHttp, |
| 253 | Detective.DeleteMembersHttp, |
| 254 | Detective.DescribeOrganizationConfigurationHttp, |
| 255 | Detective.DisableOrganizationAdminAccountHttp, |
| 256 | Detective.DisassociateMembershipHttp, |
| 257 | Detective.EnableOrganizationAdminAccountHttp, |
| 258 | Detective.GetInvestigationHttp, |
| 259 | Detective.GetMembersHttp, |
| 260 | Detective.ListDatasourcePackagesHttp, |
| 261 | Detective.ListIndicatorsHttp, |
| 262 | Detective.ListInvestigationsHttp, |
| 263 | Detective.ListInvitationsHttp, |
| 264 | Detective.ListMembersHttp, |
| 265 | Detective.ListOrganizationAdminAccountsHttp, |
| 266 | Detective.RejectInvitationHttp, |
| 267 | Detective.StartInvestigationHttp, |
| 268 | Detective.StartMonitoringMemberHttp, |
| 269 | Detective.UpdateDatasourcePackagesHttp, |
| 270 | Detective.UpdateInvestigationStateHttp, |
| 271 | Detective.UpdateOrganizationConfigurationHttp, |
| 272 | ), |
| 273 | ), |
| 274 | ), |
| 275 | ); |