Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 229 additions & 0 deletions packages/core/src/utils/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
retryWithBackoff,
isTransientCapacityError,
isUnattendedMode,
isRetryableNetworkError,
classifyError,
} from './retry.js';
import { getErrorStatus } from './errors.js';
import { setSimulate429 } from './testUtils.js';
Expand Down Expand Up @@ -1024,3 +1026,230 @@ describe('getErrorStatus', () => {
expect(getErrorStatus(new Error('HTTP_STATUS/4291'))).toBeUndefined();
});
});

describe('isRetryableNetworkError', () => {
it('should return true for ECONNRESET', () => {
const error = new Error('Connection reset');
(error as NodeJS.ErrnoException).code = 'ECONNRESET';
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for ETIMEDOUT', () => {
const error = new Error('Timed out');
(error as NodeJS.ErrnoException).code = 'ETIMEDOUT';
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for ESOCKETTIMEDOUT', () => {
const error = new Error('Socket timed out');
(error as NodeJS.ErrnoException).code = 'ESOCKETTIMEDOUT';
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for ECONNREFUSED', () => {
const error = new Error('Connection refused');
(error as NodeJS.ErrnoException).code = 'ECONNREFUSED';
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for ENOTFOUND', () => {
const error = new Error('Not found');
(error as NodeJS.ErrnoException).code = 'ENOTFOUND';
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for EHOSTUNREACH', () => {
const error = new Error('Host unreachable');
(error as NodeJS.ErrnoException).code = 'EHOSTUNREACH';
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for EAI_AGAIN', () => {
const error = new Error('Temporary failure');
(error as NodeJS.ErrnoException).code = 'EAI_AGAIN';
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for "socket closed" message', () => {
const error = new Error('The socket closed unexpectedly');
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for "stream ended" message', () => {
const error = new Error('The stream ended before completion');
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for "network error" message', () => {
const error = new Error('A network error occurred');
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for "connection reset" message', () => {
const error = new Error('connection reset by peer');
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for "econnreset" message (case-insensitive)', () => {
const error = new Error('ECONNRESET: econnreset');
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return true for "etimedout" message (case-insensitive)', () => {
const error = new Error('etimedout waiting for response');
expect(isRetryableNetworkError(error)).toBe(true);
});

it('should return false for non-retryable errors', () => {
const error = new Error('Bad request');
expect(isRetryableNetworkError(error)).toBe(false);
});

it('should return false for errors with non-retryable codes', () => {
const error = new Error('Permission denied');
(error as NodeJS.ErrnoException).code = 'EACCES';
expect(isRetryableNetworkError(error)).toBe(false);
});

it('should return false for null/undefined', () => {
expect(isRetryableNetworkError(null)).toBe(false);
expect(isRetryableNetworkError(undefined)).toBe(false);
});
});

describe('classifyError', () => {
it('should classify 400 as non-retryable', () => {
const error = { status: 400 };
const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Deterministic request error');
expect(result.status).toBe(400);
});

it('should classify 401 as non-retryable', () => {
const error = { status: 401 };
const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Deterministic request error');
});

it('should classify 403 as non-retryable', () => {
const error = { status: 403 };
const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Deterministic request error');
});

it('should classify 404 as non-retryable', () => {
const error = { status: 404 };
const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Deterministic request error');
});

it('should classify 422 as non-retryable', () => {
const error = { status: 422 };
const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Deterministic request error');
});

it('should classify 429 as retryable', () => {
const error = { status: 429 };
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('Rate limited');
expect(result.status).toBe(429);
});

it('should classify 408 as retryable', () => {
const error = { status: 408 };
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('Request timeout');
});

it('should classify 409 with transient message as retryable', () => {
const error: HttpError = new Error('Lock contention detected');
error.status = 409;
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('Transient conflict');
});

it('should classify 409 with conflict message as retryable', () => {
const error: HttpError = new Error('Conflict detected');
error.status = 409;
const result = classifyError(error);
expect(result.retryable).toBe(true);
});

it('should classify 409 with contention message as retryable', () => {
const error: HttpError = new Error('Resource contention');
error.status = 409;
const result = classifyError(error);
expect(result.retryable).toBe(true);
});

it('should classify 409 without transient message as non-retryable', () => {
const error = { status: 409, message: 'Version conflict' };

@wenshao wenshao May 3, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This test uses a plain object { status: 409, message: 'Version conflict' } instead of an Error instance. getErrorMessage returns "[object Object]" for plain objects, so isTransientConflict never sees the actual message — the test passes purely by coincidence. With a real Error instance, the "conflict" substring in "Version conflict" would match, causing the test to fail and exposing that the 'conflict' keyword in isTransientConflict is overly broad.

Suggested change
const error = { status: 409, message: 'Version conflict' };
const error: HttpError = Object.assign(new Error('Duplicate resource'), { status: 409 });
const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Deterministic conflict');

const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Deterministic conflict');
});

it('should classify 500 as retryable', () => {
const error = { status: 500 };
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('Server error');
});

it('should classify 503 as retryable', () => {
const error = { status: 503 };
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('Server error');
});

it('should classify 599 as retryable', () => {
const error = { status: 599 };
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('Server error');
});

it('should classify ECONNRESET as retryable network error', () => {
const error = new Error('Connection reset');
(error as NodeJS.ErrnoException).code = 'ECONNRESET';
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('network error');
});

it('should classify "socket closed" as retryable network error', () => {
const error = new Error('The socket closed unexpectedly');
const result = classifyError(error);
expect(result.retryable).toBe(true);
expect(result.reason).toContain('network error');
});

it('should classify unknown errors as non-retryable', () => {
const error = new Error('Something weird happened');
const result = classifyError(error);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Non-retryable');
});

it('should classify null as non-retryable', () => {
const result = classifyError(null);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Non-retryable');
});

it('should classify undefined as non-retryable', () => {
const result = classifyError(undefined);
expect(result.retryable).toBe(false);
expect(result.reason).toContain('Non-retryable');
});
});
Loading
Loading