@salesforce/apex-node
Version:
Salesforce JS library for Apex
118 lines • 4.83 kB
JavaScript
;
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSeverityScore = exports.getSummaryInfo = exports.formatTimestamp = exports.getTestNameInfo = exports.getCoveragePercentage = exports.hasPoorCoverage = exports.isPoorlyPerforming = exports.formatDuration = exports.escapeMarkdown = void 0;
/** Escapes markdown special characters */
const escapeMarkdown = (text) => text.replaceAll(/[\\`*_{}[\]()#+\-!]/g, '\\$&');
exports.escapeMarkdown = escapeMarkdown;
/** Formats duration in milliseconds to a human-readable string */
const formatDuration = (ms) => {
if (ms < 1000) {
return `${ms}ms`;
}
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
if (minutes > 0) {
return `${minutes}m ${seconds % 60}s`;
}
return `${seconds}s`;
};
exports.formatDuration = formatDuration;
/** Checks if a test is poorly performing (takes too long) */
const isPoorlyPerforming = (runTime, thresholdMs) => runTime !== undefined && runTime > thresholdMs;
exports.isPoorlyPerforming = isPoorlyPerforming;
/** Checks if a test has poor coverage */
const hasPoorCoverage = (coverage, thresholdPercent) => {
if (coverage === undefined || coverage === 'N/A') {
return false;
}
if (typeof coverage === 'string') {
const numericValue = parseFloat(coverage.replace('%', ''));
return !isNaN(numericValue) && numericValue < thresholdPercent;
}
return coverage < thresholdPercent;
};
exports.hasPoorCoverage = hasPoorCoverage;
/** Extracts numeric coverage percentage from coverage value */
const getCoveragePercentage = (coverage) => {
if (coverage === undefined || coverage === 'N/A') {
return null;
}
if (typeof coverage === 'string') {
const numericValue = parseFloat(coverage.replace('%', ''));
return isNaN(numericValue) ? null : numericValue;
}
return coverage;
};
exports.getCoveragePercentage = getCoveragePercentage;
/** Extracts test name information from a test */
const getTestNameInfo = (test) => {
const className = test.apexClass?.name ?? 'Unknown';
const namespacePrefix = test.apexClass?.namespacePrefix;
const fullClassName = namespacePrefix
? `${namespacePrefix}.${className}`
: className;
const methodName = test.methodName ?? 'Unknown';
const testName = `${fullClassName}.${methodName}`;
return { className, namespacePrefix, fullClassName, methodName, testName };
};
exports.getTestNameInfo = getTestNameInfo;
/** Formats timestamp to a string */
const formatTimestamp = (timestamp) => timestamp.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
exports.formatTimestamp = formatTimestamp;
/** Extracts summary information from test result */
const getSummaryInfo = (summary) => {
const passed = summary?.passing ?? 0;
const failed = summary?.failing ?? 0;
const skipped = summary?.skipped ?? 0;
const total = summary?.testsRan ?? 0;
const duration = summary?.outcome === 'Passed' || summary?.outcome === 'Failed'
? (summary?.testExecutionTimeInMs ?? 0)
: 0;
return { passed, failed, skipped, total, duration };
};
exports.getSummaryInfo = getSummaryInfo;
/** Calculates a severity score for sorting (higher = worse) */
const getSeverityScore = (test, codeCoverage, performanceThresholdMs, coverageThresholdPercent) => {
let score = 0;
// Both issues = highest priority (score 10000+)
const isSlow = (0, exports.isPoorlyPerforming)(test.runTime, performanceThresholdMs);
const hasLowCoverage = codeCoverage &&
(0, exports.hasPoorCoverage)(test.perClassCoverage?.[0]?.percentage, coverageThresholdPercent);
if (isSlow && hasLowCoverage) {
score += 10_000;
}
else if (isSlow) {
score += 5000;
}
else if (hasLowCoverage) {
score += 5000;
}
// Add runtime (longer = worse, but only if it's a problem)
if (test.runTime !== undefined) {
score += test.runTime;
}
// Subtract coverage (lower = worse, but only if it's a problem)
if (codeCoverage && hasLowCoverage) {
const coverage = (0, exports.getCoveragePercentage)(test.perClassCoverage?.[0]?.percentage);
if (coverage !== null) {
score += (100 - coverage) * 100; // Lower coverage = higher score
}
}
return score;
};
exports.getSeverityScore = getSeverityScore;
//# sourceMappingURL=markdownTextReporter.js.map