Skip to content

Commit

Permalink
test: Increase coverage of some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadashy committed Mar 2, 2025
1 parent 09f78b5 commit 3f8dff6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/core/file/fileManipulate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import strip from 'strip-comments';

interface FileManipulator {
export interface FileManipulator {
removeComments(content: string): string;
removeEmptyLines(content: string): string;
}
Expand Down
9 changes: 8 additions & 1 deletion src/core/file/fileProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import type { RepomixConfigMerged } from '../../config/configSchema.js';
import { logger } from '../../shared/logger.js';
import { initPiscina } from '../../shared/processConcurrency.js';
import type { RepomixProgressCallback } from '../../shared/types.js';
import { type FileManipulator, getFileManipulator } from './fileManipulate.js';
import type { ProcessedFile, RawFile } from './fileTypes.js';
import type { FileProcessTask } from './workers/fileProcessWorker.js';

type GetFileManipulator = (filePath: string) => FileManipulator | null;

const initTaskRunner = (numOfTasks: number) => {
const pool = initPiscina(numOfTasks, new URL('./workers/fileProcessWorker.js', import.meta.url).href);
return (task: FileProcessTask) => pool.run(task);
Expand All @@ -15,8 +18,12 @@ export const processFiles = async (
rawFiles: RawFile[],
config: RepomixConfigMerged,
progressCallback: RepomixProgressCallback,
deps = {
deps: {
initTaskRunner: typeof initTaskRunner;
getFileManipulator: GetFileManipulator;
} = {
initTaskRunner,
getFileManipulator,
},
): Promise<ProcessedFile[]> => {
const runTask = deps.initTaskRunner(rawFiles.length);
Expand Down
27 changes: 13 additions & 14 deletions tests/core/file/fileProcess.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { describe, expect, it, vi } from 'vitest';
import { getFileManipulator } from '../../../src/core/file/fileManipulate.js';
import type { FileManipulator } from '../../../src/core/file/fileManipulate.js';
import { processFiles } from '../../../src/core/file/fileProcess.js';
import type { RawFile } from '../../../src/core/file/fileTypes.js';
import { type FileProcessTask, processContent } from '../../../src/core/file/workers/fileProcessWorker.js';
import fileProcessWorker from '../../../src/core/file/workers/fileProcessWorker.js';
import { createMockConfig } from '../../testing/testUtils.js';

vi.mock('../../../src/core/file/fileManipulate');
const createMockFileManipulator = (): FileManipulator => ({
removeComments: (content: string) => content.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, ''),
removeEmptyLines: (content: string) => content.replace(/^\s*[\r\n]/gm, ''),
});

const mockGetFileManipulator = (filePath: string): FileManipulator | null => {
if (filePath.endsWith('.js')) {
return createMockFileManipulator();
}
return null;
};

const mockInitTaskRunner = (numOfTasks: number) => {
return async (task: FileProcessTask) => {
Expand All @@ -28,13 +38,9 @@ describe('fileProcess', () => {
},
});

vi.mocked(getFileManipulator).mockReturnValue({
removeComments: (content: string) => content.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, ''),
removeEmptyLines: (content: string) => content.replace(/^\s*[\r\n]/gm, ''),
});

const result = await processFiles(mockRawFiles, config, () => {}, {
initTaskRunner: mockInitTaskRunner,
getFileManipulator: mockGetFileManipulator,
});

expect(result).toEqual([
Expand All @@ -55,11 +61,6 @@ describe('fileProcess', () => {
},
});

vi.mocked(getFileManipulator).mockReturnValue({
removeComments: (content: string) => content.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, ''),
removeEmptyLines: (content: string) => content.replace(/^\s*[\r\n]/gm, ''),
});

const result = await processContent({ path: filePath, content }, config);

expect(result).toBe('const a = 1;\nconst b = 2;');
Expand Down Expand Up @@ -90,8 +91,6 @@ describe('fileProcess', () => {
},
});

vi.mocked(getFileManipulator).mockReturnValue(null);

const result = await processContent({ path: filePath, content }, config);

expect(result).toBe(content);
Expand Down
24 changes: 23 additions & 1 deletion tests/shared/processConcurrency.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os from 'node:os';
import { Piscina } from 'piscina';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getProcessConcurrency, getWorkerThreadCount } from '../../src/shared/processConcurrency.js';
import { getProcessConcurrency, getWorkerThreadCount, initPiscina } from '../../src/shared/processConcurrency.js';

vi.mock('node:os');
vi.mock('piscina');

describe('processConcurrency', () => {
describe('getProcessConcurrency', () => {
Expand Down Expand Up @@ -57,4 +59,24 @@ describe('processConcurrency', () => {
expect(maxThreads).toBe(1);
});
});

describe('initPiscina', () => {
beforeEach(() => {
vi.mocked(os).availableParallelism = vi.fn().mockReturnValue(4);
vi.mocked(Piscina).mockImplementation(() => ({}) as unknown as Piscina);
});

it('should initialize Piscina with correct configuration', () => {
const workerPath = '/path/to/worker.js';
const piscina = initPiscina(500, workerPath);

expect(Piscina).toHaveBeenCalledWith({
filename: workerPath,
minThreads: 1,
maxThreads: 4,
idleTimeout: 5000,
});
expect(piscina).toBeDefined();
});
});
});

0 comments on commit 3f8dff6

Please sign in to comment.