From 43f2c82ce4d35c79fab2721b09c3823bde8d5708 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 29 Nov 2024 17:36:50 +0100 Subject: [PATCH] test: unprocessable files --- ...esolvable-circular-reference-resolved.json | 67 +++++++++++++++++++ __tests__/commands/openapi/refs.test.ts | 20 +++++- 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 __tests__/__fixtures__/circular-references-oas/unresolvable-circular-reference-resolved.json diff --git a/__tests__/__fixtures__/circular-references-oas/unresolvable-circular-reference-resolved.json b/__tests__/__fixtures__/circular-references-oas/unresolvable-circular-reference-resolved.json new file mode 100644 index 000000000..f3d915aab --- /dev/null +++ b/__tests__/__fixtures__/circular-references-oas/unresolvable-circular-reference-resolved.json @@ -0,0 +1,67 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Complex Cyclic Example", + "version": "1.0.0" + }, + "paths": { + "/example": { + "get": { + "summary": "Example endpoint with unresolved references", + "responses": { + "200": { + "description": "Successful response" + } + } + } + } + }, + "components": { + "schemas": { + "Entity": { + "type": "object", + "properties": { + "relatedEntity": { + "$ref": "#/components/schemas/RelatedEntity" + }, + "Entity": { + "$ref": "#/components/schemas/EntityRef" + } + } + }, + "RelatedEntity": { + "type": "object", + "properties": { + "entity": { + "$ref": "#/components/schemas/EntityRef" + }, + "RelatedEntity": { + "$ref": "#/components/schemas/RelatedEntityRef" + } + } + }, + "RelatedEntityRef": { + "type": "object", + "properties": { + "entity": { + "type": "object" + }, + "RelatedEntity": { + "type": "object" + } + } + }, + "EntityRef": { + "type": "object", + "properties": { + "relatedEntity": { + "$ref": "#/components/schemas/RelatedEntityRef" + }, + "Entity": { + "type": "object" + } + } + } + } + } +} diff --git a/__tests__/commands/openapi/refs.test.ts b/__tests__/commands/openapi/refs.test.ts index 4abc82f2d..08062c617 100644 --- a/__tests__/commands/openapi/refs.test.ts +++ b/__tests__/commands/openapi/refs.test.ts @@ -71,16 +71,30 @@ describe('openapi:solving-circularity-recursiveness', () => { expect(processedOutput).toStrictEqual(expectedOutput); }); - it('should display warning if unresolved references remain', async () => { + it('should replace circularity that cannot be processed with empty objects', async () => { const inputFile = path.resolve( '__tests__/__fixtures__/circular-references-oas/unresolvable-circular-references.json', ); + const expectedOutputFile = path.resolve( + '__tests__/__fixtures__/circular-references-oas/unresolvable-circular-reference-resolved.json', + ); + const defaultOutputFilePath = 'unresolvable-circular-references.openapi.json'; + + prompts.inject([defaultOutputFilePath]); + + let processedOutput; + fs.writeFileSync = vi.fn((fileName, data) => { + processedOutput = JSON.parse(data as string); + }); const result = await run([inputFile]); - expect(result).toContain('File not saved due to unresolved circular references.'); + expect(result).toMatch(`Your API definition has been processed and saved to ${defaultOutputFilePath}!`); - expect(fs.writeFileSync).not.toHaveBeenCalled(); + expect(fs.writeFileSync).toHaveBeenCalledWith(defaultOutputFilePath, expect.any(String)); + + const expectedOutput = JSON.parse(fs.readFileSync(expectedOutputFile, 'utf8')); + expect(processedOutput).toStrictEqual(expectedOutput); }); }); });