Skip to content

Commit f95acdf

Browse files
committed
fix: update captureException tests for appVersion parameter
1 parent 3f00be7 commit f95acdf

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

packages/telemetry/src/__tests__/PostHogTelemetryClient.test.ts

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -416,38 +416,28 @@ describe("PostHogTelemetryClient", () => {
416416
})
417417
})
418418

419-
describe("captureException", () => {
420-
it("should not capture exceptions when telemetry is disabled", () => {
421-
const client = new PostHogTelemetryClient()
422-
client.updateTelemetryState(false)
423-
424-
const error = new Error("Test error")
425-
client.captureException(error)
426-
427-
expect(mockPostHogClient.captureException).not.toHaveBeenCalled()
428-
})
429-
430-
it("should capture exceptions when telemetry is enabled", () => {
419+
describe("captureException error filtering", () => {
420+
it("should filter out 429 rate limit errors (via status property)", () => {
431421
const client = new PostHogTelemetryClient()
432422
client.updateTelemetryState(true)
433423

434-
const error = new Error("Test error")
435-
client.captureException(error, { provider: "TestProvider" })
424+
// Create an error with status property (like OpenAI SDK errors)
425+
const error = Object.assign(new Error("Rate limit exceeded"), { status: 429 })
426+
client.captureException(error, "1.0.0")
436427

437-
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
438-
provider: "TestProvider",
439-
})
428+
// Should NOT capture 429 errors
429+
expect(mockPostHogClient.captureException).not.toHaveBeenCalled()
440430
})
441431

442-
it("should filter out 429 rate limit errors (via status property)", () => {
432+
it("should filter out 402 billing errors (via status property)", () => {
443433
const client = new PostHogTelemetryClient()
444434
client.updateTelemetryState(true)
445435

446-
// Create an error with status property (like OpenAI SDK errors)
447-
const error = Object.assign(new Error("Rate limit exceeded"), { status: 429 })
448-
client.captureException(error)
436+
// Create an error with status 402 (Payment Required)
437+
const error = Object.assign(new Error("Payment required"), { status: 402 })
438+
client.captureException(error, "1.0.0")
449439

450-
// Should NOT capture 429 errors
440+
// Should NOT capture 402 errors
451441
expect(mockPostHogClient.captureException).not.toHaveBeenCalled()
452442
})
453443

@@ -456,7 +446,7 @@ describe("PostHogTelemetryClient", () => {
456446
client.updateTelemetryState(true)
457447

458448
const error = new Error("429 Rate limit exceeded: free-models-per-day")
459-
client.captureException(error)
449+
client.captureException(error, "1.0.0")
460450

461451
// Should NOT capture errors with 429 in message
462452
expect(mockPostHogClient.captureException).not.toHaveBeenCalled()
@@ -467,30 +457,34 @@ describe("PostHogTelemetryClient", () => {
467457
client.updateTelemetryState(true)
468458

469459
const error = new Error("Request failed due to Rate Limit")
470-
client.captureException(error)
460+
client.captureException(error, "1.0.0")
471461

472462
// Should NOT capture rate limit errors
473463
expect(mockPostHogClient.captureException).not.toHaveBeenCalled()
474464
})
475465

476-
it("should capture non-rate-limit errors", () => {
466+
it("should capture non-rate-limit errors with app version", () => {
477467
const client = new PostHogTelemetryClient()
478468
client.updateTelemetryState(true)
479469

480470
const error = new Error("Internal server error")
481-
client.captureException(error)
471+
client.captureException(error, "1.0.0")
482472

483-
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", undefined)
473+
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
474+
$app_version: "1.0.0",
475+
})
484476
})
485477

486-
it("should capture errors with non-429 status codes", () => {
478+
it("should capture errors with non-429 status codes with app version", () => {
487479
const client = new PostHogTelemetryClient()
488480
client.updateTelemetryState(true)
489481

490482
const error = Object.assign(new Error("Internal server error"), { status: 500 })
491-
client.captureException(error)
483+
client.captureException(error, "1.0.0")
492484

493-
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", undefined)
485+
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
486+
$app_version: "1.0.0",
487+
})
494488
})
495489

496490
it("should use nested error message from OpenAI SDK error structure for filtering", () => {
@@ -505,7 +499,7 @@ describe("PostHogTelemetryClient", () => {
505499
metadata: { raw: "Rate limit exceeded: free-models-per-day" },
506500
},
507501
})
508-
client.captureException(error)
502+
client.captureException(error, "1.0.0")
509503

510504
// Should NOT capture - the nested metadata.raw contains rate limit message
511505
expect(mockPostHogClient.captureException).not.toHaveBeenCalled()
@@ -524,11 +518,13 @@ describe("PostHogTelemetryClient", () => {
524518
},
525519
})
526520

527-
client.captureException(error)
521+
client.captureException(error, "1.0.0")
528522

529523
// Verify error message was modified to use metadata.raw (highest priority)
530524
expect(error.message).toBe("Upstream provider error: model overloaded")
531-
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", undefined)
525+
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
526+
$app_version: "1.0.0",
527+
})
532528
})
533529

534530
it("should modify error.message with nested error.message when metadata.raw is not available", () => {
@@ -543,11 +539,13 @@ describe("PostHogTelemetryClient", () => {
543539
},
544540
})
545541

546-
client.captureException(error)
542+
client.captureException(error, "1.0.0")
547543

548544
// Verify error message was modified to use nested error.message
549545
expect(error.message).toBe("Upstream provider: connection timeout")
550-
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", undefined)
546+
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
547+
$app_version: "1.0.0",
548+
})
551549
})
552550

553551
it("should use primary message when no nested error structure exists", () => {
@@ -559,25 +557,28 @@ describe("PostHogTelemetryClient", () => {
559557
status: 500,
560558
})
561559

562-
client.captureException(error)
560+
client.captureException(error, "1.0.0")
563561

564562
// Verify error message remains the primary message
565563
expect(error.message).toBe("Primary error message")
566-
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", undefined)
564+
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
565+
$app_version: "1.0.0",
566+
})
567567
})
568568

