| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { writeFileSync } from 'node:fs';
- import { beforeEach, describe, expect, it, vi } from 'vitest';
- import { generateOperationIds } from './generate-operation-ids';
- // Mock the modules
- vi.mock('fs');
- vi.mock('./generate-operation-ids');
- const originalArgv = process.argv;
- describe('cli', () => {
- const mockJsonStrings = '{"test": "data"}';
- beforeEach(() => {
- vi.resetModules();
- vi.resetAllMocks();
- process.argv = [...originalArgv]; // Reset process.argv
- // Mock console.error to avoid actual console output during tests
- vi.spyOn(console, 'error').mockImplementation(() => {});
- });
- it('processes input file and writes output to specified file', async () => {
- // Mock generateOperationIds to return success
- vi.mocked(generateOperationIds).mockResolvedValue(mockJsonStrings);
- // Mock process.argv
- process.argv = ['node', 'cli.js', 'input.json', '-o', 'output.json'];
- // Import the module that contains the main function
- const cliModule = await import('./cli');
- await cliModule.main();
- // Verify generateOperationIds was called with correct arguments
- expect(generateOperationIds).toHaveBeenCalledWith('input.json', {
- overwriteExisting: undefined,
- });
- // Verify writeFileSync was called with correct arguments
- expect(writeFileSync).toHaveBeenCalledWith('output.json', mockJsonStrings);
- });
- it('uses input file as output when no output file is specified', async () => {
- // Mock generateOperationIds to return success
- vi.mocked(generateOperationIds).mockResolvedValue(mockJsonStrings);
- // Mock process.argv
- process.argv = ['node', 'cli.js', 'input.json'];
- // Import the module that contains the main function
- const cliModule = await import('./cli');
- await cliModule.main();
- // Verify generateOperationIds was called with correct arguments
- expect(generateOperationIds).toHaveBeenCalledWith('input.json', {
- overwriteExisting: undefined,
- });
- // Verify writeFileSync was called with input file as output
- expect(writeFileSync).toHaveBeenCalledWith('input.json', mockJsonStrings);
- });
- it('handles overwrite-existing option correctly', async () => {
- // Mock generateOperationIds to return success
- vi.mocked(generateOperationIds).mockResolvedValue(mockJsonStrings);
- // Mock process.argv
- process.argv = ['node', 'cli.js', 'input.json', '--overwrite-existing'];
- // Import the module that contains the main function
- const cliModule = await import('./cli');
- await cliModule.main();
- // Verify generateOperationIds was called with overwriteExisting option
- expect(generateOperationIds).toHaveBeenCalledWith('input.json', {
- overwriteExisting: true,
- });
- });
- it('handles generateOperationIds error correctly', async () => {
- // Mock generateOperationIds to throw error
- const error = new Error('Test error');
- vi.mocked(generateOperationIds).mockRejectedValue(error);
- // Mock process.argv
- process.argv = ['node', 'cli.js', 'input.json'];
- // Import the module that contains the main function
- const cliModule = await import('./cli');
- await cliModule.main();
- // Verify error was logged
- // biome-ignore lint/suspicious/noConsole: This is a test file
- expect(console.error).toHaveBeenCalledWith(error);
- // Verify writeFileSync was not called
- expect(writeFileSync).not.toHaveBeenCalled();
- });
- });
|