Skip to content

Commit 0d21016

Browse files
committed
Refactor testing infrastructure and enhance container management
- Updated the testing documentation to reflect a new Makefile-based approach for running tests and managing containers. - Introduced new scripts for container management, including starting, stopping, restarting, and cleaning containers while preserving logs. - Added a cleanup script to handle data ownership and permissions correctly. - Implemented a logging system that saves container logs automatically before cleanup. - Enhanced the README with detailed instructions for running tests and managing the test environment.
1 parent f08acd5 commit 0d21016

File tree

14 files changed

+671
-223
lines changed

14 files changed

+671
-223
lines changed

CLAUDE.md

Lines changed: 41 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -86,130 +86,54 @@ cp .env.template .env # Configure environment variables
8686
sudo rm -rf backends/advanced/data/
8787
```
8888

89-
### Testing Infrastructure
89+
### Running Tests
9090

91-
#### Local Test Scripts
92-
The project includes simplified test scripts that mirror CI workflows:
91+
#### Quick Commands
92+
All test operations are managed through a simple Makefile interface:
9393

94-
```bash
95-
# Run all tests from project root
96-
./run-test.sh [advanced-backend|speaker-recognition|all]
97-
98-
# Advanced backend tests only
99-
./run-test.sh advanced-backend
100-
101-
# Speaker recognition tests only
102-
./run-test.sh speaker-recognition
103-
104-
# Run all test suites (default)
105-
./run-test.sh all
106-
```
107-
108-
#### Advanced Backend Integration Tests
109-
110-
**Three Test Execution Modes:**
111-
112-
1. **No-API Tests** (Fast, No External Dependencies)
11394
```bash
11495
cd tests
11596

116-
# Run tests without API keys (excludes requires-api-keys tag)
117-
./run-no-api-tests.sh
97+
# Full test workflow (recommended)
98+
make test # Start containers + run all tests
11899

119-
# ~70% of test suite
120-
# Uses mock services config
121-
# No DEEPGRAM_API_KEY or OPENAI_API_KEY required
122-
# Fast feedback (~10-15 minutes)
123-
```
100+
# Or step by step
101+
make start # Start test containers (with health checks)
102+
make test-all # Run all test suites
103+
make stop # Stop containers (preserves volumes)
124104

125-
2. **Full Tests with API Keys** (Comprehensive)
126-
```bash
127-
cd tests
105+
# Run specific test suites
106+
make test-endpoints # API endpoint tests (~40 tests, fast)
107+
make test-integration # End-to-end workflows (~15 tests, slower)
108+
make test-infra # Infrastructure resilience (~5 tests)
128109

129-
# Requires .env file with DEEPGRAM_API_KEY and OPENAI_API_KEY
130-
cp setup/.env.test.template setup/.env.test # Configure API keys
110+
# Quick iteration (reuse existing containers)
111+
make test-quick # Run tests without restarting containers
112+
```
131113

132-
# Run full integration test suite (100% of tests)
133-
./run-robot-tests.sh
114+
#### Container Management
115+
All container operations automatically preserve logs before cleanup:
134116

135-
# Leave test containers running for debugging
136-
CLEANUP_CONTAINERS=false ./run-robot-tests.sh
117+
```bash
118+
make start # Start test containers
119+
make stop # Stop containers (keep volumes)
120+
make restart # Restart without rebuild
121+
make rebuild # Rebuild images + restart (for code changes)
122+
make containers-clean # SAVES LOGS → removes everything
123+
make status # Show container health
124+
make logs SERVICE=<name> # View specific service logs
137125
```
138126

139-
3. **API-Only Tests** (Optional)
140-
```bash
141-
cd tests
127+
**Log Preservation:** All cleanup operations save container logs to `tests/logs/YYYY-MM-DD_HH-MM-SS/`
142128

143-
# Run only tests that require API keys
144-
./run-api-tests.sh
129+
#### Test Environment
145130

