testing/surface_invariants.ts

Surface invariant assertions for AppSurface data.

Two categories:

  • Structural — validate internal consistency (error schema presence, descriptions, duplicates, middleware propagation, every error naming a code, error code consistency). No options needed.
  • Policy — enforce security policy (sensitive routes rate-limited, no public mutations, mutation method conventions). Configurable with sensible defaults.

Structural invariants catch schema/surface generation bugs. Policy invariants catch security misconfigurations. Both propagate automatically to consumers via assert_surface_invariants.

view source

Declarations
#

25 declarations

assert_descriptions_present
#

assert_error_code_status_consistency
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_error_code_status_consistency} from '@fuzdev/fuz_app/testing/surface_invariants.js';

One error code at one HTTP status: the same code declared at two statuses across the surface's routes and middleware layers fails — e.g. invalid_credentials at both 401 and 403 would be a bug.

Reads each body's error const (what rest_error writes), through every branch of an anyOf or oneOf — the surface writes a status with several definitions as an anyOf — and nested unions within them. An error with no const (an enum, a bare string) is not read; assert_error_codes_specific refuses the bare string.

surface

returns

void

assert_error_codes_specific
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_error_codes_specific} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every error a route or middleware layer declares names its code exactly: each body at each status (each branch of an anyOf or oneOf) is an object whose error is a const or an enum — what a definition built by rest_error writes. A bare string error, or a body with no error, is a contract a client can't key on.

surface

returns

void

throws

  • AssertionError - naming the route or layer, the status, and the body

assert_error_codes_well_formed
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_error_codes_well_formed} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every declared error code is a code — REST_ERROR_CODE_PATTERN, a lowercase letter then lowercase letters, digits, and _ — never prose, which belongs in message. rest_error refuses one off the pattern; this holds a surface built another way (a snapshot, a hand-written layer) to the same rule.

surface

returns

void

throws

  • AssertionError - naming the route or layer, the status, and the code

assert_input_routes_declare_400
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_input_routes_declare_400} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every route with a body — a non-null input_schema on a method other than GET, which reads no body — declares a 400 naming invalid_request_body.

surface

returns

void

assert_keeper_routes_under_prefix
#

testing/surface_invariants.ts view source

(surface: AppSurface, prefixes?: string[]): void import {assert_keeper_routes_under_prefix} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Keeper-protected routes must be under expected path prefixes.

Catches keeper routes accidentally placed outside the API namespace (e.g., a keeper route at /health or /admin/ instead of /api/...).

surface

prefixes?

type string[]
optional default DEFAULT_KEEPER_ROUTE_PREFIXES

returns

void

assert_middleware_errors_propagated
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_middleware_errors_propagated} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every applicable middleware that declares errors must have those status codes present in the route's error_schemas.

Holds by construction for a surface generate_app_surface builds: each route's error map (resolve_route_error_map) already unions every applying layer's definitions. It earns its place only on a surface built another way — a committed snapshot read back, or one written by hand.

surface

returns

void

assert_mutation_routes_use_post
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_mutation_routes_use_post} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Routes with non-null input schemas should use POST (or other mutation methods), not GET.

GET routes with request bodies are technically allowed by HTTP but semantically suspicious — they bypass browser security assumptions about GET being idempotent. Query-string-driven filtering (audit log, list endpoints) should use params schemas or query string parsing, not input schemas.

Note: RPC endpoints (create_rpc_endpoint) use input: literal(null) on their route specs — the dispatcher handles body/query parsing internally. Real input schemas live in rpc_endpoints surface, not on routes; see assert_rpc_ws_surface_invariants for the parallel checks over RPC/WS method shapes.

surface

returns

void

assert_no_duplicate_routes
#

assert_no_testing_methods
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_no_testing_methods} from '@fuzdev/fuz_app/testing/surface_invariants.js';

No _testing_* backdoor action ever appears as a method on the declared surface (RPC or WS).

