@tywalk/pcf-helper
Version:
Command line helper for building and publishing PCF controls to Dataverse.
121 lines (120 loc) • 6.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const performanceUtil_1 = require("../util/performanceUtil");
const color_logger_1 = __importDefault(require("@tywalk/color-logger"));
jest.mock('@tywalk/color-logger', () => ({
__esModule: true,
default: {
success: jest.fn(),
debug: jest.fn(),
error: jest.fn(),
log: jest.fn()
}
}));
describe('performanceUtil', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('formatMsToSec', () => {
it('should format milliseconds to seconds with provided format string', () => {
const result = (0, performanceUtil_1.formatMsToSec)('Task completed in %is', 5000);
expect(result).toBe('Task completed in 5.0s');
});
it('should handle decimal seconds', () => {
const result = (0, performanceUtil_1.formatMsToSec)('Duration: %is', 1500);
expect(result).toBe('Duration: 1.5s');
});
it('should handle very small durations', () => {
const result = (0, performanceUtil_1.formatMsToSec)('Time: %is', 100);
expect(result).toBe('Time: 0.1s');
});
it('should handle zero milliseconds', () => {
const result = (0, performanceUtil_1.formatMsToSec)('Time: %is', 0);
expect(result).toBe('Time: 0.0s');
});
it('should work with util.format multiple placeholders', () => {
const result = (0, performanceUtil_1.formatMsToSec)('%s took %is', 5000);
expect(result).toContain('5');
});
});
describe('formatTime', () => {
it('should format Date object to HH:MM:SS format', () => {
const date = new Date('2024-01-15T14:30:45Z');
const result = (0, performanceUtil_1.formatTime)(date);
// Result should match HH:MM:SS format (24-hour)
expect(result).toMatch(/\d{2}:\d{2}:\d{2}/);
});
it('should format current time', () => {
const now = new Date();
const result = (0, performanceUtil_1.formatTime)(now);
expect(result).toMatch(/\d{2}:\d{2}:\d{2}/);
});
it('should be consistent for same date', () => {
const date = new Date('2024-06-21T09:15:30Z');
const result1 = (0, performanceUtil_1.formatTime)(date);
const result2 = (0, performanceUtil_1.formatTime)(date);
expect(result1).toBe(result2);
});
});
describe('handleTaskCompletion', () => {
it('should log success when task status is 0', () => {
const task = { status: 0 };
(0, performanceUtil_1.handleTaskCompletion)(task, 'build', 1000, false);
expect(color_logger_1.default.success).toHaveBeenCalledWith(expect.stringContaining('build complete'));
});
it('should log error when task status is not 0', () => {
const task = { status: 1, signal: 'SIGTERM' };
(0, performanceUtil_1.handleTaskCompletion)(task, 'deploy', 5000, false);
expect(color_logger_1.default.error).toHaveBeenCalled();
});
it('should handle SIGTERM timeout case', () => {
const task = { status: 1, signal: 'SIGTERM', error: new Error('timeout') };
(0, performanceUtil_1.handleTaskCompletion)(task, 'import', 300000, false);
expect(color_logger_1.default.error).toHaveBeenCalledWith(expect.stringContaining('A timeout of 5 minutes was reached'), 'timeout');
});
it('should handle SIGKILL timeout case', () => {
const task = { status: 1, signal: 'SIGKILL', error: new Error('timeout') };
(0, performanceUtil_1.handleTaskCompletion)(task, 'import', 300000, false);
expect(color_logger_1.default.error).toHaveBeenCalledWith(expect.stringContaining('A timeout of 5 minutes was reached'), 'timeout');
});
it('should handle task error with message', () => {
const error = new Error('build failed');
const task = { status: 1, signal: 'SIGABRT', error };
(0, performanceUtil_1.handleTaskCompletion)(task, 'build', 1000, false);
expect(color_logger_1.default.error).toHaveBeenCalled();
});
it('should log debug info when verbose is true', () => {
const task = { status: 1, signal: 'SIGTERM', error: new Error('error') };
(0, performanceUtil_1.handleTaskCompletion)(task, 'upgrade', 2000, true);
expect(color_logger_1.default.debug).toHaveBeenCalled();
});
it('should format duration in debug output (success)', () => {
const task = { status: 0 };
(0, performanceUtil_1.handleTaskCompletion)(task, 'build', 3500, false);
expect(color_logger_1.default.debug).toHaveBeenCalledWith(expect.stringContaining('3.5s'));
});
it('should handle task without error property', () => {
const task = { status: 1 };
(0, performanceUtil_1.handleTaskCompletion)(task, 'init', 1000, false);
expect(color_logger_1.default.error).toHaveBeenCalledWith(expect.stringContaining('One or more errors'));
});
it('should return status code', () => {
const task = { status: 1 };
const result = (0, performanceUtil_1.handleTaskCompletion)(task, 'build', 1000, false);
expect(result).toBe(1);
});
it('should return 1 if task.status is null', () => {
const task = { status: null };
const result = (0, performanceUtil_1.handleTaskCompletion)(task, 'build', 1000, false);
expect(result).toBe(1);
});
it('should return success status when status is 0', () => {
const task = { status: 0 };
const result = (0, performanceUtil_1.handleTaskCompletion)(task, 'build', 1000, false);
expect(result).toBe(0);
});
});
});