packager.test.tsā¢4.19 kB
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: [],
}),
generateOutput: vi.fn().mockResolvedValue(mockOutput),
writeOutputToDisk: vi.fn().mockResolvedValue(undefined),
copyToClipboardIfEnabled: vi.fn().mockResolvedValue(undefined),
calculateMetrics: vi.fn().mockResolvedValue({
totalFiles: 2,
totalCharacters: 11,
totalTokens: 10,
fileCharCounts: {
'file1.txt': 19,
[file2Path]: 19,
},
fileTokenCounts: {
'file1.txt': 10,
[file2Path]: 10,
},
}),
};
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.writeOutputToDisk).toHaveBeenCalled();
expect(mockDeps.generateOutput).toHaveBeenCalled();
expect(mockDeps.calculateMetrics).toHaveBeenCalled();
expect(mockDeps.validateFileSafety).toHaveBeenCalledWith(
mockRawFiles,
progressCallback,
mockConfig,
undefined,
undefined,
);
expect(mockDeps.processFiles).toHaveBeenCalledWith(mockSafeRawFiles, mockConfig, progressCallback);
expect(mockDeps.generateOutput).toHaveBeenCalledWith(
['root'],
mockConfig,
mockProcessedFiles,
mockFilePaths,
undefined,
undefined,
);
expect(mockDeps.writeOutputToDisk).toHaveBeenCalledWith(mockOutput, mockConfig);
expect(mockDeps.copyToClipboardIfEnabled).toHaveBeenCalledWith(mockOutput, progressCallback, mockConfig);
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([]);
});
});