mirror of
https://github.com/yamadashy/repomix.git
synced 2026-02-03 11:33:39 +01:00
Move split/single output generation and writing logic to packager/produceOutput.ts to keep packager.ts focused on the high-level orchestration flow. - Create produceOutput module handling both output modes - Simplify packager.ts from 227 to 181 lines - Update related tests to use new dependency structure
126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
import path from 'node:path';
|
|
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
|
import { pack } from '../../src/core/packager.js';
|
|
import { createMockConfig } from '../testing/testUtils.js';
|
|
|
|
vi.mock('node:fs/promises');
|
|
vi.mock('fs/promises');
|
|
vi.mock('../../src/core/metrics/TokenCounter.js', () => {
|
|
return {
|
|
TokenCounter: vi.fn().mockImplementation(() => ({
|
|
countTokens: vi.fn().mockReturnValue(10),
|
|
free: vi.fn(),
|
|
})),
|
|
};
|
|
});
|
|
|
|
describe('packager', () => {
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
test('pack should orchestrate packing files and generating output', async () => {
|
|
const file2Path = path.join('dir1', 'file2.txt');
|
|
const mockRawFiles = [
|
|
{ path: 'file1.txt', content: 'raw content 1' },
|
|
{ path: file2Path, content: 'raw content 2' },
|
|
];
|
|
const mockSafeRawFiles = [
|
|
{ path: 'file1.txt', content: 'safed content 1' },
|
|
{ path: file2Path, content: 'safed content 2' },
|
|
];
|
|
const mockProcessedFiles = [
|
|
{ path: 'file1.txt', content: 'processed content 1' },
|
|
{ path: file2Path, content: 'processed content 2' },
|
|
];
|
|
const mockOutput = 'mock output';
|
|
const mockFilePaths = ['file1.txt', file2Path];
|
|
|
|
const mockDeps = {
|
|
searchFiles: vi.fn().mockResolvedValue({
|
|
filePaths: mockFilePaths,
|
|
emptyDirPaths: [],
|
|
}),
|
|
sortPaths: vi.fn().mockImplementation((paths) => paths),
|
|
collectFiles: vi.fn().mockResolvedValue({ rawFiles: mockRawFiles, skippedFiles: [] }),
|
|
processFiles: vi.fn().mockReturnValue(mockProcessedFiles),
|
|
validateFileSafety: vi.fn().mockResolvedValue({
|
|
safeFilePaths: mockFilePaths,
|
|
safeRawFiles: mockSafeRawFiles,
|
|
suspiciousFilesResults: [],
|
|
suspiciousGitDiffResults: [],
|
|
suspiciousGitLogResults: [],
|
|
}),
|
|
produceOutput: vi.fn().mockResolvedValue({
|
|
outputForMetrics: mockOutput,
|
|
}),
|
|
calculateMetrics: vi.fn().mockResolvedValue({
|
|
totalFiles: 2,
|
|
totalCharacters: 11,
|
|
totalTokens: 10,
|
|
fileCharCounts: {
|
|
'file1.txt': 19,
|
|
[file2Path]: 19,
|
|
},
|
|
fileTokenCounts: {
|
|
'file1.txt': 10,
|
|
[file2Path]: 10,
|
|
},
|
|
gitDiffTokenCount: 0,
|
|
gitLogTokenCount: 0,
|
|
}),
|
|
};
|
|
|
|
const mockConfig = createMockConfig();
|
|
const progressCallback = vi.fn();
|
|
const result = await pack(['root'], mockConfig, progressCallback, mockDeps);
|
|
|
|
expect(mockDeps.searchFiles).toHaveBeenCalledWith('root', mockConfig, undefined);
|
|
expect(mockDeps.collectFiles).toHaveBeenCalledWith(mockFilePaths, 'root', mockConfig, progressCallback);
|
|
expect(mockDeps.validateFileSafety).toHaveBeenCalled();
|
|
expect(mockDeps.processFiles).toHaveBeenCalled();
|
|
expect(mockDeps.produceOutput).toHaveBeenCalled();
|
|
expect(mockDeps.calculateMetrics).toHaveBeenCalled();
|
|
|
|
expect(mockDeps.validateFileSafety).toHaveBeenCalledWith(
|
|
mockRawFiles,
|
|
progressCallback,
|
|
mockConfig,
|
|
undefined,
|
|
undefined,
|
|
);
|
|
expect(mockDeps.processFiles).toHaveBeenCalledWith(mockSafeRawFiles, mockConfig, progressCallback);
|
|
expect(mockDeps.produceOutput).toHaveBeenCalledWith(
|
|
['root'],
|
|
mockConfig,
|
|
mockProcessedFiles,
|
|
mockFilePaths,
|
|
undefined,
|
|
undefined,
|
|
progressCallback,
|
|
);
|
|
expect(mockDeps.calculateMetrics).toHaveBeenCalledWith(
|
|
mockProcessedFiles,
|
|
mockOutput,
|
|
progressCallback,
|
|
mockConfig,
|
|
undefined,
|
|
undefined,
|
|
);
|
|
|
|
// Check the result of pack function
|
|
expect(result.totalFiles).toBe(2);
|
|
expect(result.totalCharacters).toBe(11);
|
|
expect(result.totalTokens).toBe(10);
|
|
expect(result.fileCharCounts).toEqual({
|
|
'file1.txt': 19,
|
|
[file2Path]: 19,
|
|
});
|
|
expect(result.fileTokenCounts).toEqual({
|
|
'file1.txt': 10,
|
|
[file2Path]: 10,
|
|
});
|
|
expect(result.skippedFiles).toEqual([]);
|
|
});
|
|
});
|