|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | + |
| 3 | +import { Configuration } from "../src/configuration/Configuration.js"; |
| 4 | +import { ExceptionlessClient } from "../src/ExceptionlessClient.js"; |
| 5 | +import { KnownEventDataKeys } from "../src/models/Event.js"; |
| 6 | + |
| 7 | +describe("documentation examples", () => { |
| 8 | + test("should configure production privacy and self-hosted endpoints", () => { |
| 9 | + const config = new Configuration(); |
| 10 | + |
| 11 | + config.apiKey = "UNIT_TEST_API_KEY"; |
| 12 | + config.serverUrl = "https://collector.example.com"; |
| 13 | + config.configServerUrl = "https://config.example.com"; |
| 14 | + config.heartbeatServerUrl = "https://heartbeat.example.com"; |
| 15 | + config.version = "1.2.3"; |
| 16 | + config.defaultTags.push("core", "production"); |
| 17 | + config.addDataExclusions("authorization", "cookie", "password", "secret", "set-cookie", "token"); |
| 18 | + config.includePrivateInformation = false; |
| 19 | + |
| 20 | + expect(config.isValid).toBe(true); |
| 21 | + expect(config.serverUrl).toBe("https://collector.example.com"); |
| 22 | + expect(config.configServerUrl).toBe("https://config.example.com"); |
| 23 | + expect(config.heartbeatServerUrl).toBe("https://heartbeat.example.com"); |
| 24 | + expect(config.version).toBe("1.2.3"); |
| 25 | + expect(config.defaultTags).toEqual(["core", "production"]); |
| 26 | + expect(config.dataExclusions).toEqual(["authorization", "cookie", "password", "secret", "set-cookie", "token"]); |
| 27 | + expect(config.includeCookies).toBe(false); |
| 28 | + expect(config.includeHeaders).toBe(false); |
| 29 | + expect(config.includeIpAddress).toBe(false); |
| 30 | + expect(config.includePostData).toBe(false); |
| 31 | + expect(config.includeQueryString).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + test("should build documented enriched exception events", () => { |
| 35 | + const client = new ExceptionlessClient(); |
| 36 | + client.config.apiKey = "UNIT_TEST_API_KEY"; |
| 37 | + client.config.addDataExclusions("creditCardNumber"); |
| 38 | + |
| 39 | + const error = new Error("Unable to create order from quote"); |
| 40 | + const builder = client |
| 41 | + .createException(error) |
| 42 | + .setReferenceId("order-12345678") |
| 43 | + .setProperty( |
| 44 | + "Order", |
| 45 | + { |
| 46 | + id: "order-123", |
| 47 | + quoteId: 123, |
| 48 | + creditCardNumber: "4111111111111111" |
| 49 | + }, |
| 50 | + 4, |
| 51 | + ["securityCode"] |
| 52 | + ) |
| 53 | + .setProperty("Quote", 123) |
| 54 | + .addTags("orders") |
| 55 | + .markAsCritical() |
| 56 | + .setGeo(43.595089, -88.444602) |
| 57 | + .setUserIdentity("user-123", "Jane Doe") |
| 58 | + .setUserDescription("jane@example.com", "The submit button returned a blank page."); |
| 59 | + |
| 60 | + expect(builder.target.type).toBe("error"); |
| 61 | + expect(builder.target.reference_id).toBe("order-12345678"); |
| 62 | + expect(builder.target.tags).toEqual(["orders", "Critical"]); |
| 63 | + expect(builder.target.geo).toBe("43.595089,-88.444602"); |
| 64 | + expect(builder.target.data?.Order).toEqual({ id: "order-123", quoteId: 123 }); |
| 65 | + expect(builder.target.data?.Quote).toBe(123); |
| 66 | + expect(builder.target.data?.[KnownEventDataKeys.UserInfo]).toEqual({ identity: "user-123", name: "Jane Doe" }); |
| 67 | + expect(builder.target.data?.[KnownEventDataKeys.UserDescription]).toEqual({ |
| 68 | + email_address: "jane@example.com", |
| 69 | + description: "The submit button returned a blank page." |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + test("should cancel events from documented runtime configuration plugins", async () => { |
| 74 | + const client = new ExceptionlessClient(); |
| 75 | + client.config.apiKey = "UNIT_TEST_API_KEY"; |
| 76 | + client.config.settings["enableCheckoutEvents"] = "false"; |
| 77 | + |
| 78 | + client.config.addPlugin("CheckoutEventToggle", 100, (context) => { |
| 79 | + const enabled = context.client.config.settings["enableCheckoutEvents"]; |
| 80 | + |
| 81 | + if (context.event.source === "checkout" && enabled === "false") { |
| 82 | + context.cancelled = true; |
| 83 | + } |
| 84 | + |
| 85 | + return Promise.resolve(); |
| 86 | + }); |
| 87 | + |
| 88 | + const context = await client.createLog("checkout", "Checkout opened", "info").submit(); |
| 89 | + |
| 90 | + expect(context.cancelled).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + test("should support documented session startup configuration", async () => { |
| 94 | + const client = new ExceptionlessClient(); |
| 95 | + |
| 96 | + await client.startup((config) => { |
| 97 | + config.apiKey = "UNIT_TEST_API_KEY"; |
| 98 | + config.setUserIdentity("user-123", "Jane Doe"); |
| 99 | + config.useSessions(true, 60000, true); |
| 100 | + config.updateSettingsWhenIdleInterval = -1; |
| 101 | + }); |
| 102 | + |
| 103 | + try { |
| 104 | + const context = await client.submitSessionStart(); |
| 105 | + |
| 106 | + expect(client.config.sessionsEnabled).toBe(true); |
| 107 | + expect(client.config.currentSessionIdentifier).toMatch(/^[0-9a-f]{32}$/); |
| 108 | + expect(context.event.reference_id).toBe(client.config.currentSessionIdentifier); |
| 109 | + } finally { |
| 110 | + await client.suspend(); |
| 111 | + } |
| 112 | + }); |
| 113 | +}); |
0 commit comments