Files
repomix-mirror/tests/core/file/fileProcessContent.test.ts
Kazuki Yamada 3e70628307 refactor(core): Separate lightweight transforms from worker processing
Extract lightweight file transforms (truncateBase64, removeEmptyLines,
trim, showLineNumbers) into applyLightweightTransforms() on the main
thread, keeping only heavy operations (removeComments, compress) in
worker processContent(). This eliminates dual management of the same
logic across worker and main thread paths.

Also pre-compile base64 regex patterns at module level to avoid
re-creation per file call.

Action: split processContent into heavy (worker) and lightweight (main thread) phases
Action: extract applyLightweightTransforms() as single source of truth for lightweight ops
Action: hoist regex patterns in truncateBase64.ts to module scope with lastIndex reset
Why: lightweight transforms were duplicated in both processFilesMainThread and processContent
Why: regex re-compilation per file added unnecessary overhead for large repos

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 17:24:32 +09:00

141 lines
4.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { RepomixConfigMerged } from '../../../src/config/configSchema.js';
import { getFileManipulator } from '../../../src/core/file/fileManipulate.js';
import { processContent } from '../../../src/core/file/fileProcessContent.js';
import type { RawFile } from '../../../src/core/file/fileTypes.js';
import { parseFile } from '../../../src/core/treeSitter/parseFile.js';
vi.mock('../../../src/core/file/fileManipulate.js');
vi.mock('../../../src/core/treeSitter/parseFile.js');
vi.mock('../../../src/shared/logger.js');
describe('processContent', () => {
const mockManipulator = {
removeComments: vi.fn((content) => content.replace(/\/\/.*/g, '')),
removeEmptyLines: vi.fn((content) => content.split('\n').filter(Boolean).join('\n')),
};
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(getFileManipulator).mockReturnValue(mockManipulator);
vi.mocked(parseFile).mockResolvedValue('parsed content');
});
it('should process content with default config', async () => {
const rawFile: RawFile = {
path: 'test.ts',
content: 'const x = 1;\n\nconst y = 2;',
};
const config: RepomixConfigMerged = {
output: {
removeComments: false,
removeEmptyLines: false,
compress: false,
showLineNumbers: false,
},
} as RepomixConfigMerged;
const result = await processContent(rawFile, config);
expect(result).toBe('const x = 1;\n\nconst y = 2;');
expect(mockManipulator.removeComments).not.toHaveBeenCalled();
});
it('should remove comments when configured', async () => {
const rawFile: RawFile = {
path: 'test.ts',
content: 'const x = 1; // comment\nconst y = 2;',
};
const config: RepomixConfigMerged = {
output: {
removeComments: true,
removeEmptyLines: false,
compress: false,
showLineNumbers: false,
},
} as RepomixConfigMerged;
const result = await processContent(rawFile, config);
expect(mockManipulator.removeComments).toHaveBeenCalledWith(rawFile.content);
expect(result).toBe('const x = 1; \nconst y = 2;');
});
it('should compress content using Tree-sitter when configured', async () => {
const rawFile: RawFile = {
path: 'test.ts',
content: 'const x = 1;\nconst y = 2;',
};
const config: RepomixConfigMerged = {
output: {
removeComments: false,
removeEmptyLines: false,
compress: true,
showLineNumbers: false,
},
} as RepomixConfigMerged;
const result = await processContent(rawFile, config);
expect(parseFile).toHaveBeenCalledWith(rawFile.content, rawFile.path, config);
expect(result).toBe('parsed content');
});
it('should handle Tree-sitter parse failure gracefully', async () => {
const rawFile: RawFile = {
path: 'test.ts',
content: 'const x = 1;\nconst y = 2;',
};
const config: RepomixConfigMerged = {
output: {
removeComments: false,
removeEmptyLines: false,
compress: true,
showLineNumbers: false,
},
} as RepomixConfigMerged;
vi.mocked(parseFile).mockResolvedValue(undefined);
const result = await processContent(rawFile, config);
expect(result).toBe(rawFile.content);
});
it('should handle Tree-sitter parse error', async () => {
const rawFile: RawFile = {
path: 'test.ts',
content: 'const x = 1;\nconst y = 2;',
};
const config: RepomixConfigMerged = {
output: {
removeComments: false,
removeEmptyLines: false,
compress: true,
showLineNumbers: false,
},
} as RepomixConfigMerged;
const error = new Error('Parse error');
vi.mocked(parseFile).mockRejectedValue(error);
await expect(processContent(rawFile, config)).rejects.toThrow('Parse error');
});
it('should handle files without a manipulator', async () => {
const rawFile: RawFile = {
path: 'test.unknown',
content: 'some content',
};
const config: RepomixConfigMerged = {
output: {
removeComments: true,
removeEmptyLines: false,
compress: false,
showLineNumbers: false,
},
} as RepomixConfigMerged;
vi.mocked(getFileManipulator).mockReturnValue(null);
const result = await processContent(rawFile, config);
expect(result).toBe('some content');
});
});