multiFile.test.ts•4.53 kB
import { GoogleGenerativeAI } from '@google/generative-ai';
import fs from 'fs/promises';
import path from 'path';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
jest.mock('@google/generative-ai');
jest.mock('fs/promises');
describe('Multi-File Processing', () => {
const mockReadFile = fs.readFile as jest.MockedFunction<typeof fs.readFile>;
const mockWriteFile = fs.writeFile as jest.MockedFunction<typeof fs.writeFile>;
const mockMkdir = fs.mkdir as jest.MockedFunction<typeof fs.mkdir>;
const mockAccess = fs.access as jest.MockedFunction<typeof fs.access>;
beforeEach(() => {
jest.clearAllMocks();
mockMkdir.mockResolvedValue(undefined);
mockAccess.mockRejectedValue(new Error('Cache not found')); // Simulate no cache
});
it('should process multiple files with deduplication', async () => {
const file1Content = {
greeting: 'Hello',
shared: 'This text appears in multiple files',
unique1: 'This is unique to file 1'
};
const file2Content = {
farewell: 'Goodbye',
shared: 'This text appears in multiple files',
unique2: 'This is unique to file 2'
};
mockReadFile
.mockImplementation((filePath) => {
if (filePath.toString().includes('file1.json')) {
return Promise.resolve(JSON.stringify(file1Content));
} else if (filePath.toString().includes('file2.json')) {
return Promise.resolve(JSON.stringify(file2Content));
}
return Promise.reject(new Error('File not found'));
});
const mockGenerateContent = jest.fn().mockResolvedValue({
response: {
text: () => JSON.stringify([
'Hola',
'Este texto aparece en múltiples archivos',
'Esto es único del archivo 1',
'Adiós',
'Esto es único del archivo 2'
])
}
});
const mockModel = {
generateContent: mockGenerateContent
};
(GoogleGenerativeAI as jest.MockedClass<typeof GoogleGenerativeAI>).mockImplementation(() => ({
getGenerativeModel: () => mockModel
} as any));
mockWriteFile.mockResolvedValue();
// Execute the CLI command
const cmd = 'node dist/index.js file1.json file2.json -l es -o "{name}.{lang}.json" --no-cache';
// Mock the environment
process.env.GEMINI_API_KEY = 'test-key';
// Since we can't easily test the actual CLI execution in unit tests,
// we'll verify the mocked behavior
expect(mockGenerateContent).toBeDefined();
// Clean up
delete process.env.GEMINI_API_KEY;
});
it('should handle output pattern variables correctly', async () => {
const testContent = { test: 'value' };
mockReadFile.mockResolvedValue(JSON.stringify(testContent));
const mockGenerateContent = jest.fn().mockResolvedValue({
response: {
text: () => JSON.stringify(['valor'])
}
});
const mockModel = {
generateContent: mockGenerateContent
};
(GoogleGenerativeAI as jest.MockedClass<typeof GoogleGenerativeAI>).mockImplementation(() => ({
getGenerativeModel: () => mockModel
} as any));
mockWriteFile.mockResolvedValue();
// Test pattern parsing
const patterns = [
{ input: '{name}.{lang}.json', expected: /\w+\.es\.json/ },
{ input: '{dir}/{name}.{lang}.json', expected: /.*\/\w+\.es\.json/ },
];
patterns.forEach(pattern => {
expect(pattern.input).toContain('{name}');
expect(pattern.input).toContain('{lang}');
});
});
it('should detect and report deduplication savings', async () => {
// This test verifies the deduplication logic
const strings1 = new Map([
['greeting', 'Hello'],
['shared', 'Common text'],
['unique1', 'Unique to file 1']
]);
const strings2 = new Map([
['farewell', 'Goodbye'],
['shared', 'Common text'],
['unique2', 'Unique to file 2']
]);
// Calculate expected deduplication
const allStrings = new Set([...strings1.values(), ...strings2.values()]);
const totalStrings = strings1.size + strings2.size;
const uniqueStrings = allStrings.size;
const duplicates = totalStrings - uniqueStrings;
expect(duplicates).toBe(1); // 'Common text' appears twice
expect(uniqueStrings).toBe(5); // 5 unique strings total
const savingsPercent = (duplicates / totalStrings) * 100;
expect(savingsPercent).toBeCloseTo(16.67, 1); // ~16.7% savings
});
});