forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry-client.test.ts
More file actions
289 lines (237 loc) · 10.8 KB
/
Copy pathtelemetry-client.test.ts
File metadata and controls
289 lines (237 loc) · 10.8 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import type { MockInstance } from "vitest";
import lambdaClient from "./lambda-client";
import { TelemetryClient, isTelemetryDisabled } from "./telemetry-client";
// Module mock so constructing TelemetryClient doesn't spin up segment's
// internal flush queue. Class-based (not vi.fn) so `vi.restoreAllMocks()`
// between tests doesn't wipe the `track` binding on subsequent `new
// Analytics(...)` calls.
const { segmentTrackMock } = vi.hoisted(() => ({
segmentTrackMock: vi.fn(),
}));
vi.mock("@segment/analytics-node", () => ({
Analytics: class {
track = segmentTrackMock;
},
}));
describe("v1 TelemetryClient", () => {
let lambdaSpy: MockInstance<typeof lambdaClient.send>;
beforeEach(() => {
lambdaSpy = vi.spyOn(lambdaClient, "send").mockResolvedValue(undefined);
segmentTrackMock.mockReset();
});
afterEach(() => {
vi.restoreAllMocks();
});
function makeClient(
overrides: Partial<ConstructorParameters<typeof TelemetryClient>[0]> = {},
): TelemetryClient {
return new TelemetryClient({
packageName: "@copilotkit/shared",
packageVersion: "1.0.0",
sampleRate: 1,
...overrides,
});
}
function jwtWith(payload: Record<string, unknown>): string {
const b64 = Buffer.from(JSON.stringify(payload)).toString("base64url");
return `header.${b64}.sig`;
}
const baseInstanceEvent = {
actionsAmount: 0,
endpointsAmount: 0,
endpointTypes: [],
"cloud.api_key_provided": false,
} as const;
test("capture sends to both sinks when sampled in (anonymous, one decision gates both)", async () => {
vi.spyOn(Math, "random").mockReturnValue(0);
const client = makeClient({ sampleRate: 0.05 });
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).toHaveBeenCalledTimes(1);
expect(segmentTrackMock).toHaveBeenCalledTimes(1);
expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
event: "oss.runtime.instance_created",
});
});
test("capture skips both sinks when anonymous and sampled out", async () => {
// Math.random=0.99 vs sampleRate=0.05 — anonymous caller is gated out;
// neither sink should fire under the new one-decision-both-sinks model.
vi.spyOn(Math, "random").mockReturnValue(0.99);
const client = makeClient({ sampleRate: 0.05 });
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).not.toHaveBeenCalled();
expect(segmentTrackMock).not.toHaveBeenCalled();
});
test("identified callers bypass the sample gate (lambda + segment fire even when Math.random would fail)", async () => {
vi.spyOn(Math, "random").mockReturnValue(0.99);
const client = makeClient({ sampleRate: 0.05 });
client.setLicenseToken(jwtWith({ telemetry_id: "abc-123" }));
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).toHaveBeenCalledTimes(1);
expect(segmentTrackMock).toHaveBeenCalledTimes(1);
});
test("identified callers send to both sinks on every capture", async () => {
const client = makeClient({ sampleRate: 0.05 });
client.setLicenseToken(jwtWith({ telemetry_id: "abc-123" }));
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).toHaveBeenCalledTimes(2);
expect(segmentTrackMock).toHaveBeenCalledTimes(2);
});
test("capture short-circuits both sinks when telemetryDisabled is true", async () => {
const client = makeClient({ telemetryDisabled: true });
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).not.toHaveBeenCalled();
expect(segmentTrackMock).not.toHaveBeenCalled();
});
test("setLicenseToken forwards the token in subsequent capture", async () => {
const token = jwtWith({ telemetry_id: "abc-123" });
const client = makeClient();
client.setLicenseToken(token);
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).toHaveBeenCalledTimes(1);
expect(lambdaSpy.mock.calls[0][0].licenseToken).toBe(token);
});
test("capture sends licenseToken=undefined when setLicenseToken was never called", async () => {
vi.spyOn(Math, "random").mockReturnValue(0);
const client = makeClient();
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy.mock.calls[0][0].licenseToken).toBeUndefined();
});
test("setLicenseToken warns once when the token has no telemetry_id", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
const client = makeClient();
client.setLicenseToken(jwtWith({ license_id: "x" }));
expect(warn).toHaveBeenCalledTimes(1);
expect(warn.mock.calls[0][0]).toMatch(/telemetry_id/);
});
test("setLicenseToken does not warn when the token carries telemetry_id", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
const client = makeClient();
client.setLicenseToken(jwtWith({ telemetry_id: "abc-123" }));
expect(warn).not.toHaveBeenCalled();
});
test("identified events carry sampleWeight=1 (anonymous events carry 1/sampleRate)", async () => {
// Anonymous: sampleWeight should be 1 / sampleRate so downstream
// weight-based extrapolation reconstructs true volume.
// Identified: bypassing the gate means each event represents itself,
// so sampleWeight must be 1 — not the population's 1/sampleRate.
vi.spyOn(Math, "random").mockReturnValue(0);
const anonClient = makeClient({ sampleRate: 0.05 });
await anonClient.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
sampleRate: 0.05,
sampleWeight: 20,
});
expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
properties: expect.objectContaining({
sampleRate: 0.05,
sampleWeight: 20,
}),
});
lambdaSpy.mockClear();
segmentTrackMock.mockReset();
const idClient = makeClient({ sampleRate: 0.05 });
idClient.setLicenseToken(jwtWith({ telemetry_id: "abc-123" }));
await idClient.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
sampleRate: 1,
sampleWeight: 1,
});
expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
properties: expect.objectContaining({ sampleRate: 1, sampleWeight: 1 }),
});
});
test("malformed license token stays anonymous and remains sample-gated", async () => {
// parseTelemetryIdFromLicense returns null for any of: empty token,
// wrong-shape (not three dot-separated segments), base64/JSON parse
// failure. A misconfigured customer must not flip to identified-bypass.
vi.spyOn(Math, "random").mockReturnValue(0.99);
vi.spyOn(console, "warn").mockImplementation(() => undefined);
const client = makeClient({ sampleRate: 0.05 });
client.setLicenseToken("not-a-jwt");
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).not.toHaveBeenCalled();
expect(segmentTrackMock).not.toHaveBeenCalled();
});
test("setLicenseToken cache is overwritable (good token replaced by bad → back to anonymous gate)", async () => {
// Pins the cache as last-write-wins so a refactor to first-write-wins
// (e.g. `this.telemetryId ??= parseAndWarnTelemetryId(...)`) doesn't
// leak identified-bypass status across license replacements.
vi.spyOn(Math, "random").mockReturnValue(0.99);
vi.spyOn(console, "warn").mockImplementation(() => undefined);
const client = makeClient({ sampleRate: 0.05 });
client.setLicenseToken(jwtWith({ telemetry_id: "abc-123" }));
client.setLicenseToken(jwtWith({ license_id: "no-telemetry-id" }));
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
expect(lambdaSpy).not.toHaveBeenCalled();
expect(segmentTrackMock).not.toHaveBeenCalled();
});
test("setCloudConfiguration writes cloud keys into globalProperties for both sinks", async () => {
vi.spyOn(Math, "random").mockReturnValue(0);
const client = makeClient({ sampleRate: 1 });
client.setCloudConfiguration({
publicApiKey: "ck_live_test.secret",
baseUrl: "https://api.cloud.copilotkit.ai",
});
await client.capture("oss.runtime.instance_created", baseInstanceEvent);
// v1 still ships cloud.publicApiKey through globalProperties — the
// lambda-client.send sanitization strips it at the wire (covered in
// lambda-client.test.ts), and Segment retains it intentionally for
// existing CopilotCloud user analytics.
expect(lambdaSpy.mock.calls[0][0].globalProperties).toMatchObject({
"cloud.publicApiKey": "ck_live_test.secret",
"cloud.baseUrl": "https://api.cloud.copilotkit.ai",
});
expect(segmentTrackMock.mock.calls[0][0]).toMatchObject({
properties: expect.objectContaining({
"cloud.publicApiKey": "ck_live_test.secret",
"cloud.baseUrl": "https://api.cloud.copilotkit.ai",
}),
});
});
test("constructor rejects sampleRate outside [0, 1]", () => {
expect(() => makeClient({ sampleRate: 1.5 })).toThrow(
"Sample rate must be between 0 and 1",
);
expect(() => makeClient({ sampleRate: -0.1 })).toThrow(
"Sample rate must be between 0 and 1",
);
});
test("constructor rejects NaN sampleRate from a malformed env override", () => {
// parseFloat('nonsense') = NaN; without the explicit guard, NaN slips
// past the range check (all NaN comparisons are false) and produces a
// silent always-drop. Guard the validator with Number.isNaN.
const original = process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE;
process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE = "not-a-number";
try {
expect(() => makeClient()).toThrow("Sample rate must be between 0 and 1");
} finally {
if (original === undefined) {
delete process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE;
} else {
process.env.COPILOTKIT_TELEMETRY_SAMPLE_RATE = original;
}
}
});
});
describe("isTelemetryDisabled", () => {
const originalEnv = { ...process.env };
afterEach(() => {
process.env = { ...originalEnv };
});
test.each([
["COPILOTKIT_TELEMETRY_DISABLED", "true"],
["COPILOTKIT_TELEMETRY_DISABLED", "1"],
["DO_NOT_TRACK", "true"],
["DO_NOT_TRACK", "1"],
])("returns true when %s=%s", (key, val) => {
process.env[key] = val;
expect(isTelemetryDisabled()).toBe(true);
});
test("returns false when no opt-out env var is set", () => {
delete process.env.COPILOTKIT_TELEMETRY_DISABLED;
delete process.env.DO_NOT_TRACK;
expect(isTelemetryDisabled()).toBe(false);
});
});