146-
# ~30% of test suite
147-
# Only E2E tests with transcription/memory extraction
148-
```
131+
Test services use isolated ports and database:
132+
- **Ports:** Backend (8001), MongoDB (27018), Redis (6380), Qdrant (6337/6338)
133+
- **Database:** `test_db` (separate from production)
134+
- **Credentials:** `test-admin@example.com` / `test-admin-password-123`
149135

150-
#### Test Separation by API Requirements
151-
152-
Tests are separated into two categories:
153-
154-
- **No API Keys Required** (~70%): Endpoint tests, infrastructure tests, basic integration
155-
- Uses `configs/mock-services.yml`
156-
- Runs on all PRs by default
157-
- Fast CI feedback
158-
159-
- **API Keys Required** (~30%): Full E2E tests with transcription and memory extraction
160-
- Uses `configs/deepgram-openai.yml`
161-
- Tagged with `requires-api-keys`
162-
- Runs on dev/main branches or when PR labeled with `test-with-api-keys`
163-
164-
#### Test Configuration Flags
165-
- **CLEANUP_CONTAINERS** (default: false): Automatically stop and remove test containers after test completion
166-
- Set to `true` for cleanup: `CLEANUP_CONTAINERS=true ./run-robot-tests.sh`
167-
- **CONFIG_FILE**: Choose test configuration
168-
- `configs/mock-services.yml` - No API keys (default for run-no-api-tests.sh)
169-
- `configs/deepgram-openai.yml` - With API keys (default for run-robot-tests.sh)
170-
- `configs/parakeet-ollama.yml` - Fully local (no external APIs)
171-
172-
#### Test Environment Variables
173-
Tests use isolated test environment with overridden credentials:
174-
- **Test Database**: `test_db` (MongoDB on port 27018, separate from production)
175-
- **Test Ports**: Backend (8001), Qdrant (6337/6338), WebUI (3001)
176-
- **Test Credentials**:
177-
- `AUTH_SECRET_KEY`: test-jwt-signing-key-for-integration-tests
178-
- `ADMIN_EMAIL`: test-admin@example.com
179-
- `ADMIN_PASSWORD`: test-admin-password-123
180-
- **API Keys**: Loaded from `.env` file (DEEPGRAM_API_KEY, OPENAI_API_KEY)
181-
- **Test Settings**: `DISABLE_SPEAKER_RECOGNITION=true` to prevent segment duplication
182-
183-
#### Test Script Features
184-
- **Environment Compatibility**: Works with both local .env files and CI environment variables
185-
- **Isolated Test Environment**: Separate ports and database prevent conflicts with running services
186-
- **Automatic Cleanup**: Configurable via CLEANUP_CONTAINERS flag (default: false for faster re-runs)
187-
- **Colored Output**: Clear progress indicators and error reporting
188-
- **Timeout Protection**: 30-minute timeout for test execution
189-
- **Fresh Testing**: Clean database and containers for each test run
190-
- **API Key Separation**: Ability to run tests with or without external API dependencies
191-
192-
#### GitHub Workflows
193-
194-
**Three workflows handle test execution:**
195-
196-
1. **`robot-tests.yml`** - PR Tests (No API Keys)
197-
- Triggers: All pull requests
198-
- Execution: Excludes `requires-api-keys` tests (~70% of suite)
199-
- No secrets required
200-
- Fast feedback for contributors
201-
202-
2. **`full-tests-with-api.yml`** - Dev/Main Tests (Full Suite)
203-
- Triggers: Push to dev/main branches
204-
- Execution: All tests including API-dependent (~100% of suite)
205-
- Requires: DEEPGRAM_API_KEY, OPENAI_API_KEY
206-
- Comprehensive validation before deployment
207-
208-
3. **`pr-tests-with-api.yml`** - Label-Triggered PR Tests
209-
- Triggers: PR with `test-with-api-keys` label
210-
- Execution: Full test suite before merge
211-
- Requires: DEEPGRAM_API_KEY, OPENAI_API_KEY
212-
- Useful for testing API integration changes
136+
**For complete test documentation, see `tests/README.md`**
213137

214138
### Mobile App Development
215139
```bash
@@ -571,12 +495,11 @@ tailscale ip -4
571495
- **Docker**: Primary deployment method with docker-compose
572496

573497
### Testing Strategy
574-
- **Local Test Scripts**: Simplified scripts (`./run-test.sh`) mirror CI workflows for local development
575-
- **End-to-End Integration**: Robot Framework tests (`tests/integration/integration_test.robot`) validate complete audio processing pipeline
576-
- **Speaker Recognition Tests**: `test_speaker_service_integration.py` validates speaker identification
498+
- **Makefile-Based**: All test operations through simple `make` commands (`make test`, `make start`, `make stop`)
499+
- **Log Preservation**: Container logs always saved before cleanup (never lose debugging info)
500+
- **End-to-End Integration**: Robot Framework validates complete audio processing pipeline
577501
- **Environment Flexibility**: Tests work with both local .env files and CI environment variables
578-
- **Automated Cleanup**: Test containers are automatically removed after execution
579-
- **CI/CD Integration**: GitHub Actions use the same local test scripts for consistency
502+
- **CI/CD Integration**: Same test logic locally and in GitHub Actions
580503

