cli.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { writeFileSync } from 'fs';
  2. import {
  3. beforeEach, describe, expect, it, vi,
  4. } from 'vitest';
  5. import { generateOperationIds } from './generate-operation-ids';
  6. // Mock the modules
  7. vi.mock('fs');
  8. vi.mock('./generate-operation-ids');
  9. const originalArgv = process.argv;
  10. describe('cli', () => {
  11. const mockJsonStrings = '{"test": "data"}';
  12. beforeEach(() => {
  13. vi.resetModules();
  14. vi.resetAllMocks();
  15. process.argv = [...originalArgv]; // Reset process.argv
  16. // Mock console.error to avoid actual console output during tests
  17. vi.spyOn(console, 'error').mockImplementation(() => {});
  18. });
  19. it('processes input file and writes output to specified file', async() => {
  20. // Mock generateOperationIds to return success
  21. vi.mocked(generateOperationIds).mockResolvedValue(mockJsonStrings);
  22. // Mock process.argv
  23. process.argv = ['node', 'cli.js', 'input.json', '-o', 'output.json'];
  24. // Import the module that contains the main function
  25. const cliModule = await import('./cli');
  26. await cliModule.main();
  27. // Verify generateOperationIds was called with correct arguments
  28. expect(generateOperationIds).toHaveBeenCalledWith('input.json', { overwriteExisting: undefined });
  29. // Verify writeFileSync was called with correct arguments
  30. expect(writeFileSync).toHaveBeenCalledWith('output.json', mockJsonStrings);
  31. });
  32. it('uses input file as output when no output file is specified', async() => {
  33. // Mock generateOperationIds to return success
  34. vi.mocked(generateOperationIds).mockResolvedValue(mockJsonStrings);
  35. // Mock process.argv
  36. process.argv = ['node', 'cli.js', 'input.json'];
  37. // Import the module that contains the main function
  38. const cliModule = await import('./cli');
  39. await cliModule.main();
  40. // Verify generateOperationIds was called with correct arguments
  41. expect(generateOperationIds).toHaveBeenCalledWith('input.json', { overwriteExisting: undefined });
  42. // Verify writeFileSync was called with input file as output
  43. expect(writeFileSync).toHaveBeenCalledWith('input.json', mockJsonStrings);
  44. });
  45. it('handles overwrite-existing option correctly', async() => {
  46. // Mock generateOperationIds to return success
  47. vi.mocked(generateOperationIds).mockResolvedValue(mockJsonStrings);
  48. // Mock process.argv
  49. process.argv = ['node', 'cli.js', 'input.json', '--overwrite-existing'];
  50. // Import the module that contains the main function
  51. const cliModule = await import('./cli');
  52. await cliModule.main();
  53. // Verify generateOperationIds was called with overwriteExisting option
  54. expect(generateOperationIds).toHaveBeenCalledWith('input.json', { overwriteExisting: true });
  55. });
  56. it('handles generateOperationIds error correctly', async() => {
  57. // Mock generateOperationIds to throw error
  58. const error = new Error('Test error');
  59. vi.mocked(generateOperationIds).mockRejectedValue(error);
  60. // Mock process.argv
  61. process.argv = ['node', 'cli.js', 'input.json'];
  62. // Import the module that contains the main function
  63. const cliModule = await import('./cli');
  64. await cliModule.main();
  65. // Verify error was logged
  66. // eslint-disable-next-line no-console
  67. expect(console.error).toHaveBeenCalledWith(error);
  68. // Verify writeFileSync was not called
  69. expect(writeFileSync).not.toHaveBeenCalled();
  70. });
  71. });