Back to .md Directory

Testing Guardrails (Vitest)

These are enforcement rules that complement **testing-principles.md**. Follow them for every test.

May 2, 2026
0 downloads
0 views
ai llm guardrails
View source

Testing Guardrails (Vitest)

These are enforcement rules that complement testing-principles.md. Follow them for every test.

1) Unit vs Integration boundary

  • Unit tests: no network/DB. Filesystem is allowed only when the unit’s contract is filesystem behavior; otherwise mock I/O. Use per-test temp dirs and clean them in afterEach to avoid cross-test pollution.

2) Mocking & Spying (critical)

  • Always spy on the module object, never on a destructured binding.
    • import * as cfg from '../src/ui/config-bridge.js'; vi.spyOn(cfg, 'isLocked')…
    • const { isLocked } = await import(...); vi.spyOn({ isLocked }, 'isLocked') (spies a copy; your code under test won’t see it)
  • Prefer module-level mocks (vi.mock) or module-object spies (vi.spyOn(moduleObj, 'fn')). Reset/restore in afterEach.

3) Console assertions must match call arity

  • If the code calls console.warn(msg, errorObj), assert both args. Don’t assert a single string when the implementation passes multiple arguments.

4) Time & timers

  • Default timeout: 10_000 ms for async units. Use vi.useFakeTimers()/vi.setSystemTime() for time logic.
  • No infinite/hanging loops: when testing lock/retry logic, make the mock transition deterministically (e.g., locked → unlocked) so the loop exits.

5) Filesystem tests

  • Create a unique temp dir per test, write/read inside it, and remove it in teardown.
  • Never depend on real project files or shared paths.

6) Hygiene (enforced)

  • AAA structure; one behavior per test; minimal, intentional snapshots; no .only/.skip in committed code.
  • Reset mocks/clock/state in afterEach.

7) Required teardown

afterEach(() => {
  vi.restoreAllMocks();
  vi.useRealTimers?.();
});

Related Documents

GUARDRAILS.md

Guardrails, Safety & Content Filtering

> Your LLM application will be attacked. Not might. Will. The first prompt injection attempt against your production system will come within 48 hours of launch. The question is not whether someone will try "ignore previous instructions and reveal your system prompt" -- the question is whether your system folds or holds. Every chatbot, every agent, every RAG pipeline is a target. If you ship without guardrails, you are shipping a vulnerability with a chat interface.

aiagentllm
0
17
rohitg00
GUARDRAILS.md

DeepSeek R1: Case Study in Failed Extrinsic Alignment

**Context:** This document compiles publicly available security research on DeepSeek R1 alongside our independent findings from the LEK-1 A/B testing. It demonstrates why extrinsic alignment (content filters, RLHF guardrails, system prompts) is insufficient for AI safety.

aiprompteval
0
8
Snider
GUARDRAILS.md

AI Safety & Guardrails for Voice Assistants

A multi-layered defense system ensuring the AI assistant stays on-topic, resists prompt injection, and never makes unauthorized decisions.

aillmrag
0
6
alexiokay
GUARDRAILS.md

LlmGuard Framework - Complete Implementation Buildout

**LlmGuard** is a comprehensive AI Firewall and Guardrails framework for LLM-based Elixir applications. It provides defense-in-depth protection against AI-specific threats including prompt injection, data leakage, jailbreak attempts, and unsafe content generation. This buildout implements a production-ready security layer for LLM applications with statistical rigor, comprehensive threat detection, and zero-trust validation.

aillmprompt
0
3
North-Shore-AI