581504
### Code Style
582505
- **Python**: Black formatter with 100-character line length, isort for imports
@@ -603,14 +526,10 @@ The system includes comprehensive health checks:
603526
- Memory debug system for transcript processing monitoring
604527

605528
### Integration Test Infrastructure
606-
- **Unified Test Scripts**: Local `./run-test.sh` scripts mirror GitHub Actions workflows
607-
- **Test Environment**: `docker-compose-test.yml` provides isolated services on separate ports
608-
- **Test Database**: Uses `test_db` database with isolated collections
609-
- **Service Ports**: Backend (8001), MongoDB (27018), Qdrant (6335/6336), WebUI (5174)
610-
- **Test Credentials**: Auto-generated `.env.test` files with secure test configurations
611-
- **Ground Truth**: Expected transcript established via `scripts/test_deepgram_direct.py`
612-
- **AI Validation**: OpenAI-powered transcript similarity comparison
613-
- **Test Audio**: 4-minute glass blowing tutorial (`extras/test-audios/DIY*mono*.wav`)
529+
- **Makefile Interface**: Simple `make` commands for all operations (see `tests/README.md`)
530+
- **Test Environment**: `docker-compose-test.yml` with isolated services on separate ports
531+
- **Test Database**: Uses `test_db` database (separate from production)
532+
- **Log Preservation**: All cleanup operations save logs to `tests/logs/` automatically
614533
- **CI Compatibility**: Same test logic runs locally and in GitHub Actions
615534

616535
### Cursor Rule Integration

backends/advanced/cleanup.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
# Wrapper script for cleanup_state.py
3+
# Usage: ./cleanup.sh --backup --export-audio
4+
#
5+
# This script runs the cleanup_state.py script inside the chronicle-backend container
6+
# to handle data ownership and permissions correctly.
7+
#
8+
# Examples:
9+
# ./cleanup.sh --dry-run # Preview what would be deleted
10+
# ./cleanup.sh --backup # Cleanup with metadata backup
11+
# ./cleanup.sh --backup --export-audio # Full backup including audio
12+
# ./cleanup.sh --backup --force # Skip confirmation prompts
13+
14+
cd "$(dirname "$0")"
15+
docker compose exec chronicle-backend uv run python src/scripts/cleanup_state.py "$@"

