@projectlibertylabs/p2p-peer-test
Version:
CLI tool to test libp2p connections and discover peer protocols
47 lines (41 loc) • 1.56 kB
JavaScript
import { describe, it, expect, vi } from 'vitest';
import {
showExamples,
shouldShowProgress,
formatProgressMessage,
formatRetryMessage
} from './cli-commands.js';
// Mock console.log for showExamples test
vi.spyOn(console, 'log').mockImplementation(() => {});
describe('showExamples', () => {
it('should return exit information', () => {
const result = showExamples();
expect(result).toEqual({
shouldExit: true,
exitCode: 0
});
expect(console.log).toHaveBeenCalled();
});
});
describe('shouldShowProgress', () => {
it('should show progress for non-JSON multi-attempt scenarios', () => {
expect(shouldShowProgress({ json: false }, 2, 3)).toBe(true);
expect(shouldShowProgress({ json: false }, 1, 3)).toBe(false); // first attempt
expect(shouldShowProgress({ json: true }, 2, 3)).toBe(false); // JSON mode
expect(shouldShowProgress({ json: false }, 2, 1)).toBe(false); // single attempt
});
});
describe('formatProgressMessage', () => {
it('should format progress message for retries', () => {
expect(formatProgressMessage(1, 2)).toBe(null); // first attempt
expect(formatProgressMessage(2, 2)).toBe('Retry attempt 1/2...');
expect(formatProgressMessage(3, 2)).toBe('Retry attempt 2/2...');
});
});
describe('formatRetryMessage', () => {
it('should format retry message appropriately', () => {
expect(formatRetryMessage(1, 3)).toBe('Attempt 1 failed, retrying...');
expect(formatRetryMessage(2, 3)).toBe('Attempt 2 failed, retrying...');
expect(formatRetryMessage(3, 3)).toBe(null); // final attempt
});
});