Files
template-showcase/test/instrumentation-client.test.ts
T

54 lines
2.9 KiB
TypeScript

import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
// @conusai-sdk/observe's replay module is off unless modules.replay is `true` or a
// ReplayOptions object (sdks/node/packages/observe/src/index.ts). The default mode,
// 'onError', only uploads a replay when an error is captured, so a plain showcase visit
// produced nothing on Analytics > Session Replays (showcase plan D22). The shipped
// instrumentation-client.ts must enable replay in a mode that records from page start.
//
// mode: 'always' alone is not enough (showcase plan D24): replay is a 'consented' item
// (core/consent.ts verdict()), and the default consent mode 'anonymous' never sends
// consented items, so the recorder never starts regardless of mode. Verified live on
// stage 2026-10-11: 'always' with no consent option produced zero /api/_conusai/envelope
// requests in 40+ seconds of interaction. The shipped file must also opt out of consent.
//
// @conusai-sdk/observe enables only `errors` by default (sdks/node/packages/observe/src/index.ts):
// every other module — analytics, vitals, traces, replay, logs — is off unless named truthy under
// `modules`. The showcase exists to exercise every SDK module, so this test fails whenever the
// shipped instrumentation-client.ts leaves one off or sets it to a bare `false` (showcase plan D25).
describe('instrumentation-client.ts', () => {
const source = readFileSync(new URL('../instrumentation-client.ts', import.meta.url), 'utf8')
// A tiny parser rather than one regex per module: a module switch can be `true` or an options
// object (replay's `{ mode: 'always' }`), and a regex permissive enough for both shapes would
// also match a module spelled out in a comment, which defeats the point of this test.
function moduleSwitches(src: string): Record<string, unknown> {
const match = src.match(/modules:\s*{([\s\S]*?)}\s*,?\s*}\)/)
const body = match?.[1]
if (!body) throw new Error('no modules: {...} block found in instrumentation-client.ts')
const result: Record<string, unknown> = {}
for (const entry of body.matchAll(/(\w+):\s*(true|false|\{[^}]*\})/g)) {
const name = entry[1]
const value = entry[2]
if (!name || !value) continue
result[name] = value === 'true' ? true : value === 'false' ? false : value
}
return result
}
it.each(['analytics', 'vitals', 'traces', 'replay', 'logs'])('enables the %s module', (name) => {
const switches = moduleSwitches(source)
expect(switches, `modules.${name} is missing`).toHaveProperty(name)
expect(switches[name], `modules.${name} must not be false`).not.toBe(false)
})
it('enables replay in a mode that records every visit, not only on error', () => {
expect(source).toMatch(/modules:\s*{[\s\S]*?replay:\s*{\s*mode:\s*['"]always['"]/)
})
it('opts consent out, so replay (a consented item) is actually allowed to send', () => {
expect(source).toMatch(/consent:\s*['"]opt-out['"]/)
})
})