tests/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Test output files
2+
output.xml
3+
log.html
4+
report.html
5+
results/
6+
results-no-api/
7+
8+
# Saved container logs (automatically generated)
9+
logs/*
10+
!logs/.gitkeep

tests/Makefile

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
11
# Chronicle Test Makefile
2-
# Shortcuts for running tests
2+
# Shortcuts for running tests and managing test containers
33

4-
.PHONY: help all clean
4+
.PHONY: help all clean \
5+
containers-start containers-stop containers-restart containers-rebuild \
6+
containers-clean containers-status containers-logs \
7+
start stop restart rebuild status logs \
8+
test test-quick clean-all
59

610
# Default output directory
711
OUTPUTDIR ?= results
812
TEST_DIR = endpoints integration infrastructure
13+
SERVICE ?= chronicle-backend-test
914

1015
help:
1116
@echo "Chronicle Test Targets:"
1217
@echo ""
18+
@echo "Quick Commands:"
19+
@echo " make test - Start containers + run all tests"
20+
@echo " make test-quick - Run tests on existing containers"
21+
@echo " make start - Start test containers"
22+
@echo " make stop - Stop containers (keep volumes)"
23+
@echo " make status - Show container status"
24+
@echo ""
1325
@echo "Running Tests:"
1426
@echo " make all - Run all tests"
1527
@echo " make endpoints - Run only endpoint tests"
1628
@echo " make integration - Run only integration tests"
1729
@echo " make infra - Run only infrastructure tests"
1830
@echo ""
31+
@echo "Container Management:"
32+
@echo " make containers-start - Start test containers"
33+
@echo " make containers-stop - Stop containers (keep volumes)"
34+
@echo " make containers-restart - Restart containers"
35+
@echo " make containers-rebuild - Rebuild + restart containers"
36+
@echo " make containers-clean - Save logs + remove everything"
37+
@echo " make containers-status - Show container health"
38+
@echo " make containers-logs - View service logs (use SERVICE=name)"
39+
@echo ""
1940
@echo "Utilities:"
2041
@echo " make clean - Remove test output files"
42+
@echo " make clean-all - Clean results + containers (saves logs)"
2143
@echo ""
2244
@echo "Environment Variables:"
2345
@echo " OUTPUTDIR - Output directory (default: results)"
46+
@echo " SERVICE - Service name for logs (default: chronicle-backend-test)"
2447
@echo ""
2548
@echo "Examples:"
26-
@echo " make all # Full test suite"
27-
@echo " make endpoints # Only endpoint tests"
28-
@echo " make all OUTPUTDIR=/tmp/out # Custom output dir"
49+
@echo " make test # Full workflow"
50+
@echo " make endpoints # Only endpoint tests"
51+
@echo " make containers-logs SERVICE=workers-test # View worker logs"
2952

3053
# Run all tests
3154
# Creates a persistent fixture conversation that won't be deleted between suites
@@ -34,6 +57,7 @@ all:
3457
CREATE_FIXTURE=true uv run --with-requirements test-requirements.txt robot --outputdir $(OUTPUTDIR) \
3558
--name "All Tests" \
3659
--console verbose \
60+
--loglevel INFO:INFO \
3761
$(TEST_DIR)
3862

3963
# Run only endpoint tests
@@ -42,6 +66,7 @@ endpoints:
4266
uv run --with-requirements test-requirements.txt robot --outputdir $(OUTPUTDIR) \
4367
--name "Endpoint Tests" \
4468
--console verbose \
69+
--loglevel INFO:INFO \
4570
endpoints
4671

4772
# Run only integration tests
@@ -50,6 +75,7 @@ integration:
5075
CREATE_FIXTURE=true uv run --with-requirements test-requirements.txt robot --outputdir $(OUTPUTDIR) \
5176
--name "Integration Tests" \
5277
--console verbose \
78+
--loglevel INFO:INFO \
5379
integration
5480

5581
# Run only infrastructure tests
@@ -58,6 +84,7 @@ infra:
5884
uv run --with-requirements test-requirements.txt robot --outputdir $(OUTPUTDIR) \
5985
--name "Infrastructure Tests" \
6086
--console verbose \
87+
--loglevel INFO:INFO \
6188
infrastructure
6289

6390
# Clean up test output files
@@ -66,3 +93,59 @@ clean:
6693
rm -f output.xml log.html report.html
6794
rm -rf $(OUTPUTDIR)
6895
@echo "Clean complete!"
96+
97+
# ============================================================================
98+
# Container Management Targets
99+
# ============================================================================
100+
101+
# Start test containers
102+
containers-start:
103+
@./bin/start-containers.sh
104+
105+
# Stop test containers (preserve volumes)
106+
containers-stop:
107+
@./bin/stop-containers.sh
108+
109+
# Restart test containers
110+
containers-restart:
111+
@./bin/restart-containers.sh
112+
113+
# Rebuild test containers
114+
containers-rebuild:
115+
@./bin/rebuild-containers.sh
116+
117+
# Clean test containers (ALWAYS saves logs first!)
118+
containers-clean:
119+
@./bin/clean-containers.sh
120+
121+
# Show container status
122+
containers-status:
123+
@./bin/status-containers.sh
124+
125+
# View container logs
126+
containers-logs:
127+
@./bin/logs-containers.sh $(SERVICE)
128+
129+
# ============================================================================
130+
# Convenient Aliases
131+
# ============================================================================
132+
133+
start: containers-start
134+
stop: containers-stop
135+
restart: containers-restart
136+
rebuild: containers-rebuild
137+
status: containers-status
138+
logs: containers-logs
139+
140+
# ============================================================================
141+
# Combined Workflows
142+
# ============================================================================
143+
144+
# Full workflow: start containers + run all tests
145+
test: containers-start all
146+
147+
# Quick workflow: run tests on existing containers
148+
test-quick: all
149+
150+
# Complete cleanup: test results + containers (saves logs)
151+
clean-all: clean containers-clean

0 commit comments

Comments
 (0)