ContextUnity Testsπ
This file is the canonical source for test placement, markers, invocation, and gate routing. Test-specific AGENTS.md files may add owner-local invariants, but must not redefine the repository taxonomy or gate commands.
Default Workflowπ
Run tests from the repository root. Start with the smallest target that can fail the changed behavior, then widen only to the lanes required by the boundary.
UV_CACHE_DIR=/tmp/contextunity-uv-cache \
uv run --all-packages pytest <nodeid-or-path> -q --tb=line- Prefer nodeid, then file, then owner directory.
- Use
-q --tb=line; do not start with-v,-vv, or-s. - Pytest ignores local
.envfiles by default. A lane that intentionally uses operator configuration setsCU_TEST_ALLOW_DOTENV=1. - On failure, rerun the same target once with
--lf -q --tb=short. - Use
--strict-markersfor owner and integration lanes. - A required lane that skips is not green.
- Run
python3 scripts/test_gate.py pre-pushfor the final ordinary gate. - Run full owner suites, the final ordinary gate, and named heavy or live gates with
ctx_shell(timeout_ms=3600000, run_in_background=true). Pollctx_shell(background_action="status", job_id=...)until completion and record the final exit code. Detached or compressed output is not a result. - For a lane expected to exceed one hour, use a durable runner that retains the log and exit status; inspect both before reporting completion.
Test Taxonomyπ
| Location | Owner and boundary | Required markers |
|---|---|---|
packages/core/tests/ | Core unit, parser, pricing, SDK, manifest, and deterministic component behavior | unit where applicable |
packages/cli/tests/ | CLI parsing, commands, local composition, and deterministic process adapters | owner convention |
packages/django/tests/ | Django SDK client, middleware, and typing integration | owner convention |
services/<service>/tests/ | Service-owned unit and deterministic component behavior | owner convention |
services/forge/tests/dashboard/ | Dashboard and Brain-data projections | owner convention |
tests/contracts/ | Alias/dataflow guards, schema/metadata/wheel contracts, and gate contracts (structural/dependency bans live in .acdd/lint/rules.yaml) | contract |
tests/integration_inproc/ | Real grpc.aio localhost servers, production servicers/interceptors, generated clients, and SDK round-trips | integration, inproc |
tests/integration/cli/ | Real CLI processes and service lifecycle | integration |
tests/integration/brain/postgres/ | Brain PostgreSQL, RLS, migrations, concurrency, restart, and durable parity | integration, integration_live |
tests/integration/router/ and root Router integration modules | Redis persistence, registration, and process-backed Router behavior | integration; add integration_live for external state and slow for explicit operator/provider evaluations |
tests/integration/shield/ | Shield subprocess or external-state behavior | integration; add integration_live for external state |
tests/integration/worker/ | Worker process, Temporal, Docker, or runtime behavior | integration; add integration_live for external state |
tests/tooling/ | Repository test tooling and split-parity utilities | contract where static-only |
services/brain/benchmarks/ | Explicit performance contracts | slow or owner marker |
Keep a test with the narrowest production owner. Do not move a test into an integration directory merely because it has several collaborators. Split mixed unit/transport/live modules at the behavioral seam before moving them.
Marker Semanticsπ
| Marker | Meaning | Ordinary pre-push |
|---|---|---|
unit | Fast isolated behavior | included |
contract | Fast deterministic static/runtime contract without real service TCP | included |
ordinary | Hermetic ordinary test | included |
integration, inproc | Real in-process transport | excluded; run explicitly |
integration | Cross-process or external integration | excluded; run explicitly |
integration_live | Real PostgreSQL, Redis, Temporal, Docker, or external state | excluded; named gate only |
slow | Timing/performance contract or explicit operator/provider evaluation | excluded; run explicitly |
e2e | Full user/system flow | explicit only |
flaky | Quarantined instability requiring investigation | never evidence of completion |
Evidence Ladderπ
| Lane | Use when behavior depends on | Required proof |
|---|---|---|
| Focused | One changed seam | Success, rejection, gate-off, and regression behavior |
| Owner component | Shared owner fixtures, storage, config, or composition | Owner suite without external infrastructure |
| In-process transport | Serialization, generated clients, interceptors, auth, or RPC wiring | Real localhost server and public client |
| Process integration | CLI, subprocess, filesystem lifecycle, or multi-process behavior | Real production entrypoint and cleanup |
| Live resource | PostgreSQL, Redis, Temporal, migrations, RLS, transactions, restart, or backend parity | Named fail-closed resource gate |
| Release | Stable intended diff | Outer pre-push-mode gate exit code plus any required named lanes |
SQLite or fake-backed success is not PostgreSQL/Redis/Temporal parity. A metadata-only test is not transport proof.
Ordinary And Staged Gatesπ
scripts/test_gate.py pre-push runs every lightweight owner (core, cli, django, brain, forge, router, shield, worker, root) serially excluding integration, integration_live, and slow, then runs ACDD workspace gates (acdd build, acdd validate, acdd docs check, acdd lint --categories arch,docs) when acdd is installed (fail-closed when CU_REQUIRE_ACDD=1). Resource paths launch Docker only when CU_REQUIRE_LIVE=1; ordinary push remains infrastructure-free. A manual clean-worktree invocation runs the all-lightweight baseline.
Test Authoring And Architecture Guardsπ
Load test-suite-rules before authoring, modifying, or refactoring tests:
- Assert observable behavior through real production callers and stores; isolate only external boundaries and clean up temporary resources (
BEFORE == AFTER). - Put static dependency, cycle, single-owner, and AST pattern boundaries in
.acdd/lint/rules.yaml+.acdd/lint/zones.yaml(or inline```acdd-ruleblocks indocs/architecture/*.md), not Python regex/grep test scripts. - Reserve
tests/contracts/and owner unit guards strictly for checks requiring Python AST dataflow/alias tracking, call cardinality, Pydantic/manifest introspection, lockfiles, deployment YAML, or runtime imports.
Pre-commit uses scripts/test_gate.py staged: it runs lint, staged-owner lightweight tests, and type checking for every staged Python file. Pulumi files use their own project environment and BasedPyright configuration. Unknown or shared staged paths and malformed or unavailable ordinary push ranges fall back to all lightweight owners. Required-live pre-push fails on an unavailable range.
Canonical entrypoints:
python3 scripts/test_gate.py ordinary
python3 scripts/test_gate.py pre-push
CU_REQUIRE_LIVE=1 python3 scripts/test_gate.py pre-push
python3 scripts/test_gate.py ordinary --keep-goingOwner Commandsπ
These commands are complete owner-verification suites and may be broader than the canonical ordinary marker expression. Use test_gate.py ordinary or the Git hooks when the lightweight ordinary boundary is required.
# Core
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest packages/core/tests -q --tb=line --strict-markers
# CLI
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest packages/cli/tests -q --tb=line --strict-markers
# Brain, without a live DSN
BRAIN_TEST_DSN= CU_DISABLE_AUTO_BRAIN_TEST_DSN=1 UV_CACHE_DIR=/tmp/contextunity-uv-cache \
uv run --all-packages pytest services/brain/tests -m 'not integration_live' -q --tb=line --strict-markers
# Forge lightweight only
BRAIN_TEST_DSN= CU_DISABLE_AUTO_BRAIN_TEST_DSN=1 UV_CACHE_DIR=/tmp/contextunity-uv-cache \
uv run --all-packages pytest services/forge/tests \
-m 'not integration and not integration_live and not slow' \
-q --tb=line --strict-markers
# Router lightweight only
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest services/router/tests \
--ignore=services/router/tests/integration \
-m 'not integration and not integration_live and not slow' -q --tb=line --strict-markers --timeout=60
# Shield and Worker
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest services/shield/tests \
-m 'not slow' -q --tb=line --strict-markers
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest services/worker/tests -q --tb=line --strict-markers
# Root in-process transport
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest tests/integration_inproc \
-m 'integration and inproc' -q --tb=line --strict-markers
# Root process integration without live resources
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest tests/integration \
-m 'integration and not integration_live' -q --tb=line --strict-markers
# Root contract, architecture, and tooling guards
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest tests \
--ignore=tests/integration --ignore=tests/integration_inproc \
-m 'not integration and not integration_live and not slow' -q --tb=line --strict-markers
# ACDD workspace validation and architectural/docs lint
acdd build && acdd validate && acdd docs check && acdd lint --categories arch,docsExtension repositories (contextunity-ext-commerce, contextunity-ext-kb) own their own TESTS.md and CI lanes in their respective Git repositories. Package-specific AGENTS.md files own only narrower fixtures and invariants.
Named Heavy And Live Gatesπ
| Boundary | Command | Automatic pre-push selection |
|---|---|---|
| Brain PostgreSQL/RLS/migrations | python3 scripts/test_gate.py live brain | matching paths only with CU_REQUIRE_LIVE=1 |
| Router Redis | python3 scripts/test_gate.py live router | matching paths only with CU_REQUIRE_LIVE=1; required selection is integration_live and not slow |
| Worker live runtime | CU_COMPOSE_E2E=1 CU_PLATFORM_SECRET=... python3 scripts/test_gate.py live worker | explicit only |
| Worker Temporal | owner Temporal integration lane | explicit only |
| CLI process | tests/integration/cli/ | explicit only |
With CU_REQUIRE_LIVE=1, pre-push provisions only resources matched by the changed paths.
The Brain gate uses BRAIN_TEST_DSN or its owned host/network-aware setup. Required gates fail when infrastructure is absent or when required tests skip.
Optional Router provider/evaluation modules carry integration, integration_live, and slow. They are excluded from the required Redis lane, so they are deselected when operator credentials are absent. Run them only with their explicit opt-in requirements:
TOON_EVAL_LIVE=1 TOON_EVAL_MODEL=openai/gpt-5.6-luna CU_TEST_ALLOW_DOTENV=1 \
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest \
tests/integration/router/test_toon_evaluation_live.py \
-m 'integration_live and slow' -q --tb=line --strict-markers
CU_COMPOSE_E2E=1 CU_CP3_REAL_LLM=1 CU_PLATFORM_SECRET="$CU_PLATFORM_SECRET" \
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest \
tests/integration/router/test_live_router_auto_extract.py \
-m 'integration_live and slow' -q --tb=line --strict-markers
CU_ENGRAM_REAL_LLM=1 CU_TEST_ALLOW_DOTENV=1 \
CU_ROUTER_MEMORY_ENGRAM_CONSOLIDATION_ENABLED=1 \
UV_CACHE_DIR=/tmp/contextunity-uv-cache uv run --all-packages pytest \
tests/integration/router/test_engram_consolidation_live.py \
-m 'integration_live and slow' -q --tb=line --strict-markersLive Infrastructure Rulesπ
- Ordinary pytest must not probe, discover, or synthesize live endpoints.
POSTGRES_DSNis a Brain runtime setting, not a test selector.- Brain live tests use
--brain-test-dsnorBRAIN_TEST_DSN. - Root pytest defaults
REDIS_ENABLED=0; enable Redis only in its named lane. - Mesh discovery occurs at service startup, not during config parsing.
- Keep external credentials and DSNs out of logs and committed files.
- Use canonical disposable-schema or isolated-resource helpers; do not create a second lifecycle pattern.
PostgreSQL Catalog Regenerationπ
Before treating unset BRAIN_TEST_DSN as a blocker, check Docker and permitted native shell access; an MCP-shell denial does not imply native-shell denial. With artifact writes and test-resource provisioning authorized, reuse the isolated PG16 helper in uv run --all-packages python:
from scripts.test_gate_live import postgres_context
from services.brain.scripts.generate_postgres_v1_catalog import generate
with postgres_context() as dsn:
generate(dsn)The helper owns startup and cleanup. Generation requires admin privileges and changes cluster-wide roles: never use production or an unapproved shared server. Keep the DSN out of logs and committed files. Then verify:
uv run --all-packages pytest services/brain/tests/test_postgres_catalog_artifact.py services/brain/tests/test_postgres_catalog_generator.py -q --tb=line --strict-markersRun python3 scripts/test_gate.py live brain for live proof; it provisions PostgreSQL but does not regenerate the artifact or export a DSN to your shell. Keep stale-artifact checks enabled. Report concrete permission, bootstrap, or scope blockers explicitly.
Supporting Proceduresπ
docs/testing/test-failure-recovery.md: diagnose and recover from failures.docs/testing/independent-review.md: independent review requirements.docs/testing/type-checking.md: test typing procedure.docs/architecture/testing-standards.md: assertion quality and architectural standards. These documents may explain methodology or implementation, butTESTS.mdalone owns taxonomy, placement, markers, and gate selection.