Gramforge

Testing API reference

Exports of @gramforge/testing. Back to the module

Functions

assertRoundtripOk

function

Assert 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

function

Assert 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

function

Classify a `callback_data` string by its version prefix.

callbackWireFormat(callbackData: string): CallbackWireFormat

createCallbackTestDeps

function

Build a deterministic {@link CallbackTestDeps}: fresh fake store + fixed secret + fixed clock + default TTL. Any field can be overridden.

createCallbackTestDeps(overrides?: Partial<CallbackTestDeps>): CallbackTestDeps

createFakePointerStore

function

Create a fresh in-memory {@link FakePointerStore}.

createFakePointerStore(): FakePointerStore

describeDecodeResult

function

Human-readable one-line description of any decode result (exhaustive).

<TPayload>(result: CallbackDecodeResult<TPayload>): string

fixedNow

function

A constant clock returning `ms` (defaults to {@link FIXED_NOW_MS}).

fixedNow(ms?: number): () => number

jsonDeepEqual

function

Recursive structural equality over JSON values (key-order-insensitive).

jsonDeepEqual(a: unknown, b: unknown): boolean

roundtripCallback

function

Encode `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

function

Derive the decode-side deps (single-secret list) from the test deps.

toDecodeDeps(deps: CallbackTestDeps): CallbackDecodeDeps

snapshotEntities

function

Snapshot 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

function

Snapshot the Telegram-HTML render of a message (a stable string).

snapshotHtml(message: MessageText | CaptionText): string

snapshotMarkdown

function

Snapshot the MarkdownV2 (fallback) render of a message (a stable string).

snapshotMarkdown(message: MessageText | CaptionText): string

assertUtf16Length

function

Assert 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

function

Wrap a fixture's raw string in a single text node for renderer input.

fixtureNodes(fixture: UnicodeFixture): readonly RichTextNode[]

unicodeFixtures

function

The Unicode fixture bank (function form, for parameterized-test ergonomics).

unicodeFixtures(): readonly UnicodeFixture[]

Types

CallbackTestDeps

type

The 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

type

Which of the codec's two wire formats a `callback_data` string uses.

type CallbackWireFormat = "compact" | "pointer" | "unknown"

Interfaces

CallbackRoundtrip

interface

The full result of encoding then decoding a payload.

interface CallbackRoundtrip

callbackData

property

The `callback_data` string the encoder produced.

callbackData: string

wireFormat

property

Which wire format the encoder chose (compact vs pointer spillover).

wireFormat: CallbackWireFormat

result

property

The decode outcome to assert against.

result: CallbackDecodeResult<TPayload>

FakePointerStore

interface

An 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

property

Number of envelopes currently held.

size: () => number

has

property

Whether a token currently resolves.

has: (token: CallbackToken) => boolean

EntitiesSnapshot

interface

A stable, JSON-serializable projection of `toEntities` output.

interface EntitiesSnapshot

text

property
text: string

entities

property
entities: readonly MessageEntity[]

UnicodeFixture

interface

A single Unicode edge case with its documented lengths in both units.

interface UnicodeFixture

label

property

Short stable identifier for the case.

label: string

text

property

The 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

const
const moduleId: "testing"

DEFAULT_CALLBACK_TTL_MS

const

Default pointer TTL used by {@link createCallbackTestDeps} (24h).

const DEFAULT_CALLBACK_TTL_MS: number

FIXED_HMAC_SECRET

const

A fixed 32-byte HMAC secret — deterministic pointer signatures across runs.

const FIXED_HMAC_SECRET: Uint8Array<ArrayBufferLike>

FIXED_NOW_MS

const

A fixed wall-clock instant (epoch ms) for reproducible TTL/expiry tests.

const FIXED_NOW_MS: number

UNICODE_FIXTURES

const

A 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[]