TSZ Test Suite
This directory contains the comprehensive automated test suite for Thyris Safe Zone (TSZ). It is structured to separate unit, integration, and end-to-end (E2E) concerns and to keep tests decoupled from production code.
Structure
tests/
unit/ # Pure unit tests (logic, helpers, AI client, SIEM, config, cache, etc.)
integration/ # HTTP-level integration tests (API + DB + Redis + AI boundary)
e2e/ # Smoke / sanity and gateway streaming tests
data/ # JSON fixtures for data-driven tests and golden files
README.md # This document
Test Coverage Overview
- Total Tests: 55+ tests (150% increase from original)
- Unit Tests: 40+ tests covering core business logic
- Integration Tests: 15+ tests covering API endpoints and error handling
- E2E Tests: 5 tests covering full system workflows
1. Unit tests (tests/unit)
Focus: fast, deterministic, no external dependencies.
Currently covered:
-
guardrails_test.go- Confidence and thresholds:
resolveAction(ALLOW / MASK / BLOCK decisions)- Allow/block thresholds from env (
CONFIDENCE_ALLOW_THRESHOLD,CONFIDENCE_BLOCK_THRESHOLD) - Category thresholds via
GetCategoryThreshold(e.g.CONFIDENCE_PII_THRESHOLD).
- Confidence engine:
ComputeConfidencefor variousConfidenceContextcombinations (blacklist hit, allowlist hit, PII/SECRET/INJECTION categories, REGEX/AI/SCHEMA sources).
- Utility helpers:
ApplyRegexHitWeight(per-hit weighting and clamping at 1.0).- Placeholder generation (
generatePlaceholder) – ensures RID and pattern name are present but does not leak raw PII.
- Format helpers:
isValidJSON,isValidXML,isValidSchemavia exported test helpers underinternal/guardrails/testing_exports.go(build-tagged fortestonly).
- Confidence and thresholds:
-
siem_ai_repository_test.go- SIEM webhook:
- Uses a fake
http.RoundTripperto assert thatpublishSecurityEventsends JSON to the URL fromSIEM_WEBHOOK_URLwith the expected payload.
- Uses a fake
- AI client:
CheckWithAIerror propagation when upstream returns non-200.CheckWithAIsuccess path when the upstream responds with aYES-like content.
- AI confidence cache:
- Basic cache roundtrip for
SetCachedConfidence/GetCachedConfidencewith a local Redis client.
- Basic cache roundtrip for
- SIEM webhook:
-
ai_provider_test.go(NEW)- AI provider initialization:
- Tests provider setup with invalid/empty configurations
- Provider state management (get/set operations)
- Hybrid confidence calculations:
- Edge cases for combining regex and AI confidence scores
- Boundary testing (zero values, high values, mixed scenarios)
- AI confidence caching:
- Cache key generation and validation
- Graceful handling when Redis is unavailable
- Cache operations with different label/text combinations
- AI provider initialization:
-
config_cache_test.go(NEW)- Configuration management:
- DSN and Redis URL generation from environment variables
- Config loading with custom environment settings
- Default value handling and environment variable precedence
- Cache operations:
- Pattern caching (set/get operations with graceful Redis handling)
- Allowlist/blocklist caching with data validation
- Cache clearing operations across different key types
- Panic recovery for unavailable cache backends
- Configuration management:
-
repository_test.go(NEW)- Repository function testing:
- Pattern retrieval with invalid IDs (graceful DB error handling)
- Pattern update operations with nil/invalid data
- Format validator CRUD operations
- Database connection failure scenarios with panic recovery
- Repository function testing:
Note: test-only helpers are defined in
internal/guardrails/testing_exports.go. These functions expose internal logic for unit testing while keeping production code encapsulated.
2. Integration tests (tests/integration)
Focus: real HTTP endpoints + DB + Redis + configuration, with external AI treated as a boundary (can be real or fake, depending on environment).
Currently covered:
-
detect_integration_test.go/detectendpoint:- PII detection with email (HTTP 200,
contains_pii=true, redaction applied, at least one detection). - Non-PII text (HTTP 200,
contains_pii=false, no detections). - Invalid JSON payload (client error like 400/422).
- PII detection with email (HTTP 200,
-
pii_matrix_integration_test.go- Data-driven PII detection using
tests/data/pii_cases.json:- Multiple cases for EMAIL, TCKN-like values, US_SSN, UK_NINO, mixed cases, and fully safe text.
- Asserts both
contains_piiand presence of expected detection types.
- Data-driven PII detection using
-
detect_golden_integration_test.go- Golden/snapshot test using
tests/data/detect_email_ssn_input.jsonandtests/data/detect_email_ssn_expect.json:- Verifies that a known input containing email + SSN produces detections for
EMAILandUS_SSN.
- Verifies that a known input containing email + SSN produces detections for
- Golden/snapshot test using
-
templates_integration_test.go/templates/import->/detectflow:- Imports a simple template with one pattern and one validator.
- Verifies that a
/detectcall after import triggers the new pattern.
-
gateway_integration_test.go/v1/chat/completionsnon-streaming:- Safe prompt: expects HTTP 200 and at least one
choicewhen upstream LLM is configured; otherwise logs and soft-fails. - Unsafe prompt with
X-TSZ-Guardrails: TOXIC_LANGUAGE:- If blocked, expects HTTP 400 + chat-completions provider-style
errorobject. - If not blocked, expects either
choicesorerrorin a valid JSON envelope.
- If blocked, expects HTTP 400 + chat-completions provider-style
- Safe prompt: expects HTTP 200 and at least one
-
mode_matrix_integration_test.go- Behavior under different configuration modes (documented behavior):
PII_MODEmatrix: verifies that PII is detected regardless of mode (MASK/BLOCK). Full behavioral checks for each mode should be exercised in dedicated environments.GATEWAY_BLOCK_MODEmatrix: verifies that gateway responses are valid JSON envelopes (eitherchoicesorerror) across different block modes (BLOCK,MASK,WARN).
- Behavior under different configuration modes (documented behavior):
-
error_handling_integration_test.go(NEW)- Comprehensive error handling scenarios:
/detectendpoint error cases (empty payload, missing fields, invalid modes, extremely long text)/v1/chat/completionserror cases (malformed requests, invalid headers, unknown models)- CRUD operation error handling (invalid regex patterns, non-existent resources)
- Concurrent request testing (multiple simultaneous API calls)
- Unicode and special character handling in requests
- Graceful degradation when upstream services are unavailable
- Comprehensive error handling scenarios:
These tests assume TSZ is running against a Postgres + Redis instance (wired by CI via source repository Actions services).
3. E2E / smoke and streaming tests (tests/e2e)
Focus: end-to-end system health and realistic user flows, including streaming behavior of the LLM gateway.
Currently covered:
-
sanity_suite_test.go- Health and readiness:
GET /healthz-> 200GET /ready-> 200 (DB + Redis ready)
- Configuration APIs:
GET /patternsGET /validatorsGET /allowlist
- Core flows:
/detectemail scenario (PII should be detected)./v1/chat/completionsbasic call (verifies response can be parsed as JSON, regardless of upstream model result).
- Health and readiness:
-
gateway_streaming_test.go- Streaming without guardrails:
stream=true, verifies SSE-likedata:chunks.- Soft-skips if upstream LLM is not configured in CI (non-200 status).
- Streaming with guardrails in
stream-syncfilter mode:X-TSZ-Guardrails: TOXIC_LANGUAGE,X-TSZ-Guardrails-Mode: stream-sync,OnFail=filter.- Ensures that a fake credit card number like
4111 1111 1111 1111does not appear in streamed content.
- Streaming with guardrails in
stream-synchalt mode:OnFail=halt.- Expects the stream to contain an error event or TSZ-specific metadata when unsafe content is encountered.
- Streaming without guardrails:
These tests exercise real network boundaries and are intended to run in CI after unit and integration tests have passed.
Running tests locally
Assuming you have Postgres + Redis and a TSZ instance running locally:
# Unit tests (do not require DB/Redis)
go test ./tests/unit/...
# Integration tests (require TSZ + DB + Redis)
export TSZ_BASE_URL=http://localhost:8080
# Optional (when AUTH_ENABLED=true on TSZ):
# export TSZ_TEST_BEARER_TOKEN=token_admin
# export TSZ_TEST_ADMIN_KEY=test-admin-key
go test ./tests/integration/...
# E2E smoke + streaming (require TSZ + DB + Redis)
export TSZ_BASE_URL=http://localhost:8080
# Optional (for CLI/client admin-key based commands):
# export TSZ_TEST_ADMIN_KEY=test-admin-key
# Optional (for raw HTTP E2E requests when auth is enabled):
# export TSZ_TEST_BEARER_TOKEN=token_admin
go test ./tests/e2e/...
Note: Some gateway tests depend on an upstream LLM being configured via
AI_MODEL_URL/AI_MODEL. If the upstream is not reachable, tests are designed to skip instead of hard-fail, to keep the suite robust across different environments.
Relation to examples/
The examples/ directory contains practical demonstration programs and integration examples:
examples/go-sdk-demo/: Shows how to use the Go client library (tszclient-go)examples/python-sdk-demo/: Demonstrates the Python client library usageexamples/go-managed model service-gateway/: managed model service integration exampleexamples/go-llm-safe-pipeline/: End-to-end LLM pipeline with guardrailsexamples/llm-redteam-playground-python/: Security testing and red team scenarios
The tests/ tree provides automated testing in standard go test format, while examples/ offers:
- Manual integration testing and exploration
- Real-world usage patterns and best practices
- Performance testing and load scenarios
- Educational resources for developers
For automated testing, use the tests/ directory. For learning and manual testing, explore the examples/ directory.