The test-backdoor actions (_testing_reset et al.) are daemon-token-gated privileged actions a consumer's test binary appends to its live RPC endpoint at assembly time — but they are deliberately excluded from surface generation (spine_rpc_endpoints and every consumer's create_*_app_surface_spec omit them) so the published attack surface, the committed *_attack_surface.json snapshot, and codegen never carry a backdoor. This invariant is the structural guard against a future wiring change that folds create_testing_actions(...) into the *declared* registry instead of the live-only append — the surface stays the authoritative "what the server exposes" map only if a backdoor can never hide in it.

Pairs with the wire-level negative-credential check (describe_testing_backdoor_cross_tests, cross-process) and the spec-level gate check: this one pins absence-from-surface, those pin the daemon-token gate and the 401/403 behavior.

surface

returns

void

throws

  • AssertionError - naming the offending endpoint + method.

assert_no_unexpected_public_mutations
#

testing/surface_invariants.ts view source

(surface: AppSurface, allowlist?: string[]): void import {assert_no_unexpected_public_mutations} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Public mutation routes (auth: none + is_mutation) must be in the allowlist.

Catches accidentally unprotected POST/PUT/DELETE routes. Routes like login and bootstrap are public mutations by design — they go in the allowlist.

surface

allowlist?

type string[]
optional default []

returns

void

assert_params_routes_declare_400
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_params_routes_declare_400} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every route with non-null params_schema declares a 400 naming invalid_route_params.

surface

returns

void

assert_protected_routes_declare_401
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_protected_routes_declare_401} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every protected route has 401 in error_schemas.

surface

returns

void

assert_query_routes_declare_400
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_query_routes_declare_400} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every route with non-null query_schema declares a 400 naming invalid_query_params.

surface

returns

void

assert_role_routes_declare_403
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_role_routes_declare_403} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every route with an authority gate that can answer 403 declares one — a role gate, or a credential-channel gate of any kind (keeper included).

The credential half is not just the keeper routes: the session-only gates point the same axis the other way and refuse with the same status, so a route carrying one and no 403 schema would have its denial body go unvalidated by the DEV-only error-schema check.

surface

returns

void

assert_rpc_method_descriptions_present
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_rpc_method_descriptions_present} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every RPC method on every endpoint has a non-empty description.

Parallel of assert_descriptions_present over surface.rpc_endpoints. Empty descriptions on RPC methods leak through library.json codegen and consumer-facing docs without the route-level check catching them.

surface

returns

void

assert_rpc_ws_surface_invariants
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_rpc_ws_surface_invariants} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Run all RPC / WS structural invariants. Options-free — applies universally to the surface.rpc_endpoints and surface.ws_endpoints slots produced by generate_app_surface.

Parallel of assert_surface_invariants for the non-REST surfaces. Within-endpoint duplicate method names and the auth-shape biconditional are already enforced at startup by compile_action_registry (see actions/CLAUDE.md §Registry compile) — these assertions cover only the contract-surface concerns that a runtime registration check cannot: empty descriptions, missing protocol-action spread on WS endpoints, kind ⇔ auth drift on WS methods, and a _testing_* backdoor action leaking onto the declared surface.

surface

returns

void

throws

  • AssertionError - on the first invariant violation; the message names the offending endpoint, method, and field.

assert_sensitive_routes_rate_limited
#

testing/surface_invariants.ts view source

(surface: AppSurface, sensitive_patterns?: (string | RegExp)[]): void import {assert_sensitive_routes_rate_limited} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Sensitive routes must declare rate limiting (rate_limit_key is non-null) or have 429 in their error schemas.

Matches routes against sensitive patterns and flags any that lack rate limit declarations. Catches forgotten rate limiting on credential-handling routes.

surface

sensitive_patterns?

type (string | RegExp)[]
optional default DEFAULT_SENSITIVE_PATTERNS

