|
| 1 | +import { getSpanPointerAttributes } from "./span-pointers"; |
| 2 | +import { eventTypes } from "../trace/trigger"; |
| 3 | +import { SPAN_LINK_KIND, S3_PTR_KIND, SPAN_POINTER_DIRECTION } from "dd-trace/packages/dd-trace/src/span_pointers"; |
| 4 | +import * as spanPointers from "dd-trace/packages/dd-trace/src/span_pointers"; |
| 5 | + |
| 6 | +// Mock the external dependencies |
| 7 | +jest.mock("./log", () => ({ |
| 8 | + logDebug: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +describe("span-pointers utils", () => { |
| 12 | + const mockS3PointerHash = "mock-hash-123"; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // Mock the generateS3PointerHash function |
| 16 | + jest.spyOn(spanPointers, "generateS3PointerHash").mockReturnValue(mockS3PointerHash); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + describe("getSpanPointerAttributes", () => { |
| 24 | + it("returns undefined when eventSource is undefined", () => { |
| 25 | + const result = getSpanPointerAttributes(undefined, {}); |
| 26 | + expect(result).toBeUndefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns undefined for unsupported event types", () => { |
| 30 | + const result = getSpanPointerAttributes("unsupported" as eventTypes, {}); |
| 31 | + expect(result).toBeUndefined(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("S3 event processing", () => { |
| 35 | + it("processes single S3 record correctly", () => { |
| 36 | + const event = { |
| 37 | + Records: [ |
| 38 | + { |
| 39 | + s3: { |
| 40 | + bucket: { name: "test-bucket" }, |
| 41 | + object: { |
| 42 | + key: "test-key", |
| 43 | + eTag: "test-etag", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }; |
| 49 | + |
| 50 | + const expected = [ |
| 51 | + { |
| 52 | + "ptr.kind": S3_PTR_KIND, |
| 53 | + "ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM, |
| 54 | + "ptr.hash": mockS3PointerHash, |
| 55 | + "link.kind": SPAN_LINK_KIND, |
| 56 | + }, |
| 57 | + ]; |
| 58 | + |
| 59 | + const result = getSpanPointerAttributes(eventTypes.s3, event); |
| 60 | + expect(result).toEqual(expected); |
| 61 | + expect(spanPointers.generateS3PointerHash).toHaveBeenCalledWith("test-bucket", "test-key", "test-etag"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("processes multiple S3 records correctly", () => { |
| 65 | + const event = { |
| 66 | + Records: [ |
| 67 | + { |
| 68 | + s3: { |
| 69 | + bucket: { name: "bucket1" }, |
| 70 | + object: { |
| 71 | + key: "key1", |
| 72 | + eTag: "etag1", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + s3: { |
| 78 | + bucket: { name: "bucket2" }, |
| 79 | + object: { |
| 80 | + key: "key2", |
| 81 | + eTag: "etag2", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }; |
| 87 | + |
| 88 | + const expected = [ |
| 89 | + { |
| 90 | + "ptr.kind": S3_PTR_KIND, |
| 91 | + "ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM, |
| 92 | + "ptr.hash": mockS3PointerHash, |
| 93 | + "link.kind": SPAN_LINK_KIND, |
| 94 | + }, |
| 95 | + { |
| 96 | + "ptr.kind": S3_PTR_KIND, |
| 97 | + "ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM, |
| 98 | + "ptr.hash": mockS3PointerHash, |
| 99 | + "link.kind": SPAN_LINK_KIND, |
| 100 | + }, |
| 101 | + ]; |
| 102 | + |
| 103 | + const result = getSpanPointerAttributes(eventTypes.s3, event); |
| 104 | + expect(result).toEqual(expected); |
| 105 | + }); |
| 106 | + |
| 107 | + it("handles empty Records array", () => { |
| 108 | + const event = { Records: [] }; |
| 109 | + const result = getSpanPointerAttributes(eventTypes.s3, event); |
| 110 | + expect(result).toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + it("handles missing Records property", () => { |
| 114 | + const event = {}; |
| 115 | + const result = getSpanPointerAttributes(eventTypes.s3, event); |
| 116 | + expect(result).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it("skips invalid records but processes valid ones", () => { |
| 120 | + const event = { |
| 121 | + Records: [ |
| 122 | + { |
| 123 | + // Invalid record missing s3 property |
| 124 | + }, |
| 125 | + { |
| 126 | + s3: { |
| 127 | + bucket: { name: "valid-bucket" }, |
| 128 | + object: { |
| 129 | + key: "valid-key", |
| 130 | + eTag: "valid-etag", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + ], |
| 135 | + }; |
| 136 | + |
| 137 | + const expected = [ |
| 138 | + { |
| 139 | + "ptr.kind": S3_PTR_KIND, |
| 140 | + "ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM, |
| 141 | + "ptr.hash": mockS3PointerHash, |
| 142 | + "link.kind": SPAN_LINK_KIND, |
| 143 | + }, |
| 144 | + ]; |
| 145 | + |
| 146 | + const result = getSpanPointerAttributes(eventTypes.s3, event); |
| 147 | + expect(result).toEqual(expected); |
| 148 | + }); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments