@kakarot-ci/core 0.6.6 → 0.7.1

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 CHANGED
@@ -1,2 +1,457 @@
1
- # kakarot-ci
2
- The core package for kakarot-ci
1
+ <div align="center">
2
+ <img src="assets/logo.svg" alt="Kakarot CI Logo" width="200">
3
+
4
+ # Kakarot CI
5
+
6
+ [![npm version](https://img.shields.io/npm/v/@kakarot-ci/core.svg)](https://www.npmjs.com/package/@kakarot-ci/core)
7
+ [![Node.js version](https://img.shields.io/node/v/@kakarot-ci/core.svg)](https://nodejs.org/)
8
+ </div>
9
+
10
+ > AI-powered unit test generation for TypeScript and JavaScript
11
+
12
+ > **⚠️ Beta Software**: This project is currently in beta. While functional, expect occasional bugs and breaking changes as we iterate and improve. Please report issues and provide feedback!
13
+
14
+ Kakarot CI automatically generates comprehensive unit tests using AI. While optimized for pull request workflows, it can be used in various scenarios: analyzing PR changes, generating tests for specific files, or creating test suites for entire codebases. It analyzes code, generates test files, runs them, and can automatically commit results back to your repository.
15
+
16
+ ## Features
17
+
18
+ - 🤖 **AI-Powered Test Generation**: Uses LLMs (OpenAI, Anthropic, Google) to generate comprehensive unit tests
19
+ - 🔍 **Smart Code Analysis**: Analyzes AST to extract functions and understand code structure
20
+ - 🎯 **Targeted Testing**: Generates tests for specific functions, files, or entire codebases
21
+ - 🔄 **Auto-Fix Loop**: Automatically fixes failing tests with multiple retry attempts
22
+ - 📊 **Coverage Reports**: Optional test coverage analysis and summaries
23
+ - 🚀 **GitHub Integration**: Seamlessly integrates with GitHub Actions and PR workflows (optional)
24
+ - ⚙️ **Flexible Configuration**: Supports Jest and Vitest, configurable test locations and patterns
25
+ - 📝 **PR Comments**: Automatically posts test generation summaries to pull requests
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ npm install --save-dev @kakarot-ci/core
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### Pull Request Workflow (Primary Use Case)
36
+
37
+ The simplest way to use Kakarot CI is via the command-line interface for pull requests:
38
+
39
+ ```bash
40
+ npx kakarot-ci --pr 123 --owner myorg --repo myrepo --token ghp_xxxxx
41
+ ```
42
+
43
+ Or in a GitHub Actions workflow:
44
+
45
+ ```yaml
46
+ - name: Generate Tests
47
+ run: npx kakarot-ci
48
+ env:
49
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50
+ KAKAROT_API_KEY: ${{ secrets.KAKAROT_API_KEY }}
51
+ ```
52
+
53
+ The CLI automatically detects:
54
+ - PR number from `GITHUB_EVENT_PATH` (GitHub Actions)
55
+ - Repository owner/repo from `GITHUB_REPOSITORY` or git remote
56
+ - GitHub token from `GITHUB_TOKEN` environment variable
57
+
58
+ ### Programmatic API
59
+
60
+ #### Pull Request Processing
61
+
62
+ ```typescript
63
+ import { runPullRequest } from '@kakarot-ci/core';
64
+
65
+ const summary = await runPullRequest({
66
+ prNumber: 123,
67
+ owner: 'myorg',
68
+ repo: 'myrepo',
69
+ githubToken: 'ghp_xxxxx',
70
+ });
71
+
72
+ console.log(`Generated ${summary.testsGenerated} tests`);
73
+ ```
74
+
75
+ #### Custom Workflows
76
+
77
+ You can also use the lower-level APIs for custom workflows:
78
+
79
+ ```typescript
80
+ import {
81
+ TestGenerator,
82
+ extractTestTargets,
83
+ analyzeFile,
84
+ writeTestFiles
85
+ } from '@kakarot-ci/core';
86
+
87
+ // Generate tests for specific files
88
+ const targets = await analyzeFile('src/utils.ts', 'main', githubClient, config);
89
+ const generator = new TestGenerator({ apiKey, provider: 'openai' });
90
+
91
+ for (const target of targets) {
92
+ const test = await generator.generateTest({
93
+ target,
94
+ framework: 'vitest',
95
+ });
96
+ // Write test file...
97
+ }
98
+
99
+ // Or extract targets from file diffs
100
+ const prFiles = [...]; // Your file changes
101
+ const targets = await extractTestTargets(prFiles, githubClient, 'sha', config);
102
+ ```
103
+
104
+ ## Configuration
105
+
106
+ Kakarot CI can be configured via:
107
+
108
+ 1. **Config file**: `kakarot.config.js`, `.kakarot-ci.config.js`, or `.kakarot-ci.config.json`
109
+ 2. **package.json**: Add a `kakarotCi` field
110
+ 3. **Environment variables**: `KAKAROT_API_KEY`, `GITHUB_TOKEN`, etc.
111
+
112
+ **Note**: TypeScript config files (`.ts`) are not supported in the compiled package. Use JavaScript (`.js`) or JSON (`.json`) config files instead.
113
+
114
+ ### Configuration Options
115
+
116
+ #### Required
117
+
118
+ - **`apiKey`** (string, required)
119
+ - Your LLM provider API key
120
+ - Can also be set via `KAKAROT_API_KEY` environment variable
121
+ - Example: `"sk-xxxxx"` (OpenAI) or `"sk-ant-xxxxx"` (Anthropic)
122
+
123
+ #### LLM Provider Settings
124
+
125
+ - **`provider`** (string, optional, default: `"openai"`)
126
+ - LLM provider to use: `"openai"`, `"anthropic"`, or `"google"`
127
+ - Can also be set via `PROVIDER` environment variable
128
+ - Example: `"anthropic"`
129
+
130
+ - **`model`** (string, optional)
131
+ - Specific model to use (provider-specific)
132
+ - Can also be set via `MODEL` environment variable
133
+ - Defaults to provider's recommended model
134
+ - Examples: `"gpt-4"`, `"claude-3-opus-20240229"`, `"gemini-pro"`
135
+
136
+ - **`maxTokens`** (number, optional, default: provider default)
137
+ - Maximum tokens in response (1-100000)
138
+ - Example: `4000`
139
+
140
+ - **`temperature`** (number, optional, default: `0.2`)
141
+ - Temperature for test generation (0-2)
142
+ - Lower values = more consistent, higher = more creative
143
+ - Example: `0.2`
144
+
145
+ - **`fixTemperature`** (number, optional, default: `0.2`)
146
+ - Temperature for test fixing attempts (0-2)
147
+ - Example: `0.3`
148
+
149
+ - **`maxFixAttempts`** (number, optional, default: `3`)
150
+ - Maximum number of attempts to fix failing tests (0-5)
151
+ - Example: `5`
152
+
153
+ #### Test Framework
154
+
155
+ - **`framework`** (string, required)
156
+ - Test framework: `"jest"` or `"vitest"`
157
+ - Example: `"vitest"`
158
+
159
+ #### Test File Organization
160
+
161
+ - **`testLocation`** (string, optional, default: `"separate"`)
162
+ - Where to place test files: `"separate"` or `"co-located"`
163
+ - `"separate"`: Tests in a dedicated test directory
164
+ - `"co-located"`: Tests next to source files
165
+ - Example: `"co-located"`
166
+
167
+ - **`testDirectory`** (string, optional, default: `"__tests__"`)
168
+ - Directory name for test files (when `testLocation` is `"separate"`)
169
+ - Example: `"tests"` or `"__tests__"`
170
+
171
+ - **`testFilePattern`** (string, optional, default: `"*.test.ts"`)
172
+ - Glob pattern for test file names
173
+ - Example: `"*.spec.ts"` or `"*.test.js"`
174
+
175
+ #### File Filtering
176
+
177
+ - **`includePatterns`** (string[], optional, default: `["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"]`)
178
+ - Glob patterns for files to include when scanning for changes
179
+ - Example: `["src/**/*.ts", "lib/**/*.ts"]`
180
+
181
+ - **`excludePatterns`** (string[], optional, default: `["**/*.test.ts", "**/*.spec.ts", "**/*.test.js", "**/*.spec.js", "**/node_modules/**"]`)
182
+ - Glob patterns for files to exclude
183
+ - Example: `["**/*.test.ts", "**/vendor/**"]`
184
+
185
+ #### Limits
186
+
187
+ - **`maxTestsPerPR`** (number, optional, default: `50`)
188
+ - Maximum number of test targets to process per PR
189
+ - Prevents excessive API usage on large PRs
190
+ - Example: `100`
191
+
192
+ #### GitHub Integration
193
+
194
+ - **`githubToken`** (string, optional)
195
+ - GitHub personal access token
196
+ - Can also be set via `GITHUB_TOKEN` environment variable
197
+ - Required for GitHub operations (commits, PR comments)
198
+ - Example: `"ghp_xxxxx"`
199
+
200
+ - **`githubOwner`** (string, optional)
201
+ - Repository owner (can be auto-detected from git remote)
202
+ - Example: `"myorg"`
203
+
204
+ - **`githubRepo`** (string, optional)
205
+ - Repository name (can be auto-detected from git remote)
206
+ - Example: `"myrepo"`
207
+
208
+ #### Commit Strategy
209
+
210
+ - **`enableAutoCommit`** (boolean, optional, default: `true`)
211
+ - Automatically commit generated tests
212
+ - Example: `false` (to review tests before committing)
213
+
214
+ - **`commitStrategy`** (string, optional, default: `"direct"`)
215
+ - How to commit tests: `"direct"` or `"branch-pr"`
216
+ - `"direct"`: Commit directly to PR branch
217
+ - `"branch-pr"`: Create a new branch and open a PR
218
+ - Example: `"branch-pr"`
219
+
220
+ - **`enablePRComments`** (boolean, optional, default: `true`)
221
+ - Post test generation summary as PR comment
222
+ - Example: `true`
223
+
224
+ #### Coverage
225
+
226
+ - **`enableCoverage`** (boolean, optional, default: `false`)
227
+ - Enable test coverage collection and reporting
228
+ - Example: `true`
229
+
230
+ #### Debugging
231
+
232
+ - **`debug`** (boolean, optional, default: `false`)
233
+ - Enable debug logging
234
+ - Can also be set via `KAKAROT_DEBUG=true` environment variable
235
+ - Example: `true`
236
+
237
+ ### Example Configuration
238
+
239
+ **`kakarot.config.js`**:
240
+
241
+ ```javascript
242
+ /** @type {import('@kakarot-ci/core').KakarotConfig} */
243
+ const config = {
244
+ // Required: API key (can also be set via KAKAROT_API_KEY env var)
245
+ apiKey: process.env.OPENAI_API_KEY,
246
+
247
+ // Required: Test framework
248
+ framework: 'vitest',
249
+
250
+ // Optional: LLM provider settings (can also be set via PROVIDER and MODEL env vars)
251
+ provider: 'openai', // 'openai' | 'anthropic' | 'google'
252
+ model: 'gpt-4',
253
+ temperature: 0.2,
254
+ fixTemperature: 0.2,
255
+ maxTokens: 4000,
256
+
257
+ // File filtering
258
+ includePatterns: ['src/**/*.ts'],
259
+ excludePatterns: [
260
+ '**/*.test.ts',
261
+ '**/*.spec.ts',
262
+ '**/node_modules/**',
263
+ '**/dist/**',
264
+ '**/coverage/**',
265
+ ],
266
+
267
+ // Test file organization
268
+ testLocation: 'co-located', // 'separate' | 'co-located'
269
+ testDirectory: '__tests__', // Only used when testLocation is 'separate'
270
+ testFilePattern: '*.test.ts',
271
+
272
+ // Limits and behavior
273
+ maxTestsPerPR: 50,
274
+ maxFixAttempts: 3,
275
+
276
+ // GitHub integration
277
+ enableAutoCommit: true,
278
+ commitStrategy: 'branch-pr', // 'direct' | 'branch-pr'
279
+ enablePRComments: true,
280
+
281
+ // Optional features
282
+ enableCoverage: false,
283
+ debug: true,
284
+ };
285
+
286
+ module.exports = config;
287
+ ```
288
+
289
+ **`.kakarot-ci.config.json`** (minimal example):
290
+
291
+ ```json
292
+ {
293
+ "framework": "vitest",
294
+ "includePatterns": ["src/**/*.ts"],
295
+ "excludePatterns": [
296
+ "**/*.test.ts",
297
+ "**/*.spec.ts",
298
+ "**/node_modules/**",
299
+ "**/dist/**"
300
+ ],
301
+ "maxFixAttempts": 3,
302
+ "enableAutoCommit": true,
303
+ "commitStrategy": "branch-pr",
304
+ "enablePRComments": true
305
+ }
306
+ ```
307
+
308
+ **Note**: The `apiKey` should be provided via the `KAKAROT_API_KEY` environment variable for security.
309
+
310
+ **`package.json`**:
311
+
312
+ ```json
313
+ {
314
+ "kakarotCi": {
315
+ "framework": "vitest",
316
+ "testLocation": "co-located",
317
+ "maxTestsPerPR": 50
318
+ }
319
+ }
320
+ ```
321
+
322
+ ## GitHub Actions Integration
323
+
324
+ Create a workflow file (`.github/workflows/kakarot-ci.yml`):
325
+
326
+ ```yaml
327
+ name: Kakarot CI
328
+
329
+ on:
330
+ pull_request:
331
+ types: [opened, synchronize, reopened]
332
+
333
+ jobs:
334
+ generate-tests:
335
+ runs-on: ubuntu-latest
336
+ permissions:
337
+ contents: write # Required to push commits directly to branch (commitStrategy: 'direct')
338
+ pull-requests: write # Required to create PRs (commitStrategy: 'branch-pr') and post comments
339
+ steps:
340
+ - uses: actions/checkout@v4
341
+ with:
342
+ fetch-depth: 0
343
+ token: ${{ secrets.GITHUB_TOKEN }}
344
+
345
+ - name: Setup Node.js
346
+ uses: actions/setup-node@v4
347
+ with:
348
+ node-version: '20'
349
+
350
+ - name: Install dependencies
351
+ run: npm ci
352
+
353
+ - name: Generate tests
354
+ run: npx kakarot-ci
355
+ env:
356
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
357
+ KAKAROT_API_KEY: ${{ secrets.KAKAROT_API_KEY }}
358
+ # Optional: Override provider/model via env vars
359
+ # PROVIDER: openai
360
+ # MODEL: gpt-4
361
+
362
+ ```
363
+
364
+ **Note on permissions:**
365
+ - `contents: write` is required to push commits directly to the PR branch (when using `commitStrategy: 'direct'`, the default)
366
+ - `pull-requests: write` is required to:
367
+ - Create pull requests (when using `commitStrategy: 'branch-pr'`)
368
+ - Post PR comments (when `enablePRComments: true`)
369
+ - Both permissions are recommended for full functionality
370
+
371
+ **Using a Personal Access Token (PAT):**
372
+
373
+ If your organization restricts `GITHUB_TOKEN` permissions, you'll need to use a Personal Access Token:
374
+
375
+ ```yaml
376
+ - uses: actions/checkout@v4
377
+ with:
378
+ fetch-depth: 0
379
+ token: ${{ secrets.GH_PAT }} # Use PAT instead of GITHUB_TOKEN
380
+
381
+ - name: Generate tests
382
+ run: npx kakarot-ci
383
+ env:
384
+ GITHUB_TOKEN: ${{ secrets.GH_PAT }} # Use PAT instead of GITHUB_TOKEN
385
+ KAKAROT_API_KEY: ${{ secrets.KAKAROT_API_KEY }}
386
+ # Optional: Override provider/model via env vars
387
+ # PROVIDER: openai
388
+ # MODEL: gpt-4
389
+ ```
390
+
391
+ Create a PAT with `repo` scope and add it as a repository secret named `GH_PAT`.
392
+
393
+ ## How It Works
394
+
395
+ ### Pull Request Workflow
396
+
397
+ 1. **Analyze PR Changes**: Scans the pull request diff to identify changed files
398
+ 2. **Extract Functions**: Uses AST analysis to find functions, methods, and classes that were modified
399
+ 3. **Generate Tests**: Sends function code to LLM with carefully crafted prompts to generate comprehensive tests
400
+ 4. **Run Tests**: Executes the generated tests using your configured test framework
401
+ 5. **Fix Failures**: Automatically attempts to fix failing tests (up to `maxFixAttempts` times)
402
+ 6. **Commit Results**: Commits generated tests back to the PR (if `enableAutoCommit` is true)
403
+ 7. **Post Summary**: Posts a summary comment to the PR with test generation results
404
+
405
+ ### Core Components
406
+
407
+ The tool is built with modular components that can be used independently:
408
+
409
+ - **AST Analysis**: Extracts functions, methods, and classes from TypeScript/JavaScript files
410
+ - **Test Generation**: Uses LLM prompts optimized for generating accurate, comprehensive tests
411
+ - **Test Execution**: Runs tests using Jest or Vitest
412
+ - **Auto-Fix Loop**: Iteratively fixes failing tests using LLM feedback
413
+ - **GitHub Integration**: Optional integration for PR workflows, commits, and comments
414
+
415
+ You can use these components programmatically for custom workflows beyond pull requests.
416
+
417
+ ## Requirements
418
+
419
+ - Node.js >= 18.0.0
420
+ - A test framework (Jest or Vitest) already set up in your project
421
+ - An API key for your chosen LLM provider (OpenAI, Anthropic, or Google)
422
+ - GitHub token with appropriate permissions (required only for GitHub integration features)
423
+
424
+ ## CLI Options
425
+
426
+ ```bash
427
+ kakarot-ci [options]
428
+
429
+ Options:
430
+ --pr <number> Pull request number
431
+ --owner <string> Repository owner
432
+ --repo <string> Repository name
433
+ --token <string> GitHub token (or use GITHUB_TOKEN env var)
434
+ -V, --version Show version number
435
+ -h, --help Display help
436
+ ```
437
+
438
+ ## Environment Variables
439
+
440
+ - `KAKAROT_API_KEY`: LLM provider API key (required, can also be set in config file)
441
+ - `PROVIDER`: LLM provider (`openai`, `anthropic`, or `google`, can also be set in config file)
442
+ - `MODEL`: LLM model name (e.g., `gpt-4`, `claude-3-opus-20240229`, can also be set in config file)
443
+ - `GITHUB_TOKEN`: GitHub personal access token (required for GitHub operations)
444
+ - `GITHUB_REPOSITORY`: Repository in format `owner/repo` (auto-detected in GitHub Actions)
445
+ - `GITHUB_EVENT_PATH`: Path to GitHub event JSON (auto-set in GitHub Actions)
446
+ - `PR_NUMBER`: Pull request number (alternative to `--pr` flag)
447
+ - `KAKAROT_DEBUG`: Enable debug logging (`true`/`false`)
448
+
449
+ ## Contributing
450
+
451
+ Contributions are welcome! Please feel free to submit a Pull Request.
452
+
453
+ **Note**: This project is maintained by a single person as a side project. While I do my best to respond to issues and review PRs, response times may vary. Your patience and understanding are appreciated!
454
+
455
+ ## License
456
+
457
+ [BUSL-1.1](LICENSE)