70 lines
3.3 KiB
TypeScript
70 lines
3.3 KiB
TypeScript
// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0
|
|
// The browser half of observability (plan item AT4.2): `instrumentation-client.ts` starts
|
|
// @conusai-sdk/observe only when the server wrote the settings into the document, with the options this
|
|
// app chose, and an error boundary reports through the same instance. The SDK is the real one with
|
|
// the recording transport of @conusai-sdk/observe-testing, so what is asserted is what would be sent.
|
|
|
|
import { createTestObserve } from "@conusai-sdk/observe-testing"
|
|
import { describe, expect, it, vi } from "vitest"
|
|
import {
|
|
browserObserveOptions,
|
|
observeMeta,
|
|
readBrowserSettings,
|
|
reportClientError,
|
|
startBrowserObserve,
|
|
} from "../apps/web/src/observe/browser.js"
|
|
|
|
/** A document with the meta element the server writes, as far as the module reads one. */
|
|
const documentWith = (attributes: Record<string, string> | undefined) => ({
|
|
querySelector: (selector: string) =>
|
|
attributes && selector === 'meta[name="app-observe"]'
|
|
? { getAttribute: (name: string) => attributes[name] ?? null }
|
|
: null,
|
|
})
|
|
|
|
describe("the settings the server writes into the document", () => {
|
|
it("round-trips through the meta element", () => {
|
|
const settings = { release: "2026.10.1", environment: "preview" }
|
|
expect(readBrowserSettings(documentWith(observeMeta(settings)) as never)).toEqual(settings)
|
|
})
|
|
|
|
it("reads nothing from a document without the element, or with it off", () => {
|
|
expect(readBrowserSettings(documentWith(undefined) as never)).toBeUndefined()
|
|
expect(readBrowserSettings(documentWith({ content: "off" }) as never)).toBeUndefined()
|
|
})
|
|
|
|
it("names neither ConusAI nor a build-time variable: the element is the app's own", () => {
|
|
expect(JSON.stringify(observeMeta({ release: "r", environment: "e" })).toLowerCase()).not.toContain("conusai")
|
|
})
|
|
})
|
|
|
|
describe("starting the browser SDK", () => {
|
|
it("starts nothing when the deployment has no observability", () => {
|
|
const start = vi.fn()
|
|
expect(startBrowserObserve(documentWith(undefined) as never, start)).toBeUndefined()
|
|
expect(start).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it("starts the SDK with the release and environment the server wrote, anonymously, errors and traces only", () => {
|
|
const start = vi.fn((options) => createTestObserve({ ...options, modules: { errors: false } }))
|
|
startBrowserObserve(documentWith(observeMeta({ release: "r-9", environment: "production" })) as never, start)
|
|
expect(start).toHaveBeenCalledWith(
|
|
expect.objectContaining({ release: "r-9", environment: "production", consent: "anonymous" }),
|
|
)
|
|
const options = start.mock.calls[0]?.[0]
|
|
expect(options?.modules).toEqual({ traces: true })
|
|
expect(options?.modules).not.toHaveProperty("analytics")
|
|
expect(options?.modules).not.toHaveProperty("replay")
|
|
expect(browserObserveOptions({ release: "r-9", environment: "production" })).toEqual(options)
|
|
})
|
|
|
|
it("sends an error a boundary caught, through the instance the page started", () => {
|
|
const instance = createTestObserve({ modules: { errors: false } })
|
|
startBrowserObserve(documentWith(observeMeta({ release: "r", environment: "e" })) as never, () => instance)
|
|
reportClientError(new Error("the page threw"))
|
|
expect(instance.mock.kinds()).toContain("error")
|
|
const [item] = instance.mock.items()
|
|
expect(JSON.stringify(item)).toContain("the page threw")
|
|
})
|
|
})
|