codeguard-testgen 1.0.9 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +627 -40
- package/dist/config.d.ts +28 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +69 -2
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1898 -862
- package/dist/index.js.map +1 -1
- package/dist/reviewEngine.d.ts +38 -0
- package/dist/reviewEngine.d.ts.map +1 -0
- package/dist/reviewEngine.js +571 -0
- package/dist/reviewEngine.js.map +1 -0
- package/dist/toolExecutors.d.ts +82 -0
- package/dist/toolExecutors.d.ts.map +1 -0
- package/dist/toolExecutors.js +354 -0
- package/dist/toolExecutors.js.map +1 -0
- package/dist/typeValidator.d.ts +25 -0
- package/dist/typeValidator.d.ts.map +1 -0
- package/dist/typeValidator.js +180 -0
- package/dist/typeValidator.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,23 +1,39 @@
|
|
|
1
|
-
# CodeGuard Test Generator
|
|
1
|
+
# CodeGuard - AI Code Review & Test Generator
|
|
2
2
|
|
|
3
|
-
AI-powered unit test generator with AST analysis for TypeScript/JavaScript projects. Automatically generates comprehensive Jest tests using Claude, OpenAI, or Gemini.
|
|
3
|
+
AI-powered code review and unit test generator with AST analysis for TypeScript/JavaScript projects. Automatically reviews code quality and generates comprehensive Jest tests using Claude, OpenAI, or Gemini.
|
|
4
4
|
|
|
5
|
-
> **⚡ NEW: Auto Mode** - Automatically
|
|
5
|
+
> **⚡ NEW: Unified Auto Mode** - Automatically review code quality AND generate tests for staged functions!
|
|
6
6
|
> ```bash
|
|
7
|
-
> testgen auto
|
|
7
|
+
> testgen auto # Reviews code + generates tests
|
|
8
|
+
> testgen review # Only code review
|
|
9
|
+
> testgen test # Only test generation
|
|
10
|
+
> testgen doc # Generate API documentation
|
|
8
11
|
> ```
|
|
9
12
|
|
|
10
13
|
## Features
|
|
11
14
|
|
|
15
|
+
### Code Review
|
|
16
|
+
- 🔍 **AI Code Review**: Comprehensive analysis of code quality, bugs, performance, and security
|
|
17
|
+
- 👨💻 **Senior Developer Perspective**: AI reviews code like an experienced developer
|
|
18
|
+
- 📊 **Structured Reports**: Markdown reviews with severity levels (Critical, High, Medium, Low)
|
|
19
|
+
- 🎯 **Context-Aware**: Uses AST analysis to understand full code context before reviewing
|
|
20
|
+
- 📁 **Review History**: All reviews saved to `reviews/` directory for reference
|
|
21
|
+
|
|
22
|
+
### Test Generation
|
|
12
23
|
- 🤖 **AI-Powered**: Uses Claude, OpenAI GPT, or Google Gemini to generate intelligent tests
|
|
13
24
|
- 🔍 **AST Analysis**: Deep code analysis using Babel parser for accurate test generation
|
|
14
25
|
- 📦 **Codebase Indexing**: Optional caching for 100x faster analysis on large projects
|
|
15
26
|
- 🎯 **Multiple Modes**: File-wise, folder-wise, function-wise, or auto test generation
|
|
16
|
-
- ⚡ **Auto Mode**: Detects git changes and automatically generates tests for modified functions
|
|
17
27
|
- ✅ **Smart Validation**: Detects incomplete tests, missing assertions, and legitimate failures
|
|
18
28
|
- 🔄 **Iterative Fixing**: Automatically fixes import errors, missing mocks, and test issues
|
|
19
29
|
- 📋 **TypeScript Support**: Full support for TypeScript types, interfaces, and decorators
|
|
20
30
|
|
|
31
|
+
### Unified Workflow
|
|
32
|
+
- ⚡ **Auto Mode**: Reviews code quality + generates tests for changed functions
|
|
33
|
+
- 🔄 **Git Integration**: Detects changes via git diff (staged and unstaged)
|
|
34
|
+
- 🚀 **CI/CD Ready**: Non-interactive modes perfect for automation
|
|
35
|
+
- 📚 **Documentation Mode**: AI-powered OpenAPI/Swagger documentation generation
|
|
36
|
+
|
|
21
37
|
## Installation
|
|
22
38
|
|
|
23
39
|
### Global Installation (Recommended)
|
|
@@ -51,7 +67,14 @@ Create a `codeguard.json` file in your project root:
|
|
|
51
67
|
},
|
|
52
68
|
"testDir": "src/tests",
|
|
53
69
|
"extensions": [".ts", ".tsx", ".js", ".jsx"],
|
|
54
|
-
"excludeDirs": ["node_modules", "dist", "build", ".git", "coverage"]
|
|
70
|
+
"excludeDirs": ["node_modules", "dist", "build", ".git", "coverage"],
|
|
71
|
+
"validationInterval": 5,
|
|
72
|
+
"docsDir": "docs",
|
|
73
|
+
"docFormat": "json",
|
|
74
|
+
"docTitle": "My API Documentation",
|
|
75
|
+
"docVersion": "1.0.0",
|
|
76
|
+
"includeGenericFunctions": true,
|
|
77
|
+
"repoDoc": false
|
|
55
78
|
}
|
|
56
79
|
```
|
|
57
80
|
|
|
@@ -65,6 +88,177 @@ Create a `codeguard.json` file in your project root:
|
|
|
65
88
|
| `testDir` | No | Directory for test files (default: `src/tests`) |
|
|
66
89
|
| `extensions` | No | File extensions to process (default: `.ts`, `.tsx`, `.js`, `.jsx`) |
|
|
67
90
|
| `excludeDirs` | No | Directories to exclude from scanning |
|
|
91
|
+
| `validationInterval` | No | Validation frequency in function-wise mode: `undefined` = no validation, `0` = only at end, `N` = validate every N functions |
|
|
92
|
+
| `docsDir` | No | Directory for generated documentation (default: `docs`) |
|
|
93
|
+
| `docFormat` | No | Documentation format: `json` or `yaml` (default: `json`) |
|
|
94
|
+
| `docTitle` | No | API documentation title (default: from package.json name) |
|
|
95
|
+
| `docVersion` | No | API version (default: from package.json version) |
|
|
96
|
+
| `includeGenericFunctions` | No | Include non-API functions in documentation (default: `true`) |
|
|
97
|
+
| `repoDoc` | No | Document entire repository (`true`) or only staged changes (`false`, default) |
|
|
98
|
+
| `reviewSteps` | No | Array of review steps with custom rulesets (see below) |
|
|
99
|
+
| `reviewExecutionMode` | No | How to execute review steps: `parallel` or `sequential` (default: `parallel`) |
|
|
100
|
+
|
|
101
|
+
### Configurable Review Steps
|
|
102
|
+
|
|
103
|
+
Configure custom review steps with rulesets defined in markdown files. Each ruleset is stored in the `codeguard-ruleset/` folder at your project root.
|
|
104
|
+
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"reviewExecutionMode": "parallel",
|
|
108
|
+
"reviewSteps": [
|
|
109
|
+
{
|
|
110
|
+
"id": "code-quality",
|
|
111
|
+
"name": "Code Quality",
|
|
112
|
+
"category": "quality",
|
|
113
|
+
"type": "ai",
|
|
114
|
+
"enabled": true,
|
|
115
|
+
"ruleset": "code-quality.md"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"id": "security",
|
|
119
|
+
"name": "Security Vulnerabilities",
|
|
120
|
+
"category": "security",
|
|
121
|
+
"type": "ai",
|
|
122
|
+
"enabled": true,
|
|
123
|
+
"ruleset": "security.md"
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Review Step Options:**
|
|
130
|
+
|
|
131
|
+
| Option | Required | Description |
|
|
132
|
+
|--------|----------|-------------|
|
|
133
|
+
| `id` | Yes | Unique identifier for the step |
|
|
134
|
+
| `name` | Yes | Display name for the review step |
|
|
135
|
+
| `category` | Yes | Category of the review (e.g., `quality`, `security`, `performance`) |
|
|
136
|
+
| `type` | Yes | Type of review (currently only `ai` supported) |
|
|
137
|
+
| `enabled` | Yes | Whether this step is active (`true` or `false`) |
|
|
138
|
+
| `ruleset` | Yes | Filename of markdown ruleset in `codeguard-ruleset/` folder |
|
|
139
|
+
|
|
140
|
+
**Ruleset Files:**
|
|
141
|
+
|
|
142
|
+
Rulesets are markdown files stored in `codeguard-ruleset/` at your project root:
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
your-project/
|
|
146
|
+
├── codeguard.json
|
|
147
|
+
├── codeguard-ruleset/
|
|
148
|
+
│ ├── code-quality.md
|
|
149
|
+
│ ├── security.md
|
|
150
|
+
│ ├── performance.md
|
|
151
|
+
│ └── custom-rules.md
|
|
152
|
+
└── src/
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Each ruleset file can contain:
|
|
156
|
+
- Detailed review criteria
|
|
157
|
+
- Specific rules and guidelines
|
|
158
|
+
- Examples and code snippets
|
|
159
|
+
- Severity guidelines
|
|
160
|
+
- OWASP references (for security)
|
|
161
|
+
- Best practices documentation
|
|
162
|
+
|
|
163
|
+
**Example Ruleset File** (`codeguard-ruleset/code-quality.md`):
|
|
164
|
+
|
|
165
|
+
```markdown
|
|
166
|
+
# Code Quality Review Ruleset
|
|
167
|
+
|
|
168
|
+
## Review Criteria
|
|
169
|
+
|
|
170
|
+
### 1. Naming Conventions
|
|
171
|
+
- Functions: Use clear, descriptive names
|
|
172
|
+
- Variables: Use meaningful names
|
|
173
|
+
- Boolean variables: Prefix with is, has, should
|
|
174
|
+
|
|
175
|
+
### 2. Code Complexity
|
|
176
|
+
- Functions should be concise (< 50 lines)
|
|
177
|
+
- Cyclomatic complexity should be low (< 10)
|
|
178
|
+
- Avoid deeply nested conditionals
|
|
179
|
+
|
|
180
|
+
...
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**Execution Modes:**
|
|
184
|
+
|
|
185
|
+
- **`parallel`** (default): All enabled review steps run simultaneously for faster completion
|
|
186
|
+
- **`sequential`**: Steps run one after another in the order defined
|
|
187
|
+
|
|
188
|
+
**Default Review Steps:**
|
|
189
|
+
|
|
190
|
+
If you don't specify `reviewSteps` in your config, CodeGuard uses these default steps:
|
|
191
|
+
- ✅ **Code Quality** (`code-quality.md`) - Naming, complexity, readability, best practices
|
|
192
|
+
- ✅ **Potential Bugs** (`potential-bugs.md`) - Logic errors, edge cases, type issues, async problems
|
|
193
|
+
- ✅ **Performance** (`performance.md`) - Algorithm efficiency, unnecessary computations, memory leaks
|
|
194
|
+
- ✅ **Security** (`security.md`) - Input validation, injection risks, OWASP vulnerabilities
|
|
195
|
+
|
|
196
|
+
**Included Ruleset Files:**
|
|
197
|
+
|
|
198
|
+
CodeGuard comes with comprehensive default rulesets in `codeguard-ruleset/`:
|
|
199
|
+
- `code-quality.md` - 8 categories including naming, complexity, patterns, error handling
|
|
200
|
+
- `potential-bugs.md` - 8 categories covering logic errors, edge cases, async issues
|
|
201
|
+
- `performance.md` - 8 categories for algorithms, caching, data structures, optimizations
|
|
202
|
+
- `security.md` - OWASP Top 10 coverage with specific checks and references
|
|
203
|
+
|
|
204
|
+
You can customize these files or create your own rulesets for project-specific requirements.
|
|
205
|
+
|
|
206
|
+
**Output Format:**
|
|
207
|
+
|
|
208
|
+
Reviews are organized by step in the final markdown file:
|
|
209
|
+
|
|
210
|
+
```markdown
|
|
211
|
+
# Code Review
|
|
212
|
+
|
|
213
|
+
## Summary
|
|
214
|
+
[Overall assessment]
|
|
215
|
+
|
|
216
|
+
## Files Changed
|
|
217
|
+
[List of files]
|
|
218
|
+
|
|
219
|
+
## Code Quality
|
|
220
|
+
[Findings from code quality step]
|
|
221
|
+
|
|
222
|
+
## Security Vulnerabilities
|
|
223
|
+
[Findings from security step]
|
|
224
|
+
|
|
225
|
+
## Performance Issues
|
|
226
|
+
[Findings from performance step]
|
|
227
|
+
|
|
228
|
+
## Conclusion
|
|
229
|
+
[Final assessment]
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
See `codeguard.example.json` for a complete configuration example with additional review steps like Accessibility and Documentation Quality.
|
|
233
|
+
|
|
234
|
+
### Validation Interval
|
|
235
|
+
|
|
236
|
+
The `validationInterval` option controls when the full test suite is validated during function-wise test generation:
|
|
237
|
+
|
|
238
|
+
- **`undefined` (default)**: No periodic validation - fastest, tests each function independently
|
|
239
|
+
- **`0`**: Validate only at the end after all functions are processed
|
|
240
|
+
- **`N` (number)**: Validate every N functions to catch integration issues early
|
|
241
|
+
|
|
242
|
+
**Example use cases:**
|
|
243
|
+
```json
|
|
244
|
+
{
|
|
245
|
+
"validationInterval": undefined // Fast - no validation checkpoints
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
```json
|
|
250
|
+
{
|
|
251
|
+
"validationInterval": 5 // Validate after every 5 functions
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
```json
|
|
256
|
+
{
|
|
257
|
+
"validationInterval": 0 // Validate only once at the end
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**Recommendation**: Use `5` or `10` for large files with many functions to catch integration issues early. Use `undefined` for fastest processing.
|
|
68
262
|
|
|
69
263
|
### Getting API Keys
|
|
70
264
|
|
|
@@ -77,7 +271,9 @@ Create a `codeguard.json` file in your project root:
|
|
|
77
271
|
|
|
78
272
|
| Command | Description | Use Case |
|
|
79
273
|
|---------|-------------|----------|
|
|
80
|
-
| `testgen auto` |
|
|
274
|
+
| `testgen auto` | Review code quality + generate tests | Complete workflow, CI/CD |
|
|
275
|
+
| `testgen review` | Only review code changes | Code review, quality checks |
|
|
276
|
+
| `testgen test` | Only generate tests for changes | Testing workflow |
|
|
81
277
|
| `testgen` | Interactive mode - choose mode manually | Exploratory testing |
|
|
82
278
|
| Mode 1: File-wise | Generate tests for entire file | New files, comprehensive coverage |
|
|
83
279
|
| Mode 2: Folder-wise | Generate tests for all files in folder | Batch processing |
|
|
@@ -85,18 +281,94 @@ Create a `codeguard.json` file in your project root:
|
|
|
85
281
|
|
|
86
282
|
## Usage
|
|
87
283
|
|
|
88
|
-
### Auto Mode (Recommended
|
|
284
|
+
### Auto Mode - Complete Workflow (Recommended)
|
|
89
285
|
|
|
90
|
-
Automatically
|
|
286
|
+
Automatically review code quality and generate tests for changed functions:
|
|
91
287
|
|
|
92
288
|
```bash
|
|
93
289
|
testgen auto
|
|
94
290
|
```
|
|
95
291
|
|
|
96
|
-
|
|
292
|
+
**What it does:**
|
|
293
|
+
1. **Reviews changed code** for quality, bugs, performance, and security issues
|
|
294
|
+
2. **Generates comprehensive tests** for modified functions
|
|
295
|
+
3. Saves review to `reviews/{filename}.review.md`
|
|
296
|
+
4. Creates or updates test files
|
|
297
|
+
|
|
298
|
+
**Example output:**
|
|
299
|
+
```
|
|
300
|
+
🔍 Scanning git changes for review...
|
|
301
|
+
📝 Found changes in 1 file(s) to review
|
|
302
|
+
|
|
303
|
+
🔄 Reviewing: src/services/user.service.ts
|
|
304
|
+
📦 Changed functions: createUser, updateUser
|
|
305
|
+
✅ Review completed
|
|
306
|
+
|
|
307
|
+
📁 Reviews saved to: reviews/ directory
|
|
308
|
+
|
|
309
|
+
============================================================
|
|
310
|
+
|
|
311
|
+
🔍 Scanning git changes for testing...
|
|
312
|
+
📝 Found changes in 1 file(s)
|
|
313
|
+
|
|
314
|
+
🔄 Processing: src/services/user.service.ts
|
|
315
|
+
📦 Changed functions: createUser, updateUser
|
|
316
|
+
✅ Tests generated successfully
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Review Only Mode
|
|
320
|
+
|
|
321
|
+
Get AI code review without generating tests:
|
|
97
322
|
|
|
98
323
|
```bash
|
|
99
|
-
|
|
324
|
+
testgen review
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
**What gets reviewed:**
|
|
328
|
+
- 🎯 **Code Quality**: Naming, complexity, readability, best practices
|
|
329
|
+
- 🐛 **Potential Bugs**: Logic errors, edge cases, type mismatches, async issues
|
|
330
|
+
- ⚡ **Performance**: Inefficient algorithms, memory leaks, unnecessary computations
|
|
331
|
+
- 🔒 **Security**: Input validation, injection risks, authentication issues
|
|
332
|
+
|
|
333
|
+
**Review output** (`reviews/{filename}.review.md`):
|
|
334
|
+
```markdown
|
|
335
|
+
# Code Review: user.service.ts
|
|
336
|
+
|
|
337
|
+
## Summary
|
|
338
|
+
Overall code quality is good with some areas for improvement...
|
|
339
|
+
|
|
340
|
+
## Findings
|
|
341
|
+
|
|
342
|
+
### 🔴 Critical Issues
|
|
343
|
+
#### [Security] Missing Input Validation
|
|
344
|
+
**Function**: `createUser`
|
|
345
|
+
**Issue**: Email parameter not validated before database insertion...
|
|
346
|
+
**Recommended Fix**:
|
|
347
|
+
...typescript
|
|
348
|
+
if (!email || !email.includes('@')) {
|
|
349
|
+
throw new Error('Invalid email');
|
|
350
|
+
}
|
|
351
|
+
...
|
|
352
|
+
|
|
353
|
+
### 🟡 Medium Priority Issues
|
|
354
|
+
#### [Performance] Inefficient Loop
|
|
355
|
+
...
|
|
356
|
+
|
|
357
|
+
## ✅ Positive Aspects
|
|
358
|
+
- Well-structured error handling
|
|
359
|
+
- Clear function naming
|
|
360
|
+
|
|
361
|
+
## 💡 General Recommendations
|
|
362
|
+
1. Add input validation for all public functions
|
|
363
|
+
2. Consider adding JSDoc comments
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Test Generation Only Mode
|
|
367
|
+
|
|
368
|
+
Generate tests without code review:
|
|
369
|
+
|
|
370
|
+
```bash
|
|
371
|
+
testgen test
|
|
100
372
|
```
|
|
101
373
|
|
|
102
374
|
**How it works:**
|
|
@@ -106,7 +378,9 @@ codeguard auto
|
|
|
106
378
|
4. Automatically generates or updates tests for those functions
|
|
107
379
|
5. No user interaction required - perfect for automation!
|
|
108
380
|
|
|
109
|
-
**Example
|
|
381
|
+
**Example workflows:**
|
|
382
|
+
|
|
383
|
+
**Complete workflow (review + test):**
|
|
110
384
|
```bash
|
|
111
385
|
# Make changes to your code
|
|
112
386
|
vim src/services/user.service.ts
|
|
@@ -114,10 +388,31 @@ vim src/services/user.service.ts
|
|
|
114
388
|
# Stage your changes
|
|
115
389
|
git add src/services/user.service.ts
|
|
116
390
|
|
|
117
|
-
#
|
|
391
|
+
# Review code quality and generate tests
|
|
118
392
|
testgen auto
|
|
119
393
|
```
|
|
120
394
|
|
|
395
|
+
**Review only:**
|
|
396
|
+
```bash
|
|
397
|
+
# Get code review for staged changes
|
|
398
|
+
testgen review
|
|
399
|
+
|
|
400
|
+
# Check the review
|
|
401
|
+
cat reviews/user.service.review.md
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
**Test generation only:**
|
|
405
|
+
```bash
|
|
406
|
+
# Generate tests without review
|
|
407
|
+
testgen test
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
**Documentation generation:**
|
|
411
|
+
```bash
|
|
412
|
+
# Generate API documentation
|
|
413
|
+
testgen doc
|
|
414
|
+
```
|
|
415
|
+
|
|
121
416
|
**Output:**
|
|
122
417
|
```
|
|
123
418
|
🧪 AI-Powered Unit Test Generator with AST Analysis
|
|
@@ -143,10 +438,12 @@ testgen auto
|
|
|
143
438
|
```
|
|
144
439
|
|
|
145
440
|
**Benefits:**
|
|
441
|
+
- 🔍 **Quality Assurance**: Catch issues before they reach production
|
|
146
442
|
- ⚡ **Fast**: Only processes changed files
|
|
147
|
-
- 🎯 **Targeted**:
|
|
443
|
+
- 🎯 **Targeted**: Reviews and tests only modified functions
|
|
148
444
|
- 🔄 **CI/CD Ready**: Non-interactive, perfect for automation
|
|
149
445
|
- 🛡️ **Safe**: Preserves existing tests for unchanged functions
|
|
446
|
+
- 📊 **Trackable**: All reviews saved for historical reference
|
|
150
447
|
|
|
151
448
|
**What files are processed:**
|
|
152
449
|
- ✅ Source files with supported extensions (`.ts`, `.tsx`, `.js`, `.jsx`)
|
|
@@ -195,16 +492,174 @@ Generate tests for specific functions:
|
|
|
195
492
|
- Preserves existing tests for other functions
|
|
196
493
|
- Ideal for incremental test development
|
|
197
494
|
|
|
198
|
-
#### 4. Auto Mode
|
|
199
|
-
|
|
495
|
+
#### 4. Auto Mode (Unified)
|
|
496
|
+
Review code quality and generate tests automatically:
|
|
200
497
|
- Analyzes git diff (staged and unstaged changes)
|
|
201
|
-
- AI
|
|
202
|
-
- Generates
|
|
498
|
+
- AI reviews code for quality, bugs, performance, security
|
|
499
|
+
- Generates comprehensive review markdown files
|
|
500
|
+
- Creates tests for changed exported functions
|
|
203
501
|
- Non-interactive - perfect for CI/CD pipelines
|
|
204
502
|
- Use: `testgen auto`
|
|
205
503
|
|
|
504
|
+
#### 5. Review Mode
|
|
505
|
+
AI-powered code review only:
|
|
506
|
+
- Comprehensive analysis by senior-level AI reviewer
|
|
507
|
+
- Reviews code quality, potential bugs, performance issues, security vulnerabilities
|
|
508
|
+
- Uses AST tools to understand full context
|
|
509
|
+
- Generates structured markdown reports
|
|
510
|
+
- Use: `testgen review`
|
|
511
|
+
|
|
512
|
+
#### 6. Test Mode
|
|
513
|
+
Test generation only:
|
|
514
|
+
- Generates tests for changed functions
|
|
515
|
+
- Skips code review process
|
|
516
|
+
- Faster when you only need tests
|
|
517
|
+
- Use: `testgen test`
|
|
518
|
+
|
|
519
|
+
#### 7. Documentation Mode
|
|
520
|
+
AI-powered API documentation generation:
|
|
521
|
+
- **Default**: Documents only staged/changed functions (like review/test modes)
|
|
522
|
+
- **Full Repo**: Set `"repoDoc": true` to document entire codebase
|
|
523
|
+
- Analyzes codebase using AST tools
|
|
524
|
+
- Auto-detects API endpoints (Express, NestJS, Fastify, Koa)
|
|
525
|
+
- Generates comprehensive OpenAPI 3.0 specification
|
|
526
|
+
- Documents both API routes and generic functions
|
|
527
|
+
- Smart merge with existing documentation
|
|
528
|
+
- Supports JSON and YAML formats
|
|
529
|
+
- Use: `testgen doc`
|
|
530
|
+
|
|
531
|
+
**Two modes:**
|
|
532
|
+
|
|
533
|
+
**1. Changed Files Only (Default)** - `"repoDoc": false` or omitted
|
|
534
|
+
- Works like `testgen review` and `testgen test`
|
|
535
|
+
- Only documents staged/changed functions
|
|
536
|
+
- Fast and targeted
|
|
537
|
+
- Perfect for incremental updates
|
|
538
|
+
- Requires git repository
|
|
539
|
+
|
|
540
|
+
**2. Full Repository** - `"repoDoc": true`
|
|
541
|
+
- Documents entire codebase
|
|
542
|
+
- Comprehensive documentation generation
|
|
543
|
+
- Useful for initial documentation or major updates
|
|
544
|
+
- No git requirement
|
|
545
|
+
|
|
546
|
+
**What it documents:**
|
|
547
|
+
- ✅ **API Endpoints**: All REST API routes with methods, paths, parameters
|
|
548
|
+
- ✅ **Request/Response Schemas**: Inferred from TypeScript types
|
|
549
|
+
- ✅ **Authentication**: Detects and documents auth requirements
|
|
550
|
+
- ✅ **Error Responses**: Documents error cases and status codes
|
|
551
|
+
- ✅ **Generic Functions**: Optional documentation for utility functions
|
|
552
|
+
- ✅ **Usage Examples**: AI-generated examples for each endpoint
|
|
553
|
+
|
|
554
|
+
**Supported Frameworks:**
|
|
555
|
+
- **Express**: `app.get()`, `router.post()`, route methods
|
|
556
|
+
- **NestJS**: `@Controller()`, `@Get()`, `@Post()`, decorators
|
|
557
|
+
- **Fastify**: `fastify.route()`, route configurations
|
|
558
|
+
- **Koa**: `router.get()`, middleware patterns
|
|
559
|
+
|
|
560
|
+
**Example usage:**
|
|
561
|
+
```bash
|
|
562
|
+
# Document only changed/staged functions (default)
|
|
563
|
+
testgen doc
|
|
564
|
+
|
|
565
|
+
# Output:
|
|
566
|
+
# 📚 Documentation Mode: Generating API documentation
|
|
567
|
+
#
|
|
568
|
+
# 🔍 Scanning git changes for documentation...
|
|
569
|
+
#
|
|
570
|
+
# 📝 Found changes in 2 file(s)
|
|
571
|
+
#
|
|
572
|
+
# 🤖 Generating OpenAPI specification...
|
|
573
|
+
#
|
|
574
|
+
# ✅ Documentation generated successfully
|
|
575
|
+
#
|
|
576
|
+
# ============================================================
|
|
577
|
+
# 📊 Documentation Summary
|
|
578
|
+
# ============================================================
|
|
579
|
+
# ✅ API Endpoints documented: 5
|
|
580
|
+
# ✅ Generic functions documented: 8
|
|
581
|
+
# 📁 Output: docs/openapi.json
|
|
582
|
+
# ============================================================
|
|
583
|
+
|
|
584
|
+
# For full repository documentation, set in codeguard.json:
|
|
585
|
+
# {
|
|
586
|
+
# "repoDoc": true
|
|
587
|
+
# }
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
**Generated OpenAPI spec:**
|
|
591
|
+
```json
|
|
592
|
+
{
|
|
593
|
+
"openapi": "3.0.0",
|
|
594
|
+
"info": {
|
|
595
|
+
"title": "My API",
|
|
596
|
+
"version": "1.0.0"
|
|
597
|
+
},
|
|
598
|
+
"paths": {
|
|
599
|
+
"/users": {
|
|
600
|
+
"get": {
|
|
601
|
+
"summary": "Get all users",
|
|
602
|
+
"responses": {
|
|
603
|
+
"200": {
|
|
604
|
+
"description": "Success",
|
|
605
|
+
"content": {
|
|
606
|
+
"application/json": {
|
|
607
|
+
"schema": {
|
|
608
|
+
"type": "array",
|
|
609
|
+
"items": { "$ref": "#/components/schemas/User" }
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
},
|
|
618
|
+
"components": {
|
|
619
|
+
"schemas": {
|
|
620
|
+
"User": {
|
|
621
|
+
"type": "object",
|
|
622
|
+
"properties": {
|
|
623
|
+
"id": { "type": "string" },
|
|
624
|
+
"name": { "type": "string" },
|
|
625
|
+
"email": { "type": "string" }
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
**Smart merging:**
|
|
634
|
+
When existing documentation is found, CodeGuard intelligently merges:
|
|
635
|
+
- ✅ Preserves manually edited descriptions and summaries
|
|
636
|
+
- ✅ Updates schemas with latest types from code
|
|
637
|
+
- ✅ Adds new endpoints without removing manual changes
|
|
638
|
+
- ✅ Maintains custom examples and documentation
|
|
639
|
+
- ✅ Tracks generation metadata and timestamps
|
|
640
|
+
|
|
206
641
|
## How It Works
|
|
207
642
|
|
|
643
|
+
### Code Review Process
|
|
644
|
+
1. **Git Diff Analysis**: Detects changed files and functions
|
|
645
|
+
2. **AST Analysis**: Deep parse of code structure using Babel
|
|
646
|
+
3. **Context Understanding**: AI uses tools to analyze:
|
|
647
|
+
- Function implementations
|
|
648
|
+
- Dependencies and imports
|
|
649
|
+
- Type definitions
|
|
650
|
+
- Related code context
|
|
651
|
+
4. **Multi-Aspect Review**: Analyzes for:
|
|
652
|
+
- Code quality and best practices
|
|
653
|
+
- Potential bugs and edge cases
|
|
654
|
+
- Performance bottlenecks
|
|
655
|
+
- Security vulnerabilities
|
|
656
|
+
5. **Structured Report**: Generates markdown with:
|
|
657
|
+
- Severity-based findings
|
|
658
|
+
- Code snippets and fixes
|
|
659
|
+
- Positive observations
|
|
660
|
+
- Actionable recommendations
|
|
661
|
+
|
|
662
|
+
### Test Generation Process
|
|
208
663
|
1. **AST Analysis**: Parses your code using Babel to understand structure
|
|
209
664
|
2. **Dependency Resolution**: Analyzes imports and calculates correct paths
|
|
210
665
|
3. **AI Generation**: Uses AI to generate comprehensive test cases
|
|
@@ -217,6 +672,24 @@ Automatically detect and test changed functions:
|
|
|
217
672
|
- Type mismatches
|
|
218
673
|
7. **Failure Detection**: Distinguishes between test bugs and source code bugs
|
|
219
674
|
|
|
675
|
+
### Documentation Generation Process
|
|
676
|
+
1. **File Scanning**: Recursively scans all source files in the project
|
|
677
|
+
2. **AST Analysis**: Parses each file using Babel to understand structure
|
|
678
|
+
3. **Endpoint Detection**: AI identifies API routes across different frameworks:
|
|
679
|
+
- Express: `app.METHOD()`, `router.METHOD()`
|
|
680
|
+
- NestJS: `@Controller()`, `@Get()`, `@Post()`, etc.
|
|
681
|
+
- Fastify: `fastify.route()`, route configurations
|
|
682
|
+
- Koa: `router.METHOD()`, middleware chains
|
|
683
|
+
4. **Schema Inference**: Extracts TypeScript types for request/response schemas
|
|
684
|
+
5. **AI Enhancement**: AI generates:
|
|
685
|
+
- Meaningful descriptions for each endpoint
|
|
686
|
+
- Parameter documentation
|
|
687
|
+
- Response examples
|
|
688
|
+
- Error scenarios
|
|
689
|
+
6. **OpenAPI Generation**: Builds complete OpenAPI 3.0 specification
|
|
690
|
+
7. **Smart Merge**: Intelligently merges with existing documentation
|
|
691
|
+
8. **File Output**: Writes to `docs/openapi.json` or `.yaml`
|
|
692
|
+
|
|
220
693
|
## Generated Test Features
|
|
221
694
|
|
|
222
695
|
The AI generates tests with:
|
|
@@ -236,21 +709,19 @@ The AI generates tests with:
|
|
|
236
709
|
|
|
237
710
|
### CI/CD Integration
|
|
238
711
|
|
|
239
|
-
|
|
712
|
+
CodeGuard modes are designed for continuous integration workflows:
|
|
240
713
|
|
|
241
|
-
**GitHub Actions
|
|
714
|
+
**GitHub Actions - Complete Workflow (Review + Tests):**
|
|
242
715
|
|
|
243
716
|
```yaml
|
|
244
|
-
name:
|
|
717
|
+
name: AI Code Review & Test Generation
|
|
245
718
|
|
|
246
719
|
on:
|
|
247
|
-
push:
|
|
248
|
-
branches: [ main, develop ]
|
|
249
720
|
pull_request:
|
|
250
|
-
branches: [ main ]
|
|
721
|
+
branches: [ main, develop ]
|
|
251
722
|
|
|
252
723
|
jobs:
|
|
253
|
-
|
|
724
|
+
review-and-test:
|
|
254
725
|
runs-on: ubuntu-latest
|
|
255
726
|
steps:
|
|
256
727
|
- uses: actions/checkout@v3
|
|
@@ -268,33 +739,98 @@ jobs:
|
|
|
268
739
|
- name: Install CodeGuard
|
|
269
740
|
run: npm install -g codeguard-testgen
|
|
270
741
|
|
|
271
|
-
- name:
|
|
742
|
+
- name: Review code and generate tests
|
|
272
743
|
env:
|
|
273
744
|
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
|
274
745
|
run: testgen auto
|
|
275
746
|
|
|
276
|
-
- name:
|
|
747
|
+
- name: Upload review reports
|
|
748
|
+
uses: actions/upload-artifact@v3
|
|
749
|
+
with:
|
|
750
|
+
name: code-reviews
|
|
751
|
+
path: reviews/
|
|
752
|
+
|
|
753
|
+
- name: Commit generated tests and reviews
|
|
277
754
|
run: |
|
|
278
755
|
git config --local user.email "action@github.com"
|
|
279
756
|
git config --local user.name "GitHub Action"
|
|
280
|
-
git add src/tests/
|
|
281
|
-
git commit -m "🤖
|
|
757
|
+
git add src/tests/ reviews/
|
|
758
|
+
git commit -m "🤖 AI code review + tests for changed functions" || echo "No changes"
|
|
282
759
|
git push
|
|
283
760
|
```
|
|
284
761
|
|
|
762
|
+
**GitHub Actions - Review Only:**
|
|
763
|
+
|
|
764
|
+
```yaml
|
|
765
|
+
name: AI Code Review
|
|
766
|
+
|
|
767
|
+
on:
|
|
768
|
+
pull_request:
|
|
769
|
+
branches: [ main ]
|
|
770
|
+
|
|
771
|
+
jobs:
|
|
772
|
+
code-review:
|
|
773
|
+
runs-on: ubuntu-latest
|
|
774
|
+
steps:
|
|
775
|
+
- uses: actions/checkout@v3
|
|
776
|
+
with:
|
|
777
|
+
fetch-depth: 0
|
|
778
|
+
|
|
779
|
+
- name: Setup Node.js
|
|
780
|
+
uses: actions/setup-node@v3
|
|
781
|
+
with:
|
|
782
|
+
node-version: '18'
|
|
783
|
+
|
|
784
|
+
- name: Install CodeGuard
|
|
785
|
+
run: npm install -g codeguard-testgen
|
|
786
|
+
|
|
787
|
+
- name: AI Code Review
|
|
788
|
+
env:
|
|
789
|
+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
|
790
|
+
run: testgen review
|
|
791
|
+
|
|
792
|
+
- name: Comment PR with review
|
|
793
|
+
uses: actions/github-script@v6
|
|
794
|
+
with:
|
|
795
|
+
script: |
|
|
796
|
+
const fs = require('fs');
|
|
797
|
+
const reviews = fs.readdirSync('reviews/');
|
|
798
|
+
for (const review of reviews) {
|
|
799
|
+
const content = fs.readFileSync(`reviews/${review}`, 'utf8');
|
|
800
|
+
github.rest.issues.createComment({
|
|
801
|
+
issue_number: context.issue.number,
|
|
802
|
+
owner: context.repo.owner,
|
|
803
|
+
repo: context.repo.repo,
|
|
804
|
+
body: `## AI Code Review: ${review}\n\n${content}`
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
```
|
|
808
|
+
|
|
285
809
|
**GitLab CI Example:**
|
|
286
810
|
|
|
287
811
|
```yaml
|
|
288
|
-
|
|
289
|
-
stage:
|
|
812
|
+
review-and-test:
|
|
813
|
+
stage: quality
|
|
290
814
|
script:
|
|
291
815
|
- npm install -g codeguard-testgen
|
|
292
|
-
- testgen auto
|
|
816
|
+
- testgen auto # Review + tests
|
|
293
817
|
artifacts:
|
|
294
818
|
paths:
|
|
819
|
+
- reviews/
|
|
295
820
|
- src/tests/
|
|
296
821
|
only:
|
|
297
822
|
- merge_requests
|
|
823
|
+
|
|
824
|
+
review-only:
|
|
825
|
+
stage: quality
|
|
826
|
+
script:
|
|
827
|
+
- npm install -g codeguard-testgen
|
|
828
|
+
- testgen review
|
|
829
|
+
artifacts:
|
|
830
|
+
reports:
|
|
831
|
+
codequality: reviews/
|
|
832
|
+
only:
|
|
833
|
+
- merge_requests
|
|
298
834
|
```
|
|
299
835
|
|
|
300
836
|
**Pre-commit Hook:**
|
|
@@ -303,11 +839,24 @@ generate-tests:
|
|
|
303
839
|
#!/bin/bash
|
|
304
840
|
# .git/hooks/pre-commit
|
|
305
841
|
|
|
306
|
-
#
|
|
842
|
+
# Review code and generate tests for staged changes
|
|
307
843
|
testgen auto
|
|
308
844
|
|
|
309
|
-
# Add generated tests to commit
|
|
310
|
-
git add src/tests/
|
|
845
|
+
# Add generated tests and reviews to commit
|
|
846
|
+
git add src/tests/ reviews/
|
|
847
|
+
```
|
|
848
|
+
|
|
849
|
+
**Pre-push Hook (Review Only):**
|
|
850
|
+
|
|
851
|
+
```bash
|
|
852
|
+
#!/bin/bash
|
|
853
|
+
# .git/hooks/pre-push
|
|
854
|
+
|
|
855
|
+
# Quick code review before pushing
|
|
856
|
+
testgen review
|
|
857
|
+
|
|
858
|
+
# Show review summary
|
|
859
|
+
echo "📊 Code Review Complete - Check reviews/ directory"
|
|
311
860
|
```
|
|
312
861
|
|
|
313
862
|
### Codebase Indexing
|
|
@@ -346,7 +895,7 @@ When legitimate bugs are found, they're reported with details for you to fix in
|
|
|
346
895
|
|
|
347
896
|
## Examples
|
|
348
897
|
|
|
349
|
-
### Example 1: Auto Mode
|
|
898
|
+
### Example 1: Complete Workflow (Auto Mode)
|
|
350
899
|
|
|
351
900
|
**Step 1: Make changes to a function**
|
|
352
901
|
```typescript
|
|
@@ -373,7 +922,16 @@ testgen auto
|
|
|
373
922
|
|
|
374
923
|
**Output:**
|
|
375
924
|
```
|
|
376
|
-
🔍 Scanning git changes...
|
|
925
|
+
🔍 Scanning git changes for review...
|
|
926
|
+
📝 Found changes in 1 file(s)
|
|
927
|
+
|
|
928
|
+
🔄 Reviewing: src/services/user.service.ts
|
|
929
|
+
📦 Changed functions: createUser
|
|
930
|
+
✅ Review completed
|
|
931
|
+
|
|
932
|
+
============================================================
|
|
933
|
+
|
|
934
|
+
🔍 Scanning git changes for testing...
|
|
377
935
|
📝 Found changes in 1 file(s)
|
|
378
936
|
|
|
379
937
|
🔄 Processing: src/services/user.service.ts
|
|
@@ -381,7 +939,26 @@ testgen auto
|
|
|
381
939
|
✅ Tests generated successfully
|
|
382
940
|
```
|
|
383
941
|
|
|
384
|
-
**
|
|
942
|
+
**Results:**
|
|
943
|
+
- `reviews/user.service.review.md` created with code quality analysis
|
|
944
|
+
- Only `createUser` gets new tests, `deleteUser` tests remain unchanged!
|
|
945
|
+
|
|
946
|
+
**Review excerpt:**
|
|
947
|
+
```markdown
|
|
948
|
+
### 🟡 Medium Priority Issues
|
|
949
|
+
|
|
950
|
+
#### [Code Quality] Weak Email Validation
|
|
951
|
+
**Function**: `createUser`
|
|
952
|
+
**Issue**: Email validation only checks for '@' symbol, not comprehensive
|
|
953
|
+
|
|
954
|
+
**Recommended Fix**:
|
|
955
|
+
```typescript
|
|
956
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
957
|
+
if (!emailRegex.test(email)) {
|
|
958
|
+
throw new Error('Invalid email format');
|
|
959
|
+
}
|
|
960
|
+
```
|
|
961
|
+
```
|
|
385
962
|
|
|
386
963
|
### Example 2: Testing a User Service
|
|
387
964
|
|
|
@@ -425,10 +1002,10 @@ describe('UserService', () => {
|
|
|
425
1002
|
|
|
426
1003
|
## Troubleshooting
|
|
427
1004
|
|
|
428
|
-
###
|
|
1005
|
+
### Command Mode Issues
|
|
429
1006
|
|
|
430
1007
|
#### "Not a git repository"
|
|
431
|
-
|
|
1008
|
+
The `auto`, `test`, and `review` commands require git to detect changes. Initialize git in your project:
|
|
432
1009
|
```bash
|
|
433
1010
|
git init
|
|
434
1011
|
```
|
|
@@ -445,6 +1022,14 @@ git status
|
|
|
445
1022
|
git diff
|
|
446
1023
|
```
|
|
447
1024
|
|
|
1025
|
+
#### Review/Test mode not working
|
|
1026
|
+
Make sure you're using the correct command:
|
|
1027
|
+
```bash
|
|
1028
|
+
testgen auto # Review + tests
|
|
1029
|
+
testgen review # Only review
|
|
1030
|
+
testgen test # Only tests
|
|
1031
|
+
```
|
|
1032
|
+
|
|
448
1033
|
#### "No exported functions changed"
|
|
449
1034
|
Possible causes:
|
|
450
1035
|
1. **AI model misconfigured**: Check your `codeguard.json` has a valid model name:
|
|
@@ -534,6 +1119,8 @@ your-project/
|
|
|
534
1119
|
│ └── tests/ # Generated tests
|
|
535
1120
|
│ └── services/
|
|
536
1121
|
│ └── user.service.test.ts
|
|
1122
|
+
├── reviews/ # AI code reviews
|
|
1123
|
+
│ └── user.service.review.md
|
|
537
1124
|
└── .codeguard-cache/ # Optional index cache
|
|
538
1125
|
```
|
|
539
1126
|
|