/** * Assertion library with semantic naming. * * Philosophy: * - `assert()` — for validating external input / user data * - `invariant()` — for internal logic that should never fail * - Type guards — for runtime type narrowing * * All throw on failure. No-op in production correctness checks — if an * invariant is violated, the program is broken and should crash loudly. */ import { dbg } from "./dbg.ts"; // ── Core Assertions ────────────────────────────────────────────────────── /** * Assert a condition is true. Use for validating input, preconditions. * * assert(userId.length > 0, "userId must be non-empty"); */ export function assert(condition: boolean, message = "assertion failed"): asserts condition { if (!condition) { dbg(`ASSERT: ${message}`); throw new Error(`[assert] ${message}`); } } /** * Assert an invariant. Use for internal logic that should NEVER fail. * If this throws, it's a bug in the code, not bad user input. * * invariant(curr !== null, "cursor should be positioned"); */ export function invariant(condition: boolean, message = "invariant violated"): asserts condition { if (!condition) { dbg(`INVARIANT: ${message}`); throw new Error(`[invariant] ${message}`); } } // ── Equality Assertions ────────────────────────────────────────────────── /** * Assert two values are equal. * * assertEq(actual, expected, "IDs should match"); */ export function assertEq(actual: T, expected: T, message = ""): void { if (actual !== expected) { const msg = message || `expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`; dbg(`ASSERT_EQ: ${msg}`); throw new Error(`[assert] ${msg}`); } } /** * Assert two values are not equal. * * assertNeq(id, "", "ID must not be empty"); */ export function assertNeq(actual: T, expected: T, message = ""): void { if (actual === expected) { const msg = message || `expected NOT ${JSON.stringify(expected)}, but got it`; dbg(`ASSERT_NEQ: ${msg}`); throw new Error(`[assert] ${msg}`); } } // ── Type Guards ────────────────────────────────────────────────────────── /** Assert value is a non-empty string. */ export function assertString(val: unknown, message = "expected string"): asserts val is string { if (typeof val !== "string" || val.length === 0) { dbg(`ASSERT_STRING: ${message} (got ${typeof val}: "${String(val).slice(0, 20)}")`); throw new Error(`[assert] ${message}`); } } /** Assert value is a string. */ export function assertDefinedString(val: unknown, message = "expected defined string"): asserts val is string { if (typeof val !== "string") { dbg(`ASSERT_STRING: ${message} (got ${typeof val})`); throw new Error(`[assert] ${message}`); } } /** Assert value is a number. */ export function assertNumber(val: unknown, message = "expected number"): asserts val is number { if (typeof val !== "number" || Number.isNaN(val)) { dbg(`ASSERT_NUMBER: ${message} (got ${typeof val}: ${val})`); throw new Error(`[assert] ${message}`); } } /** Assert value is an array. */ export function assertArray(val: unknown, message = "expected array"): asserts val is unknown[] { if (!Array.isArray(val)) { dbg(`ASSERT_ARRAY: ${message} (got ${typeof val})`); throw new Error(`[assert] ${message}`); } } // ── Object Assertions ──────────────────────────────────────────────────── /** Assert object has required keys. */ export function assertHasKeys( val: unknown, keys: readonly (keyof T)[], message?: string ): asserts val is T { assert(typeof val === "object" && val !== null, message ?? "expected object"); const obj = val as Record; for (const key of keys) { if (!(key in obj)) { throw new Error(`[assert] missing key "${String(key)}"${message ? `: ${message}` : ""}`); } } }