|
| 1 | +import { parseDurableExecutionArn, extractDurableFunctionContext } from "./durable-function-context"; |
| 2 | + |
| 3 | +describe("durable-function-context", () => { |
| 4 | + describe("parseDurableExecutionArn", () => { |
| 5 | + it("returns execution name and ID for a valid ARN", () => { |
| 6 | + const arn = |
| 7 | + "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123/550e8400-e29b-41d4-a716-446655440001"; |
| 8 | + const result = parseDurableExecutionArn(arn); |
| 9 | + |
| 10 | + expect(result).toEqual({ |
| 11 | + executionName: "order-123", |
| 12 | + executionId: "550e8400-e29b-41d4-a716-446655440001", |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + it("returns undefined for ARN without durable-execution marker", () => { |
| 17 | + const arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST"; |
| 18 | + const result = parseDurableExecutionArn(arn); |
| 19 | + |
| 20 | + expect(result).toBeUndefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("returns undefined for malformed ARN with only execution name", () => { |
| 24 | + const arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123"; |
| 25 | + const result = parseDurableExecutionArn(arn); |
| 26 | + |
| 27 | + expect(result).toBeUndefined(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns undefined for malformed ARN with empty execution name", () => { |
| 31 | + const arn = |
| 32 | + "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution//550e8400-e29b-41d4-a716-446655440002"; |
| 33 | + const result = parseDurableExecutionArn(arn); |
| 34 | + |
| 35 | + expect(result).toBeUndefined(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("returns undefined for malformed ARN with empty execution ID", () => { |
| 39 | + const arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123/"; |
| 40 | + const result = parseDurableExecutionArn(arn); |
| 41 | + |
| 42 | + expect(result).toBeUndefined(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("extractDurableFunctionContext", () => { |
| 47 | + it("extracts context from event.DurableExecutionArn", () => { |
| 48 | + const event = { |
| 49 | + DurableExecutionArn: |
| 50 | + "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004", |
| 51 | + CheckpointToken: "some-token", |
| 52 | + InitialExecutionState: { |
| 53 | + Operations: [], |
| 54 | + }, |
| 55 | + }; |
| 56 | + const result = extractDurableFunctionContext(event); |
| 57 | + |
| 58 | + expect(result).toEqual({ |
| 59 | + durable_function_execution_name: "my-execution", |
| 60 | + durable_function_execution_id: "550e8400-e29b-41d4-a716-446655440004", |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns undefined for regular Lambda event without DurableExecutionArn", () => { |
| 65 | + const event = { |
| 66 | + body: '{"key": "value"}', |
| 67 | + headers: { |
| 68 | + "Content-Type": "application/json", |
| 69 | + }, |
| 70 | + }; |
| 71 | + const result = extractDurableFunctionContext(event); |
| 72 | + |
| 73 | + expect(result).toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("returns undefined when event is null", () => { |
| 77 | + const result = extractDurableFunctionContext(null); |
| 78 | + |
| 79 | + expect(result).toBeUndefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("returns undefined when event is undefined", () => { |
| 83 | + const result = extractDurableFunctionContext(undefined); |
| 84 | + |
| 85 | + expect(result).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("returns undefined when DurableExecutionArn cannot be parsed", () => { |
| 89 | + const event = { |
| 90 | + DurableExecutionArn: "invalid-arn-without-durable-execution-marker", |
| 91 | + }; |
| 92 | + const result = extractDurableFunctionContext(event); |
| 93 | + |
| 94 | + expect(result).toBeUndefined(); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments