Skip to content

Commit 04c66e0

Browse files
CoverBot: Covered core analytics and ESM loader utilities (#153)
Co-authored-by: tusk-dev[bot] <139941764+tusk-dev[bot]@users.noreply.github.com> Co-authored-by: marcel-tan <marceltancw@hotmail.com>
1 parent 6a1fdd8 commit 04c66e0

3 files changed

Lines changed: 422 additions & 4 deletions

File tree

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import test from "ava";
2+
import { sendVersionMismatchAlert, sendUnpatchedDependencyAlert } from "./analyticsUtils";
3+
import { TuskDriftCore, TuskDriftMode } from "../TuskDrift";
4+
5+
type MockCore = {
6+
getMode: () => TuskDriftMode;
7+
getProtobufCommunicator: () => unknown;
8+
};
9+
10+
/**
11+
* Temporarily replace TuskDriftCore.getInstance with a stub.
12+
* Restores the original in a finally block.
13+
*/
14+
function withMockedInstance(mock: MockCore, fn: () => void): void {
15+
const originalGetInstance = (TuskDriftCore as any).getInstance;
16+
(TuskDriftCore as any).getInstance = () => mock;
17+
try {
18+
fn();
19+
} finally {
20+
(TuskDriftCore as any).getInstance = originalGetInstance;
21+
}
22+
}
23+
24+
// ---- sendVersionMismatchAlert ----
25+
26+
test.serial("sendVersionMismatchAlert: does not send when mode is RECORD", (t) => {
27+
let sendCalled = false;
28+
withMockedInstance(
29+
{
30+
getMode: () => TuskDriftMode.RECORD,
31+
getProtobufCommunicator: () => ({
32+
sendInstrumentationVersionMismatchAlert: () => {
33+
sendCalled = true;
34+
},
35+
}),
36+
},
37+
() => {
38+
sendVersionMismatchAlert({
39+
moduleName: "my-module",
40+
foundVersion: "1.0.0",
41+
supportedVersions: ["1.0.0"],
42+
});
43+
},
44+
);
45+
t.false(sendCalled);
46+
});
47+
48+
test.serial("sendVersionMismatchAlert: does not send when mode is DISABLED", (t) => {
49+
let sendCalled = false;
50+
withMockedInstance(
51+
{
52+
getMode: () => TuskDriftMode.DISABLED,
53+
getProtobufCommunicator: () => ({
54+
sendInstrumentationVersionMismatchAlert: () => {
55+
sendCalled = true;
56+
},
57+
}),
58+
},
59+
() => {
60+
sendVersionMismatchAlert({
61+
moduleName: "my-module",
62+
foundVersion: "1.0.0",
63+
supportedVersions: ["1.0.0"],
64+
});
65+
},
66+
);
67+
t.false(sendCalled);
68+
});
69+
70+
test.serial("sendVersionMismatchAlert: does not send when protobufComm is null in REPLAY mode", (t) => {
71+
let sendCalled = false;
72+
withMockedInstance(
73+
{
74+
getMode: () => TuskDriftMode.REPLAY,
75+
getProtobufCommunicator: () => null,
76+
},
77+
() => {
78+
sendVersionMismatchAlert({
79+
moduleName: "my-module",
80+
foundVersion: "1.0.0",
81+
supportedVersions: ["1.0.0"],
82+
});
83+
},
84+
);
85+
t.false(sendCalled);
86+
});
87+
88+
test.serial("sendVersionMismatchAlert: sends alert in REPLAY mode with protobufComm", (t) => {
89+
t.plan(3);
90+
withMockedInstance(
91+
{
92+
getMode: () => TuskDriftMode.REPLAY,
93+
getProtobufCommunicator: () => ({
94+
sendInstrumentationVersionMismatchAlert: (data: {
95+
moduleName: string;
96+
requestedVersion: string | undefined;
97+
supportedVersions: string[];
98+
}) => {
99+
t.is(data.moduleName, "express");
100+
t.is(data.requestedVersion, "4.18.0");
101+
t.deepEqual(data.supportedVersions, ["4.17.0", "4.18.0"]);
102+
},
103+
}),
104+
},
105+
() => {
106+
sendVersionMismatchAlert({
107+
moduleName: "express",
108+
foundVersion: "4.18.0",
109+
supportedVersions: ["4.17.0", "4.18.0"],
110+
});
111+
},
112+
);
113+
});
114+
115+
test.serial("sendVersionMismatchAlert: passes undefined foundVersion as requestedVersion", (t) => {
116+
t.plan(1);
117+
withMockedInstance(
118+
{
119+
getMode: () => TuskDriftMode.REPLAY,
120+
getProtobufCommunicator: () => ({
121+
sendInstrumentationVersionMismatchAlert: (data: { requestedVersion: string | undefined }) => {
122+
t.is(data.requestedVersion, undefined);
123+
},
124+
}),
125+
},
126+
() => {
127+
sendVersionMismatchAlert({
128+
moduleName: "my-module",
129+
foundVersion: undefined,
130+
supportedVersions: ["1.0.0"],
131+
});
132+
},
133+
);
134+
});
135+
136+
test.serial("sendVersionMismatchAlert: handles exception without throwing", (t) => {
137+
withMockedInstance(
138+
{
139+
getMode: () => {
140+
throw new Error("simulated error");
141+
},
142+
getProtobufCommunicator: () => null,
143+
},
144+
() => {
145+
t.notThrows(() => {
146+
sendVersionMismatchAlert({
147+
moduleName: "my-module",
148+
foundVersion: "1.0.0",
149+
supportedVersions: [],
150+
});
151+
});
152+
},
153+
);
154+
});
155+
156+
// ---- sendUnpatchedDependencyAlert ----
157+
158+
test.serial("sendUnpatchedDependencyAlert: does nothing when protobufComm is null", (t) => {
159+
let sendCalled = false;
160+
withMockedInstance(
161+
{
162+
getMode: () => TuskDriftMode.RECORD,
163+
getProtobufCommunicator: () => null,
164+
},
165+
() => {
166+
sendUnpatchedDependencyAlert({
167+
traceTestServerSpanId: "span-123",
168+
stackTrace: "Error: some error",
169+
});
170+
},
171+
);
172+
t.false(sendCalled);
173+
});
174+
175+
test.serial("sendUnpatchedDependencyAlert: does nothing when stackTrace is undefined", (t) => {
176+
let sendCalled = false;
177+
withMockedInstance(
178+
{
179+
getMode: () => TuskDriftMode.RECORD,
180+
getProtobufCommunicator: () => ({
181+
sendUnpatchedDependencyAlert: () => {
182+
sendCalled = true;
183+
},
184+
}),
185+
},
186+
() => {
187+
sendUnpatchedDependencyAlert({
188+
traceTestServerSpanId: "span-123",
189+
// stackTrace intentionally omitted
190+
});
191+
},
192+
);
193+
t.false(sendCalled);
194+
});
195+
196+
test.serial("sendUnpatchedDependencyAlert: sends alert when protobufComm and stackTrace are present", (t) => {
197+
t.plan(2);
198+
withMockedInstance(
199+
{
200+
getMode: () => TuskDriftMode.RECORD,
201+
getProtobufCommunicator: () => ({
202+
sendUnpatchedDependencyAlert: (data: {
203+
traceTestServerSpanId: string;
204+
stackTrace: string;
205+
}) => {
206+
t.is(data.traceTestServerSpanId, "span-789");
207+
t.is(data.stackTrace, "Error: test\n at foo.ts:1:1");
208+
},
209+
}),
210+
},
211+
() => {
212+
sendUnpatchedDependencyAlert({
213+
traceTestServerSpanId: "span-789",
214+
stackTrace: "Error: test\n at foo.ts:1:1",
215+
});
216+
},
217+
);
218+
});
219+
220+
test.serial("sendUnpatchedDependencyAlert: handles exception without throwing", (t) => {
221+
withMockedInstance(
222+
{
223+
getMode: () => TuskDriftMode.RECORD,
224+
getProtobufCommunicator: () => {
225+
throw new Error("simulated error");
226+
},
227+
},
228+
() => {
229+
t.notThrows(() => {
230+
sendUnpatchedDependencyAlert({
231+
traceTestServerSpanId: "span-123",
232+
stackTrace: "Error: test",
233+
});
234+
});
235+
},
236+
);
237+
});

0 commit comments

Comments
 (0)