testing/audit_drift_guard.ts

view source

Declarations
#

6 declarations

assert_audit_event_metadata
#

testing/audit_drift_guard.ts view source

(event: Pick<{ id: Branded<string, "Uuid">; seq: number; event_type: string; outcome: "success" | "failure"; actor_id: Branded<string, "Uuid"> | null; account_id: Branded<string, "Uuid"> | null; ... 4 more ...; metadata: Record<...> | null; }, "event_type" | ... 1 more ... | "metadata">, options?: AssertAuditEventMetadataOptions | undefined): void import {assert_audit_event_metadata} from '@fuzdev/fuz_app/testing/audit_drift_guard.js';

Assert an audit row read off the wire carries the metadata its event type declares — parsed against audit_metadata_schemas[event_type] as declared, so a mistyped, null, or missing field fails, and with reject_unknown_keys so does a key the event doesn't declare. The wire schema (AuditLogEventJson.metadata) is a loose record, and only the TS spine validates metadata, at insert and fail-open, so this is the check that holds a spine's rows to their event's contract.

Builtin event types only: a consumer-registered type has no entry here, and passes unexamined.

event

an audit row from audit_log_list or an audit read like it

type Pick<AuditLogEventJson, "event_type" | "outcome" | "metadata">

options?

reject_unknown_keys for the closed-world check

optional

returns

void

throws

  • AssertionError - naming the event type and the schema issues

assert_no_audit_drift
#

testing/audit_drift_guard.ts view source

(): void import {assert_no_audit_drift} from '@fuzdev/fuz_app/testing/audit_drift_guard.js';

Assert the process-wide audit counters are zero — the check install_audit_drift_guard runs after each test. Exported for a test that manages the counters itself.

returns

void

throws

  • AssertionError - naming the first counter that is not zero

AssertAuditEventMetadataOptions
#

testing/audit_drift_guard.ts view source

AssertAuditEventMetadataOptions import type {AssertAuditEventMetadataOptions} from '@fuzdev/fuz_app/testing/audit_drift_guard.js';

reject_unknown_keys?

Refuse metadata keys the event type doesn't declare. Off by default: the builtin schemas admit consumer-added fields. For a suite whose backend adds none — a spine's own — so an undeclared key on any spine fails.

type boolean

readonly

create_recording_audit_emitter
#

testing/audit_drift_guard.ts view source

(calls_ref?: AuditLogInput<"login" | "logout" | "bootstrap" | "signup" | "password_change" | "session_revoke" | "session_revoke_all" | "token_create" | "token_revoke" | "token_revoke_all" | "role_grant_create" | ... 16 more ... | "db_admin_row_delete">[] | undefined): RecordingAuditEmitter import {create_recording_audit_emitter} from '@fuzdev/fuz_app/testing/audit_drift_guard.js';

Build a no-op AuditEmitter that records every emit, emit_pool, and emit_role_grant_target call into calls as an AuditLogInput. Use to capture audit metadata shapes in unit tests (e.g. password change failure outcome, role-grant create denial) without standing up a database and the query_audit_log pipeline.

Capture scope — all four production fan-out shapes. emit_role_grant_target mirrors create_audit_emitter's lift logic in place — actor_id / account_id / ip are populated from auth + ctx and the event_type / outcome / target_*_id / metadata fields forward from the input envelope. Tests asserting on role-grant-shape emissions read out of the same homogeneous calls array. notify is a no-op; add_listener records into a local array that listener_count reports, and returns a remover with the production semantics (per registration, idempotent) — registered listeners never fire, since this emitter captures emit shapes, not fan-out. drain_inflight resolves at once and is_tracking_inflight answers false: a recorded call writes nothing, so nothing is ever in flight.

emit AND emit_pool both append to calls, so a test reads either entry point's writes off the same array.

Pass calls_ref to write into a caller-owned array (callers that declared const events: Array<AuditLogInput> = [] and want to keep the reference). Omit to let the helper allocate a fresh array and return it on the calls field of the result.

The returned emitter is deliberately NOT frozen — slots stay mutable so a test can override one when it needs bespoke shape (e.g. an emit_pool that throws on the first call). The production create_audit_emitter freeze invariant exists to catch the patch_audit_emit_capture hot-patch footgun against the closure-captured emit; the recording emitter has no inner closure, so the freeze isn't load-bearing here.

calls_ref?

type AuditLogInput<"login" | "logout" | "bootstrap" | "signup" | "password_change" | "session_revoke" | "session_revoke_all" | "token_create" | "token_revoke" | "token_revoke_all" | "role_grant_create" | "role_grant_revoke" | "role_grant_offer_create" | "role_grant_offer_accept" | "role_grant_offer_decline" | "role_grant_offer_retract" | "role_grant_offer_expire" | "role_grant_offer_supersede" | "invite_create" | "invite_delete" | "account_delete" | "account_purge" | "account_undelete" | "actor_delete" | "actor_purge" | "actor_undelete" | "app_settings_update" | "db_admin_row_delete">[]
optional

returns

RecordingAuditEmitter

install_audit_drift_guard
#

testing/audit_drift_guard.ts view source

(): void import {install_audit_drift_guard} from '@fuzdev/fuz_app/testing/audit_drift_guard.js';

Register per-test beforeEach + afterEach hooks that catch any audit emission with a metadata shape that fails its audit_metadata_schemas entry, an event_type not present in the active AuditLogConfig, or a row the emitter failed to write.

The production validation in query_audit_log is fail-open — it bumps process-wide counters and proceeds, so a regression that emits an undeclared metadata field or a typo'd event-type lands a row that passes downstream queries but breaks forensics. The emitter's pool write is fail-open too: a refused INSERT is logged and counted, the request succeeds, and the row is gone. Tests that exercise audit emits should fail loudly when either happens.

Call at the top of every describe / describe_db block that fires audit writes through deps.audit.emit. Resets counters before each test and asserts zero on completion. A test that makes a write fail on purpose calls reset_audit_write_failures (auth/audit_emitter.ts) once it has asserted the count.

Pair with await_pending_effects: true (the default for create_test_app) so fire-and-forget audit writes have completed by the time the after-each check observes counter state.

returns

void

RecordingAuditEmitter
#

testing/audit_drift_guard.ts view source

RecordingAuditEmitter import type {RecordingAuditEmitter} from '@fuzdev/fuz_app/testing/audit_drift_guard.js';

Pair returned by create_recording_audit_emitter — the AuditEmitter to inject as deps.audit, plus the shared calls array that records every captured emission. Both fields are live — callers read calls after exercising the handler to assert on the audit metadata shape.

emitter

type AuditEmitter

calls

type AuditLogInput<"login" | "logout" | "bootstrap" | "signup" | "password_change" | "session_revoke" | "session_revoke_all" | "token_create" | "token_revoke" | "token_revoke_all" | "role_grant_create" | "role_grant_revoke" | "role_grant_offer_create" | "role_grant_offer_accept" | "role_grant_offer_decline" | "role_grant_offer_retract" | "role_grant_offer_expire" | "role_grant_offer_supersede" | "invite_create" | "invite_delete" | "account_delete" | "account_purge" | "account_undelete" | "actor_delete" | "actor_purge" | "actor_undelete" | "app_settings_update" | "db_admin_row_delete">[]

Depends on
#

Imported by
#