Skip to content

Commit d0a666e

Browse files
committed
Add unit tests
1 parent d6816ab commit d0a666e

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

src/utils/span-pointers.spec.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
"ptr.kind": S3_PTR_KIND,
52+
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
53+
"ptr.hash": mockS3PointerHash,
54+
"link.kind": SPAN_LINK_KIND,
55+
}];
56+
57+
const result = getSpanPointerAttributes(eventTypes.s3, event);
58+
expect(result).toEqual(expected);
59+
expect(spanPointers.generateS3PointerHash).toHaveBeenCalledWith(
60+
"test-bucket",
61+
"test-key",
62+
"test-etag"
63+
);
64+
});
65+
66+
it("processes multiple S3 records correctly", () => {
67+
const event = {
68+
Records: [
69+
{
70+
s3: {
71+
bucket: { name: "bucket1" },
72+
object: {
73+
key: "key1",
74+
eTag: "etag1"
75+
}
76+
}
77+
},
78+
{
79+
s3: {
80+
bucket: { name: "bucket2" },
81+
object: {
82+
key: "key2",
83+
eTag: "etag2"
84+
}
85+
}
86+
}
87+
]
88+
};
89+
90+
const expected = [
91+
{
92+
"ptr.kind": S3_PTR_KIND,
93+
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
94+
"ptr.hash": mockS3PointerHash,
95+
"link.kind": SPAN_LINK_KIND,
96+
},
97+
{
98+
"ptr.kind": S3_PTR_KIND,
99+
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
100+
"ptr.hash": mockS3PointerHash,
101+
"link.kind": SPAN_LINK_KIND,
102+
}
103+
];
104+
105+
const result = getSpanPointerAttributes(eventTypes.s3, event);
106+
expect(result).toEqual(expected);
107+
});
108+
109+
it("handles empty Records array", () => {
110+
const event = { Records: [] };
111+
const result = getSpanPointerAttributes(eventTypes.s3, event);
112+
expect(result).toEqual([]);
113+
});
114+
115+
it("handles missing Records property", () => {
116+
const event = {};
117+
const result = getSpanPointerAttributes(eventTypes.s3, event);
118+
expect(result).toEqual([]);
119+
});
120+
121+
it("skips invalid records but processes valid ones", () => {
122+
const event = {
123+
Records: [
124+
{
125+
// Invalid record missing s3 property
126+
},
127+
{
128+
s3: {
129+
bucket: { name: "valid-bucket" },
130+
object: {
131+
key: "valid-key",
132+
eTag: "valid-etag"
133+
}
134+
}
135+
}
136+
]
137+
};
138+
139+
const expected = [{
140+
"ptr.kind": S3_PTR_KIND,
141+
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
142+
"ptr.hash": mockS3PointerHash,
143+
"link.kind": SPAN_LINK_KIND,
144+
}];
145+
146+
const result = getSpanPointerAttributes(eventTypes.s3, event);
147+
expect(result).toEqual(expected);
148+
});
149+
});
150+
});
151+
});

0 commit comments

Comments
 (0)