TNG - AI-Powered Test Generation & Code Analysis for Rails
TNG is a comprehensive AI-powered tool for Rails applications that combines intelligent test generation with advanced static analysis. Analyze your code structure, detect issues, find duplicates, and generate high-quality tests that follow your project's conventions.
Owner: Binary Dreams LLC
Features
- ๐งช AI Test Generation - Generate comprehensive tests for controllers, models, services, and 20+ file types
- ๐ Code Audit - Deep security, performance, and best-practice analysis
- ๐ฌ Symbolic Trace - Visualize execution paths and logic flow with Mermaid diagrams
- ๐ Clone Detection - Find duplicate code with 3 detection levels (Token, Structural, Fuzzy)
- ๐ Dead Code Detection - Identify unreachable code, unused variables, and unused private methods
- โก Blazing Fast - Powered by Rust for near-instant analysis
Installation
Add this line to your application's Gemfile:
gem 'tng'
And then execute:
bundle install
Quick Start
1. Initialize Configuration
Generate the configuration file:
rails generate tng:install
This creates config/initializers/tng.rb:
Tng.configure do |config|
# Get your API key from https://app.tng.sh/
config.api_key = ENV["TNG_API_KEY"]
# API endpoint (do not modify unless instructed)
config.base_url = ENV["API_BASE_URI"] || "https://app.tng.sh/"
end
2. Set Your API Key
Add to your .env file or export:
export TNG_API_KEY=your_api_key_here
Get your API key from https://app.tng.sh/
3. Start Using TNG
# Interactive mode (recommended)
bundle exec tng
# Or use direct commands
bundle exec tng --file app/models/user.rb --clones
Usage
Interactive Mode (TUI)
The most powerful way to use TNG:
bundle exec tng
Interactive Features:
- ๐ Browse your application structure
- ๐งช Generate Tests for specific methods
- ๐ Audit methods for issues and improvements
- ๐ฌ Trace symbolic execution paths
- ๐ Check Duplicates across your codebase
- ๐ Find Dead Code in files
- ๐ View Stats and usage metrics
Command Line Interface
Test Generation
# Generate test for a specific method
bundle exec tng --file app/controllers/users_controller.rb --method index
# Short form
bundle exec tng -f users_controller.rb -m show
# Works from subdirectories (auto-detection)
bundle exec tng users_controller.rb index
Supported File Types: Controllers, Models, Services, Jobs, Helpers, Lib, Policies, Presenters, Mailers, Serializers, Decorators, Forms, Queries, Channels, GraphQL (Resolvers, Types, Mutations, Loaders, Schemas), and more.
Code Audit
Analyze code for security, performance, and best practices:
# Audit a specific method
bundle exec tng --file app/services/payment_service.rb --method process_payment --audit
# Interactive audit mode
bundle exec tng -f payment_service.rb -m process_payment -a
Audit Checks:
- Security vulnerabilities
- Performance bottlenecks
- Code smells
- Best practice violations
- Rails-specific patterns
Symbolic Trace
Visualize execution paths and generate Mermaid diagrams:
# Trace a method's execution flow
bundle exec tng --file app/models/order.rb --method calculate_total --trace
# Short form
bundle exec tng -f order.rb -m calculate_total -t
# Raw trace as JSON
bundle exec tng --file app/models/order.rb --method calculate_total --trace --json > trace.json
Trace Features:
- Control flow visualization
- Conditional path analysis
- Loop detection
- Method call chains
- Mermaid diagram generation
Regression Check
Compare a method against its committed (HEAD) version and surface blast radius:
# Run regression check for a method
bundle exec tng --file app/services/payment_service.rb --method process_payment --impact
# JSON output for automation/CI
bundle exec tng --file app/services/payment_service.rb --method process_payment --impact --json
Impact Signals:
- Added/removed required parameters
- Parameter kind/requirement changes
- Return type changes
- Variable type drift (inferred)
- Impacted call sites (files/lines)
Clone Detection
Find duplicate code with 3 levels of detection:
# Check for all types of clones
bundle exec tng --file app/services/pricing_service.rb --clones
# Specific detection level
bundle exec tng --file app/services/pricing_service.rb --clones --level=1
bundle exec tng --file app/services/pricing_service.rb --clones --level=2
bundle exec tng --file app/services/pricing_service.rb --clones --level=3
# Short form
bundle exec tng -f pricing_service.rb -c
Detection Levels:
- Level 1 (Token) - Exact or near-exact duplicates (copy-paste detection)
- Level 2 (Structural) - Same AST structure with different names
- Level 3 (Fuzzy) - Similar logic with variations (up to 20% difference)
Dead Code Detection
Identify unreachable code and unused elements:
# Detect dead code in a file
bundle exec tng --file app/models/user.rb --deadcode
# Short form
bundle exec tng -f user.rb -d
# JSON output for CI/CD
bundle exec tng -f user.rb --deadcode --json
Dead Code Types:
- Unreachable Code - Code after
return,raise,exit, etc. - Unused Variables - Local variables assigned but never used
- Unused Private Methods - Private methods never called
- Unused Parameters - Method parameters never referenced
JSON Output Mode
Get machine-readable output for CI/CD integration:
# Any command with --json flag
bundle exec tng --file app/models/user.rb --clones --json
bundle exec tng --file app/services/payment.rb --deadcode --json
bundle exec tng --file app/controllers/api_controller.rb --trace --json
bundle exec tng --file app/services/payment.rb --method process_payment --impact --json
Advanced Features
Method-Specific Analysis
TNG focuses on precise, method-level analysis:
- Select specific methods from any file type
- Interactive browsing with search and filter
- Focused generation for individual methods
- No bulk generation - designed for precision
File Type Auto-Detection
No need to specify file types - TNG automatically detects:
# All of these work automatically
bundle exec tng users_controller.rb index
bundle exec tng user.rb validate_email
bundle exec tng payment_service.rb process
bundle exec tng user_mailer.rb welcome_email
bundle exec tng user_resolver.rb find_user
Debug Mode
Enable detailed logging for troubleshooting:
DEBUG=1 bundle exec tng
DEBUG=1 bundle exec tng -f users_controller.rb -m index
Examples
Example 1: Find Duplicate Code
$ bundle exec tng --file app/services/pricing_service.rb --clones
๐ Clone Detection Results
Level 1 (Token): 2 matches found
โโ Lines 45-52 โ Lines 78-85 (8 lines, exact match)
โโ Lines 120-127 โ Lines 156-163 (8 lines, exact match)
Level 2 (Structural): 1 match found
โโ Lines 200-215 โ Lines 240-255 (16 nodes, complexity: 85)
Level 3 (Fuzzy): 1 match found
โโ Lines 300-320 โ Lines 350-370 (similar logic, 15% variation)
Example 2: Detect Dead Code
$ bundle exec tng --file app/models/user.rb --deadcode
๐ Dead Code Detection Results
[Unreachable Code] Line 42: Code follows terminal statement
42: puts "This will never execute"
[Unused Variable] Line 67: Variable 'temp' is assigned but never used
67: temp = calculate_something
[Unused Method] Line 120: Private method 'unused_helper' is never used
120: def unused_helper
[Unused Parameter] Line 85: Parameter 'options' is never used
85: def process(data, options)
Example 3: Trace Execution Flow
$ bundle exec tng --file app/models/order.rb --method calculate_total --trace
๐ฌ Symbolic Trace Results
Execution Paths: 3
Complexity Score: 12
Path 1: items.empty? โ return 0
Path 2: discount_code.present? โ apply_discount โ calculate_tax โ total
Path 3: !discount_code.present? โ calculate_tax โ total
[Mermaid diagram saved to: trace_order_calculate_total.md]
CLI Reference
Flags
| Flag | Short | Description |
|---|---|---|
--file |
-f |
Target file path |
--method |
-m |
Method name to analyze |
--audit |
-a |
Run audit mode |
--trace |
-t |
Run symbolic trace mode |
--impact |
Run regression check mode | |
--clones |
-c |
Run clone detection |
--deadcode |
-d |
Run dead code detection |
--level |
-l |
Clone detection level (1, 2, 3, or all) |
--json |
Output as JSON | |
--help |
-h |
Show help message |
Examples
# Test generation
bundle exec tng -f users_controller.rb -m index
# Code audit
bundle exec tng -f payment_service.rb -m process -a
# Symbolic trace
bundle exec tng -f order.rb -m calculate_total -t
# Regression check
bundle exec tng -f payment_service.rb -m process --impact
# Clone detection (all levels)
bundle exec tng -f pricing_service.rb -c
# Clone detection (specific level)
bundle exec tng -f pricing_service.rb -c -l 2
# Dead code detection
bundle exec tng -f user.rb -d
# JSON output
bundle exec tng -f user.rb -c --json
Troubleshooting
API Key Issues
Error: "Invalid API key"
- Verify your API key from https://app.tng.sh/
- Check:
echo $TNG_API_KEY - Ensure no extra spaces in
.env:TNG_API_KEY=your_key_here
Error: "API key not found"
- Set environment variable:
export TNG_API_KEY=your_key - Or add to
.envfile:TNG_API_KEY=your_key - Restart Rails server after changes
Configuration Issues
Error: "Invalid configuration"
- Run:
rails consolethenTng.configuration - Verify
config/initializers/tng.rbexists - Check for typos in configuration file
Generation Issues
No tests generated:
- Verify file path:
ls app/controllers/users_controller.rb - Check file contains valid Ruby code
- Verify API key is working
- Check internet connection
Poor test quality:
- Ensure you're using the latest version
- Try audit mode for better insights
- Check file is properly formatted
Debug Mode
Enable debug output for detailed information:
DEBUG=1 bundle exec tng -f users_controller.rb -m index
Debug output includes:
- API request/response details
- File analysis results
- Configuration validation
- Error stack traces
What's New in v0.4.8
New Features
- โจ Level 3 Fuzzy Clone Detection - Find similar code with variations
- โจ Dead Code Detection - Identify unreachable code and unused elements
- โจ Simplified Configuration - Only API key required to get started
- โจ Enhanced Clone Detection - 3 levels of duplicate detection
- โจ Improved UI - Better visualization of results
Improvements
- โก Faster file analysis with optimized Rust parsers
- ๐จ Better error messages and user feedback
- ๐ Enhanced reporting for all analysis modes
- ๐ง Auto-detection of file types and test frameworks
Getting Help
If you're experiencing issues:
- Check the troubleshooting section above
- Enable debug mode:
DEBUG=1 bundle exec tng - Contact support at https://app.tng.sh/
- Open a GitHub issue for bug reports
License
Copyright (c) 2025 Binary Dreams LLC. All rights reserved.
See LICENSE.md for details.