returns

void

assert_surface_invariants
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_surface_invariants} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Run all structural invariants. Options-free — applies universally.

Catches schema/surface generation bugs: missing 401/403/400 declarations, empty descriptions, duplicate routes, middleware-injected error codes unpropagated to routes (by construction on a generated surface, so a snapshot's check), an error that doesn't name its code or whose code is not one, and error codes appearing at multiple statuses.

surface

returns

void

throws

  • AssertionError - on the first invariant violation; the message names the offending route and the missing/inconsistent field.

assert_surface_security_policy
#

testing/surface_invariants.ts view source

(surface: AppSurface, options?: SurfaceSecurityPolicyOptions): void import {assert_surface_security_policy} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Run security policy invariants. Configurable with sensible defaults.

Checks:

  • Sensitive routes are rate-limited
  • No unexpected public mutation routes
  • Input schemas use mutation methods (not GET)
  • Keeper routes under expected prefixes

surface

options?

optional default {}

returns

void

throws

  • AssertionError - on the first policy violation; the message names the offending route.

assert_ws_endpoints_include_protocol_actions
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_ws_endpoints_include_protocol_actions} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Every WS endpoint's methods includes every protocol action method (heartbeat, cancel).

Consumers register WS endpoints by spreading protocol_actions from actions/protocol.ts before their own actions:

ws_endpoints: [{path: '/api/ws', actions: [...protocol_actions, ...consumer_actions], ...}]

Forgetting the spread compiles cleanly but breaks at runtime: client-side heartbeats and cancel notifications get method_not_found from the dispatcher, so disconnect detection silently regresses and per-request cancel never aborts the matching handler. Catch the mistake at the surface layer rather than at runtime.

surface

returns

void

assert_ws_method_descriptions_present
#

assert_ws_notifications_have_null_auth
#

testing/surface_invariants.ts view source

(surface: AppSurface): void import {assert_ws_notifications_have_null_auth} from '@fuzdev/fuz_app/testing/surface_invariants.js';

WS methods follow the kind ⇔ auth biconditional emitted by surface generation: kind === 'remote_notification' ⟺ auth === null.

generate_app_surface produces this shape directly from the action spec union (notifications carry auth: null per ActionSpecUnion; request_response carries a RouteAuth). The assertion guards against drift if a future surface emitter, transform, or test fixture violates it — and gives consumers a clear failure message when a hand-built surface mocks the shape incorrectly.

surface

returns

void

SurfaceSecurityPolicyOptions
#

testing/surface_invariants.ts view source

SurfaceSecurityPolicyOptions import type {SurfaceSecurityPolicyOptions} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Configuration for security policy invariants.

All fields have sensible defaults. Pass overrides for project-specific needs.

sensitive_route_patterns?

Path patterns for routes that should be rate-limited. Default: common sensitive REST patterns (login, password, bootstrap). account_token_create lives on the RPC surface; per-method RPC rate limiting is a separate invariant if consumers want it.

type (string | RegExp)[]

public_mutation_allowlist?

Routes explicitly allowed to be public mutations (e.g., webhooks, bootstrap). Format: 'METHOD /path' (e.g., 'POST /api/account/login').

type string[]

keeper_route_prefixes?

Allowed path prefixes for keeper-protected routes. Default: ['/api/']. Catches keeper routes outside expected namespaces.

type string[]

TESTING_METHOD_PREFIX
#

testing/surface_invariants.ts view source

"_testing_" import {TESTING_METHOD_PREFIX} from '@fuzdev/fuz_app/testing/surface_invariants.js';

Reserved method-name prefix for the daemon-token-gated test-backdoor actions (_testing_reset, _testing_mint_session, _testing_put_fact, _testing_drain_effects, _testing_schema_snapshot). Test binaries live-mount these on their RPC endpoint but they must never appear on a declared surface.

Depends on
#

Imported by
#