-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathhevy-client-observability.test.ts
More file actions
131 lines (114 loc) · 3.58 KB
/
Copy pathhevy-client-observability.test.ts
File metadata and controls
131 lines (114 loc) · 3.58 KB
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
import { beforeEach, describe, expect, it, vi } from "vitest";
import { HevyHttpError } from "./hevy-http-error.js";
import { createNodeHevyClientOptions } from "./hevy-client-observability.js";
import { getCurrentUserHash } from "./telemetry.js";
const testDoubles = vi.hoisted(() => ({
span: {
addEvent: vi.fn(),
end: vi.fn(),
setStatus: vi.fn(),
},
startSpan: vi.fn(),
apiCallsAdd: vi.fn(),
apiDurationRecord: vi.fn(),
}));
testDoubles.startSpan.mockReturnValue(testDoubles.span);
vi.mock("./telemetry.js", () => ({
getCurrentUserHash: vi.fn(() => undefined),
tracer: { startSpan: testDoubles.startSpan },
}));
vi.mock("./metrics.js", () => ({
apiCalls: { add: testDoubles.apiCallsAdd },
apiDuration: { record: testDoubles.apiDurationRecord },
}));
vi.mock("@opentelemetry/api", () => ({
SpanStatusCode: { OK: 1, ERROR: 2 },
}));
describe("createNodeHevyClientOptions", () => {
beforeEach(() => {
vi.clearAllMocks();
delete process.env.HEVY_MCP_API_TIMEOUT;
vi.mocked(getCurrentUserHash).mockReturnValue(undefined);
});
it("records successful requests with bounded operational metadata", () => {
vi.mocked(getCurrentUserHash).mockReturnValue("user-123");
const options = createNodeHevyClientOptions();
options.onRequestComplete?.({
method: "GET",
endpoint: "/v1/user/info",
status: 200,
durationMs: 12,
});
expect(testDoubles.startSpan).toHaveBeenCalledWith("hevy.api.GET", {
attributes: {
"http.method": "GET",
"http.status_code": 200,
"hevy.api.endpoint": "/v1/user/info",
"user.hash": "user-123",
},
});
expect(testDoubles.span.setStatus).toHaveBeenCalledWith({ code: 1 });
expect(testDoubles.span.addEvent).not.toHaveBeenCalled();
expect(testDoubles.span.end).toHaveBeenCalledOnce();
expect(testDoubles.apiCallsAdd).toHaveBeenCalledWith(1, {
method: "GET",
endpoint: "/v1/user/info",
status_code: 200,
});
expect(testDoubles.apiDurationRecord).toHaveBeenCalledWith(12, {
method: "GET",
endpoint: "/v1/user/info",
});
});
it("never records raw request errors", () => {
const secret = "sentinel-client-observation";
const error = new HevyHttpError(secret, {
status: 503,
method: "GET",
endpoint: "/v1/user/info",
code: secret,
data: { secret },
cause: new Error(secret),
});
const options = createNodeHevyClientOptions();
options.onRequestComplete?.({
method: "GET",
endpoint: "/v1/user/info",
status: 503,
durationMs: 25,
error,
});
expect(testDoubles.span.setStatus).toHaveBeenCalledWith({ code: 2 });
expect(testDoubles.span.addEvent).toHaveBeenCalledWith("hevy.api.failure", {
"error.category": "HevyHttpError",
});
expect(JSON.stringify(testDoubles.span.addEvent.mock.calls)).not.toContain(
secret,
);
});
it("records allowlisted error codes and normalizes an absent status", () => {
const error = new HevyHttpError("private retry message", {
method: "GET",
endpoint: "/v1/user/info",
code: "HEVY_RETRY_EXHAUSTED",
});
const options = createNodeHevyClientOptions();
options.onRequestComplete?.({
method: "GET",
endpoint: "/v1/user/info",
status: 0,
durationMs: 25,
error,
});
expect(testDoubles.span.addEvent).toHaveBeenCalledWith("hevy.api.failure", {
"error.category": "HevyHttpError",
"error.code": "HEVY_RETRY_EXHAUSTED",
});
});
it("accepts only a positive finite timeout override", () => {
process.env.HEVY_MCP_API_TIMEOUT = "1500.9";
expect(createNodeHevyClientOptions().timeoutMs).toBe(1500);
process.env.HEVY_MCP_API_TIMEOUT = "invalid";
expect(createNodeHevyClientOptions().timeoutMs).toBeUndefined();
});
});