Testing API reference
Exports of @gramforge/testing. Back to the module
Functions
assertRoundtripOk
functionAssert a roundtrip decoded `ok` and reconstructed a payload deep-equal to `expected`. Throws a descriptive `Error` on any other decode outcome or a value mismatch.
<TPayload extends Record<string, unknown>>(roundtrip: CallbackRoundtrip<TPayload>, expected: TPayload): void
assertSurvivesBothPaths
functionAssert one payload survives the COMPACT path and another survives the SPILLOVER (pointer) path — both decoding `ok` and equal to their input. The caller supplies a small payload known to fit inline and a large payload known to overflow the 64-byte budget; this proves both codec branches are lossless. A mis-sized payload (compact that spills, or spillover that fits) throws, surfacing the test's wrong assumption rather than passing silently.
<TPayload extends Record<string, unknown>, TActions extends Record<string, AnyActionDef>>(namespace: CallbackNamespace<TActions>, actionKey: keyof TActions & string, payloads: { readonly compact: TPayload; readonly spillover: TPayload; }, deps: CallbackTestDeps): Promise<void>callbackWireFormat
functionClassify a `callback_data` string by its version prefix.
callbackWireFormat(callbackData: string): CallbackWireFormat
createCallbackTestDeps
functionBuild a deterministic {@link CallbackTestDeps}: fresh fake store + fixed secret + fixed clock + default TTL. Any field can be overridden.
createCallbackTestDeps(overrides?: Partial<CallbackTestDeps>): CallbackTestDeps
createFakePointerStore
functionCreate a fresh in-memory {@link FakePointerStore}.
createFakePointerStore(): FakePointerStore
describeDecodeResult
functionHuman-readable one-line description of any decode result (exhaustive).
<TPayload>(result: CallbackDecodeResult<TPayload>): string
fixedNow
functionA constant clock returning `ms` (defaults to {@link FIXED_NOW_MS}).
fixedNow(ms?: number): () => number
jsonDeepEqual
functionRecursive structural equality over JSON values (key-order-insensitive).
jsonDeepEqual(a: unknown, b: unknown): boolean
roundtripCallback
functionEncode `payload` for `actionKey` in `namespace`, then decode the resulting `callback_data` back — returning the wire string, the format it used, and the `CallbackDecodeResult` for assertion.
<TPayload extends Record<string, unknown>, TActions extends Record<string, AnyActionDef>>(namespace: CallbackNamespace<TActions>, actionKey: keyof TActions & string, payload: TPayload, deps: CallbackTestDeps): Promise<CallbackRoundtrip<TPayload>>
toDecodeDeps
functionDerive the decode-side deps (single-secret list) from the test deps.
toDecodeDeps(deps: CallbackTestDeps): CallbackDecodeDeps
snapshotEntities
functionSnapshot the DEFAULT `{ text, entities }` render of a message, with entities sorted into a stable order. Prefer this over snapshotting `toEntities` directly so snapshots don't churn on walk-order changes.
snapshotEntities(message: MessageText | CaptionText): EntitiesSnapshot
snapshotHtml
functionSnapshot the Telegram-HTML render of a message (a stable string).
snapshotHtml(message: MessageText | CaptionText): string
snapshotMarkdown
functionSnapshot the MarkdownV2 (fallback) render of a message (a stable string).
snapshotMarkdown(message: MessageText | CaptionText): string
assertUtf16Length
functionAssert the UTF-16 rendered length of `nodes` equals `expected`. Throws a descriptive `Error` on mismatch (framework-agnostic — no vitest import), so it works under any runner or a bare `node`.
assertUtf16Length(nodes: readonly RichTextNode[], expected: number): void
fixtureNodes
functionWrap a fixture's raw string in a single text node for renderer input.
fixtureNodes(fixture: UnicodeFixture): readonly RichTextNode[]
unicodeFixtures
functionThe Unicode fixture bank (function form, for parameterized-test ergonomics).
unicodeFixtures(): readonly UnicodeFixture[]
Types
CallbackTestDeps
typeThe combined encode-side deps a roundtrip needs. Structurally identical to core's `CallbackEncodeDeps`; {@link toDecodeDeps} derives the decode-side shape (a single-secret list) from it.
type CallbackTestDeps = CallbackEncodeDeps
CallbackWireFormat
typeWhich of the codec's two wire formats a `callback_data` string uses.
type CallbackWireFormat = "compact" | "pointer" | "unknown"
Interfaces
CallbackRoundtrip
interfaceThe full result of encoding then decoding a payload.
interface CallbackRoundtrip
callbackData
propertyThe `callback_data` string the encoder produced.
callbackData: string
wireFormat
propertyWhich wire format the encoder chose (compact vs pointer spillover).
wireFormat: CallbackWireFormat
result
propertyThe decode outcome to assert against.
result: CallbackDecodeResult<TPayload>
FakePointerStore
interfaceAn in-memory {@link PointerStore} with test-only inspection (`size`, `has`). Non-durable, non-evicting — the same caveats as core's in-memory store.
interface FakePointerStore
size
propertyNumber of envelopes currently held.
size: () => number
has
propertyWhether a token currently resolves.
has: (token: CallbackToken) => boolean
EntitiesSnapshot
interfaceA stable, JSON-serializable projection of `toEntities` output.
interface EntitiesSnapshot
text
propertytext: string
entities
propertyentities: readonly MessageEntity[]
UnicodeFixture
interfaceA single Unicode edge case with its documented lengths in both units.
interface UnicodeFixture
label
propertyShort stable identifier for the case.
label: string
text
propertyThe raw string under test.
text: string
utf16Length
property`text.length` — UTF-16 code units, the unit Telegram counts.
utf16Length: number
codePointLength
property`[...text].length` — Unicode code points (grapheme-ish, not clusters).
codePointLength: number
Constants
moduleId
constconst moduleId: "testing"
DEFAULT_CALLBACK_TTL_MS
constDefault pointer TTL used by {@link createCallbackTestDeps} (24h).
const DEFAULT_CALLBACK_TTL_MS: number
FIXED_HMAC_SECRET
constA fixed 32-byte HMAC secret — deterministic pointer signatures across runs.
const FIXED_HMAC_SECRET: Uint8Array<ArrayBufferLike>
FIXED_NOW_MS
constA fixed wall-clock instant (epoch ms) for reproducible TTL/expiry tests.
const FIXED_NOW_MS: number
UNICODE_FIXTURES
constA cross-section of Unicode length hazards: plain ASCII, BMP CJK, an astral emoji (surrogate pair), an emoji + skin-tone modifier, a ZWJ family sequence, decomposed combining marks, and a regional-indicator flag pair.
const UNICODE_FIXTURES: readonly UnicodeFixture[]