ci4sManagement-cloud/react-ui/tests/parseK8sLog.test.ts

41 lines
1.6 KiB
TypeScript

import { parseK8sLog } from '../src/utils';
describe('test parseK8sLog()', () => {
// 测试 isLine 为 true 的情况
describe('when isLine is true', () => {
test('should remove timestamp and stdout prefix and add newline', () => {
const log = '2023-01-01T00:00:00.000+00:00 stdout F test log';
const result = parseK8sLog(log, true);
expect(result).toBe('test log\n');
});
test('should remove timestamp and stderr prefix and add newline', () => {
const log = '2023-01-01T00:00:00.000+00:00 stderr P error log';
const result = parseK8sLog(log, true);
expect(result).toBe('error log\n');
});
test('should return original log if no timestamp and prefix', () => {
const log = 'test log without prefix';
const result = parseK8sLog(log, true);
expect(result).toBe(log);
});
});
// 测试 isLine 为 false 的情况
describe('when isLine is false', () => {
test('should remove all timestamp and prefixes and add newlines before time brackets', () => {
const log =
'[2026-03-12 18:02:03] 2023-01-01T00:00:00.000+00:00 stdout F first line[2026-03-12 18:02:03] 2023-01-01T00:00:01.000+00:00 stderr P second line';
const result = parseK8sLog(log, false);
expect(result).toBe('[2026-03-12 18:02:03] first line\n[2026-03-12 18:02:03] second line');
});
test('should return original log if no timestamp and prefix', () => {
const log = '[2026-03-12 18:02:03] first line\n[2026-03-12 18:02:03] second line';
const result = parseK8sLog(log, false);
expect(result).toBe(log);
});
});
});