/**
* Test utilities for DeepSource tests
*/
import { DeepSourceClient, MetricShortcode, ReportType } from '../../deepsource.js';
import { MetricDirection, MetricKey } from '../../types/metrics.js';
import { SecurityClient } from '../../client/security-client.js';
import { MetricsClient } from '../../client/metrics-client.js';
import { RunsClient } from '../../client/runs-client.js';
import { IssuesClient } from '../../client/issues-client.js';
/**
* TestableDeepSourceClient extends DeepSourceClient to expose private methods for testing.
* This class provides test-only methods to access private static and instance methods
* in the DeepSourceClient class.
*/
export class TestableDeepSourceClient extends DeepSourceClient {
/**
* Test method for handleTestEnvironment private static method
*/
static async testHandleTestEnvironment(params: {
projectKey: string;
metricShortcode: MetricShortcode;
metricKey: MetricKey;
}): Promise<
| {
shortcode: string;
metricKey: string;
name: string;
unit: string;
positiveDirection: string;
threshold?: number;
isTrendingPositive?: boolean;
values: Array<{
value: number;
valueDisplay: string;
threshold?: number;
thresholdStatus?: string;
commitOid?: string;
createdAt?: string;
}>;
}
| null
| undefined
> {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient['handleTestEnvironment'](params);
}
/**
* Test method for validateAndGetMetricInfo private instance method
*/
async testValidateAndGetMetricInfo(params: {
projectKey: string;
metricShortcode: MetricShortcode;
metricKey: MetricKey;
}) {
// @ts-expect-error Accessing private method for testing
return this.validateAndGetMetricInfo(params);
}
/**
* Test method for processRegularMetricHistory private instance method
*/
async testProcessRegularMetricHistory(params: {
projectKey: string;
metricShortcode: MetricShortcode;
metricKey: MetricKey;
limit?: number;
}) {
// @ts-expect-error Accessing private method for testing
return this.processRegularMetricHistory(params);
}
/**
* Test method for fetchHistoricalValues private instance method
*/
async testFetchHistoricalValues(
params: {
projectKey: string;
metricShortcode: MetricShortcode;
metricKey: MetricKey;
limit?: number;
},
project: { name: string; repository: { login: string; provider: string } },
metricItem: { id: string; key: string; threshold?: number }
) {
// @ts-expect-error Accessing private method for testing
return this.fetchHistoricalValues(params, project, metricItem);
}
/**
* Test method for createMetricHistoryResponse private static method
*/
static testCreateMetricHistoryResponse(
params: { projectKey: string; metricShortcode: MetricShortcode; metricKey: MetricKey },
metric: { name: string; shortcode: string; positiveDirection: string; unit: string },
metricItem: { id: string; key: string; threshold?: number },
historyValues: Array<{
value: number;
valueDisplay: string;
threshold?: number;
thresholdStatus?: string;
commitOid?: string;
createdAt?: string;
}>
) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.createMetricHistoryResponse(params, metric, metricItem, historyValues);
}
/**
* Test method for calculateTrendDirection private static method
*/
static testCalculateTrendDirection(
values: Array<{
value: number;
valueDisplay: string;
threshold?: number;
thresholdStatus?: string;
commitOid?: string;
createdAt?: string;
}>,
positiveDirection: string | MetricDirection
) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.calculateTrendDirection(values, positiveDirection);
}
/**
* Test method for isError private static method
*/
static testIsError(error: unknown): boolean {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.isError(error);
}
/**
* Test method for isErrorWithMessage private static method
*/
static testIsErrorWithMessage(error: unknown, substring: string): boolean {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.isErrorWithMessage(error, substring);
}
/**
* Test method for validateProjectRepository private static method
*/
static testValidateProjectRepository(project: Record<string, unknown>, projectKey: string): void {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.validateProjectRepository(project, projectKey);
}
/**
* Test method for extractReportData private static method
*/
static testExtractReportData(response: Record<string, unknown>, reportType: ReportType) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.extractReportData(response, reportType);
}
/**
* Test method for handling NoneType errors
*/
static async testNoneTypeErrorHandler(): Promise<unknown[]> {
try {
// Force an error that contains 'NoneType'
throw new Error('NoneType object has no attribute get');
} catch (error) {
// This is the exact code from getQualityMetrics (lines 2448-2456)
if (TestableDeepSourceClient.testIsError(error)) {
if (TestableDeepSourceClient.testIsErrorWithMessage(error, 'NoneType')) {
// This is line 2452 that we want to cover
return [];
}
}
throw error;
}
}
/**
* Test method for getQualityMetricsWithNoneTypeError
*/
static async testGetQualityMetricsWithNoneTypeError(): Promise<unknown[]> {
try {
// Force an error
throw new Error('NoneType object has no attribute get');
} catch (error) {
// This is the exact code from getQualityMetrics catch block (lines 2448-2456)
// Handle errors
if (TestableDeepSourceClient.testIsError(error)) {
if (TestableDeepSourceClient.testIsErrorWithMessage(error, 'NoneType')) {
return [];
}
}
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.handleGraphQLError(error);
}
}
/**
* Test method for processVulnerabilityEdge private static method
*/
static testProcessVulnerabilityEdge(edge: unknown) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.processVulnerabilityEdge(edge);
}
/**
* Test method for isValidVulnerabilityNode private static method
*/
static testIsValidVulnerabilityNode(node: unknown) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.isValidVulnerabilityNode(node);
}
/**
* Test method for mapVulnerabilityOccurrence private static method
*/
static testMapVulnerabilityOccurrence(node: Record<string, unknown>) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.mapVulnerabilityOccurrence(node);
}
/**
* Test method for iterateVulnerabilities private static method
*/
static testIterateVulnerabilities(edges: unknown[]) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.iterateVulnerabilities(edges);
}
/**
* Test method for processVulnerabilityResponse private static method
*/
static testProcessVulnerabilityResponse(response: unknown) {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.processVulnerabilityResponse(response);
}
/**
* Test method for getReportField private static method
*/
static testGetReportField(reportType: string): string {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.getReportField(reportType);
}
/**
* Test method for isAxiosErrorWithCriteria private static method
*/
static testIsAxiosErrorWithCriteria(
error: unknown,
statusCode?: number,
errorCode?: string
): boolean {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.isAxiosErrorWithCriteria(error, statusCode, errorCode);
}
/**
* Test method for handleNetworkError private static method
*/
static testHandleNetworkError(error: unknown): never | false {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.handleNetworkError(error);
}
/**
* Test method for handleHttpStatusError private static method
*/
static testHandleHttpStatusError(error: unknown): never | false {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.handleHttpStatusError(error);
}
/**
* Test method for handleGraphQLError private static method
*/
static testHandleGraphQLError(error: unknown): never {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.handleGraphQLError(error);
}
/**
* Test method for handleGraphQLSpecificError private static method
*/
static testHandleGraphQLSpecificError(error: unknown): never | false {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.handleGraphQLSpecificError(error);
}
/**
* Test method for validateNumber private static method
*/
static testValidateNumber(value: unknown): number | null {
// @ts-expect-error Accessing private method for testing
return DeepSourceClient.validateNumber(value);
}
}
/**
* TestableSecurityClient extends SecurityClient to expose private methods for testing.
*/
export class TestableSecurityClient extends SecurityClient {
/**
* Test method for buildComplianceReportQuery private static method
*/
static testBuildComplianceReportQuery(): string {
// @ts-expect-error Accessing private method for testing
return SecurityClient.buildComplianceReportQuery();
}
/**
* Test method for buildVulnerabilitiesQuery private static method
*/
static testBuildVulnerabilitiesQuery(): string {
// @ts-expect-error Accessing private method for testing
return SecurityClient.buildVulnerabilitiesQuery();
}
}
/**
* TestableMetricsClient extends MetricsClient to expose private methods for testing.
*/
export class TestableMetricsClient extends MetricsClient {
/**
* Test method for buildQualityMetricsQuery private static method
*/
static testBuildQualityMetricsQuery(): string {
// @ts-expect-error Accessing private method for testing
return MetricsClient.buildQualityMetricsQuery();
}
/**
* Test method for buildUpdateThresholdMutation private static method
*/
static testBuildUpdateThresholdMutation(): string {
// @ts-expect-error Accessing private method for testing
return MetricsClient.buildUpdateThresholdMutation();
}
/**
* Test method for buildUpdateSettingMutation private static method
*/
static testBuildUpdateSettingMutation(): string {
// @ts-expect-error Accessing private method for testing
return MetricsClient.buildUpdateSettingMutation();
}
/**
* Test method for buildMetricHistoryQuery private static method
*/
static testBuildMetricHistoryQuery(): string {
// @ts-expect-error Accessing private method for testing
return MetricsClient.buildMetricHistoryQuery();
}
/**
* Test method for calculateTrend private static method
*/
static testCalculateTrend(values: unknown[]): unknown {
// @ts-expect-error Accessing private method for testing
return MetricsClient.calculateTrend(values);
}
/**
* Test method for handleTestEnvironment private static method
*/
static testHandleTestEnvironment(params: unknown): unknown {
// @ts-expect-error Accessing private method for testing
return MetricsClient.handleTestEnvironment(params);
}
}
/**
* TestableRunsClient extends RunsClient to expose private methods for testing.
*/
export class TestableRunsClient extends RunsClient {
/**
* Test method for buildRunsQuery private static method
*/
static testBuildRunsQuery(): string {
// @ts-expect-error Accessing private method for testing
return RunsClient.buildRunsQuery();
}
/**
* Test method for buildRunByUidQuery private static method
*/
static testBuildRunByUidQuery(): string {
// @ts-expect-error Accessing private method for testing
return RunsClient.buildRunByUidQuery();
}
/**
* Test method for buildRunByCommitQuery private static method
*/
static testBuildRunByCommitQuery(): string {
// @ts-expect-error Accessing private method for testing
return RunsClient.buildRunByCommitQuery();
}
/**
* Test method for mapRunNode private static method
*/
static testMapRunNode(node: unknown): unknown {
// @ts-expect-error Accessing private method for testing
return RunsClient.mapRunNode(node);
}
}
/**
* TestableIssuesClient extends IssuesClient to expose private methods for testing.
*/
export class TestableIssuesClient extends IssuesClient {
/**
* Test method for buildIssuesQuery private static method
*/
static testBuildIssuesQuery(): string {
// @ts-expect-error Accessing private method for testing
return IssuesClient.buildIssuesQuery();
}
}