Home/Docs/Roadmap & Testing/Testing Standards

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 .env files by default. A lane that intentionally uses operator configuration sets CU_TEST_ALLOW_DOTENV=1.
  • On failure, rerun the same target once with --lf -q --tb=short.
  • Use --strict-markers for owner and integration lanes.
  • A required lane that skips is not green.
  • Run python3 scripts/test_gate.py pre-push for 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). Poll ctx_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πŸ”—

LocationOwner and boundaryRequired markers
packages/core/tests/Core unit, parser, pricing, SDK, manifest, and deterministic component behaviorunit where applicable
packages/cli/tests/CLI parsing, commands, local composition, and deterministic process adaptersowner convention
packages/django/tests/Django SDK client, middleware, and typing integrationowner convention
services/<service>/tests/Service-owned unit and deterministic component behaviorowner convention
services/forge/tests/dashboard/Dashboard and Brain-data projectionsowner 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-tripsintegration, inproc
tests/integration/cli/Real CLI processes and service lifecycleintegration
tests/integration/brain/postgres/Brain PostgreSQL, RLS, migrations, concurrency, restart, and durable parityintegration, integration_live
tests/integration/router/ and root Router integration modulesRedis persistence, registration, and process-backed Router behaviorintegration; add integration_live for external state and slow for explicit operator/provider evaluations
tests/integration/shield/Shield subprocess or external-state behaviorintegration; add integration_live for external state
tests/integration/worker/Worker process, Temporal, Docker, or runtime behaviorintegration; add integration_live for external state
tests/tooling/Repository test tooling and split-parity utilitiescontract where static-only
services/brain/benchmarks/Explicit performance contractsslow 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πŸ”—

MarkerMeaningOrdinary pre-push
unitFast isolated behaviorincluded
contractFast deterministic static/runtime contract without real service TCPincluded
ordinaryHermetic ordinary testincluded
integration, inprocReal in-process transportexcluded; run explicitly
integrationCross-process or external integrationexcluded; run explicitly
integration_liveReal PostgreSQL, Redis, Temporal, Docker, or external stateexcluded; named gate only
slowTiming/performance contract or explicit operator/provider evaluationexcluded; run explicitly
e2eFull user/system flowexplicit only
flakyQuarantined instability requiring investigationnever evidence of completion

Evidence LadderπŸ”—

LaneUse when behavior depends onRequired proof
FocusedOne changed seamSuccess, rejection, gate-off, and regression behavior
Owner componentShared owner fixtures, storage, config, or compositionOwner suite without external infrastructure
In-process transportSerialization, generated clients, interceptors, auth, or RPC wiringReal localhost server and public client
Process integrationCLI, subprocess, filesystem lifecycle, or multi-process behaviorReal production entrypoint and cleanup
Live resourcePostgreSQL, Redis, Temporal, migrations, RLS, transactions, restart, or backend parityNamed fail-closed resource gate
ReleaseStable intended diffOuter 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-rule blocks in docs/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-going

Owner 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,docs

Extension 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πŸ”—

BoundaryCommandAutomatic pre-push selection
Brain PostgreSQL/RLS/migrationspython3 scripts/test_gate.py live brainmatching paths only with CU_REQUIRE_LIVE=1
Router Redispython3 scripts/test_gate.py live routermatching paths only with CU_REQUIRE_LIVE=1; required selection is integration_live and not slow
Worker live runtimeCU_COMPOSE_E2E=1 CU_PLATFORM_SECRET=... python3 scripts/test_gate.py live workerexplicit only
Worker Temporalowner Temporal integration laneexplicit only
CLI processtests/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-markers

Live Infrastructure RulesπŸ”—

  • Ordinary pytest must not probe, discover, or synthesize live endpoints.
  • POSTGRES_DSN is a Brain runtime setting, not a test selector.
  • Brain live tests use --brain-test-dsn or BRAIN_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-markers

Run 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πŸ”—