569-
it("should auto-extract properties from ApiProviderError", () => {
569+
it("should auto-extract properties from ApiProviderError with app version", () => {
570570
const client = new PostHogTelemetryClient()
571571
client.updateTelemetryState(true)
572572

573573
const error = new ApiProviderError("Test error", "OpenRouter", "gpt-4", "createMessage", 500)
574-
client.captureException(error)
574+
client.captureException(error, "1.0.0")
575575

576576
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
577577
provider: "OpenRouter",
578578
modelId: "gpt-4",
579579
operation: "createMessage",
580580
errorCode: 500,
581+
$app_version: "1.0.0",
581582
})
582583
})
583584

@@ -586,12 +587,13 @@ describe("PostHogTelemetryClient", () => {
586587
client.updateTelemetryState(true)
587588

588589
const error = new ApiProviderError("Test error", "OpenRouter", "gpt-4", "completePrompt")
589-
client.captureException(error)
590+
client.captureException(error, "1.0.0")
590591

591592
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
592593
provider: "OpenRouter",
593594
modelId: "gpt-4",
594595
operation: "completePrompt",
596+
$app_version: "1.0.0",
595597
})
596598
})
597599

@@ -600,13 +602,14 @@ describe("PostHogTelemetryClient", () => {
600602
client.updateTelemetryState(true)
601603

602604
const error = new ApiProviderError("Test error", "OpenRouter", "gpt-4", "createMessage")
603-
client.captureException(error, { customProperty: "value" })
605+
client.captureException(error, "1.0.0", { customProperty: "value" })
604606

605607
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
606608
provider: "OpenRouter",
607609
modelId: "gpt-4",
608610
operation: "createMessage",
609611
customProperty: "value",
612+
$app_version: "1.0.0",
610613
})
611614
})
612615

@@ -616,12 +619,13 @@ describe("PostHogTelemetryClient", () => {
616619

617620
const error = new ApiProviderError("Test error", "OpenRouter", "gpt-4", "createMessage")
618621
// Explicitly override the provider value
619-
client.captureException(error, { provider: "OverriddenProvider" })
622+
client.captureException(error, "1.0.0", { provider: "OverriddenProvider" })
620623

621624
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
622625
provider: "OverriddenProvider", // additionalProperties takes precedence
623626
modelId: "gpt-4",
624627
operation: "createMessage",
628+
$app_version: "1.0.0",
625629
})
626630
})
627631

@@ -630,11 +634,12 @@ describe("PostHogTelemetryClient", () => {
630634
client.updateTelemetryState(true)
631635

632636
const error = new Error("Regular error")
633-
client.captureException(error, { customProperty: "value" })
637+
client.captureException(error, "1.0.0", { customProperty: "value" })
634638

635639
// Should only have the additionalProperties, not any auto-extracted ones
636640
expect(mockPostHogClient.captureException).toHaveBeenCalledWith(error, "test-machine-id", {
637641
customProperty: "value",
642+
$app_version: "1.0.0",
638643
})
639644
})
640645
})

0 commit comments

Comments
 (0)