codeguard-testgen 1.0.14 → 1.0.16
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 +157 -1034
- package/dist/ai.d.ts +8 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +332 -0
- package/dist/ai.js.map +1 -0
- package/dist/ast.d.ts +8 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +988 -0
- package/dist/ast.js.map +1 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +4 -0
- package/dist/config.js.map +1 -1
- package/dist/git.d.ts +18 -0
- package/dist/git.d.ts.map +1 -0
- package/dist/git.js +208 -0
- package/dist/git.js.map +1 -0
- package/dist/globals.d.ts +24 -0
- package/dist/globals.d.ts.map +1 -0
- package/dist/globals.js +40 -0
- package/dist/globals.js.map +1 -0
- package/dist/index.d.ts +9 -54
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +85 -5434
- package/dist/index.js.map +1 -1
- package/dist/pathResolver.d.ts +12 -0
- package/dist/pathResolver.d.ts.map +1 -0
- package/dist/pathResolver.js +44 -0
- package/dist/pathResolver.js.map +1 -0
- package/dist/reviewer.d.ts +13 -0
- package/dist/reviewer.d.ts.map +1 -0
- package/dist/reviewer.js +402 -0
- package/dist/reviewer.js.map +1 -0
- package/dist/testGenerator.d.ts +24 -0
- package/dist/testGenerator.d.ts.map +1 -0
- package/dist/testGenerator.js +1107 -0
- package/dist/testGenerator.js.map +1 -0
- package/dist/toolDefinitions.d.ts +6 -0
- package/dist/toolDefinitions.d.ts.map +1 -0
- package/dist/toolDefinitions.js +370 -0
- package/dist/toolDefinitions.js.map +1 -0
- package/dist/toolHandlers.d.ts +76 -0
- package/dist/toolHandlers.d.ts.map +1 -0
- package/dist/toolHandlers.js +1430 -0
- package/dist/toolHandlers.js.map +1 -0
- package/dist/types.d.ts +74 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -1,1157 +1,280 @@
|
|
|
1
|
-
# CodeGuard
|
|
1
|
+
# CodeGuard
|
|
2
2
|
|
|
3
|
-
AI-powered code review and unit test generator
|
|
3
|
+
**AI-powered code review and unit test generator for TypeScript/JavaScript projects.**
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
> ```bash
|
|
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
|
|
11
|
-
> ```
|
|
5
|
+
CodeGuard combines [Abstract Syntax Tree analysis](#how-it-works) with large language models (Claude, OpenAI, or Gemini) to automatically review code quality and generate comprehensive test suites — either on demand or as part of your CI/CD pipeline.
|
|
12
6
|
|
|
13
|
-
|
|
7
|
+
---
|
|
14
8
|
|
|
15
|
-
|
|
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
|
|
9
|
+
## Documentation
|
|
21
10
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
|
11
|
+
| Document | Contents |
|
|
12
|
+
|----------|---------|
|
|
13
|
+
| **This file** | Quick-start, commands, and feature overview |
|
|
14
|
+
| [docs/CONFIGURATION.md](docs/CONFIGURATION.md) | Complete `codeguard.json` reference |
|
|
15
|
+
| [docs/API.md](docs/API.md) | Exported functions, types, and interfaces |
|
|
16
|
+
| [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) | System design, module map, data-flow diagrams |
|
|
17
|
+
| [docs/CONTRIBUTING.md](docs/CONTRIBUTING.md) | Development setup and contribution guide |
|
|
18
|
+
|
|
19
|
+
---
|
|
36
20
|
|
|
37
21
|
## Installation
|
|
38
22
|
|
|
39
|
-
|
|
23
|
+
**Global (recommended):**
|
|
40
24
|
|
|
41
25
|
```bash
|
|
42
26
|
npm install -g codeguard-testgen
|
|
43
27
|
```
|
|
44
28
|
|
|
45
|
-
|
|
29
|
+
**Local (project dev dependency):**
|
|
46
30
|
|
|
47
31
|
```bash
|
|
48
32
|
npm install --save-dev codeguard-testgen
|
|
49
33
|
```
|
|
50
34
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
Create a `codeguard.json` file in your project root:
|
|
54
|
-
|
|
55
|
-
```json
|
|
56
|
-
{
|
|
57
|
-
"aiProvider": "claude",
|
|
58
|
-
"apiKeys": {
|
|
59
|
-
"claude": "sk-ant-api03-...",
|
|
60
|
-
"openai": "sk-...",
|
|
61
|
-
"gemini": "..."
|
|
62
|
-
},
|
|
63
|
-
"models": {
|
|
64
|
-
"claude": "claude-sonnet-4-20250514",
|
|
65
|
-
"openai": "gpt-4o-mini",
|
|
66
|
-
"gemini": "gemini-2.0-flash-exp"
|
|
67
|
-
},
|
|
68
|
-
"testEnv": "vitest/jest",
|
|
69
|
-
"testDir": "src/tests",
|
|
70
|
-
"excludeDirs": ["node_modules", "dist", "build", ".git", "coverage"],
|
|
71
|
-
"validationInterval": 5,
|
|
72
|
-
"reviewExecutionMode": "parallel",
|
|
73
|
-
"reviewSteps": [
|
|
74
|
-
{
|
|
75
|
-
"id": "code-quality",
|
|
76
|
-
"name": "Code Quality",
|
|
77
|
-
"category": "quality",
|
|
78
|
-
"type": "ai",
|
|
79
|
-
"enabled": true,
|
|
80
|
-
"ruleset": "code-quality.md"
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
"id": "security",
|
|
84
|
-
"name": "Security",
|
|
85
|
-
"category": "security",
|
|
86
|
-
"type": "ai",
|
|
87
|
-
"enabled": true,
|
|
88
|
-
"ruleset": "security.md"
|
|
89
|
-
}
|
|
90
|
-
]
|
|
91
|
-
}
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Configuration Options
|
|
95
|
-
|
|
96
|
-
| Option | Required | Description |
|
|
97
|
-
|--------|----------|-------------|
|
|
98
|
-
| `aiProvider` | Yes | AI provider to use: `claude`, `openai`, or `gemini` |
|
|
99
|
-
| `apiKeys` | Yes | API keys for the AI providers |
|
|
100
|
-
| `models` | No | Custom model names (uses defaults if not specified) |
|
|
101
|
-
| `testDir` | No | Directory for test files (default: `src/tests`) |
|
|
102
|
-
| `extensions` | No | File extensions to process (default: `.ts`, `.tsx`, `.js`, `.jsx`) |
|
|
103
|
-
| `excludeDirs` | No | Directories to exclude from scanning |
|
|
104
|
-
| `validationInterval` | No | Validation frequency in function-wise mode: `undefined` = no validation, `0` = only at end, `N` = validate every N functions |
|
|
105
|
-
| `docsDir` | No | Directory for generated documentation (default: `docs`) |
|
|
106
|
-
| `docFormat` | No | Documentation format: `json` or `yaml` (default: `json`) |
|
|
107
|
-
| `docTitle` | No | API documentation title (default: from package.json name) |
|
|
108
|
-
| `docVersion` | No | API version (default: from package.json version) |
|
|
109
|
-
| `includeGenericFunctions` | No | Include non-API functions in documentation (default: `true`) |
|
|
110
|
-
| `repoDoc` | No | Document entire repository (`true`) or only staged changes (`false`, default) |
|
|
111
|
-
| `reviewSteps` | No | Array of review steps with custom rulesets (see below) |
|
|
112
|
-
| `reviewExecutionMode` | No | How to execute review steps: `parallel` or `sequential` (default: `parallel`) |
|
|
113
|
-
|
|
114
|
-
### Configurable Review Steps
|
|
115
|
-
|
|
116
|
-
Configure custom review steps with rulesets defined in markdown files. Each ruleset is stored in the `codeguard-ruleset/` folder at your project root.
|
|
117
|
-
|
|
118
|
-
```json
|
|
119
|
-
{
|
|
120
|
-
"reviewExecutionMode": "parallel",
|
|
121
|
-
"reviewSteps": [
|
|
122
|
-
{
|
|
123
|
-
"id": "code-quality",
|
|
124
|
-
"name": "Code Quality",
|
|
125
|
-
"category": "quality",
|
|
126
|
-
"type": "ai",
|
|
127
|
-
"enabled": true,
|
|
128
|
-
"ruleset": "code-quality.md"
|
|
129
|
-
},
|
|
130
|
-
{
|
|
131
|
-
"id": "security",
|
|
132
|
-
"name": "Security Vulnerabilities",
|
|
133
|
-
"category": "security",
|
|
134
|
-
"type": "ai",
|
|
135
|
-
"enabled": true,
|
|
136
|
-
"ruleset": "security.md"
|
|
137
|
-
}
|
|
138
|
-
]
|
|
139
|
-
}
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
**Review Step Options:**
|
|
143
|
-
|
|
144
|
-
| Option | Required | Description |
|
|
145
|
-
|--------|----------|-------------|
|
|
146
|
-
| `id` | Yes | Unique identifier for the step |
|
|
147
|
-
| `name` | Yes | Display name for the review step |
|
|
148
|
-
| `category` | Yes | Category of the review (e.g., `quality`, `security`, `performance`) |
|
|
149
|
-
| `type` | Yes | Type of review (currently only `ai` supported) |
|
|
150
|
-
| `enabled` | Yes | Whether this step is active (`true` or `false`) |
|
|
151
|
-
| `ruleset` | Yes | Filename of markdown ruleset in `codeguard-ruleset/` folder |
|
|
152
|
-
|
|
153
|
-
**Ruleset Files:**
|
|
154
|
-
|
|
155
|
-
Rulesets are markdown files stored in `codeguard-ruleset/` at your project root:
|
|
156
|
-
|
|
157
|
-
```
|
|
158
|
-
your-project/
|
|
159
|
-
├── codeguard.json
|
|
160
|
-
├── codeguard-ruleset/
|
|
161
|
-
│ ├── code-quality.md
|
|
162
|
-
│ ├── security.md
|
|
163
|
-
│ ├── performance.md
|
|
164
|
-
│ └── custom-rules.md
|
|
165
|
-
└── src/
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
Each ruleset file can contain:
|
|
169
|
-
- Detailed review criteria
|
|
170
|
-
- Specific rules and guidelines
|
|
171
|
-
- Examples and code snippets
|
|
172
|
-
- Severity guidelines
|
|
173
|
-
- OWASP references (for security)
|
|
174
|
-
- Best practices documentation
|
|
175
|
-
|
|
176
|
-
**Example Ruleset File** (`codeguard-ruleset/code-quality.md`):
|
|
177
|
-
|
|
178
|
-
```markdown
|
|
179
|
-
# Code Quality Review Ruleset
|
|
180
|
-
|
|
181
|
-
## Review Criteria
|
|
182
|
-
|
|
183
|
-
### 1. Naming Conventions
|
|
184
|
-
- Functions: Use clear, descriptive names
|
|
185
|
-
- Variables: Use meaningful names
|
|
186
|
-
- Boolean variables: Prefix with is, has, should
|
|
187
|
-
|
|
188
|
-
### 2. Code Complexity
|
|
189
|
-
- Functions should be concise (< 50 lines)
|
|
190
|
-
- Cyclomatic complexity should be low (< 10)
|
|
191
|
-
- Avoid deeply nested conditionals
|
|
192
|
-
|
|
193
|
-
...
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
**Execution Modes:**
|
|
197
|
-
|
|
198
|
-
- **`parallel`** (default): All enabled review steps run simultaneously for faster completion
|
|
199
|
-
- **`sequential`**: Steps run one after another in the order defined
|
|
200
|
-
|
|
201
|
-
**Default Review Steps:**
|
|
202
|
-
|
|
203
|
-
If you don't specify `reviewSteps` in your config, CodeGuard uses these default steps:
|
|
204
|
-
- ✅ **Code Quality** (`code-quality.md`) - Naming, complexity, readability, best practices
|
|
205
|
-
- ✅ **Potential Bugs** (`potential-bugs.md`) - Logic errors, edge cases, type issues, async problems
|
|
206
|
-
- ✅ **Performance** (`performance.md`) - Algorithm efficiency, unnecessary computations, memory leaks
|
|
207
|
-
- ✅ **Security** (`security.md`) - Input validation, injection risks, OWASP vulnerabilities
|
|
208
|
-
|
|
209
|
-
**Included Ruleset Files:**
|
|
210
|
-
|
|
211
|
-
CodeGuard comes with comprehensive default rulesets in `codeguard-ruleset/`:
|
|
212
|
-
- `code-quality.md` - 8 categories including naming, complexity, patterns, error handling
|
|
213
|
-
- `potential-bugs.md` - 8 categories covering logic errors, edge cases, async issues
|
|
214
|
-
- `performance.md` - 8 categories for algorithms, caching, data structures, optimizations
|
|
215
|
-
- `security.md` - OWASP Top 10 coverage with specific checks and references
|
|
216
|
-
|
|
217
|
-
You can customize these files or create your own rulesets for project-specific requirements.
|
|
218
|
-
|
|
219
|
-
**Output Format:**
|
|
220
|
-
|
|
221
|
-
Reviews are organized by step in the final markdown file:
|
|
222
|
-
|
|
223
|
-
```markdown
|
|
224
|
-
# Code Review
|
|
35
|
+
**Requirements:** Node.js ≥ 16, and `@babel/parser` + `@babel/traverse` in the target project:
|
|
225
36
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
## Files Changed
|
|
230
|
-
[List of files]
|
|
231
|
-
|
|
232
|
-
## Code Quality
|
|
233
|
-
[Findings from code quality step]
|
|
234
|
-
|
|
235
|
-
## Security Vulnerabilities
|
|
236
|
-
[Findings from security step]
|
|
237
|
-
|
|
238
|
-
## Performance Issues
|
|
239
|
-
[Findings from performance step]
|
|
240
|
-
|
|
241
|
-
## Conclusion
|
|
242
|
-
[Final assessment]
|
|
37
|
+
```bash
|
|
38
|
+
npm install --save-dev @babel/parser @babel/traverse
|
|
243
39
|
```
|
|
244
40
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
### Validation Interval
|
|
41
|
+
---
|
|
248
42
|
|
|
249
|
-
|
|
43
|
+
## Quick Start
|
|
250
44
|
|
|
251
|
-
|
|
252
|
-
- **`0`**: Validate only at the end after all functions are processed
|
|
253
|
-
- **`N` (number)**: Validate every N functions to catch integration issues early
|
|
254
|
-
|
|
255
|
-
**Example use cases:**
|
|
256
|
-
```json
|
|
257
|
-
{
|
|
258
|
-
"validationInterval": undefined // Fast - no validation checkpoints
|
|
259
|
-
}
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
```json
|
|
263
|
-
{
|
|
264
|
-
"validationInterval": 5 // Validate after every 5 functions
|
|
265
|
-
}
|
|
266
|
-
```
|
|
45
|
+
### 1. Create `codeguard.json` in your project root
|
|
267
46
|
|
|
268
47
|
```json
|
|
269
48
|
{
|
|
270
|
-
"
|
|
49
|
+
"aiProvider": "claude",
|
|
50
|
+
"apiKeys": {
|
|
51
|
+
"claude": "sk-ant-..."
|
|
52
|
+
},
|
|
53
|
+
"testEnv": "vitest",
|
|
54
|
+
"testDir": "src/tests"
|
|
271
55
|
}
|
|
272
56
|
```
|
|
273
57
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
### Getting API Keys
|
|
58
|
+
See [docs/CONFIGURATION.md](docs/CONFIGURATION.md) for all options.
|
|
277
59
|
|
|
278
|
-
|
|
279
|
-
- **OpenAI**: https://platform.openai.com/api-keys
|
|
280
|
-
- **Gemini (Google)**: https://makersuite.google.com/app/apikey
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
## Quick Reference
|
|
284
|
-
|
|
285
|
-
| Command | Description | Use Case |
|
|
286
|
-
|---------|-------------|----------|
|
|
287
|
-
| `testgen auto` | Review code quality + generate tests | Complete workflow, CI/CD |
|
|
288
|
-
| `testgen review` | Only review code changes | Code review, quality checks |
|
|
289
|
-
| `testgen test` | Only generate tests for changes | Testing workflow |
|
|
290
|
-
| `testgen` | Interactive mode - choose mode manually | Exploratory testing |
|
|
291
|
-
| Mode 1: File-wise | Generate tests for entire file | New files, comprehensive coverage |
|
|
292
|
-
| Mode 2: Folder-wise | Generate tests for all files in folder | Batch processing |
|
|
293
|
-
| Mode 3: Function-wise | Generate tests for specific functions | Incremental testing |
|
|
294
|
-
|
|
295
|
-
## Usage
|
|
296
|
-
|
|
297
|
-
### Auto Mode - Complete Workflow (Recommended)
|
|
298
|
-
|
|
299
|
-
Automatically review code quality and generate tests for changed functions:
|
|
60
|
+
### 2. Stage your changes and run
|
|
300
61
|
|
|
301
62
|
```bash
|
|
63
|
+
git add src/services/user.service.ts
|
|
302
64
|
testgen auto
|
|
303
65
|
```
|
|
304
66
|
|
|
305
|
-
|
|
306
|
-
1. **Reviews changed code** for quality, bugs, performance, and security issues
|
|
307
|
-
2. **Generates comprehensive tests** for modified functions
|
|
308
|
-
3. Saves review to `reviews/{filename}.review.md`
|
|
309
|
-
4. Creates or updates test files
|
|
67
|
+
CodeGuard will review the changed code and generate tests for modified functions. Results are saved to `reviews/code_review.md` and your `testDir`.
|
|
310
68
|
|
|
311
|
-
|
|
312
|
-
```
|
|
313
|
-
🔍 Scanning git changes for review...
|
|
314
|
-
📝 Found changes in 1 file(s) to review
|
|
315
|
-
|
|
316
|
-
🔄 Reviewing: src/services/user.service.ts
|
|
317
|
-
📦 Changed functions: createUser, updateUser
|
|
318
|
-
✅ Review completed
|
|
69
|
+
---
|
|
319
70
|
|
|
320
|
-
|
|
71
|
+
## Commands
|
|
321
72
|
|
|
322
|
-
|
|
73
|
+
| Command | Description |
|
|
74
|
+
|---------|-------------|
|
|
75
|
+
| `testgen auto` | Code review **and** test generation for staged changes |
|
|
76
|
+
| `testgen review` | Code review only |
|
|
77
|
+
| `testgen test` | Test generation only |
|
|
78
|
+
| `testgen doc` | API documentation generation *(in development)* |
|
|
79
|
+
| `testgen` | Interactive mode — choose mode and files manually |
|
|
323
80
|
|
|
324
|
-
|
|
325
|
-
📝 Found changes in 1 file(s)
|
|
81
|
+
Both `testgen` and `codeguard` are registered as CLI entry points.
|
|
326
82
|
|
|
327
|
-
|
|
328
|
-
📦 Changed functions: createUser, updateUser
|
|
329
|
-
✅ Tests generated successfully
|
|
330
|
-
```
|
|
83
|
+
---
|
|
331
84
|
|
|
332
|
-
|
|
85
|
+
## Features
|
|
333
86
|
|
|
334
|
-
|
|
87
|
+
### Code Review (`testgen review`)
|
|
335
88
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
89
|
+
- Multi-step review pipeline: code quality, potential bugs, performance, security
|
|
90
|
+
- Each step is driven by a customisable markdown **ruleset** file
|
|
91
|
+
- Steps run in parallel by default
|
|
92
|
+
- Output: `reviews/code_review.md` with severity-tagged findings
|
|
339
93
|
|
|
340
|
-
|
|
341
|
-
- 🎯 **Code Quality**: Naming, complexity, readability, best practices
|
|
342
|
-
- 🐛 **Potential Bugs**: Logic errors, edge cases, type mismatches, async issues
|
|
343
|
-
- ⚡ **Performance**: Inefficient algorithms, memory leaks, unnecessary computations
|
|
344
|
-
- 🔒 **Security**: Input validation, injection risks, authentication issues
|
|
345
|
-
|
|
346
|
-
**Review output** (`reviews/{filename}.review.md`):
|
|
347
|
-
```markdown
|
|
348
|
-
# Code Review: user.service.ts
|
|
349
|
-
|
|
350
|
-
## Summary
|
|
351
|
-
Overall code quality is good with some areas for improvement...
|
|
352
|
-
|
|
353
|
-
## Findings
|
|
354
|
-
|
|
355
|
-
### 🔴 Critical Issues
|
|
356
|
-
#### [Security] Missing Input Validation
|
|
357
|
-
**Function**: `createUser`
|
|
358
|
-
**Issue**: Email parameter not validated before database insertion...
|
|
359
|
-
**Recommended Fix**:
|
|
360
|
-
...typescript
|
|
361
|
-
if (!email || !email.includes('@')) {
|
|
362
|
-
throw new Error('Invalid email');
|
|
363
|
-
}
|
|
364
|
-
...
|
|
94
|
+
### Test Generation (`testgen test`)
|
|
365
95
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
96
|
+
- Detects changed files and modified exported functions via git diff
|
|
97
|
+
- Uses AST analysis to understand function signatures, types, and dependencies
|
|
98
|
+
- Generates tests with proper mocks, edge cases, and error scenarios
|
|
99
|
+
- Supports Vitest and Jest
|
|
100
|
+
- Iterative AI loop automatically fixes import errors, missing mocks, and type issues
|
|
101
|
+
- Preserves existing tests for unchanged functions
|
|
369
102
|
|
|
370
|
-
|
|
371
|
-
- Well-structured error handling
|
|
372
|
-
- Clear function naming
|
|
103
|
+
### Unified Auto Mode (`testgen auto`)
|
|
373
104
|
|
|
374
|
-
|
|
375
|
-
1. Add input validation for all public functions
|
|
376
|
-
2. Consider adding JSDoc comments
|
|
377
|
-
```
|
|
105
|
+
Runs review and test generation sequentially in one command. Suitable for pre-commit hooks and CI/CD pipelines.
|
|
378
106
|
|
|
379
|
-
###
|
|
107
|
+
### Interactive Mode (`testgen`)
|
|
380
108
|
|
|
381
|
-
|
|
109
|
+
Guides you through selecting files and functions manually. Supports:
|
|
110
|
+
- **File-wise:** generate tests for all exported functions in a file
|
|
111
|
+
- **Folder-wise:** batch process every file in a directory
|
|
112
|
+
- **Function-wise:** target specific functions with optional periodic validation
|
|
382
113
|
|
|
383
|
-
|
|
384
|
-
testgen test
|
|
385
|
-
```
|
|
114
|
+
### Codebase Indexing
|
|
386
115
|
|
|
387
|
-
**
|
|
388
|
-
1. Reads both `git diff --staged` and `git diff` to find all changes
|
|
389
|
-
2. Identifies which files have been modified
|
|
390
|
-
3. Uses AI to detect which exported functions have changes
|
|
391
|
-
4. Automatically generates or updates tests for those functions
|
|
392
|
-
5. No user interaction required - perfect for automation!
|
|
116
|
+
On the first interactive run you will be offered the option to build a codebase index (stored in `.codeguard-cache/index.json`). The index caches AST analysis results, providing roughly **100× speedup** on subsequent runs.
|
|
393
117
|
|
|
394
|
-
|
|
118
|
+
---
|
|
395
119
|
|
|
396
|
-
|
|
397
|
-
```bash
|
|
398
|
-
# Make changes to your code
|
|
399
|
-
vim src/services/user.service.ts
|
|
120
|
+
## How It Works
|
|
400
121
|
|
|
401
|
-
|
|
402
|
-
git add src/services/user.service.ts
|
|
122
|
+
### Test Generation
|
|
403
123
|
|
|
404
|
-
# Review code quality and generate tests
|
|
405
|
-
testgen auto
|
|
406
124
|
```
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
125
|
+
1. git diff → identify changed source files
|
|
126
|
+
2. AI analysis → identify which exported functions changed
|
|
127
|
+
3. For each changed function:
|
|
128
|
+
a. Build prompt: function AST + imports + type definitions
|
|
129
|
+
b. AI conversation loop (max 30 iterations)
|
|
130
|
+
AI calls tools: get_function_ast, get_imports_ast, find_file,
|
|
131
|
+
upsert_function_tests, run_tests
|
|
132
|
+
c. Write test file → run tests → parse failures
|
|
133
|
+
d. If tests fail: AI fixes and loops again
|
|
134
|
+
4. Report results
|
|
415
135
|
```
|
|
416
136
|
|
|
417
|
-
|
|
418
|
-
```bash
|
|
419
|
-
# Generate tests without review
|
|
420
|
-
testgen test
|
|
421
|
-
```
|
|
137
|
+
### Code Review
|
|
422
138
|
|
|
423
|
-
**Documentation generation:**
|
|
424
|
-
```bash
|
|
425
|
-
# Generate API documentation
|
|
426
|
-
testgen doc
|
|
427
139
|
```
|
|
428
|
-
|
|
429
|
-
|
|
140
|
+
1. git diff → identify changed source files
|
|
141
|
+
2. Load enabled review steps from codeguard.json
|
|
142
|
+
3. For each step (in parallel or sequential):
|
|
143
|
+
a. Read ruleset markdown from codeguard-ruleset/
|
|
144
|
+
b. AI conversation loop with AST tools
|
|
145
|
+
c. Write findings to temporary markdown file
|
|
146
|
+
4. Merge all step outputs → reviews/code_review.md
|
|
430
147
|
```
|
|
431
|
-
🧪 AI-Powered Unit Test Generator with AST Analysis
|
|
432
|
-
|
|
433
|
-
🤖 Auto Mode: Detecting changes via git diff
|
|
434
|
-
|
|
435
|
-
✅ Using OPENAI (gpt-4o-mini) with AST-powered analysis
|
|
436
|
-
|
|
437
|
-
🔍 Scanning git changes...
|
|
438
148
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
🔄 Processing: src/services/user.service.ts
|
|
442
|
-
📦 Changed functions: createUser, updateUser
|
|
443
|
-
✅ Tests generated successfully
|
|
444
|
-
|
|
445
|
-
============================================================
|
|
446
|
-
📊 Auto-Generation Summary
|
|
447
|
-
============================================================
|
|
448
|
-
✅ Successfully processed: 1 file(s)
|
|
449
|
-
📝 Functions tested: 2
|
|
450
|
-
============================================================
|
|
451
|
-
```
|
|
149
|
+
### AI Tool System
|
|
452
150
|
|
|
453
|
-
|
|
454
|
-
- 🔍 **Quality Assurance**: Catch issues before they reach production
|
|
455
|
-
- ⚡ **Fast**: Only processes changed files
|
|
456
|
-
- 🎯 **Targeted**: Reviews and tests only modified functions
|
|
457
|
-
- 🔄 **CI/CD Ready**: Non-interactive, perfect for automation
|
|
458
|
-
- 🛡️ **Safe**: Preserves existing tests for unchanged functions
|
|
459
|
-
- 📊 **Trackable**: All reviews saved for historical reference
|
|
151
|
+
CodeGuard does not allow the AI to read or write files directly. All file access is mediated through typed tools (`read_file`, `analyze_file_ast`, `upsert_function_tests`, `run_tests`, etc.) that the host process executes. This provides auditability and safe error handling. See [docs/API.md — Tool Definitions](docs/API.md#tool-definitions) for the full list.
|
|
460
152
|
|
|
461
|
-
|
|
462
|
-
- ✅ Source files with supported extensions (`.ts`, `.tsx`, `.js`, `.jsx`)
|
|
463
|
-
- ✅ Files with exported functions
|
|
464
|
-
- ❌ Test files (`.test.`, `.spec.`, `__tests__/`, `/tests/`)
|
|
465
|
-
- ❌ Files in `node_modules`, `dist`, `build`, etc.
|
|
466
|
-
- ❌ Non-source files (configs, markdown, etc.)
|
|
153
|
+
---
|
|
467
154
|
|
|
468
|
-
|
|
155
|
+
## Configuration Overview
|
|
469
156
|
|
|
470
|
-
|
|
157
|
+
Key `codeguard.json` fields:
|
|
471
158
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
159
|
+
| Field | Default | Description |
|
|
160
|
+
|-------|---------|-------------|
|
|
161
|
+
| `aiProvider` | — | `"claude"`, `"openai"`, or `"gemini"` (required) |
|
|
162
|
+
| `apiKeys` | — | API keys for the chosen provider (required) |
|
|
163
|
+
| `testEnv` | `"vitest"` | `"vitest"` or `"jest"` |
|
|
164
|
+
| `testDir` | `"src/tests"` | Output directory for generated tests |
|
|
165
|
+
| `reviewExecutionMode` | `"parallel"` | `"parallel"` or `"sequential"` |
|
|
166
|
+
| `reviewSteps` | 4 built-in steps | Array of `ReviewStep` objects |
|
|
475
167
|
|
|
476
|
-
|
|
168
|
+
Full reference: [docs/CONFIGURATION.md](docs/CONFIGURATION.md)
|
|
477
169
|
|
|
478
|
-
|
|
479
|
-
codeguard
|
|
480
|
-
```
|
|
170
|
+
---
|
|
481
171
|
|
|
482
|
-
|
|
483
|
-
1. Selecting test generation mode (file/folder/function-wise)
|
|
484
|
-
2. Choosing files or functions to test
|
|
485
|
-
3. Optional codebase indexing for faster processing
|
|
486
|
-
|
|
487
|
-
### Test Generation Modes
|
|
488
|
-
|
|
489
|
-
#### 1. File-wise Mode
|
|
490
|
-
Generate tests for a single file:
|
|
491
|
-
- Select from a list of source files
|
|
492
|
-
- Generates comprehensive tests for all exported functions
|
|
493
|
-
- Creates test file with proper structure and mocks
|
|
494
|
-
|
|
495
|
-
#### 2. Folder-wise Mode
|
|
496
|
-
Generate tests for all files in a directory:
|
|
497
|
-
- Select a folder from your project
|
|
498
|
-
- Processes all matching files recursively
|
|
499
|
-
- Batch generates tests with progress tracking
|
|
500
|
-
|
|
501
|
-
#### 3. Function-wise Mode
|
|
502
|
-
Generate tests for specific functions:
|
|
503
|
-
- Select a file
|
|
504
|
-
- Choose which functions to test
|
|
505
|
-
- Preserves existing tests for other functions
|
|
506
|
-
- Ideal for incremental test development
|
|
507
|
-
|
|
508
|
-
#### 4. Auto Mode (Unified)
|
|
509
|
-
Review code quality and generate tests automatically:
|
|
510
|
-
- Analyzes git diff (staged and unstaged changes)
|
|
511
|
-
- AI reviews code for quality, bugs, performance, security
|
|
512
|
-
- Generates comprehensive review markdown files
|
|
513
|
-
- Creates tests for changed exported functions
|
|
514
|
-
- Non-interactive - perfect for CI/CD pipelines
|
|
515
|
-
- Use: `testgen auto`
|
|
516
|
-
|
|
517
|
-
#### 5. Review Mode
|
|
518
|
-
AI-powered code review only:
|
|
519
|
-
- Comprehensive analysis by senior-level AI reviewer
|
|
520
|
-
- Reviews code quality, potential bugs, performance issues, security vulnerabilities
|
|
521
|
-
- Uses AST tools to understand full context
|
|
522
|
-
- Generates structured markdown reports
|
|
523
|
-
- Use: `testgen review`
|
|
524
|
-
|
|
525
|
-
#### 6. Test Mode
|
|
526
|
-
Test generation only:
|
|
527
|
-
- Generates tests for changed functions
|
|
528
|
-
- Skips code review process
|
|
529
|
-
- Faster when you only need tests
|
|
530
|
-
- Use: `testgen test`
|
|
531
|
-
|
|
532
|
-
#### 7. Documentation Mode
|
|
533
|
-
AI-powered API documentation generation:
|
|
534
|
-
- **Default**: Documents only staged/changed functions (like review/test modes)
|
|
535
|
-
- **Full Repo**: Set `"repoDoc": true` to document entire codebase
|
|
536
|
-
- Analyzes codebase using AST tools
|
|
537
|
-
- Auto-detects API endpoints (Express, NestJS, Fastify, Koa)
|
|
538
|
-
- Generates comprehensive OpenAPI 3.0 specification
|
|
539
|
-
- Documents both API routes and generic functions
|
|
540
|
-
- Smart merge with existing documentation
|
|
541
|
-
- Supports JSON and YAML formats
|
|
542
|
-
- Use: `testgen doc`
|
|
543
|
-
|
|
544
|
-
**Two modes:**
|
|
545
|
-
|
|
546
|
-
**1. Changed Files Only (Default)** - `"repoDoc": false` or omitted
|
|
547
|
-
- Works like `testgen review` and `testgen test`
|
|
548
|
-
- Only documents staged/changed functions
|
|
549
|
-
- Fast and targeted
|
|
550
|
-
- Perfect for incremental updates
|
|
551
|
-
- Requires git repository
|
|
552
|
-
|
|
553
|
-
**2. Full Repository** - `"repoDoc": true`
|
|
554
|
-
- Documents entire codebase
|
|
555
|
-
- Comprehensive documentation generation
|
|
556
|
-
- Useful for initial documentation or major updates
|
|
557
|
-
- No git requirement
|
|
558
|
-
|
|
559
|
-
**What it documents:**
|
|
560
|
-
- ✅ **API Endpoints**: All REST API routes with methods, paths, parameters
|
|
561
|
-
- ✅ **Request/Response Schemas**: Inferred from TypeScript types
|
|
562
|
-
- ✅ **Authentication**: Detects and documents auth requirements
|
|
563
|
-
- ✅ **Error Responses**: Documents error cases and status codes
|
|
564
|
-
- ✅ **Generic Functions**: Optional documentation for utility functions
|
|
565
|
-
- ✅ **Usage Examples**: AI-generated examples for each endpoint
|
|
566
|
-
|
|
567
|
-
**Supported Frameworks:**
|
|
568
|
-
- **Express**: `app.get()`, `router.post()`, route methods
|
|
569
|
-
- **NestJS**: `@Controller()`, `@Get()`, `@Post()`, decorators
|
|
570
|
-
- **Fastify**: `fastify.route()`, route configurations
|
|
571
|
-
- **Koa**: `router.get()`, middleware patterns
|
|
572
|
-
|
|
573
|
-
**Example usage:**
|
|
574
|
-
```bash
|
|
575
|
-
# Document only changed/staged functions (default)
|
|
576
|
-
testgen doc
|
|
577
|
-
|
|
578
|
-
# Output:
|
|
579
|
-
# 📚 Documentation Mode: Generating API documentation
|
|
580
|
-
#
|
|
581
|
-
# 🔍 Scanning git changes for documentation...
|
|
582
|
-
#
|
|
583
|
-
# 📝 Found changes in 2 file(s)
|
|
584
|
-
#
|
|
585
|
-
# 🤖 Generating OpenAPI specification...
|
|
586
|
-
#
|
|
587
|
-
# ✅ Documentation generated successfully
|
|
588
|
-
#
|
|
589
|
-
# ============================================================
|
|
590
|
-
# 📊 Documentation Summary
|
|
591
|
-
# ============================================================
|
|
592
|
-
# ✅ API Endpoints documented: 5
|
|
593
|
-
# ✅ Generic functions documented: 8
|
|
594
|
-
# 📁 Output: docs/openapi.json
|
|
595
|
-
# ============================================================
|
|
596
|
-
|
|
597
|
-
# For full repository documentation, set in codeguard.json:
|
|
598
|
-
# {
|
|
599
|
-
# "repoDoc": true
|
|
600
|
-
# }
|
|
601
|
-
```
|
|
172
|
+
## Output Files
|
|
602
173
|
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
"title": "My API",
|
|
609
|
-
"version": "1.0.0"
|
|
610
|
-
},
|
|
611
|
-
"paths": {
|
|
612
|
-
"/users": {
|
|
613
|
-
"get": {
|
|
614
|
-
"summary": "Get all users",
|
|
615
|
-
"responses": {
|
|
616
|
-
"200": {
|
|
617
|
-
"description": "Success",
|
|
618
|
-
"content": {
|
|
619
|
-
"application/json": {
|
|
620
|
-
"schema": {
|
|
621
|
-
"type": "array",
|
|
622
|
-
"items": { "$ref": "#/components/schemas/User" }
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
},
|
|
631
|
-
"components": {
|
|
632
|
-
"schemas": {
|
|
633
|
-
"User": {
|
|
634
|
-
"type": "object",
|
|
635
|
-
"properties": {
|
|
636
|
-
"id": { "type": "string" },
|
|
637
|
-
"name": { "type": "string" },
|
|
638
|
-
"email": { "type": "string" }
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
```
|
|
174
|
+
| Path | Contents |
|
|
175
|
+
|------|---------|
|
|
176
|
+
| `reviews/code_review.md` | Merged review report from all enabled steps |
|
|
177
|
+
| `<testDir>/**/*.test.ts` | Generated/updated test files |
|
|
178
|
+
| `.codeguard-cache/index.json` | Optional codebase index (auto-managed) |
|
|
645
179
|
|
|
646
|
-
|
|
647
|
-
When existing documentation is found, CodeGuard intelligently merges:
|
|
648
|
-
- ✅ Preserves manually edited descriptions and summaries
|
|
649
|
-
- ✅ Updates schemas with latest types from code
|
|
650
|
-
- ✅ Adds new endpoints without removing manual changes
|
|
651
|
-
- ✅ Maintains custom examples and documentation
|
|
652
|
-
- ✅ Tracks generation metadata and timestamps
|
|
180
|
+
---
|
|
653
181
|
|
|
654
|
-
##
|
|
182
|
+
## CI/CD Integration
|
|
655
183
|
|
|
656
|
-
###
|
|
657
|
-
1. **Git Diff Analysis**: Detects changed files and functions
|
|
658
|
-
2. **AST Analysis**: Deep parse of code structure using Babel
|
|
659
|
-
3. **Context Understanding**: AI uses tools to analyze:
|
|
660
|
-
- Function implementations
|
|
661
|
-
- Dependencies and imports
|
|
662
|
-
- Type definitions
|
|
663
|
-
- Related code context
|
|
664
|
-
4. **Multi-Aspect Review**: Analyzes for:
|
|
665
|
-
- Code quality and best practices
|
|
666
|
-
- Potential bugs and edge cases
|
|
667
|
-
- Performance bottlenecks
|
|
668
|
-
- Security vulnerabilities
|
|
669
|
-
5. **Structured Report**: Generates markdown with:
|
|
670
|
-
- Severity-based findings
|
|
671
|
-
- Code snippets and fixes
|
|
672
|
-
- Positive observations
|
|
673
|
-
- Actionable recommendations
|
|
674
|
-
|
|
675
|
-
### Test Generation Process
|
|
676
|
-
1. **AST Analysis**: Parses your code using Babel to understand structure
|
|
677
|
-
2. **Dependency Resolution**: Analyzes imports and calculates correct paths
|
|
678
|
-
3. **AI Generation**: Uses AI to generate comprehensive test cases
|
|
679
|
-
4. **Validation**: Checks for completeness, assertions, and coverage
|
|
680
|
-
5. **Execution**: Runs tests with Jest to verify correctness
|
|
681
|
-
6. **Iterative Fixing**: Automatically fixes common issues like:
|
|
682
|
-
- Import path errors
|
|
683
|
-
- Missing mocks
|
|
684
|
-
- Database initialization errors
|
|
685
|
-
- Type mismatches
|
|
686
|
-
7. **Failure Detection**: Distinguishes between test bugs and source code bugs
|
|
687
|
-
|
|
688
|
-
### Documentation Generation Process
|
|
689
|
-
1. **File Scanning**: Recursively scans all source files in the project
|
|
690
|
-
2. **AST Analysis**: Parses each file using Babel to understand structure
|
|
691
|
-
3. **Endpoint Detection**: AI identifies API routes across different frameworks:
|
|
692
|
-
- Express: `app.METHOD()`, `router.METHOD()`
|
|
693
|
-
- NestJS: `@Controller()`, `@Get()`, `@Post()`, etc.
|
|
694
|
-
- Fastify: `fastify.route()`, route configurations
|
|
695
|
-
- Koa: `router.METHOD()`, middleware chains
|
|
696
|
-
4. **Schema Inference**: Extracts TypeScript types for request/response schemas
|
|
697
|
-
5. **AI Enhancement**: AI generates:
|
|
698
|
-
- Meaningful descriptions for each endpoint
|
|
699
|
-
- Parameter documentation
|
|
700
|
-
- Response examples
|
|
701
|
-
- Error scenarios
|
|
702
|
-
6. **OpenAPI Generation**: Builds complete OpenAPI 3.0 specification
|
|
703
|
-
7. **Smart Merge**: Intelligently merges with existing documentation
|
|
704
|
-
8. **File Output**: Writes to `docs/openapi.json` or `.yaml`
|
|
705
|
-
|
|
706
|
-
## Generated Test Features
|
|
707
|
-
|
|
708
|
-
The AI generates tests with:
|
|
709
|
-
|
|
710
|
-
- ✅ Proper imports and type definitions
|
|
711
|
-
- ✅ Jest mocks for dependencies
|
|
712
|
-
- ✅ Multiple test cases per function:
|
|
713
|
-
- Happy path scenarios
|
|
714
|
-
- Edge cases (null, undefined, empty arrays)
|
|
715
|
-
- Error conditions
|
|
716
|
-
- Async behavior testing
|
|
717
|
-
- ✅ Clear, descriptive test names
|
|
718
|
-
- ✅ Complete implementations (no placeholder comments)
|
|
719
|
-
- ✅ Real assertions with expect() statements
|
|
720
|
-
|
|
721
|
-
## Advanced Features
|
|
722
|
-
|
|
723
|
-
### CI/CD Integration
|
|
724
|
-
|
|
725
|
-
CodeGuard modes are designed for continuous integration workflows:
|
|
726
|
-
|
|
727
|
-
**GitHub Actions - Complete Workflow (Review + Tests):**
|
|
184
|
+
### GitHub Actions — auto mode
|
|
728
185
|
|
|
729
186
|
```yaml
|
|
730
|
-
name: AI Code Review &
|
|
187
|
+
name: AI Code Review & Tests
|
|
731
188
|
|
|
732
189
|
on:
|
|
733
190
|
pull_request:
|
|
734
|
-
branches: [
|
|
191
|
+
branches: [main]
|
|
735
192
|
|
|
736
193
|
jobs:
|
|
737
|
-
|
|
194
|
+
codeguard:
|
|
738
195
|
runs-on: ubuntu-latest
|
|
739
196
|
steps:
|
|
740
197
|
- uses: actions/checkout@v3
|
|
741
198
|
with:
|
|
742
|
-
fetch-depth: 0
|
|
743
|
-
|
|
744
|
-
-
|
|
745
|
-
uses: actions/setup-node@v3
|
|
199
|
+
fetch-depth: 0
|
|
200
|
+
|
|
201
|
+
- uses: actions/setup-node@v3
|
|
746
202
|
with:
|
|
747
203
|
node-version: '18'
|
|
748
|
-
|
|
749
|
-
- name: Install dependencies
|
|
750
|
-
run: npm install
|
|
751
|
-
|
|
752
|
-
- name: Install CodeGuard
|
|
753
|
-
run: npm install -g codeguard-testgen
|
|
754
|
-
|
|
755
|
-
- name: Review code and generate tests
|
|
756
|
-
env:
|
|
757
|
-
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
|
758
|
-
run: testgen auto
|
|
759
|
-
|
|
760
|
-
- name: Upload review reports
|
|
761
|
-
uses: actions/upload-artifact@v3
|
|
762
|
-
with:
|
|
763
|
-
name: code-reviews
|
|
764
|
-
path: reviews/
|
|
765
|
-
|
|
766
|
-
- name: Commit generated tests and reviews
|
|
767
|
-
run: |
|
|
768
|
-
git config --local user.email "action@github.com"
|
|
769
|
-
git config --local user.name "GitHub Action"
|
|
770
|
-
git add src/tests/ reviews/
|
|
771
|
-
git commit -m "🤖 AI code review + tests for changed functions" || echo "No changes"
|
|
772
|
-
git push
|
|
773
|
-
```
|
|
774
|
-
|
|
775
|
-
**GitHub Actions - Review Only:**
|
|
776
204
|
|
|
777
|
-
|
|
778
|
-
|
|
205
|
+
- run: npm install
|
|
206
|
+
- run: npm install -g codeguard-testgen
|
|
779
207
|
|
|
780
|
-
|
|
781
|
-
pull_request:
|
|
782
|
-
branches: [ main ]
|
|
783
|
-
|
|
784
|
-
jobs:
|
|
785
|
-
code-review:
|
|
786
|
-
runs-on: ubuntu-latest
|
|
787
|
-
steps:
|
|
788
|
-
- uses: actions/checkout@v3
|
|
789
|
-
with:
|
|
790
|
-
fetch-depth: 0
|
|
791
|
-
|
|
792
|
-
- name: Setup Node.js
|
|
793
|
-
uses: actions/setup-node@v3
|
|
794
|
-
with:
|
|
795
|
-
node-version: '18'
|
|
796
|
-
|
|
797
|
-
- name: Install CodeGuard
|
|
798
|
-
run: npm install -g codeguard-testgen
|
|
799
|
-
|
|
800
|
-
- name: AI Code Review
|
|
208
|
+
- name: Review and generate tests
|
|
801
209
|
env:
|
|
802
210
|
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
|
803
|
-
run: testgen
|
|
804
|
-
|
|
805
|
-
- name: Comment PR with review
|
|
806
|
-
uses: actions/github-script@v6
|
|
807
|
-
with:
|
|
808
|
-
script: |
|
|
809
|
-
const fs = require('fs');
|
|
810
|
-
const reviews = fs.readdirSync('reviews/');
|
|
811
|
-
for (const review of reviews) {
|
|
812
|
-
const content = fs.readFileSync(`reviews/${review}`, 'utf8');
|
|
813
|
-
github.rest.issues.createComment({
|
|
814
|
-
issue_number: context.issue.number,
|
|
815
|
-
owner: context.repo.owner,
|
|
816
|
-
repo: context.repo.repo,
|
|
817
|
-
body: `## AI Code Review: ${review}\n\n${content}`
|
|
818
|
-
});
|
|
819
|
-
}
|
|
820
|
-
```
|
|
821
|
-
|
|
822
|
-
**GitLab CI Example:**
|
|
211
|
+
run: testgen auto
|
|
823
212
|
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
- npm install -g codeguard-testgen
|
|
829
|
-
- testgen auto # Review + tests
|
|
830
|
-
artifacts:
|
|
831
|
-
paths:
|
|
832
|
-
- reviews/
|
|
833
|
-
- src/tests/
|
|
834
|
-
only:
|
|
835
|
-
- merge_requests
|
|
836
|
-
|
|
837
|
-
review-only:
|
|
838
|
-
stage: quality
|
|
839
|
-
script:
|
|
840
|
-
- npm install -g codeguard-testgen
|
|
841
|
-
- testgen review
|
|
842
|
-
artifacts:
|
|
843
|
-
reports:
|
|
844
|
-
codequality: reviews/
|
|
845
|
-
only:
|
|
846
|
-
- merge_requests
|
|
213
|
+
- uses: actions/upload-artifact@v3
|
|
214
|
+
with:
|
|
215
|
+
name: code-review
|
|
216
|
+
path: reviews/
|
|
847
217
|
```
|
|
848
218
|
|
|
849
|
-
|
|
219
|
+
### Pre-commit hook
|
|
850
220
|
|
|
851
221
|
```bash
|
|
852
222
|
#!/bin/bash
|
|
853
223
|
# .git/hooks/pre-commit
|
|
854
|
-
|
|
855
|
-
# Review code and generate tests for staged changes
|
|
856
224
|
testgen auto
|
|
857
|
-
|
|
858
|
-
# Add generated tests and reviews to commit
|
|
859
225
|
git add src/tests/ reviews/
|
|
860
226
|
```
|
|
861
227
|
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
```bash
|
|
865
|
-
#!/bin/bash
|
|
866
|
-
# .git/hooks/pre-push
|
|
867
|
-
|
|
868
|
-
# Quick code review before pushing
|
|
869
|
-
testgen review
|
|
870
|
-
|
|
871
|
-
# Show review summary
|
|
872
|
-
echo "📊 Code Review Complete - Check reviews/ directory"
|
|
873
|
-
```
|
|
874
|
-
|
|
875
|
-
### Codebase Indexing
|
|
876
|
-
|
|
877
|
-
On first run, you'll be prompted to enable codebase indexing:
|
|
878
|
-
|
|
879
|
-
```
|
|
880
|
-
Enable codebase indexing? (y/n)
|
|
881
|
-
```
|
|
882
|
-
|
|
883
|
-
Benefits:
|
|
884
|
-
- 100x+ faster analysis on subsequent runs
|
|
885
|
-
- Instant dependency lookups
|
|
886
|
-
- Cached AST parsing
|
|
887
|
-
- Automatic update detection
|
|
888
|
-
|
|
889
|
-
The index is stored in `.codeguard-cache/` and automatically updates when files change.
|
|
890
|
-
|
|
891
|
-
### Legitimate Failure Detection
|
|
892
|
-
|
|
893
|
-
The tool distinguishes between:
|
|
894
|
-
|
|
895
|
-
**Fixable Test Issues** (automatically fixed):
|
|
896
|
-
- Wrong import paths
|
|
897
|
-
- Missing mocks
|
|
898
|
-
- Incorrect assertions
|
|
899
|
-
- TypeScript errors
|
|
900
|
-
|
|
901
|
-
**Legitimate Source Code Bugs** (reported, not fixed):
|
|
902
|
-
- Function returns wrong type
|
|
903
|
-
- Missing null checks
|
|
904
|
-
- Logic errors
|
|
905
|
-
- Unhandled edge cases
|
|
906
|
-
|
|
907
|
-
When legitimate bugs are found, they're reported with details for you to fix in the source code.
|
|
908
|
-
|
|
909
|
-
## Examples
|
|
910
|
-
|
|
911
|
-
### Example 1: Complete Workflow (Auto Mode)
|
|
912
|
-
|
|
913
|
-
**Step 1: Make changes to a function**
|
|
914
|
-
```typescript
|
|
915
|
-
// src/services/user.service.ts
|
|
916
|
-
export const createUser = async (name: string, email: string) => {
|
|
917
|
-
// Added email validation
|
|
918
|
-
if (!email.includes('@')) {
|
|
919
|
-
throw new Error('Invalid email');
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
return await db.users.create({ name, email });
|
|
923
|
-
};
|
|
924
|
-
|
|
925
|
-
export const deleteUser = async (id: string) => {
|
|
926
|
-
return await db.users.delete(id);
|
|
927
|
-
};
|
|
928
|
-
```
|
|
929
|
-
|
|
930
|
-
**Step 2: Stage changes and run auto mode**
|
|
931
|
-
```bash
|
|
932
|
-
git add src/services/user.service.ts
|
|
933
|
-
testgen auto
|
|
934
|
-
```
|
|
935
|
-
|
|
936
|
-
**Output:**
|
|
937
|
-
```
|
|
938
|
-
🔍 Scanning git changes for review...
|
|
939
|
-
📝 Found changes in 1 file(s)
|
|
940
|
-
|
|
941
|
-
🔄 Reviewing: src/services/user.service.ts
|
|
942
|
-
📦 Changed functions: createUser
|
|
943
|
-
✅ Review completed
|
|
944
|
-
|
|
945
|
-
============================================================
|
|
946
|
-
|
|
947
|
-
🔍 Scanning git changes for testing...
|
|
948
|
-
📝 Found changes in 1 file(s)
|
|
949
|
-
|
|
950
|
-
🔄 Processing: src/services/user.service.ts
|
|
951
|
-
📦 Changed functions: createUser
|
|
952
|
-
✅ Tests generated successfully
|
|
953
|
-
```
|
|
954
|
-
|
|
955
|
-
**Results:**
|
|
956
|
-
- `reviews/user.service.review.md` created with code quality analysis
|
|
957
|
-
- Only `createUser` gets new tests, `deleteUser` tests remain unchanged!
|
|
228
|
+
---
|
|
958
229
|
|
|
959
|
-
|
|
960
|
-
```markdown
|
|
961
|
-
### 🟡 Medium Priority Issues
|
|
230
|
+
## Supported AI Providers
|
|
962
231
|
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
232
|
+
| Provider | Config value | Default model |
|
|
233
|
+
|----------|-------------|---------------|
|
|
234
|
+
| Anthropic Claude | `"claude"` | `claude-sonnet-4-5-20250929` |
|
|
235
|
+
| OpenAI | `"openai"` | `gpt-5-mini` |
|
|
236
|
+
| Google Gemini | `"gemini"` | `gemini-2.0-flash-lite` |
|
|
966
237
|
|
|
967
|
-
|
|
968
|
-
```typescript
|
|
969
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
970
|
-
if (!emailRegex.test(email)) {
|
|
971
|
-
throw new Error('Invalid email format');
|
|
972
|
-
}
|
|
973
|
-
```
|
|
974
|
-
```
|
|
238
|
+
All providers share the same retry logic: exponential backoff with jitter, retrying on 429/5xx responses.
|
|
975
239
|
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
```typescript
|
|
979
|
-
// src/services/user.service.ts
|
|
980
|
-
export class UserService {
|
|
981
|
-
async getUser(id: string): Promise<User> {
|
|
982
|
-
return await this.db.findUser(id);
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
```
|
|
986
|
-
|
|
987
|
-
Generated test:
|
|
988
|
-
|
|
989
|
-
```typescript
|
|
990
|
-
// src/tests/services/user.service.test.ts
|
|
991
|
-
import { UserService } from '../../services/user.service';
|
|
992
|
-
|
|
993
|
-
jest.mock('../../database');
|
|
994
|
-
|
|
995
|
-
describe('UserService', () => {
|
|
996
|
-
describe('getUser', () => {
|
|
997
|
-
test('should return user when id exists', async () => {
|
|
998
|
-
const mockUser = { id: '123', name: 'John' };
|
|
999
|
-
const service = new UserService();
|
|
1000
|
-
service.db.findUser = jest.fn().mockResolvedValue(mockUser);
|
|
1001
|
-
|
|
1002
|
-
const result = await service.getUser('123');
|
|
1003
|
-
|
|
1004
|
-
expect(result).toEqual(mockUser);
|
|
1005
|
-
expect(service.db.findUser).toHaveBeenCalledWith('123');
|
|
1006
|
-
});
|
|
1007
|
-
|
|
1008
|
-
test('should handle null id', async () => {
|
|
1009
|
-
const service = new UserService();
|
|
1010
|
-
await expect(service.getUser(null)).rejects.toThrow();
|
|
1011
|
-
});
|
|
1012
|
-
});
|
|
1013
|
-
});
|
|
1014
|
-
```
|
|
240
|
+
---
|
|
1015
241
|
|
|
1016
242
|
## Troubleshooting
|
|
1017
243
|
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
git
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
#### "No changes detected in source files"
|
|
1027
|
-
This means:
|
|
1028
|
-
- No staged or unstaged changes exist
|
|
1029
|
-
- Only test files were modified (test files are excluded)
|
|
1030
|
-
- Changes are in non-source files
|
|
1031
|
-
|
|
1032
|
-
Check your changes:
|
|
1033
|
-
```bash
|
|
1034
|
-
git status
|
|
1035
|
-
git diff
|
|
1036
|
-
```
|
|
1037
|
-
|
|
1038
|
-
#### Review/Test mode not working
|
|
1039
|
-
Make sure you're using the correct command:
|
|
1040
|
-
```bash
|
|
1041
|
-
testgen auto # Review + tests
|
|
1042
|
-
testgen review # Only review
|
|
1043
|
-
testgen test # Only tests
|
|
1044
|
-
```
|
|
1045
|
-
|
|
1046
|
-
#### "No exported functions changed"
|
|
1047
|
-
Possible causes:
|
|
1048
|
-
1. **AI model misconfigured**: Check your `codeguard.json` has a valid model name:
|
|
1049
|
-
```json
|
|
1050
|
-
{
|
|
1051
|
-
"models": {
|
|
1052
|
-
"openai": "gpt-4o-mini" // ✅ Correct
|
|
1053
|
-
// NOT "gpt-5-mini" ❌
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
```
|
|
1057
|
-
2. **Only internal functions changed**: Auto mode only generates tests for exported functions
|
|
1058
|
-
3. **File has no exported functions**: Make sure functions are exported:
|
|
1059
|
-
```typescript
|
|
1060
|
-
export const myFunction = () => { } // ✅ Will be tested
|
|
1061
|
-
const internalFunc = () => { } // ❌ Will be skipped
|
|
1062
|
-
```
|
|
1063
|
-
|
|
1064
|
-
#### Debugging Auto Mode
|
|
1065
|
-
Enable detailed logging by checking the console output:
|
|
1066
|
-
```bash
|
|
1067
|
-
testgen auto
|
|
1068
|
-
```
|
|
1069
|
-
|
|
1070
|
-
Look for:
|
|
1071
|
-
- `📦 Found X exported function(s): ...` - Shows detected functions
|
|
1072
|
-
- `🤖 AI response: ...` - Shows what AI detected
|
|
1073
|
-
- `📊 AST Analysis result: ...` - Shows file parsing results
|
|
1074
|
-
|
|
1075
|
-
### "Configuration Error: codeguard.json not found"
|
|
1076
|
-
|
|
1077
|
-
Create a `codeguard.json` file in your project root. See Configuration section above.
|
|
1078
|
-
|
|
1079
|
-
### "API key not configured"
|
|
244
|
+
| Error | Cause | Fix |
|
|
245
|
+
|-------|-------|-----|
|
|
246
|
+
| `codeguard.json not found` | Missing config file | Create it in the project root |
|
|
247
|
+
| `API key not configured` | Missing key | Add to `codeguard.json` or set env var |
|
|
248
|
+
| `Not a git repository` | `auto`/`test`/`review` require git | Run `git init` |
|
|
249
|
+
| `No changes detected` | No staged/unstaged source changes | Check `git status` and `git diff` |
|
|
250
|
+
| `No exported functions changed` | Only internal functions modified | Ensure the changed function is exported |
|
|
251
|
+
| `Missing required packages` | Babel not installed | `npm install --save-dev @babel/parser @babel/traverse` |
|
|
1080
252
|
|
|
1081
|
-
|
|
253
|
+
More troubleshooting: [docs/CONFIGURATION.md#troubleshooting](docs/CONFIGURATION.md#troubleshooting)
|
|
1082
254
|
|
|
1083
|
-
|
|
1084
|
-
{
|
|
1085
|
-
"aiProvider": "claude",
|
|
1086
|
-
"apiKeys": {
|
|
1087
|
-
"claude": "sk-ant-..."
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
```
|
|
1091
|
-
|
|
1092
|
-
### Tests fail with import errors
|
|
1093
|
-
|
|
1094
|
-
The tool automatically detects and fixes import path errors. If issues persist:
|
|
1095
|
-
1. Check that all dependencies are installed
|
|
1096
|
-
2. Verify your project structure matches expected paths
|
|
1097
|
-
3. Ensure TypeScript is configured correctly
|
|
1098
|
-
|
|
1099
|
-
### "Missing required packages"
|
|
1100
|
-
|
|
1101
|
-
Install Babel dependencies:
|
|
1102
|
-
|
|
1103
|
-
```bash
|
|
1104
|
-
npm install --save-dev @babel/parser @babel/traverse
|
|
1105
|
-
```
|
|
1106
|
-
|
|
1107
|
-
## Programmatic Usage
|
|
1108
|
-
|
|
1109
|
-
You can also use CodeGuard as a library:
|
|
1110
|
-
|
|
1111
|
-
```typescript
|
|
1112
|
-
import { generateTests, analyzeFileAST } from 'codeguard-testgen';
|
|
1113
|
-
|
|
1114
|
-
// Generate tests for a file
|
|
1115
|
-
await generateTests('src/services/user.service.ts');
|
|
1116
|
-
|
|
1117
|
-
// Analyze a file's AST
|
|
1118
|
-
const analysis = analyzeFileAST('src/utils/helpers.ts');
|
|
1119
|
-
console.log(analysis.functions);
|
|
1120
|
-
```
|
|
1121
|
-
|
|
1122
|
-
## Project Structure
|
|
1123
|
-
|
|
1124
|
-
After installation, your project will have:
|
|
1125
|
-
|
|
1126
|
-
```
|
|
1127
|
-
your-project/
|
|
1128
|
-
├── codeguard.json # Configuration file
|
|
1129
|
-
├── src/
|
|
1130
|
-
│ ├── services/
|
|
1131
|
-
│ │ └── user.service.ts
|
|
1132
|
-
│ └── tests/ # Generated tests
|
|
1133
|
-
│ └── services/
|
|
1134
|
-
│ └── user.service.test.ts
|
|
1135
|
-
├── reviews/ # AI code reviews
|
|
1136
|
-
│ └── user.service.review.md
|
|
1137
|
-
└── .codeguard-cache/ # Optional index cache
|
|
1138
|
-
```
|
|
255
|
+
---
|
|
1139
256
|
|
|
1140
257
|
## Requirements
|
|
1141
258
|
|
|
1142
259
|
- Node.js >= 16.0.0
|
|
1143
|
-
-
|
|
1144
|
-
-
|
|
260
|
+
- Git (for `auto`, `review`, `test` commands)
|
|
261
|
+
- `@babel/parser` and `@babel/traverse` in the target project
|
|
262
|
+
- Vitest or Jest (for running generated tests)
|
|
263
|
+
|
|
264
|
+
---
|
|
1145
265
|
|
|
1146
266
|
## License
|
|
1147
267
|
|
|
1148
|
-
MIT
|
|
268
|
+
MIT — see [LICENSE](LICENSE).
|
|
1149
269
|
|
|
1150
|
-
##
|
|
270
|
+
## Author
|
|
271
|
+
|
|
272
|
+
[Ekansh Gahlot](https://github.com/Ekansh-Gahlot)
|
|
1151
273
|
|
|
1152
|
-
|
|
274
|
+
## Contributing
|
|
1153
275
|
|
|
1154
|
-
|
|
276
|
+
See [docs/CONTRIBUTING.md](docs/CONTRIBUTING.md).
|
|
1155
277
|
|
|
1156
|
-
|
|
278
|
+
## Issues
|
|
1157
279
|
|
|
280
|
+
[github.com/Ekansh-Gahlot/codeguard-testgen/issues](https://github.com/Ekansh-Gahlot/codeguard-testgen/issues)
|