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 EntityResolution from "@alchemy.run/aws/EntityResolution"; |
| 2 | import * as Glue from "@alchemy.run/aws/Glue"; |
| 3 | import * as IAM from "@alchemy.run/aws/IAM"; |
| 4 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 5 | import * as S3 from "@alchemy.run/aws/S3"; |
| 6 | import * as Duration from "effect/Duration"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 9 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import path from "pathe"; |
| 12 | |
| 13 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 14 | |
| 15 | // Deterministic fixture names (bucket names are account-global; this suite |
| 16 | // owns them in the testing account). |
| 17 | export const FIXTURE_BUCKET_NAME = "alchemy-test-entityres-bindings"; |
| 18 | |
| 19 | // Well-formed-but-nonexistent job id used to drive the typed |
| 20 | // ResourceNotFoundException paths. An IAM gap would surface |
| 21 | // AccessDeniedException (a 500 through the handler's orDie), so a typed |
| 22 | // not-found tag proves the grant end-to-end. |
| 23 | const BOGUS_JOB_ID = "00000000000000000000000000000000"; |
| 24 | |
| 25 | export class EntityResolutionTestFunction extends Lambda.Function<Lambda.Function>()( |
| 26 | "EntityResolutionTestFunction", |
| 27 | ) {} |
| 28 | |
| 29 | export default EntityResolutionTestFunction.make( |
| 30 | { |
| 31 | main, |
| 32 | functionUrl: true, |
| 33 | timeout: Duration.seconds(60), |
| 34 | }, |
| 35 | Effect.gen(function* () { |
| 36 | const bucket = yield* S3.Bucket("ErBindingsBucket", { |
| 37 | bucketName: FIXTURE_BUCKET_NAME, |
| 38 | forceDestroy: true, |
| 39 | }); |
| 40 | // The role Entity Resolution assumes to read the Glue input tables (and |
| 41 | // their underlying S3 data) and write matched output to S3. |
| 42 | const role = yield* IAM.Role("ErBindingsRole", { |
| 43 | assumeRolePolicyDocument: { |
| 44 | Version: "2012-10-17", |
| 45 | Statement: [ |
| 46 | { |
| 47 | Effect: "Allow", |
| 48 | Principal: { Service: "entityresolution.amazonaws.com" }, |
| 49 | Action: ["sts:AssumeRole"], |
| 50 | }, |
| 51 | ], |
| 52 | }, |
| 53 | inlinePolicies: { |
| 54 | workflow: { |
| 55 | Version: "2012-10-17", |
| 56 | Statement: [ |
| 57 | { |
| 58 | Effect: "Allow", |
| 59 | Action: [ |
| 60 | "glue:GetDatabase", |
| 61 | "glue:GetTable", |
| 62 | "glue:GetPartition", |
| 63 | "glue:GetPartitions", |
| 64 | "glue:GetSchema", |
| 65 | "glue:GetSchemaVersion", |
| 66 | ], |
| 67 | Resource: ["*"], |
| 68 | }, |
| 69 | { |
| 70 | Effect: "Allow", |
| 71 | Action: ["s3:GetObject", "s3:ListBucket", "s3:PutObject"], |
| 72 | Resource: [ |
| 73 | `arn:aws:s3:::${FIXTURE_BUCKET_NAME}`, |
| 74 | `arn:aws:s3:::${FIXTURE_BUCKET_NAME}/*`, |
| 75 | ], |
| 76 | }, |
| 77 | ], |
| 78 | }, |
| 79 | }, |
| 80 | }); |
| 81 | const database = yield* Glue.Database("ErBindingsDb", {}); |
| 82 | const table = yield* Glue.Table("Customers", { |
| 83 | databaseName: database.databaseName, |
| 84 | storageDescriptor: { |
| 85 | location: `s3://${FIXTURE_BUCKET_NAME}/input/`, |
| 86 | inputFormat: "org.apache.hadoop.mapred.TextInputFormat", |
| 87 | outputFormat: "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat", |
| 88 | serdeInfo: { |
| 89 | serializationLibrary: "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe", |
| 90 | parameters: { "field.delim": ",", "skip.header.line.count": "1" }, |
| 91 | }, |
| 92 | columns: [ |
| 93 | { name: "id", type: "string" }, |
| 94 | { name: "email", type: "string" }, |
| 95 | { name: "name", type: "string" }, |
| 96 | ], |
| 97 | }, |
| 98 | parameters: { classification: "csv" }, |
| 99 | }); |
| 100 | const schema = yield* EntityResolution.SchemaMapping("ErBindingsSchema", { |
| 101 | mappedInputFields: [ |
| 102 | { fieldName: "id", type: "UNIQUE_ID" }, |
| 103 | { fieldName: "email", type: "EMAIL_ADDRESS", matchKey: "email" }, |
| 104 | { fieldName: "name", type: "NAME", matchKey: "name" }, |
| 105 | ], |
| 106 | }); |
| 107 | const matchingWorkflow = yield* EntityResolution.MatchingWorkflow("ErBindingsMatching", { |
| 108 | inputSourceConfig: [{ inputSourceARN: table.tableArn, schemaName: schema.schemaName }], |
| 109 | outputSourceConfig: [ |
| 110 | { |
| 111 | outputS3Path: `s3://${FIXTURE_BUCKET_NAME}/matches/`, |
| 112 | output: [{ name: "id" }, { name: "email" }, { name: "name" }], |
| 113 | }, |
| 114 | ], |
| 115 | resolutionTechniques: { |
| 116 | resolutionType: "RULE_MATCHING", |
| 117 | ruleBasedProperties: { |
| 118 | rules: [{ ruleName: "ByEmail", matchingKeys: ["email"] }], |
| 119 | attributeMatchingModel: "ONE_TO_ONE", |
| 120 | }, |
| 121 | }, |
| 122 | roleArn: role.roleArn, |
| 123 | }); |
| 124 | |
| 125 | // Rule-based ID mapping: SOURCE and TARGET namespaces over the same Glue |
| 126 | // table, with the matching rules declared on the TARGET namespace. |
| 127 | const sourceNamespace = yield* EntityResolution.IdNamespace("ErBindingsSourceNs", { |
| 128 | type: "SOURCE", |
| 129 | inputSourceConfig: [{ inputSourceARN: table.tableArn, schemaName: schema.schemaName }], |
| 130 | // The workflow-create validation requires the SOURCE namespace to |
| 131 | // declare its rule-based capabilities (rules themselves live on the |
| 132 | // TARGET namespace). |
| 133 | idMappingWorkflowProperties: [ |
| 134 | { |
| 135 | idMappingType: "RULE_BASED", |
| 136 | ruleBasedProperties: { |
| 137 | ruleDefinitionTypes: ["TARGET"], |
| 138 | attributeMatchingModel: "ONE_TO_ONE", |
| 139 | recordMatchingModels: ["ONE_SOURCE_TO_ONE_TARGET"], |
| 140 | }, |
| 141 | }, |
| 142 | ], |
| 143 | roleArn: role.roleArn, |
| 144 | }); |
| 145 | // The service requires TARGET namespace input sources to be matching |
| 146 | // workflow ARNs (the target ids are the workflow's match IDs): |
| 147 | // "Check that it follows the pattern: |
| 148 | // arn:(aws|...):entityresolution:...:matchingworkflow/{resource_name}". |
| 149 | const targetNamespace = yield* EntityResolution.IdNamespace("ErBindingsTargetNs", { |
| 150 | type: "TARGET", |
| 151 | inputSourceConfig: [{ inputSourceARN: matchingWorkflow.workflowArn }], |
| 152 | idMappingWorkflowProperties: [ |
| 153 | { |
| 154 | idMappingType: "RULE_BASED", |
| 155 | ruleBasedProperties: { |
| 156 | rules: [{ ruleName: "ByEmail", matchingKeys: ["email"] }], |
| 157 | ruleDefinitionTypes: ["TARGET"], |
| 158 | attributeMatchingModel: "ONE_TO_ONE", |
| 159 | recordMatchingModels: ["ONE_SOURCE_TO_ONE_TARGET"], |
| 160 | }, |
| 161 | }, |
| 162 | ], |
| 163 | roleArn: role.roleArn, |
| 164 | }); |
| 165 | const idMappingWorkflow = yield* EntityResolution.IdMappingWorkflow("ErBindingsIdMapping", { |
| 166 | inputSourceConfig: [ |
| 167 | { |
| 168 | inputSourceARN: sourceNamespace.idNamespaceArn, |
| 169 | type: "SOURCE", |
| 170 | }, |
| 171 | { |
| 172 | inputSourceARN: targetNamespace.idNamespaceArn, |
| 173 | type: "TARGET", |
| 174 | }, |
| 175 | ], |
| 176 | idMappingTechniques: { |
| 177 | idMappingType: "RULE_BASED", |
| 178 | ruleBasedProperties: { |
| 179 | ruleDefinitionType: "TARGET", |
| 180 | attributeMatchingModel: "ONE_TO_ONE", |
| 181 | recordMatchingModel: "ONE_SOURCE_TO_ONE_TARGET", |
| 182 | }, |
| 183 | }, |
| 184 | outputSourceConfig: [{ outputS3Path: `s3://${FIXTURE_BUCKET_NAME}/idmapping/` }], |
| 185 | roleArn: role.roleArn, |
| 186 | }); |
| 187 | |
| 188 | // Used by the real-time routes: records reference the Glue table ARN. |
| 189 | const tableArn = yield* table.tableArn; |
| 190 | |
| 191 | // --- matching workflow bindings --- |
| 192 | const startMatchingJob = yield* EntityResolution.StartMatchingJob(matchingWorkflow); |
| 193 | const getMatchingJob = yield* EntityResolution.GetMatchingJob(matchingWorkflow); |
| 194 | const listMatchingJobs = yield* EntityResolution.ListMatchingJobs(matchingWorkflow); |
| 195 | const generateMatchId = yield* EntityResolution.GenerateMatchId(matchingWorkflow); |
| 196 | const getMatchId = yield* EntityResolution.GetMatchId(matchingWorkflow); |
| 197 | const batchDeleteUniqueId = yield* EntityResolution.BatchDeleteUniqueId(matchingWorkflow); |
| 198 | |
| 199 | // --- id mapping workflow bindings --- |
| 200 | const startIdMappingJob = yield* EntityResolution.StartIdMappingJob(idMappingWorkflow); |
| 201 | const getIdMappingJob = yield* EntityResolution.GetIdMappingJob(idMappingWorkflow); |
| 202 | const listIdMappingJobs = yield* EntityResolution.ListIdMappingJobs(idMappingWorkflow); |
| 203 | |
| 204 | const bound = { |
| 205 | startMatchingJob, |
| 206 | getMatchingJob, |
| 207 | listMatchingJobs, |
| 208 | generateMatchId, |
| 209 | getMatchId, |
| 210 | batchDeleteUniqueId, |
| 211 | startIdMappingJob, |
| 212 | getIdMappingJob, |
| 213 | listIdMappingJobs, |
| 214 | }; |
| 215 | |
| 216 | return { |
| 217 | fetch: Effect.gen(function* () { |
| 218 | const request = yield* HttpServerRequest; |
| 219 | const url = new URL(request.originalUrl); |
| 220 | const pathname = url.pathname; |
| 221 | |
| 222 | // Cheap readiness route — no Entity Resolution call. |
| 223 | if (request.method === "GET" && pathname === "/bindings") { |
| 224 | return yield* HttpServerResponse.json({ |
| 225 | bound: Object.keys(bound), |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | // Workflow-scoped job listings (both empty on a fresh deploy). |
| 230 | if (request.method === "GET" && pathname === "/jobs") { |
| 231 | const matching = yield* listMatchingJobs({}); |
| 232 | const idMapping = yield* listIdMappingJobs({}); |
| 233 | return yield* HttpServerResponse.json({ |
| 234 | matchingJobs: (matching.jobs ?? []).length, |
| 235 | idMappingJobs: (idMapping.jobs ?? []).length, |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | // Drives GetMatchingJob + GetIdMappingJob through their typed |
| 240 | // ResourceNotFoundException path (an IAM gap would be a 500). |
| 241 | if (request.method === "GET" && pathname === "/jobs/not-found") { |
| 242 | const matching = yield* getMatchingJob({ jobId: BOGUS_JOB_ID }).pipe( |
| 243 | Effect.map(() => "Found"), |
| 244 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 245 | ); |
| 246 | const idMapping = yield* getIdMappingJob({ |
| 247 | jobId: BOGUS_JOB_ID, |
| 248 | }).pipe( |
| 249 | Effect.map(() => "Found"), |
| 250 | Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 251 | ); |
| 252 | return yield* HttpServerResponse.json({ matching, idMapping }); |
| 253 | } |
| 254 | |
| 255 | // Real-time lookup of a record that has never been processed — a |
| 256 | // successful empty response proves the grant + request plumbing. |
| 257 | if (request.method === "POST" && pathname === "/match-id") { |
| 258 | const response = yield* getMatchId({ |
| 259 | record: { id: "1", email: "jane@example.com", name: "Jane Doe" }, |
| 260 | }).pipe( |
| 261 | Effect.map((r) => ({ |
| 262 | result: "ok" as const, |
| 263 | matchId: r.matchId ?? null, |
| 264 | matchRule: r.matchRule ?? null, |
| 265 | message: null as string | null, |
| 266 | })), |
| 267 | // A workflow that has never run a job may reject real-time |
| 268 | // lookups — surface the typed rejection so the test can assert |
| 269 | // on it (an IAM gap would still be a 500). |
| 270 | Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) => |
| 271 | Effect.succeed({ |
| 272 | result: e._tag as string, |
| 273 | matchId: null, |
| 274 | matchRule: null, |
| 275 | message: e.message ?? null, |
| 276 | }), |
| 277 | ), |
| 278 | ); |
| 279 | return yield* HttpServerResponse.json(response); |
| 280 | } |
| 281 | |
| 282 | // Real-time match-id generation for two records that match by email. |
| 283 | if (request.method === "POST" && pathname === "/generate-match-id") { |
| 284 | const result = yield* generateMatchId({ |
| 285 | records: [ |
| 286 | { |
| 287 | inputSourceARN: yield* tableArn, |
| 288 | uniqueId: "1", |
| 289 | recordAttributeMap: { |
| 290 | id: "1", |
| 291 | email: "jane@example.com", |
| 292 | name: "Jane Doe", |
| 293 | }, |
| 294 | }, |
| 295 | { |
| 296 | inputSourceARN: yield* tableArn, |
| 297 | uniqueId: "2", |
| 298 | recordAttributeMap: { |
| 299 | id: "2", |
| 300 | email: "jane@example.com", |
| 301 | name: "Jane D", |
| 302 | }, |
| 303 | }, |
| 304 | ], |
| 305 | }).pipe( |
| 306 | Effect.map((r) => ({ |
| 307 | result: "ok" as const, |
| 308 | matchGroups: r.matchGroups.length, |
| 309 | failedRecords: r.failedRecords.length, |
| 310 | })), |
| 311 | // Real-time generation requires the workflow family to support |
| 312 | // it — surface the typed rejection instead of a 500 so the test |
| 313 | // can assert on it. |
| 314 | Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) => |
| 315 | Effect.succeed({ |
| 316 | result: e._tag as string, |
| 317 | message: e.message ?? null, |
| 318 | }), |
| 319 | ), |
| 320 | ); |
| 321 | return yield* HttpServerResponse.json(result); |
| 322 | } |
| 323 | |
| 324 | // Deletes unknown unique ids from the match store — per-id errors |
| 325 | // (or a typed ValidationException when the workflow family doesn't |
| 326 | // support deletes) prove the grant end-to-end. |
| 327 | if (request.method === "POST" && pathname === "/delete-unique-ids") { |
| 328 | // Fully total: report ANY typed failure as its tag so the test can |
| 329 | // assert on the observed behavior (an IAM gap would surface here as |
| 330 | // AccessDeniedException rather than an opaque 500). |
| 331 | const outcome = yield* batchDeleteUniqueId({ |
| 332 | uniqueIds: ["ghost-1", "ghost-2"], |
| 333 | }).pipe(Effect.result); |
| 334 | const result = |
| 335 | outcome._tag === "Success" |
| 336 | ? { |
| 337 | result: "ok" as const, |
| 338 | status: outcome.success.status, |
| 339 | deleted: (outcome.success.deleted ?? []).length, |
| 340 | errors: (outcome.success.errors ?? []).length, |
| 341 | disconnected: (outcome.success.disconnectedUniqueIds ?? []).length, |
| 342 | } |
| 343 | : { |
| 344 | result: outcome.failure._tag as string, |
| 345 | message: String(outcome.failure), |
| 346 | }; |
| 347 | return yield* HttpServerResponse.json(result); |
| 348 | } |
| 349 | |
| 350 | // Gated by the test: really starts batch jobs (matching runs take |
| 351 | // many minutes and cannot be cancelled — only exercised when |
| 352 | // AWS_TEST_ENTITYRESOLUTION_RUN is set). |
| 353 | if (request.method === "POST" && pathname === "/start-matching-job") { |
| 354 | const { jobId } = yield* startMatchingJob({}); |
| 355 | const job = yield* getMatchingJob({ jobId }); |
| 356 | return yield* HttpServerResponse.json({ |
| 357 | jobId, |
| 358 | status: job.status, |
| 359 | }); |
| 360 | } |
| 361 | if (request.method === "POST" && pathname === "/start-id-mapping-job") { |
| 362 | const { jobId } = yield* startIdMappingJob({}); |
| 363 | const job = yield* getIdMappingJob({ jobId }); |
| 364 | return yield* HttpServerResponse.json({ |
| 365 | jobId, |
| 366 | status: job.status, |
| 367 | }); |
| 368 | } |
| 369 | |
| 370 | return yield* HttpServerResponse.json( |
| 371 | { error: "Not found", method: request.method, pathname }, |
| 372 | { status: 404 }, |
| 373 | ); |
| 374 | }).pipe(Effect.orDie), |
| 375 | }; |
| 376 | }).pipe( |
| 377 | Effect.provide( |
| 378 | Layer.mergeAll( |
| 379 | EntityResolution.StartMatchingJobHttp, |
| 380 | EntityResolution.GetMatchingJobHttp, |
| 381 | EntityResolution.ListMatchingJobsHttp, |
| 382 | EntityResolution.GenerateMatchIdHttp, |
| 383 | EntityResolution.GetMatchIdHttp, |
| 384 | EntityResolution.BatchDeleteUniqueIdHttp, |
| 385 | EntityResolution.StartIdMappingJobHttp, |
| 386 | EntityResolution.GetIdMappingJobHttp, |
| 387 | EntityResolution.ListIdMappingJobsHttp, |
| 388 | ), |
| 389 | ), |
| 390 | ), |
| 391 | ); |