upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/grasp-audit/test-ngit-relay.sh
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-11-05 12:05:38 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-11-05 12:05:38 +0000
commit1e267c282d07c68dbee4682c66d3933032ed7fec (patch)
tree54b24878684ad2a569cb939508d48faaa24c3758 /grasp-audit/test-ngit-relay.sh
parentd9c9ef2ff92b687f5ff5585b08b2eead8f139a02 (diff)
feat(grasp-audit): standardize testing with test-ngit-relay.sh
Establish test-ngit-relay.sh as the canonical testing approach for GRASP-01 compliance tests, eliminating manual relay setup and ensuring consistent, reproducible test environments. **Enhanced test-ngit-relay.sh:** - Add command-line argument parsing (--mode, --spec, --help) - Support both audit and test execution modes - Comprehensive inline documentation - Backward compatible (default behavior unchanged) **Documentation updates:** - AGENTS.md: Add "Standard Testing Process" section - AGENTS.md: Update Quick Reference to prioritize test-ngit-relay.sh - AGENTS.md: Add Critical Gotcha #7 about using the test script - grasp-audit/README.md: Add prominent Quick Start section - grasp-audit/README.md: Reorganize testing documentation **Benefits:** - Automatic relay lifecycle management (start, cleanup) - Random port selection prevents conflicts - Isolated temporary directories per run - Guaranteed cleanup on success or failure - Consistent test environment across all developers All changes tested and verified working.
Diffstat (limited to 'grasp-audit/test-ngit-relay.sh')
-rwxr-xr-xgrasp-audit/test-ngit-relay.sh175
1 files changed, 159 insertions, 16 deletions
diff --git a/grasp-audit/test-ngit-relay.sh b/grasp-audit/test-ngit-relay.sh
index 356eed1..bd4b155 100755
--- a/grasp-audit/test-ngit-relay.sh
+++ b/grasp-audit/test-ngit-relay.sh
@@ -1,36 +1,144 @@
1#!/bin/bash 1#!/bin/bash
2set -e 2set -e
3 3
4# =============================================================================
5# Script: test-ngit-relay.sh
6# Purpose: Test ngit-relay against GRASP specifications
7#
8# This script automates the process of:
9# 1. Starting a fresh ngit-relay instance in Docker
10# 2. Running either grasp-audit CLI or cargo test suite
11# 3. Cleaning up all resources on exit (via EXIT trap)
12#
13# Features:
14# - Automatic cleanup via EXIT trap (runs even on failure)
15# - Random port selection (20000-30000) to avoid conflicts
16# - Unique temporary directories per run
17# - Support for both audit and test execution modes
18# =============================================================================
19
20# -----------------------------------------------------------------------------
21# Help Function
22# -----------------------------------------------------------------------------
23show_help() {
24 cat << EOF
25Usage: $(basename "$0") [OPTIONS]
26
27Test ngit-relay against GRASP specifications
28
29OPTIONS:
30 --mode <audit|test> Execution mode (default: audit)
31 audit: Run grasp-audit CLI tool
32 test: Run cargo test suite
33 --spec <spec> Specification to test (default: nip01-smoke)
34 Only used in audit mode
35 --help Show this help message
36
37EXAMPLES:
38 # Run audit with default settings (current behavior)
39 ./test-ngit-relay.sh
40
41 # Run cargo tests instead
42 ./test-ngit-relay.sh --mode test
43
44 # Run audit with specific spec
45 ./test-ngit-relay.sh --mode audit --spec grasp01
46
47EOF
48 exit 0
49}
50
51# -----------------------------------------------------------------------------
52# Argument Parsing
53# -----------------------------------------------------------------------------
54# Default values maintain backward compatibility
55MODE="audit"
56SPEC="nip01-smoke"
57
58# Parse command-line arguments
59while [[ $# -gt 0 ]]; do
60 case $1 in
61 --mode)
62 MODE="$2"
63 shift 2
64 ;;
65 --spec)
66 SPEC="$2"
67 shift 2
68 ;;
69 --help)
70 show_help
71 ;;
72 *)
73 echo "โŒ Unknown option: $1"
74 echo "Run with --help for usage information"
75 exit 1
76 ;;
77 esac
78done
79
80# Validate mode parameter
81if [ "$MODE" != "audit" ] && [ "$MODE" != "test" ]; then
82 echo "โŒ Invalid mode: $MODE"
83 echo "Mode must be 'audit' or 'test'"
84 exit 1
85fi
86
87# -----------------------------------------------------------------------------
88# Environment Setup
89# -----------------------------------------------------------------------------
4# Create temporary directory with random name 90# Create temporary directory with random name
91# This ensures each test run is isolated and doesn't interfere with others
5TEST_DIR=$(mktemp -d -t grasp-audit-run-XXXXXXXXXX) 92TEST_DIR=$(mktemp -d -t grasp-audit-run-XXXXXXXXXX)
6# Pick a random port in the range 20000-30000 93
94# Pick a random port in the range 20000-30000 to avoid conflicts
7PORT=$((20000 + RANDOM % 10000)) 95PORT=$((20000 + RANDOM % 10000))
8# Generate a unique container name suffix 96
97# Generate a unique container name suffix for parallel runs
9CONTAINER_SUFFIX=$RANDOM 98CONTAINER_SUFFIX=$RANDOM
10 99
11echo "๐Ÿงน Using temporary directory: $TEST_DIR" 100echo "๐Ÿงน Using temporary directory: $TEST_DIR"
12echo "๐Ÿ”Œ Using port: $PORT" 101echo "๐Ÿ”Œ Using port: $PORT"
102echo "๐ŸŽฏ Mode: $MODE $([ "$MODE" = "audit" ] && echo "(spec: $SPEC)" || echo "")"
13 103
14# Cleanup function 104# -----------------------------------------------------------------------------
105# Cleanup Function
106# -----------------------------------------------------------------------------
107# This function is called automatically on script exit via trap
108# The '|| true' pattern ensures cleanup continues even if individual steps fail
15cleanup() { 109cleanup() {
110 echo ""
16 echo "๐Ÿ›‘ Stopping relay..." 111 echo "๐Ÿ›‘ Stopping relay..."
112 # Stop container gracefully, ignore errors if already stopped
17 docker stop "grasp-audit-run-$CONTAINER_SUFFIX" 2>/dev/null || true 113 docker stop "grasp-audit-run-$CONTAINER_SUFFIX" 2>/dev/null || true
18 114
19 echo "๐Ÿงน Cleaning up temporary directory..." 115 echo "๐Ÿงน Cleaning up temporary directory..."
116 # Use alpine container to clean Docker-created files (may have different ownership)
20 docker run --rm -v "$TEST_DIR:/data" alpine sh -c "rm -rf /data/*" 2>/dev/null || true 117 docker run --rm -v "$TEST_DIR:/data" alpine sh -c "rm -rf /data/*" 2>/dev/null || true
118 # Remove the temporary directory itself
21 rm -rf "$TEST_DIR" 119 rm -rf "$TEST_DIR"
22} 120}
23 121
24# Set trap to cleanup on exit 122# Set trap to run cleanup on ANY exit (success, failure, or interrupt)
123# This ensures resources are always cleaned up
25trap cleanup EXIT 124trap cleanup EXIT
26 125
126# -----------------------------------------------------------------------------
127# Docker Setup
128# -----------------------------------------------------------------------------
27echo "๐Ÿ“ Creating data directories..." 129echo "๐Ÿ“ Creating data directories..."
130# Create all required directories for ngit-relay in one command
28mkdir -p "$TEST_DIR"/{repos,blossom,relay-db,logs} 131mkdir -p "$TEST_DIR"/{repos,blossom,relay-db,logs}
29 132
30echo "๐Ÿš€ Starting ngit-relay..." 133echo "๐Ÿš€ Starting ngit-relay..."
31# Remove any existing container with this name 134# Remove any existing container with this name (defensive cleanup)
32CONTAINER_NAME="grasp-audit-run-$CONTAINER_SUFFIX" 135CONTAINER_NAME="grasp-audit-run-$CONTAINER_SUFFIX"
33docker rm -f "$CONTAINER_NAME" 2>/dev/null || true 136docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
137
138# Start ngit-relay in detached mode with all required configuration
139# - Port mapping: expose internal 8081 on our random port
140# - Volume mounts: persist data in temp directory
141# - Proactive sync disabled: we control event submission via tests
34docker run --rm -d \ 142docker run --rm -d \
35 --name "$CONTAINER_NAME" \ 143 --name "$CONTAINER_NAME" \
36 -p "$PORT:8081" \ 144 -p "$PORT:8081" \
@@ -49,19 +157,54 @@ docker run --rm -d \
49 ghcr.io/danconwaydev/ngit-relay:latest 157 ghcr.io/danconwaydev/ngit-relay:latest
50 158
51echo "โณ Waiting for relay to start..." 159echo "โณ Waiting for relay to start..."
160# Give the relay time to initialize before running tests
52sleep 3 161sleep 3
53 162
54echo "๐Ÿงช Running tests..." 163# -----------------------------------------------------------------------------
55echo "" 164# Test Execution
56echo "Note: ngit-relay only accepts Git-related events (NIP-34)." 165# -----------------------------------------------------------------------------
57echo "Some NIP-01 smoke tests will fail (expected behavior)." 166# Execute tests based on selected mode
58echo "Validation tests should pass." 167# The EXIT trap ensures cleanup happens regardless of test outcome
59echo ""
60 168
61# Run the CLI tool (cleanup happens via trap even on failure) 169if [ "$MODE" = "audit" ]; then
62RELAY_URL="ws://localhost:$PORT" cargo run -- audit --relay "ws://localhost:$PORT" --mode ci --spec nip01-smoke || { 170 echo "๐Ÿงช Running audit mode (spec: $SPEC)..."
63 echo "โš ๏ธ Some tests failed (expected for ngit-relay)" 171 echo ""
64 echo " Validation tests should have passed" 172 echo "Note: ngit-relay only accepts Git-related events (NIP-34)."
65} 173 echo "Some NIP-01 smoke tests will fail (expected behavior)."
174 echo "Validation tests should pass."
175 echo ""
176
177 # Run grasp-audit CLI tool
178 # - RELAY_URL: Environment variable used by audit tool
179 # - --relay: Command-line parameter for relay address
180 # - --mode ci: Continuous integration mode (structured output)
181 # - --spec: Which specification to test
182 # The '|| { }' block provides user-friendly messaging on failure
183 RELAY_URL="ws://localhost:$PORT" cargo run -- audit \
184 --relay "ws://localhost:$PORT" \
185 --mode ci \
186 --spec "$SPEC" || {
187 echo "โš ๏ธ Some tests failed (expected for ngit-relay)"
188 echo " Validation tests should have passed"
189 }
190
191elif [ "$MODE" = "test" ]; then
192 echo "๐Ÿงช Running cargo test mode..."
193 echo ""
194
195 # Run cargo test suite
196 # - RELAY_URL: Environment variable tests use to connect
197 # - --lib: Only library tests (not integration tests in tests/)
198 # - test_grasp01_nostr_relay_against_relay: Specific test module
199 # - --ignored: Run tests marked with #[ignore] (these need relay)
200 # - --nocapture: Show println! output from tests
201 RELAY_URL="ws://localhost:$PORT" cargo test \
202 --lib test_grasp01_nostr_relay_against_relay \
203 -- --ignored --nocapture || {
204 echo "โš ๏ธ Some tests failed"
205 echo " Review output above for details"
206 }
207fi
66 208
209echo ""
67echo "โœ… Done!" 210echo "โœ… Done!"