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