@kakarot-ci/core 0.2.0 → 0.3.0
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/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +2288 -0
- package/dist/cli/index.js.map +7 -0
- package/dist/core/orchestrator.d.ts +32 -0
- package/dist/core/orchestrator.d.ts.map +1 -0
- package/dist/index.cjs +862 -101
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +851 -100
- package/dist/index.js.map +4 -4
- package/dist/llm/prompts/coverage-summary.d.ts +8 -0
- package/dist/llm/prompts/coverage-summary.d.ts.map +1 -0
- package/dist/llm/test-generator.d.ts +7 -0
- package/dist/llm/test-generator.d.ts.map +1 -1
- package/dist/types/config.d.ts +9 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/coverage.d.ts +40 -0
- package/dist/types/coverage.d.ts.map +1 -0
- package/dist/types/test-runner.d.ts +30 -0
- package/dist/types/test-runner.d.ts.map +1 -0
- package/dist/utils/config-loader.d.ts +4 -0
- package/dist/utils/config-loader.d.ts.map +1 -1
- package/dist/utils/coverage-reader.d.ts +6 -0
- package/dist/utils/coverage-reader.d.ts.map +1 -0
- package/dist/utils/package-manager-detector.d.ts +6 -0
- package/dist/utils/package-manager-detector.d.ts.map +1 -0
- package/dist/utils/test-file-path.d.ts +7 -0
- package/dist/utils/test-file-path.d.ts.map +1 -0
- package/dist/utils/test-file-writer.d.ts +8 -0
- package/dist/utils/test-file-writer.d.ts.map +1 -0
- package/dist/utils/test-runner/factory.d.ts +6 -0
- package/dist/utils/test-runner/factory.d.ts.map +1 -0
- package/dist/utils/test-runner/jest-runner.d.ts +5 -0
- package/dist/utils/test-runner/jest-runner.d.ts.map +1 -0
- package/dist/utils/test-runner/vitest-runner.d.ts +5 -0
- package/dist/utils/test-runner/vitest-runner.d.ts.map +1 -0
- package/package.json +10 -2
package/dist/index.js
CHANGED
|
@@ -3,12 +3,15 @@ import { z } from "zod";
|
|
|
3
3
|
var KakarotConfigSchema = z.object({
|
|
4
4
|
apiKey: z.string(),
|
|
5
5
|
githubToken: z.string().optional(),
|
|
6
|
+
githubOwner: z.string().optional(),
|
|
7
|
+
githubRepo: z.string().optional(),
|
|
6
8
|
provider: z.enum(["openai", "anthropic", "google"]).optional(),
|
|
7
9
|
model: z.string().optional(),
|
|
8
10
|
maxTokens: z.number().int().min(1).max(1e5).optional(),
|
|
9
11
|
temperature: z.number().min(0).max(2).optional(),
|
|
10
12
|
fixTemperature: z.number().min(0).max(2).optional(),
|
|
11
13
|
maxFixAttempts: z.number().int().min(0).max(5).default(3),
|
|
14
|
+
framework: z.enum(["jest", "vitest"]),
|
|
12
15
|
testLocation: z.enum(["separate", "co-located"]).default("separate"),
|
|
13
16
|
testDirectory: z.string().default("__tests__"),
|
|
14
17
|
testFilePattern: z.string().default("*.test.ts"),
|
|
@@ -22,8 +25,8 @@ var KakarotConfigSchema = z.object({
|
|
|
22
25
|
});
|
|
23
26
|
|
|
24
27
|
// src/utils/config-loader.ts
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
28
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
29
|
+
import { findUp } from "find-up";
|
|
27
30
|
|
|
28
31
|
// src/utils/logger.ts
|
|
29
32
|
var debugMode = false;
|
|
@@ -78,108 +81,70 @@ function progress(step, total, message, ...args) {
|
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
// src/utils/config-loader.ts
|
|
81
|
-
function findProjectRoot(startPath) {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
previous = current;
|
|
90
|
-
current = dirname(current);
|
|
91
|
-
}
|
|
92
|
-
return start;
|
|
93
|
-
}
|
|
94
|
-
async function loadTypeScriptConfig(root) {
|
|
95
|
-
const configPath = join(root, "kakarot.config.ts");
|
|
96
|
-
if (!existsSync(configPath)) {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
try {
|
|
100
|
-
const configModule = await import(configPath);
|
|
101
|
-
return configModule.default || configModule.config || null;
|
|
102
|
-
} catch (err) {
|
|
103
|
-
error(`Failed to load kakarot.config.ts: ${err instanceof Error ? err.message : String(err)}`);
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
async function loadJavaScriptConfig(root) {
|
|
108
|
-
const configPath = join(root, ".kakarot-ci.config.js");
|
|
109
|
-
if (!existsSync(configPath)) {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
const configModule = await import(configPath);
|
|
114
|
-
return configModule.default || configModule.config || null;
|
|
115
|
-
} catch (err) {
|
|
116
|
-
error(`Failed to load .kakarot-ci.config.js: ${err instanceof Error ? err.message : String(err)}`);
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function loadJsonConfig(root) {
|
|
121
|
-
const configPath = join(root, ".kakarot-ci.config.json");
|
|
122
|
-
if (!existsSync(configPath)) {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
try {
|
|
126
|
-
const content = readFileSync(configPath, "utf-8");
|
|
127
|
-
return JSON.parse(content);
|
|
128
|
-
} catch (err) {
|
|
129
|
-
error(`Failed to load .kakarot-ci.config.json: ${err instanceof Error ? err.message : String(err)}`);
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
function loadPackageJsonConfig(root) {
|
|
134
|
-
const packagePath = join(root, "package.json");
|
|
135
|
-
if (!existsSync(packagePath)) {
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
try {
|
|
139
|
-
const content = readFileSync(packagePath, "utf-8");
|
|
140
|
-
const pkg = JSON.parse(content);
|
|
141
|
-
return pkg.kakarotCi || null;
|
|
142
|
-
} catch (err) {
|
|
143
|
-
error(`Failed to load package.json: ${err instanceof Error ? err.message : String(err)}`);
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
function mergeEnvConfig(config) {
|
|
148
|
-
const merged = { ...config };
|
|
149
|
-
if (!merged.apiKey && process.env.KAKAROT_API_KEY) {
|
|
150
|
-
merged.apiKey = process.env.KAKAROT_API_KEY;
|
|
151
|
-
}
|
|
152
|
-
if (!merged.githubToken && process.env.GITHUB_TOKEN) {
|
|
153
|
-
merged.githubToken = process.env.GITHUB_TOKEN;
|
|
84
|
+
async function findProjectRoot(startPath) {
|
|
85
|
+
const packageJsonPath = await findUp("package.json", {
|
|
86
|
+
cwd: startPath ?? process.cwd()
|
|
87
|
+
});
|
|
88
|
+
if (packageJsonPath) {
|
|
89
|
+
const { dirname: dirname2 } = await import("path");
|
|
90
|
+
return dirname2(packageJsonPath);
|
|
154
91
|
}
|
|
155
|
-
return
|
|
92
|
+
return startPath ?? process.cwd();
|
|
156
93
|
}
|
|
157
94
|
async function loadConfig() {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
95
|
+
const explorer = cosmiconfig("kakarot", {
|
|
96
|
+
searchPlaces: [
|
|
97
|
+
"kakarot.config.ts",
|
|
98
|
+
"kakarot.config.js",
|
|
99
|
+
".kakarot-ci.config.ts",
|
|
100
|
+
".kakarot-ci.config.js",
|
|
101
|
+
".kakarot-ci.config.json",
|
|
102
|
+
"package.json"
|
|
103
|
+
],
|
|
104
|
+
loaders: {
|
|
105
|
+
".ts": async (filepath) => {
|
|
106
|
+
try {
|
|
107
|
+
const configModule = await import(filepath);
|
|
108
|
+
return configModule.default || configModule.config || null;
|
|
109
|
+
} catch (err) {
|
|
110
|
+
error(`Failed to load TypeScript config: ${err instanceof Error ? err.message : String(err)}`);
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
177
116
|
try {
|
|
178
|
-
|
|
117
|
+
const result = await explorer.search();
|
|
118
|
+
let config = {};
|
|
119
|
+
if (result?.config) {
|
|
120
|
+
config = result.config;
|
|
121
|
+
}
|
|
122
|
+
if (!result || result.filepath?.endsWith("package.json")) {
|
|
123
|
+
const packageJsonPath = await findUp("package.json");
|
|
124
|
+
if (packageJsonPath) {
|
|
125
|
+
const { readFileSync: readFileSync2 } = await import("fs");
|
|
126
|
+
try {
|
|
127
|
+
const pkg = JSON.parse(readFileSync2(packageJsonPath, "utf-8"));
|
|
128
|
+
if (pkg.kakarotCi) {
|
|
129
|
+
config = { ...config, ...pkg.kakarotCi };
|
|
130
|
+
}
|
|
131
|
+
} catch {
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (!config.apiKey && process.env.KAKAROT_API_KEY) {
|
|
136
|
+
config.apiKey = process.env.KAKAROT_API_KEY;
|
|
137
|
+
}
|
|
138
|
+
if (!config.githubToken && process.env.GITHUB_TOKEN) {
|
|
139
|
+
config.githubToken = process.env.GITHUB_TOKEN;
|
|
140
|
+
}
|
|
141
|
+
return KakarotConfigSchema.parse(config);
|
|
179
142
|
} catch (err) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
143
|
+
if (err instanceof Error && err.message.includes("apiKey")) {
|
|
144
|
+
error(
|
|
145
|
+
"Missing required apiKey. Provide it via:\n - Config file (kakarot.config.ts, .kakarot-ci.config.js/json, or package.json)\n - Environment variable: KAKAROT_API_KEY"
|
|
146
|
+
);
|
|
147
|
+
}
|
|
183
148
|
throw err;
|
|
184
149
|
}
|
|
185
150
|
}
|
|
@@ -785,6 +750,29 @@ async function extractTestTargets(files, githubClient, prHeadRef, config) {
|
|
|
785
750
|
return targets;
|
|
786
751
|
}
|
|
787
752
|
|
|
753
|
+
// src/utils/test-file-path.ts
|
|
754
|
+
function getTestFilePath(target, config) {
|
|
755
|
+
const sourcePath = target.filePath;
|
|
756
|
+
const dir = sourcePath.substring(0, sourcePath.lastIndexOf("/"));
|
|
757
|
+
const baseName = sourcePath.substring(sourcePath.lastIndexOf("/") + 1).replace(/\.(ts|tsx|js|jsx)$/, "");
|
|
758
|
+
let ext;
|
|
759
|
+
if (sourcePath.endsWith(".tsx"))
|
|
760
|
+
ext = "tsx";
|
|
761
|
+
else if (sourcePath.endsWith(".jsx"))
|
|
762
|
+
ext = "jsx";
|
|
763
|
+
else if (sourcePath.endsWith(".ts"))
|
|
764
|
+
ext = "ts";
|
|
765
|
+
else
|
|
766
|
+
ext = "js";
|
|
767
|
+
const testExt = ext === "tsx" || ext === "ts" ? "ts" : "js";
|
|
768
|
+
if (config.testLocation === "co-located") {
|
|
769
|
+
return `${dir}/${baseName}.test.${testExt}`;
|
|
770
|
+
} else {
|
|
771
|
+
const testFileName = config.testFilePattern.replace("*", baseName);
|
|
772
|
+
return `${config.testDirectory}/${testFileName}`;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
788
776
|
// src/llm/providers/base.ts
|
|
789
777
|
var BaseLLMProvider = class {
|
|
790
778
|
constructor(apiKey, model, defaultOptions) {
|
|
@@ -1356,27 +1344,790 @@ var TestGenerator = class {
|
|
|
1356
1344
|
throw err;
|
|
1357
1345
|
}
|
|
1358
1346
|
}
|
|
1347
|
+
/**
|
|
1348
|
+
* Generate a human-readable coverage summary
|
|
1349
|
+
*/
|
|
1350
|
+
async generateCoverageSummary(messages) {
|
|
1351
|
+
try {
|
|
1352
|
+
const response = await this.provider.generate(messages, {
|
|
1353
|
+
temperature: 0.3,
|
|
1354
|
+
maxTokens: 500
|
|
1355
|
+
});
|
|
1356
|
+
return response.content;
|
|
1357
|
+
} catch (err) {
|
|
1358
|
+
error(`Failed to generate coverage summary: ${err instanceof Error ? err.message : String(err)}`);
|
|
1359
|
+
throw err;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
};
|
|
1363
|
+
|
|
1364
|
+
// src/utils/package-manager-detector.ts
|
|
1365
|
+
import { existsSync } from "fs";
|
|
1366
|
+
import { join } from "path";
|
|
1367
|
+
function detectPackageManager(projectRoot) {
|
|
1368
|
+
if (existsSync(join(projectRoot, "pnpm-lock.yaml"))) {
|
|
1369
|
+
return "pnpm";
|
|
1370
|
+
}
|
|
1371
|
+
if (existsSync(join(projectRoot, "yarn.lock"))) {
|
|
1372
|
+
return "yarn";
|
|
1373
|
+
}
|
|
1374
|
+
if (existsSync(join(projectRoot, "package-lock.json"))) {
|
|
1375
|
+
return "npm";
|
|
1376
|
+
}
|
|
1377
|
+
return "npm";
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
// src/utils/test-runner/jest-runner.ts
|
|
1381
|
+
import { exec } from "child_process";
|
|
1382
|
+
import { promisify } from "util";
|
|
1383
|
+
var execAsync = promisify(exec);
|
|
1384
|
+
var JestRunner = class {
|
|
1385
|
+
async runTests(options) {
|
|
1386
|
+
const { testFiles, packageManager, projectRoot, coverage } = options;
|
|
1387
|
+
debug(`Running Jest tests for ${testFiles.length} file(s)`);
|
|
1388
|
+
const testFilesArg = testFiles.map((f) => `"${f}"`).join(" ");
|
|
1389
|
+
const coverageFlag = coverage ? "--coverage --coverageReporters=json" : "--no-coverage";
|
|
1390
|
+
const cmd = `${packageManager} test -- --json ${coverageFlag} ${testFilesArg}`;
|
|
1391
|
+
try {
|
|
1392
|
+
const { stdout, stderr } = await execAsync(cmd, {
|
|
1393
|
+
cwd: projectRoot,
|
|
1394
|
+
maxBuffer: 10 * 1024 * 1024
|
|
1395
|
+
// 10MB
|
|
1396
|
+
});
|
|
1397
|
+
if (stderr && !stderr.includes("PASS") && !stderr.includes("FAIL")) {
|
|
1398
|
+
debug(`Jest stderr: ${stderr}`);
|
|
1399
|
+
}
|
|
1400
|
+
const result = JSON.parse(stdout);
|
|
1401
|
+
return testFiles.map((testFile, index) => {
|
|
1402
|
+
const testResult = result.testResults[index] || result.testResults[0];
|
|
1403
|
+
const failures = [];
|
|
1404
|
+
if (testResult) {
|
|
1405
|
+
for (const assertion of testResult.assertionResults) {
|
|
1406
|
+
if (assertion.status === "failed" && assertion.failureMessages.length > 0) {
|
|
1407
|
+
const failureMessage = assertion.failureMessages[0];
|
|
1408
|
+
failures.push({
|
|
1409
|
+
testName: assertion.title,
|
|
1410
|
+
message: failureMessage,
|
|
1411
|
+
stack: failureMessage
|
|
1412
|
+
});
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
return {
|
|
1417
|
+
success: result.numFailedTests === 0,
|
|
1418
|
+
testFile,
|
|
1419
|
+
passed: result.numPassedTests,
|
|
1420
|
+
failed: result.numFailedTests,
|
|
1421
|
+
total: result.numTotalTests,
|
|
1422
|
+
duration: 0,
|
|
1423
|
+
// Jest JSON doesn't include duration per file
|
|
1424
|
+
failures
|
|
1425
|
+
};
|
|
1426
|
+
});
|
|
1427
|
+
} catch (err) {
|
|
1428
|
+
if (err && typeof err === "object" && "stdout" in err) {
|
|
1429
|
+
try {
|
|
1430
|
+
const result = JSON.parse(err.stdout);
|
|
1431
|
+
return testFiles.map((testFile) => {
|
|
1432
|
+
const failures = [];
|
|
1433
|
+
for (const testResult of result.testResults) {
|
|
1434
|
+
for (const assertion of testResult.assertionResults) {
|
|
1435
|
+
if (assertion.status === "failed" && assertion.failureMessages.length > 0) {
|
|
1436
|
+
failures.push({
|
|
1437
|
+
testName: assertion.title,
|
|
1438
|
+
message: assertion.failureMessages[0],
|
|
1439
|
+
stack: assertion.failureMessages[0]
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
return {
|
|
1445
|
+
success: result.numFailedTests === 0,
|
|
1446
|
+
testFile,
|
|
1447
|
+
passed: result.numPassedTests,
|
|
1448
|
+
failed: result.numFailedTests,
|
|
1449
|
+
total: result.numTotalTests,
|
|
1450
|
+
duration: 0,
|
|
1451
|
+
failures
|
|
1452
|
+
};
|
|
1453
|
+
});
|
|
1454
|
+
} catch (parseErr) {
|
|
1455
|
+
error(`Failed to parse Jest output: ${parseErr instanceof Error ? parseErr.message : String(parseErr)}`);
|
|
1456
|
+
throw err;
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
error(`Jest test execution failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
1460
|
+
throw err;
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1359
1463
|
};
|
|
1464
|
+
|
|
1465
|
+
// src/utils/test-runner/vitest-runner.ts
|
|
1466
|
+
import { exec as exec2 } from "child_process";
|
|
1467
|
+
import { promisify as promisify2 } from "util";
|
|
1468
|
+
var execAsync2 = promisify2(exec2);
|
|
1469
|
+
var VitestRunner = class {
|
|
1470
|
+
async runTests(options) {
|
|
1471
|
+
const { testFiles, packageManager, projectRoot, coverage } = options;
|
|
1472
|
+
debug(`Running Vitest tests for ${testFiles.length} file(s)`);
|
|
1473
|
+
const testFilesArg = testFiles.map((f) => `"${f}"`).join(" ");
|
|
1474
|
+
const coverageFlag = coverage ? "--coverage" : "";
|
|
1475
|
+
const cmd = `${packageManager} test -- --reporter=json ${coverageFlag} ${testFilesArg}`;
|
|
1476
|
+
try {
|
|
1477
|
+
const { stdout, stderr } = await execAsync2(cmd, {
|
|
1478
|
+
cwd: projectRoot,
|
|
1479
|
+
maxBuffer: 10 * 1024 * 1024
|
|
1480
|
+
// 10MB
|
|
1481
|
+
});
|
|
1482
|
+
if (stderr && !stderr.includes("PASS") && !stderr.includes("FAIL")) {
|
|
1483
|
+
debug(`Vitest stderr: ${stderr}`);
|
|
1484
|
+
}
|
|
1485
|
+
const lines = stdout.trim().split("\n");
|
|
1486
|
+
const jsonLine = lines[lines.length - 1];
|
|
1487
|
+
if (!jsonLine || !jsonLine.startsWith("{")) {
|
|
1488
|
+
throw new Error("No valid JSON output from Vitest");
|
|
1489
|
+
}
|
|
1490
|
+
const result = JSON.parse(jsonLine);
|
|
1491
|
+
return testFiles.map((testFile, index) => {
|
|
1492
|
+
const testResult = result.testResults[index] || result.testResults[0];
|
|
1493
|
+
const failures = [];
|
|
1494
|
+
if (testResult) {
|
|
1495
|
+
for (const assertion of testResult.assertionResults) {
|
|
1496
|
+
if (assertion.status === "failed" && assertion.failureMessages.length > 0) {
|
|
1497
|
+
const failureMessage = assertion.failureMessages[0];
|
|
1498
|
+
failures.push({
|
|
1499
|
+
testName: assertion.title,
|
|
1500
|
+
message: failureMessage,
|
|
1501
|
+
stack: failureMessage
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
return {
|
|
1507
|
+
success: result.numFailedTests === 0,
|
|
1508
|
+
testFile,
|
|
1509
|
+
passed: result.numPassedTests,
|
|
1510
|
+
failed: result.numFailedTests,
|
|
1511
|
+
total: result.numTotalTests,
|
|
1512
|
+
duration: 0,
|
|
1513
|
+
// Vitest JSON doesn't include duration per file
|
|
1514
|
+
failures
|
|
1515
|
+
};
|
|
1516
|
+
});
|
|
1517
|
+
} catch (err) {
|
|
1518
|
+
if (err && typeof err === "object" && "stdout" in err) {
|
|
1519
|
+
try {
|
|
1520
|
+
const lines = err.stdout.trim().split("\n");
|
|
1521
|
+
const jsonLine = lines[lines.length - 1];
|
|
1522
|
+
if (jsonLine && jsonLine.startsWith("{")) {
|
|
1523
|
+
const result = JSON.parse(jsonLine);
|
|
1524
|
+
return testFiles.map((testFile) => {
|
|
1525
|
+
const failures = [];
|
|
1526
|
+
for (const testResult of result.testResults) {
|
|
1527
|
+
for (const assertion of testResult.assertionResults) {
|
|
1528
|
+
if (assertion.status === "failed" && assertion.failureMessages.length > 0) {
|
|
1529
|
+
failures.push({
|
|
1530
|
+
testName: assertion.title,
|
|
1531
|
+
message: assertion.failureMessages[0],
|
|
1532
|
+
stack: assertion.failureMessages[0]
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
return {
|
|
1538
|
+
success: result.numFailedTests === 0,
|
|
1539
|
+
testFile,
|
|
1540
|
+
passed: result.numPassedTests,
|
|
1541
|
+
failed: result.numFailedTests,
|
|
1542
|
+
total: result.numTotalTests,
|
|
1543
|
+
duration: 0,
|
|
1544
|
+
failures
|
|
1545
|
+
};
|
|
1546
|
+
});
|
|
1547
|
+
}
|
|
1548
|
+
} catch (parseErr) {
|
|
1549
|
+
error(`Failed to parse Vitest output: ${parseErr instanceof Error ? parseErr.message : String(parseErr)}`);
|
|
1550
|
+
throw err;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
error(`Vitest test execution failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
1554
|
+
throw err;
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
};
|
|
1558
|
+
|
|
1559
|
+
// src/utils/test-runner/factory.ts
|
|
1560
|
+
function createTestRunner(framework) {
|
|
1561
|
+
switch (framework) {
|
|
1562
|
+
case "jest":
|
|
1563
|
+
return new JestRunner();
|
|
1564
|
+
case "vitest":
|
|
1565
|
+
return new VitestRunner();
|
|
1566
|
+
default:
|
|
1567
|
+
throw new Error(`Unsupported test framework: ${framework}`);
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
// src/utils/test-file-writer.ts
|
|
1572
|
+
import { writeFileSync, mkdirSync, existsSync as existsSync2 } from "fs";
|
|
1573
|
+
import { dirname, join as join2 } from "path";
|
|
1574
|
+
function writeTestFiles(testFiles, projectRoot) {
|
|
1575
|
+
const writtenPaths = [];
|
|
1576
|
+
for (const [relativePath, fileData] of testFiles.entries()) {
|
|
1577
|
+
const fullPath = join2(projectRoot, relativePath);
|
|
1578
|
+
const dir = dirname(fullPath);
|
|
1579
|
+
if (!existsSync2(dir)) {
|
|
1580
|
+
mkdirSync(dir, { recursive: true });
|
|
1581
|
+
debug(`Created directory: ${dir}`);
|
|
1582
|
+
}
|
|
1583
|
+
writeFileSync(fullPath, fileData.content, "utf-8");
|
|
1584
|
+
writtenPaths.push(relativePath);
|
|
1585
|
+
debug(`Wrote test file: ${relativePath}`);
|
|
1586
|
+
}
|
|
1587
|
+
return writtenPaths;
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
// src/utils/coverage-reader.ts
|
|
1591
|
+
import { readFileSync, existsSync as existsSync3 } from "fs";
|
|
1592
|
+
import { join as join3 } from "path";
|
|
1593
|
+
function parseJestCoverage(data) {
|
|
1594
|
+
const files = [];
|
|
1595
|
+
let totalStatements = 0;
|
|
1596
|
+
let coveredStatements = 0;
|
|
1597
|
+
let totalBranches = 0;
|
|
1598
|
+
let coveredBranches = 0;
|
|
1599
|
+
let totalFunctions = 0;
|
|
1600
|
+
let coveredFunctions = 0;
|
|
1601
|
+
let totalLines = 0;
|
|
1602
|
+
let coveredLines = 0;
|
|
1603
|
+
for (const [filePath, coverage] of Object.entries(data)) {
|
|
1604
|
+
const statementCounts = Object.values(coverage.statements);
|
|
1605
|
+
const branchCounts = Object.values(coverage.branches);
|
|
1606
|
+
const functionCounts = Object.values(coverage.functions);
|
|
1607
|
+
const lineCounts = Object.values(coverage.lines);
|
|
1608
|
+
const fileStatements = {
|
|
1609
|
+
total: statementCounts.length,
|
|
1610
|
+
covered: statementCounts.filter((c) => c > 0).length,
|
|
1611
|
+
percentage: statementCounts.length > 0 ? statementCounts.filter((c) => c > 0).length / statementCounts.length * 100 : 100
|
|
1612
|
+
};
|
|
1613
|
+
const fileBranches = {
|
|
1614
|
+
total: branchCounts.length,
|
|
1615
|
+
covered: branchCounts.filter((c) => c > 0).length,
|
|
1616
|
+
percentage: branchCounts.length > 0 ? branchCounts.filter((c) => c > 0).length / branchCounts.length * 100 : 100
|
|
1617
|
+
};
|
|
1618
|
+
const fileFunctions = {
|
|
1619
|
+
total: functionCounts.length,
|
|
1620
|
+
covered: functionCounts.filter((c) => c > 0).length,
|
|
1621
|
+
percentage: functionCounts.length > 0 ? functionCounts.filter((c) => c > 0).length / functionCounts.length * 100 : 100
|
|
1622
|
+
};
|
|
1623
|
+
const fileLines = {
|
|
1624
|
+
total: lineCounts.length,
|
|
1625
|
+
covered: lineCounts.filter((c) => c > 0).length,
|
|
1626
|
+
percentage: lineCounts.length > 0 ? lineCounts.filter((c) => c > 0).length / lineCounts.length * 100 : 100
|
|
1627
|
+
};
|
|
1628
|
+
files.push({
|
|
1629
|
+
path: filePath,
|
|
1630
|
+
metrics: {
|
|
1631
|
+
statements: fileStatements,
|
|
1632
|
+
branches: fileBranches,
|
|
1633
|
+
functions: fileFunctions,
|
|
1634
|
+
lines: fileLines
|
|
1635
|
+
}
|
|
1636
|
+
});
|
|
1637
|
+
totalStatements += fileStatements.total;
|
|
1638
|
+
coveredStatements += fileStatements.covered;
|
|
1639
|
+
totalBranches += fileBranches.total;
|
|
1640
|
+
coveredBranches += fileBranches.covered;
|
|
1641
|
+
totalFunctions += fileFunctions.total;
|
|
1642
|
+
coveredFunctions += fileFunctions.covered;
|
|
1643
|
+
totalLines += fileLines.total;
|
|
1644
|
+
coveredLines += fileLines.covered;
|
|
1645
|
+
}
|
|
1646
|
+
return {
|
|
1647
|
+
total: {
|
|
1648
|
+
statements: {
|
|
1649
|
+
total: totalStatements,
|
|
1650
|
+
covered: coveredStatements,
|
|
1651
|
+
percentage: totalStatements > 0 ? coveredStatements / totalStatements * 100 : 100
|
|
1652
|
+
},
|
|
1653
|
+
branches: {
|
|
1654
|
+
total: totalBranches,
|
|
1655
|
+
covered: coveredBranches,
|
|
1656
|
+
percentage: totalBranches > 0 ? coveredBranches / totalBranches * 100 : 100
|
|
1657
|
+
},
|
|
1658
|
+
functions: {
|
|
1659
|
+
total: totalFunctions,
|
|
1660
|
+
covered: coveredFunctions,
|
|
1661
|
+
percentage: totalFunctions > 0 ? coveredFunctions / totalFunctions * 100 : 100
|
|
1662
|
+
},
|
|
1663
|
+
lines: {
|
|
1664
|
+
total: totalLines,
|
|
1665
|
+
covered: coveredLines,
|
|
1666
|
+
percentage: totalLines > 0 ? coveredLines / totalLines * 100 : 100
|
|
1667
|
+
}
|
|
1668
|
+
},
|
|
1669
|
+
files
|
|
1670
|
+
};
|
|
1671
|
+
}
|
|
1672
|
+
function parseVitestCoverage(data) {
|
|
1673
|
+
return parseJestCoverage(data);
|
|
1674
|
+
}
|
|
1675
|
+
function readCoverageReport(projectRoot, framework) {
|
|
1676
|
+
const coveragePath = join3(projectRoot, "coverage", "coverage-final.json");
|
|
1677
|
+
if (!existsSync3(coveragePath)) {
|
|
1678
|
+
debug(`Coverage file not found at ${coveragePath}`);
|
|
1679
|
+
return null;
|
|
1680
|
+
}
|
|
1681
|
+
try {
|
|
1682
|
+
const content = readFileSync(coveragePath, "utf-8");
|
|
1683
|
+
const data = JSON.parse(content);
|
|
1684
|
+
if (framework === "jest") {
|
|
1685
|
+
return parseJestCoverage(data);
|
|
1686
|
+
} else {
|
|
1687
|
+
return parseVitestCoverage(data);
|
|
1688
|
+
}
|
|
1689
|
+
} catch (err) {
|
|
1690
|
+
warn(`Failed to read coverage report: ${err instanceof Error ? err.message : String(err)}`);
|
|
1691
|
+
return null;
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
// src/llm/prompts/coverage-summary.ts
|
|
1696
|
+
function buildCoverageSummaryPrompt(coverageReport, testResults, functionsTested, coverageDelta) {
|
|
1697
|
+
const systemPrompt = `You are a technical writer specializing in test coverage reports. Your task is to generate a clear, concise, and actionable summary of test coverage metrics.
|
|
1698
|
+
|
|
1699
|
+
Requirements:
|
|
1700
|
+
1. Use clear, professional language
|
|
1701
|
+
2. Highlight key metrics (lines, branches, functions, statements)
|
|
1702
|
+
3. Mention which functions were tested
|
|
1703
|
+
4. If coverage delta is provided, explain the change
|
|
1704
|
+
5. Provide actionable insights or recommendations
|
|
1705
|
+
6. Format as markdown suitable for GitHub PR comments
|
|
1706
|
+
7. Keep it concise (2-3 paragraphs max)`;
|
|
1707
|
+
const totalTests = testResults.reduce((sum, r) => sum + r.total, 0);
|
|
1708
|
+
const passedTests = testResults.reduce((sum, r) => sum + r.passed, 0);
|
|
1709
|
+
const failedTests = testResults.reduce((sum, r) => sum + r.failed, 0);
|
|
1710
|
+
const userPrompt = `Generate a human-readable test coverage summary with the following information:
|
|
1711
|
+
|
|
1712
|
+
**Coverage Metrics:**
|
|
1713
|
+
- Lines: ${coverageReport.total.lines.percentage.toFixed(1)}% (${coverageReport.total.lines.covered}/${coverageReport.total.lines.total})
|
|
1714
|
+
- Branches: ${coverageReport.total.branches.percentage.toFixed(1)}% (${coverageReport.total.branches.covered}/${coverageReport.total.branches.total})
|
|
1715
|
+
- Functions: ${coverageReport.total.functions.percentage.toFixed(1)}% (${coverageReport.total.functions.covered}/${coverageReport.total.functions.total})
|
|
1716
|
+
- Statements: ${coverageReport.total.statements.percentage.toFixed(1)}% (${coverageReport.total.statements.covered}/${coverageReport.total.statements.total})
|
|
1717
|
+
|
|
1718
|
+
**Test Results:**
|
|
1719
|
+
- Total tests: ${totalTests}
|
|
1720
|
+
- Passed: ${passedTests}
|
|
1721
|
+
- Failed: ${failedTests}
|
|
1722
|
+
|
|
1723
|
+
**Functions Tested:**
|
|
1724
|
+
${functionsTested.length > 0 ? functionsTested.map((f) => `- ${f}`).join("\n") : "None"}
|
|
1725
|
+
|
|
1726
|
+
${coverageDelta ? `**Coverage Changes:**
|
|
1727
|
+
- Lines: ${coverageDelta.lines > 0 ? "+" : ""}${coverageDelta.lines.toFixed(1)}%
|
|
1728
|
+
- Branches: ${coverageDelta.branches > 0 ? "+" : ""}${coverageDelta.branches.toFixed(1)}%
|
|
1729
|
+
- Functions: ${coverageDelta.functions > 0 ? "+" : ""}${coverageDelta.functions.toFixed(1)}%
|
|
1730
|
+
- Statements: ${coverageDelta.statements > 0 ? "+" : ""}${coverageDelta.statements.toFixed(1)}%
|
|
1731
|
+
` : ""}
|
|
1732
|
+
|
|
1733
|
+
Generate a concise, professional summary that explains what was tested and the coverage achieved.`;
|
|
1734
|
+
return [
|
|
1735
|
+
{ role: "system", content: systemPrompt },
|
|
1736
|
+
{ role: "user", content: userPrompt }
|
|
1737
|
+
];
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
// src/core/orchestrator.ts
|
|
1741
|
+
async function runPullRequest(context) {
|
|
1742
|
+
const config = await loadConfig();
|
|
1743
|
+
initLogger(config);
|
|
1744
|
+
info(`Processing PR #${context.prNumber} for ${context.owner}/${context.repo}`);
|
|
1745
|
+
const githubToken = context.githubToken || config.githubToken;
|
|
1746
|
+
if (!githubToken) {
|
|
1747
|
+
throw new Error("GitHub token is required. Provide it via config.githubToken or context.githubToken");
|
|
1748
|
+
}
|
|
1749
|
+
const githubClient = new GitHubClient({
|
|
1750
|
+
token: githubToken,
|
|
1751
|
+
owner: context.owner,
|
|
1752
|
+
repo: context.repo
|
|
1753
|
+
});
|
|
1754
|
+
const pr = await githubClient.getPullRequest(context.prNumber);
|
|
1755
|
+
if (pr.state !== "open") {
|
|
1756
|
+
warn(`PR #${context.prNumber} is ${pr.state}, skipping test generation`);
|
|
1757
|
+
return {
|
|
1758
|
+
targetsProcessed: 0,
|
|
1759
|
+
testsGenerated: 0,
|
|
1760
|
+
testsFailed: 0,
|
|
1761
|
+
testFiles: [],
|
|
1762
|
+
errors: []
|
|
1763
|
+
};
|
|
1764
|
+
}
|
|
1765
|
+
info(`PR: ${pr.title} (${pr.head.ref} -> ${pr.base.ref})`);
|
|
1766
|
+
const prFiles = await githubClient.listPullRequestFiles(context.prNumber);
|
|
1767
|
+
if (prFiles.length === 0) {
|
|
1768
|
+
info("No files changed in this PR");
|
|
1769
|
+
return {
|
|
1770
|
+
targetsProcessed: 0,
|
|
1771
|
+
testsGenerated: 0,
|
|
1772
|
+
testsFailed: 0,
|
|
1773
|
+
testFiles: [],
|
|
1774
|
+
errors: []
|
|
1775
|
+
};
|
|
1776
|
+
}
|
|
1777
|
+
info(`Found ${prFiles.length} file(s) changed in PR`);
|
|
1778
|
+
const prHeadRef = pr.head.sha;
|
|
1779
|
+
const targets = await extractTestTargets(
|
|
1780
|
+
prFiles,
|
|
1781
|
+
githubClient,
|
|
1782
|
+
prHeadRef,
|
|
1783
|
+
config
|
|
1784
|
+
);
|
|
1785
|
+
if (targets.length === 0) {
|
|
1786
|
+
info("No test targets found in changed files");
|
|
1787
|
+
return {
|
|
1788
|
+
targetsProcessed: 0,
|
|
1789
|
+
testsGenerated: 0,
|
|
1790
|
+
testsFailed: 0,
|
|
1791
|
+
testFiles: [],
|
|
1792
|
+
errors: []
|
|
1793
|
+
};
|
|
1794
|
+
}
|
|
1795
|
+
const limitedTargets = targets.slice(0, config.maxTestsPerPR);
|
|
1796
|
+
if (targets.length > limitedTargets.length) {
|
|
1797
|
+
warn(`Limiting to ${config.maxTestsPerPR} test targets (found ${targets.length})`);
|
|
1798
|
+
}
|
|
1799
|
+
info(`Found ${limitedTargets.length} test target(s)`);
|
|
1800
|
+
const framework = config.framework;
|
|
1801
|
+
info(`Using test framework: ${framework}`);
|
|
1802
|
+
const testGenerator = new TestGenerator({
|
|
1803
|
+
apiKey: config.apiKey,
|
|
1804
|
+
provider: config.provider,
|
|
1805
|
+
model: config.model,
|
|
1806
|
+
maxTokens: config.maxTokens,
|
|
1807
|
+
maxFixAttempts: config.maxFixAttempts,
|
|
1808
|
+
temperature: config.temperature,
|
|
1809
|
+
fixTemperature: config.fixTemperature
|
|
1810
|
+
});
|
|
1811
|
+
let testFiles = /* @__PURE__ */ new Map();
|
|
1812
|
+
const errors = [];
|
|
1813
|
+
let testsGenerated = 0;
|
|
1814
|
+
let testsFailed = 0;
|
|
1815
|
+
for (let i = 0; i < limitedTargets.length; i++) {
|
|
1816
|
+
const target = limitedTargets[i];
|
|
1817
|
+
progress(i + 1, limitedTargets.length, `Generating test for ${target.functionName}`);
|
|
1818
|
+
try {
|
|
1819
|
+
const testFilePath = getTestFilePath(target, config);
|
|
1820
|
+
let existingTestFile;
|
|
1821
|
+
const testFileExists = await githubClient.fileExists(prHeadRef, testFilePath);
|
|
1822
|
+
if (testFileExists) {
|
|
1823
|
+
try {
|
|
1824
|
+
const fileContents = await githubClient.getFileContents(prHeadRef, testFilePath);
|
|
1825
|
+
existingTestFile = fileContents.content;
|
|
1826
|
+
debug(`Found existing test file at ${testFilePath}`);
|
|
1827
|
+
} catch (err) {
|
|
1828
|
+
debug(`Could not fetch existing test file ${testFilePath}: ${err instanceof Error ? err.message : String(err)}`);
|
|
1829
|
+
}
|
|
1830
|
+
} else {
|
|
1831
|
+
debug(`No existing test file at ${testFilePath}, will create new file`);
|
|
1832
|
+
}
|
|
1833
|
+
const result = await testGenerator.generateTest({
|
|
1834
|
+
target: {
|
|
1835
|
+
filePath: target.filePath,
|
|
1836
|
+
functionName: target.functionName,
|
|
1837
|
+
functionType: target.functionType,
|
|
1838
|
+
code: target.code,
|
|
1839
|
+
context: target.context
|
|
1840
|
+
},
|
|
1841
|
+
framework,
|
|
1842
|
+
existingTestFile
|
|
1843
|
+
});
|
|
1844
|
+
if (!testFiles.has(testFilePath)) {
|
|
1845
|
+
const baseContent = existingTestFile || "";
|
|
1846
|
+
testFiles.set(testFilePath, { content: baseContent, targets: [] });
|
|
1847
|
+
}
|
|
1848
|
+
const fileData = testFiles.get(testFilePath);
|
|
1849
|
+
if (fileData.content) {
|
|
1850
|
+
fileData.content += "\n\n" + result.testCode;
|
|
1851
|
+
} else {
|
|
1852
|
+
fileData.content = result.testCode;
|
|
1853
|
+
}
|
|
1854
|
+
fileData.targets.push(target.functionName);
|
|
1855
|
+
testsGenerated++;
|
|
1856
|
+
info(`\u2713 Generated test for ${target.functionName}`);
|
|
1857
|
+
} catch (err) {
|
|
1858
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
1859
|
+
error(`\u2717 Failed to generate test for ${target.functionName}: ${errorMessage}`);
|
|
1860
|
+
errors.push({
|
|
1861
|
+
target: `${target.filePath}:${target.functionName}`,
|
|
1862
|
+
error: errorMessage
|
|
1863
|
+
});
|
|
1864
|
+
testsFailed++;
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
const projectRoot = await findProjectRoot();
|
|
1868
|
+
const packageManager = detectPackageManager(projectRoot);
|
|
1869
|
+
info(`Detected package manager: ${packageManager}`);
|
|
1870
|
+
if (testFiles.size > 0) {
|
|
1871
|
+
const writtenPaths = writeTestFiles(testFiles, projectRoot);
|
|
1872
|
+
info(`Wrote ${writtenPaths.length} test file(s) to disk`);
|
|
1873
|
+
const testRunner = createTestRunner(framework);
|
|
1874
|
+
const finalTestFiles = await runTestsAndFix(
|
|
1875
|
+
testRunner,
|
|
1876
|
+
testFiles,
|
|
1877
|
+
writtenPaths,
|
|
1878
|
+
framework,
|
|
1879
|
+
packageManager,
|
|
1880
|
+
projectRoot,
|
|
1881
|
+
testGenerator,
|
|
1882
|
+
config.maxFixAttempts
|
|
1883
|
+
);
|
|
1884
|
+
testFiles = finalTestFiles;
|
|
1885
|
+
}
|
|
1886
|
+
const summary = {
|
|
1887
|
+
targetsProcessed: limitedTargets.length,
|
|
1888
|
+
testsGenerated,
|
|
1889
|
+
testsFailed,
|
|
1890
|
+
testFiles: Array.from(testFiles.entries()).map(([path, data]) => ({
|
|
1891
|
+
path,
|
|
1892
|
+
targets: data.targets
|
|
1893
|
+
})),
|
|
1894
|
+
errors
|
|
1895
|
+
};
|
|
1896
|
+
if (testFiles.size > 0) {
|
|
1897
|
+
const testRunner = createTestRunner(framework);
|
|
1898
|
+
const writtenPaths = Array.from(testFiles.keys());
|
|
1899
|
+
info("Running tests with coverage...");
|
|
1900
|
+
const finalTestResults = await testRunner.runTests({
|
|
1901
|
+
testFiles: writtenPaths,
|
|
1902
|
+
framework,
|
|
1903
|
+
packageManager,
|
|
1904
|
+
projectRoot,
|
|
1905
|
+
coverage: true
|
|
1906
|
+
});
|
|
1907
|
+
const coverageReport = readCoverageReport(projectRoot, framework);
|
|
1908
|
+
if (coverageReport) {
|
|
1909
|
+
info(`Coverage collected: ${coverageReport.total.lines.percentage.toFixed(1)}% lines`);
|
|
1910
|
+
summary.coverageReport = coverageReport;
|
|
1911
|
+
summary.testResults = finalTestResults;
|
|
1912
|
+
} else {
|
|
1913
|
+
warn("Could not read coverage report");
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
if (config.enableAutoCommit && testFiles.size > 0) {
|
|
1917
|
+
await commitTests(
|
|
1918
|
+
githubClient,
|
|
1919
|
+
pr,
|
|
1920
|
+
Array.from(testFiles.entries()).map(([path, data]) => ({
|
|
1921
|
+
path,
|
|
1922
|
+
content: data.content
|
|
1923
|
+
})),
|
|
1924
|
+
config,
|
|
1925
|
+
summary
|
|
1926
|
+
);
|
|
1927
|
+
}
|
|
1928
|
+
if (config.enablePRComments) {
|
|
1929
|
+
await postPRComment(githubClient, context.prNumber, summary, framework, testGenerator);
|
|
1930
|
+
}
|
|
1931
|
+
success(`Completed: ${testsGenerated} test(s) generated, ${testsFailed} failed`);
|
|
1932
|
+
return summary;
|
|
1933
|
+
}
|
|
1934
|
+
async function runTestsAndFix(testRunner, testFiles, testFilePaths, framework, packageManager, projectRoot, testGenerator, maxFixAttempts) {
|
|
1935
|
+
const currentTestFiles = new Map(testFiles);
|
|
1936
|
+
let attempt = 0;
|
|
1937
|
+
while (attempt < maxFixAttempts) {
|
|
1938
|
+
info(`Running tests (attempt ${attempt + 1}/${maxFixAttempts})`);
|
|
1939
|
+
const results = await testRunner.runTests({
|
|
1940
|
+
testFiles: testFilePaths,
|
|
1941
|
+
framework,
|
|
1942
|
+
packageManager,
|
|
1943
|
+
projectRoot,
|
|
1944
|
+
coverage: false
|
|
1945
|
+
});
|
|
1946
|
+
const allPassed = results.every((r) => r.success);
|
|
1947
|
+
if (allPassed) {
|
|
1948
|
+
success(`All tests passed on attempt ${attempt + 1}`);
|
|
1949
|
+
return currentTestFiles;
|
|
1950
|
+
}
|
|
1951
|
+
const failures = [];
|
|
1952
|
+
for (const result of results) {
|
|
1953
|
+
if (!result.success && result.failures.length > 0) {
|
|
1954
|
+
failures.push({ testFile: result.testFile, result });
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
info(`Found ${failures.length} failing test file(s), attempting fixes...`);
|
|
1958
|
+
let fixedAny = false;
|
|
1959
|
+
for (const { testFile, result } of failures) {
|
|
1960
|
+
const testFileContent = currentTestFiles.get(testFile)?.content;
|
|
1961
|
+
if (!testFileContent) {
|
|
1962
|
+
warn(`Could not find content for test file: ${testFile}`);
|
|
1963
|
+
continue;
|
|
1964
|
+
}
|
|
1965
|
+
const firstFailure = result.failures[0];
|
|
1966
|
+
if (!firstFailure)
|
|
1967
|
+
continue;
|
|
1968
|
+
try {
|
|
1969
|
+
const fixedResult = await testGenerator.fixTest({
|
|
1970
|
+
testCode: testFileContent,
|
|
1971
|
+
errorMessage: firstFailure.message,
|
|
1972
|
+
testOutput: firstFailure.stack,
|
|
1973
|
+
originalCode: "",
|
|
1974
|
+
// We'd need to pass this from the target
|
|
1975
|
+
framework,
|
|
1976
|
+
attempt: attempt + 1,
|
|
1977
|
+
maxAttempts: maxFixAttempts
|
|
1978
|
+
});
|
|
1979
|
+
currentTestFiles.set(testFile, {
|
|
1980
|
+
content: fixedResult.testCode,
|
|
1981
|
+
targets: currentTestFiles.get(testFile)?.targets || []
|
|
1982
|
+
});
|
|
1983
|
+
const { writeFileSync: writeFileSync2 } = await import("fs");
|
|
1984
|
+
const { join: join4 } = await import("path");
|
|
1985
|
+
writeFileSync2(join4(projectRoot, testFile), fixedResult.testCode, "utf-8");
|
|
1986
|
+
fixedAny = true;
|
|
1987
|
+
info(`\u2713 Fixed test file: ${testFile}`);
|
|
1988
|
+
} catch (err) {
|
|
1989
|
+
error(`Failed to fix test file ${testFile}: ${err instanceof Error ? err.message : String(err)}`);
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
if (!fixedAny) {
|
|
1993
|
+
warn(`Could not fix any failing tests on attempt ${attempt + 1}`);
|
|
1994
|
+
break;
|
|
1995
|
+
}
|
|
1996
|
+
attempt++;
|
|
1997
|
+
}
|
|
1998
|
+
if (attempt >= maxFixAttempts) {
|
|
1999
|
+
warn(`Reached maximum fix attempts (${maxFixAttempts}), some tests may still be failing`);
|
|
2000
|
+
}
|
|
2001
|
+
return currentTestFiles;
|
|
2002
|
+
}
|
|
2003
|
+
async function commitTests(githubClient, pr, testFiles, config, summary) {
|
|
2004
|
+
info(`Committing ${testFiles.length} test file(s)`);
|
|
2005
|
+
try {
|
|
2006
|
+
if (config.commitStrategy === "branch-pr") {
|
|
2007
|
+
const branchName = `kakarot-ci/tests-pr-${pr.number}`;
|
|
2008
|
+
const baseSha = await githubClient.createBranch(branchName, pr.head.ref);
|
|
2009
|
+
await githubClient.commitFiles({
|
|
2010
|
+
files: testFiles.map((file) => ({
|
|
2011
|
+
path: file.path,
|
|
2012
|
+
content: file.content
|
|
2013
|
+
})),
|
|
2014
|
+
message: `test: add unit tests for PR #${pr.number}
|
|
2015
|
+
|
|
2016
|
+
Generated ${summary.testsGenerated} test(s) for ${summary.targetsProcessed} function(s)`,
|
|
2017
|
+
branch: branchName,
|
|
2018
|
+
baseSha
|
|
2019
|
+
});
|
|
2020
|
+
const testPR = await githubClient.createPullRequest(
|
|
2021
|
+
`test: Add unit tests for PR #${pr.number}`,
|
|
2022
|
+
`This PR contains automatically generated unit tests for PR #${pr.number}.
|
|
2023
|
+
|
|
2024
|
+
- ${summary.testsGenerated} test(s) generated
|
|
2025
|
+
- ${summary.targetsProcessed} function(s) tested
|
|
2026
|
+
- ${testFiles.length} test file(s) created/updated`,
|
|
2027
|
+
branchName,
|
|
2028
|
+
pr.head.ref
|
|
2029
|
+
);
|
|
2030
|
+
success(`Created PR #${testPR.number} with generated tests`);
|
|
2031
|
+
} else {
|
|
2032
|
+
await githubClient.commitFiles({
|
|
2033
|
+
files: testFiles.map((file) => ({
|
|
2034
|
+
path: file.path,
|
|
2035
|
+
content: file.content
|
|
2036
|
+
})),
|
|
2037
|
+
message: `test: add unit tests
|
|
2038
|
+
|
|
2039
|
+
Generated ${summary.testsGenerated} test(s) for ${summary.targetsProcessed} function(s)`,
|
|
2040
|
+
branch: pr.head.ref,
|
|
2041
|
+
baseSha: pr.head.sha
|
|
2042
|
+
});
|
|
2043
|
+
success(`Committed ${testFiles.length} test file(s) to ${pr.head.ref}`);
|
|
2044
|
+
}
|
|
2045
|
+
} catch (err) {
|
|
2046
|
+
error(`Failed to commit tests: ${err instanceof Error ? err.message : String(err)}`);
|
|
2047
|
+
throw err;
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
async function postPRComment(githubClient, prNumber, summary, framework, testGenerator) {
|
|
2051
|
+
let comment = `## \u{1F9EA} Kakarot CI Test Generation Summary
|
|
2052
|
+
|
|
2053
|
+
**Framework:** ${framework}
|
|
2054
|
+
**Targets Processed:** ${summary.targetsProcessed}
|
|
2055
|
+
**Tests Generated:** ${summary.testsGenerated}
|
|
2056
|
+
**Failures:** ${summary.testsFailed}
|
|
2057
|
+
|
|
2058
|
+
### Test Files
|
|
2059
|
+
${summary.testFiles.length > 0 ? summary.testFiles.map((f) => `- \`${f.path}\` (${f.targets.length} test(s))`).join("\n") : "No test files generated"}
|
|
2060
|
+
|
|
2061
|
+
${summary.errors.length > 0 ? `### Errors
|
|
2062
|
+
${summary.errors.map((e) => `- \`${e.target}\`: ${e.error}`).join("\n")}` : ""}`;
|
|
2063
|
+
if (summary.coverageReport && summary.testResults) {
|
|
2064
|
+
try {
|
|
2065
|
+
const functionsTested = summary.testFiles.flatMap((f) => f.targets);
|
|
2066
|
+
const messages = buildCoverageSummaryPrompt(
|
|
2067
|
+
summary.coverageReport,
|
|
2068
|
+
summary.testResults,
|
|
2069
|
+
functionsTested
|
|
2070
|
+
);
|
|
2071
|
+
const coverageSummary = await testGenerator.generateCoverageSummary(messages);
|
|
2072
|
+
comment += `
|
|
2073
|
+
|
|
2074
|
+
## \u{1F4CA} Coverage Summary
|
|
2075
|
+
|
|
2076
|
+
${coverageSummary}`;
|
|
2077
|
+
} catch (err) {
|
|
2078
|
+
warn(`Failed to generate coverage summary: ${err instanceof Error ? err.message : String(err)}`);
|
|
2079
|
+
const cov = summary.coverageReport.total;
|
|
2080
|
+
comment += `
|
|
2081
|
+
|
|
2082
|
+
## \u{1F4CA} Coverage Summary
|
|
2083
|
+
|
|
2084
|
+
- **Lines:** ${cov.lines.percentage.toFixed(1)}% (${cov.lines.covered}/${cov.lines.total})
|
|
2085
|
+
- **Branches:** ${cov.branches.percentage.toFixed(1)}% (${cov.branches.covered}/${cov.branches.total})
|
|
2086
|
+
- **Functions:** ${cov.functions.percentage.toFixed(1)}% (${cov.functions.covered}/${cov.functions.total})
|
|
2087
|
+
- **Statements:** ${cov.statements.percentage.toFixed(1)}% (${cov.statements.covered}/${cov.statements.total})`;
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
comment += `
|
|
2091
|
+
|
|
2092
|
+
---
|
|
2093
|
+
*Generated by [Kakarot CI](https://github.com/kakarot-ci)*`;
|
|
2094
|
+
try {
|
|
2095
|
+
await githubClient.commentPR(prNumber, comment);
|
|
2096
|
+
info("Posted PR comment with test generation summary");
|
|
2097
|
+
} catch (err) {
|
|
2098
|
+
warn(`Failed to post PR comment: ${err instanceof Error ? err.message : String(err)}`);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
1360
2101
|
export {
|
|
1361
2102
|
GitHubClient,
|
|
2103
|
+
JestRunner,
|
|
1362
2104
|
KakarotConfigSchema,
|
|
1363
2105
|
TestGenerator,
|
|
2106
|
+
VitestRunner,
|
|
1364
2107
|
analyzeFile,
|
|
2108
|
+
buildCoverageSummaryPrompt,
|
|
1365
2109
|
buildTestFixPrompt,
|
|
1366
2110
|
buildTestGenerationPrompt,
|
|
1367
2111
|
createLLMProvider,
|
|
2112
|
+
createTestRunner,
|
|
1368
2113
|
debug,
|
|
2114
|
+
detectPackageManager,
|
|
1369
2115
|
error,
|
|
1370
2116
|
extractTestTargets,
|
|
2117
|
+
findProjectRoot,
|
|
1371
2118
|
getChangedRanges,
|
|
2119
|
+
getTestFilePath,
|
|
1372
2120
|
info,
|
|
1373
2121
|
initLogger,
|
|
1374
2122
|
loadConfig,
|
|
1375
2123
|
parsePullRequestFiles,
|
|
1376
2124
|
parseTestCode,
|
|
1377
2125
|
progress,
|
|
2126
|
+
readCoverageReport,
|
|
2127
|
+
runPullRequest,
|
|
1378
2128
|
success,
|
|
1379
2129
|
validateTestCodeStructure,
|
|
1380
|
-
warn
|
|
2130
|
+
warn,
|
|
2131
|
+
writeTestFiles
|
|
1381
2132
|
};
|
|
1382
2133
|
//# sourceMappingURL=index.js.map
|