|
| 1 | +import { handleRespect, type RespectArgv } from '../../../commands/respect/index.js'; |
| 2 | +import { handleRun } from '@redocly/respect-core'; |
| 3 | +import { Config } from '@redocly/openapi-core'; |
| 4 | + |
| 5 | +// Mock node:fs |
| 6 | +vi.mock('node:fs', async () => { |
| 7 | + return { |
| 8 | + writeFileSync: vi.fn(), |
| 9 | + existsSync: vi.fn(() => false), // Return false to prevent plugin loading |
| 10 | + readFileSync: vi.fn(() => ''), |
| 11 | + accessSync: vi.fn(() => true), |
| 12 | + constants: { |
| 13 | + F_OK: 0, |
| 14 | + R_OK: 4, |
| 15 | + }, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +// Mock the handleRun function |
| 20 | +vi.mock('@redocly/respect-core', async () => { |
| 21 | + const actual = await vi.importActual('@redocly/respect-core'); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + handleRun: vi.fn(), |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +// Mock @redocly/openapi-core |
| 29 | +vi.mock('@redocly/openapi-core', async () => { |
| 30 | + const actual = await vi.importActual('@redocly/openapi-core'); |
| 31 | + return { |
| 32 | + ...actual, |
| 33 | + logger: { |
| 34 | + info: vi.fn(), |
| 35 | + output: vi.fn(), |
| 36 | + printNewLine: vi.fn(), |
| 37 | + indent: vi.fn((text) => text), |
| 38 | + }, |
| 39 | + stringifyYaml: vi.fn(() => 'mocked yaml'), |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +// Mock displayFilesSummaryTable |
| 44 | +vi.mock('../../../commands/respect/display-files-summary-table.js', () => ({ |
| 45 | + displayFilesSummaryTable: vi.fn(), |
| 46 | +})); |
| 47 | + |
| 48 | +describe('handleRespect', () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + }); |
| 52 | + it('should call handleRun with the correct arguments', async () => { |
| 53 | + const mockConfig = new Config({}); |
| 54 | + const commandArgs = { |
| 55 | + argv: { |
| 56 | + files: ['test.arazzo.yaml'], |
| 57 | + input: 'name=John', |
| 58 | + server: 'server1=http://localhost:3000', |
| 59 | + workflow: ['workflow3'], |
| 60 | + verbose: true, |
| 61 | + 'max-steps': 2000, |
| 62 | + severity: 'STATUS_CODE_CHECK=warn', |
| 63 | + 'max-fetch-timeout': 40_000, |
| 64 | + 'execution-timeout': 3_600_000, |
| 65 | + } as RespectArgv, |
| 66 | + config: mockConfig, |
| 67 | + version: '1.0.0', |
| 68 | + collectSpecData: vi.fn(), |
| 69 | + }; |
| 70 | + |
| 71 | + vi.mocked(handleRun).mockResolvedValue([ |
| 72 | + { |
| 73 | + hasProblems: false, |
| 74 | + hasWarnings: false, |
| 75 | + file: 'test.arazzo.yaml', |
| 76 | + executedWorkflows: [], |
| 77 | + options: {} as any, |
| 78 | + ctx: {} as any, |
| 79 | + totalTimeMs: 100, |
| 80 | + totalRequests: 1, |
| 81 | + globalTimeoutError: false, |
| 82 | + harLogs: {}, |
| 83 | + }, |
| 84 | + ]); |
| 85 | + |
| 86 | + await handleRespect(commandArgs); |
| 87 | + |
| 88 | + expect(handleRun).toHaveBeenCalledWith( |
| 89 | + expect.objectContaining({ |
| 90 | + files: ['test.arazzo.yaml'], |
| 91 | + input: 'name=John', |
| 92 | + server: 'server1=http://localhost:3000', |
| 93 | + workflow: ['workflow3'], |
| 94 | + verbose: true, |
| 95 | + config: expect.anything(), |
| 96 | + version: '1.0.0', |
| 97 | + collectSpecData: expect.anything(), |
| 98 | + severity: 'STATUS_CODE_CHECK=warn', |
| 99 | + harOutput: undefined, |
| 100 | + jsonOutput: undefined, |
| 101 | + maxSteps: 2000, |
| 102 | + maxFetchTimeout: 40_000, |
| 103 | + executionTimeout: 3_600_000, |
| 104 | + }) |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments