cli.spec.ts 3.4 KB

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