testing/assertions.ts

Assertion helpers for auth attack surface testing.

Plain functions called inside explicit test() blocks to verify surface snapshots, public routes, and middleware stacks.

view source

Declarations
#

8 declarations

assert_error_response_declared
#

testing/assertions.ts view source

(errors: RouteErrorMap, jsonrpc: boolean, response: Response, label: string): Promise<void> import {assert_error_response_declared} from '@fuzdev/fuz_app/testing/assertions.js';

Assert that a response is not an error its route leaves undeclared: a JSON body at an error status must be at a status the route's error map declares and parse against it, by the rule the DEV error check runs (check_route_error — a 500 internal_error and a 503 service_unavailable always accepted, a JSON-RPC error envelope on a jsonrpc route not a REST error body). A success status or a non-JSON response passes.

errors

the route's error map

jsonrpc

whether the route answers JSON-RPC envelopes (RouteSpec.jsonrpc)

type boolean

response

the response, read through a clone

type Response

label

what names the request in a failure

type string

returns

Promise<void>

throws

  • Error - on a JSON error the route's map doesn't declare

assert_error_schema_valid
#

testing/assertions.ts view source

(lookup: ReadonlyMap<string, RouteErrorMap>, route: AppSurfaceRoute, status: number, body: unknown): void import {assert_error_schema_valid} from '@fuzdev/fuz_app/testing/assertions.js';

Assert that a route declares an error — its status, and its error code at that status — and that the body parses against the route's schema there. The schema at a status is a union of its definitions' bodies, each error a literal of its code for a definition made by rest_error (to_error_schemas), so a code the route doesn't declare at the status fails the parse.

Protected routes should always have auto-derived error schemas (401 for authenticated, 403 for role-restricted). A missing schema indicates a gap in error schema derivation.

lookup

map from "METHOD /path" to each route's error map (AppSurfaceSpec.route_error_maps)

type ReadonlyMap<string, RouteErrorMap>

route

the surface route to validate against

status

expected HTTP status code

type number

body

the parsed response body to validate

type unknown

returns

void

throws

  • AssertionError - if no schema is declared for the route+status pair.
  • Error - if the body does not satisfy the declared schema, a code the route doesn't declare at the status included, its message carrying the failed parse's issues (`parse`).

assert_full_middleware_stack
#

testing/assertions.ts view source

(surface: AppSurface, path_prefix: string, expected_middleware: string[]): void import {assert_full_middleware_stack} from '@fuzdev/fuz_app/testing/assertions.js';

Verify every route under a path prefix has the exact expected middleware stack, less the layers that carve it out.

A route's expected stack is expected_middleware in order, without each name whose surface layers carve the route out: a layer of that name whose path applies to the route and one of whose carve_outs does too (middleware_carves_out), with no other layer of the name running on it (middleware_spec_applies) — the rule the surface's applicable_middleware and the route error map read. Anything else is held exactly: a route missing a name no carve-out explains fails, and so does a carved-out route still listing the name.

surface

the app surface to check

path_prefix

prefix to filter routes (e.g. '/api/')

type string

expected_middleware

the full middleware names in order

type string[]

returns

void

throws

  • AssertionError - if no routes match `path_prefix`, or if any matching route's `applicable_middleware` does not deep-equal its expected stack

assert_only_expected_public_routes
#

testing/assertions.ts view source

(surface: AppSurface, expected_public: string[]): void import {assert_only_expected_public_routes} from '@fuzdev/fuz_app/testing/assertions.js';

Bidirectional check: no unexpected public routes, no missing expected ones.

surface

the app surface to check

expected_public

format: ['GET /health', 'POST /api/account/login']

type string[]

returns

void

throws

  • AssertionError - if the live surface has public routes not in `expected_public`, or if any entry in `expected_public` is missing from the live surface.

assert_surface_deterministic
#

testing/assertions.ts view source

(build_surface: () => AppSurface): void import {assert_surface_deterministic} from '@fuzdev/fuz_app/testing/assertions.js';

Verify surface generation is deterministic (build twice, compare).

build_surface

type () => AppSurface

returns

void

assert_surface_matches_snapshot
#

testing/assertions.ts view source

(surface: AppSurface, snapshot_path: string): void import {assert_surface_matches_snapshot} from '@fuzdev/fuz_app/testing/assertions.js';

Compare live surface against a committed snapshot JSON file.

Failure message instructs the developer to run gro gen to update the snapshot — every fuz_app consumer wires the snapshot through a *.gen.ts file so regeneration goes through the same pipeline as the rest of the generated artifacts.

surface

the live surface to check

snapshot_path

absolute path to the committed JSON snapshot

type string

returns

void

throws

  • AssertionError - if the live surface does not deep-equal the snapshot, or `Error` if the snapshot file is unreadable / malformed JSON.

get_route_error_schema
#

testing/assertions.ts view source

(lookup: ReadonlyMap<string, RouteErrorMap>, route: AppSurfaceRoute, status: number): AnySchema | undefined import {get_route_error_schema} from '@fuzdev/fuz_app/testing/assertions.js';

Look up the error schema for a route+status in its error map.

lookup

map from "METHOD /path" to each route's error map (AppSurfaceSpec.route_error_maps)

type ReadonlyMap<string, RouteErrorMap>

route

the surface route to look up

status

HTTP status code

type number

returns

AnySchema | undefined

the @fuzdev/fuz_schema schema for that route+status, or undefined when no error schema is declared for the status code

resolve_fixture_path
#

testing/assertions.ts view source

(filename: string, import_meta_url: string): string import {resolve_fixture_path} from '@fuzdev/fuz_app/testing/assertions.js';

Resolve an absolute path relative to the caller's module.

filename

type string

import_meta_url

the caller's import.meta.url

type string

returns

string

Depends on
#

Imported by
#