audit-infra
Infrastructure-first security audit — secrets, supply chain, CI/CD, LLM/skill security, OWASP, STRIDE. Complements /audit-solana (program-level)
Run tests and automatically fix common issues
> /plugin marketplace add solanabr/solana-ai-kit > /plugin install solana-ai-kit@stbr
How it fires
How this command gets triggered: by you, by Claude, or both.
/test-and-fixContext preview
What this command does when you run it.
Run tests and automatically fix common issues
description: "Run tests and automatically fix common issues"
You are running tests and fixing any issues found. This command iteratively tests, diagnoses problems, fixes them, and retests.
This command follows a **test → diagnose → fix → retest** loop until all tests pass or manual intervention is needed.
echo "🧪 Running initial tests..."
# Determine project type and run appropriate tests
if [ -f "Anchor.toml" ]; then
# Anchor project
anchor build && anchor test --skip-deploy
TEST_STATUS=$?
elif grep -q "solana-program" Cargo.toml 2>/dev/null; then
# Native Solana program
cargo build-sbf && cargo test
TEST_STATUS=$?
else
# Standard Rust or backend
cargo test
TEST_STATUS=$?
fi
if [ $TEST_STATUS -eq 0 ]; then
echo "✅ All tests passed!"
exit 0
fi
echo "❌ Tests failed. Starting diagnosis..."Analyze the test output to categorize failures:
1. **Format Issues**
2. **Clippy Warnings**
3. **Compilation Errors**
4. **Test Failures**
5. **Runtime Errors**
# Always start with formatting
echo "📝 Fixing format issues..."
cargo fmt
# For TypeScript tests
if [ -d "tests" ] && ls tests/*.ts >/dev/null 2>&1; then
npx prettier --write "tests/**/*.ts"
fi
echo "✅ Format fixed"echo "🔧 Fixing clippy warnings..." # Apply automatic clippy fixes cargo clippy --fix --allow-dirty --allow-staged # Note: Not all clippy warnings are auto-fixable # Manual fixes may be needed for: # - Logic issues # - Unsafe code # - Complex refactorings echo "✅ Clippy auto-fixes applied"
# If test fails with "Missing account: system_program" # Add to test accounts: # systemProgram: anchor.web3.SystemProgram.programId,
# If error: "Account not mutable" # Update account definition to include 'mut': # #[account(mut)]
# If error: "Exceeded CU limit"
# Add compute budget instruction in test:
# .preInstructions([
# ComputeBudgetProgram.setComputeUnitLimit({ units: 400000 })
# ])echo "🔄 Retesting after fixes..."
# Run tests again
if [ -f "Anchor.toml" ]; then
anchor test --skip-deploy
TEST_STATUS=$?
elif grep -q "solana-program" Cargo.toml 2>/dev/null; then
cargo test
TEST_STATUS=$?
else
cargo test
TEST_STATUS=$?
fi
if [ $TEST_STATUS -eq 0 ]; then
echo "✅ All tests now pass!"
exit 0
fiIf automatic fixes didn't resolve all issues, provide guidance:
echo "⚠️ Manual fixes needed. Analyzing failures..." # Parse test output for specific errors # This is where AI assistance helps diagnose complex issues
// Error: "A has_one constraint was violated"
// Fix: Ensure account relationships match constraints
#[account(
mut,
has_one = authority @ ErrorCode::Unauthorized, // authority must match
)]
pub vault: Account<'info, Vault>,// Error: "Invalid PDA seed"
// Fix: Verify seeds match between program and test
// Program:
seeds = [b"vault", authority.key().as_ref()]
// Test:
const [vaultPda] = PublicKey.findProgramAddressSync(
[Buffer.from("vault"), authority.publicKey.toBuffer()],
programId
);// Error: "Arithmetic overflow"
// Fix: Use checked arithmetic
// Bad:
vault.balance = vault.balance + amount;
// Good:
vault.balance = vault
.balance
.checked_add(amount)
.ok_or(ErrorCode::Overflow)?;// Error: "Missing required signature" // Fix: Ensure account is marked as Signer #[account(mut)] pub authority: Signer<'info>, // Must be Signer, not SystemAccount
// Error: "Cannot call blocking function in async context"
// Fix: Use async version or spawn_blocking
// Bad:
async fn handler() -> Result<String> {
std::fs::read_to_string("file.txt")? // Blocking!
}
// Good:
async fn handler() -> Result<String> {
tokio::fs::read_to_string("file.txt").await?
}// Error: "Connection pool exhausted"
// Fix: Increase pool size or close connections properly
let pool = PgPoolOptions::new()
.max_connections(50) // Increase if needed
.connect(&database_url)
.await?;# Maximum fix iterations
MAX_ITERATIONS=5
ITERATION=1
while [ $ITERATION -le $MAX_ITERATIONS ]; do
echo "🔄 Fix iteration $ITERATION/$MAX_ITERATIONS"
# Apply automatic fixes
cargo fmt
cargo clippy --fix --allow-dirty --allow-staged
# Run tests
if [ -f "Anchor.toml" ]; then
anchor test --skip-deploy
else
cargo test
fi
if [ $? -eq 0 ]; then
echo "✅ All tests pass after $ITERATION iteration(s)!"
exit 0
fi
ITERATION=$((ITERATION + 1))
# If still failing after max iterations, need manual intervention
if [ $ITERATION -gt $MAX_ITERATIONS ]; then
echo "⚠️ Maximum iterations reached. Manual fixes required."
echo "Please review the test ouProduction-ready Claude Code configuration for full-stack Solana development. Combines best practices from multiple sources into an agent-optimized, token-efficient config you can install and adapt to your specific project.
Repo: solanabr/solana-ai-kit
Infrastructure-first security audit — secrets, supply chain, CI/CD, LLM/skill security, OWASP, STRIDE. Complements /audit-solana (program-level)
Benchmark CU usage and compare against baseline for regression detection