import { describe, expect, it } from 'bun:test'; import { OpenAPIParseError } from './error'; import { parseOpenAPI } from './parse'; const spec = await Bun.file(new URL('./fixtures/recursive-spec.json', import.meta.url)).text(); const specV2 = await Bun.file(new URL('./fixtures/spec-v2.json', import.meta.url)).text(); const petstoreyaml = await Bun.file(new URL('./fixtures/petstore.yaml', import.meta.url)).text(); const petstoreInvalid = await Bun.file( new URL('./fixtures/petstore-invalid.json', import.meta.url) ).text(); const html = ` API Documentation `; describe('#parseOpenAPI', () => { it( 'parses a recursive OpenAPI document', async () => { const result = await parseOpenAPI({ value: spec, rootURL: null, }); // Ensure the structure returned is not recursive (not dereferenced). JSON.stringify(result.filesystem); }, { timeout: 30_000 } ); it('parses a swagger v2', async () => { const result = await parseOpenAPI({ value: specV2, rootURL: null, }); // Ensure the structure returned is not recursive (not dereferenced). JSON.stringify(result.filesystem); }); it('throws an error for invalid OpenAPI document', async () => { expect.assertions(1); try { await parseOpenAPI({ value: html, rootURL: null, }); } catch (error) { if (error instanceof OpenAPIParseError) { expect(error.message).toContain( 'Can’t find supported Swagger/OpenAPI version in the provided document, version must be a string.' ); } } }); it('allows a document yaml', async () => { const result = await parseOpenAPI({ value: petstoreyaml, rootURL: null, }); // Ensure the structure returned is not recursive (not dereferenced). JSON.stringify(result.filesystem); }); it('allows a document with errors', async () => { const result = await parseOpenAPI({ value: petstoreInvalid, rootURL: null, }); // Ensure the structure returned is not recursive (not dereferenced). JSON.stringify(result.filesystem); expect(result.errors).toHaveLength(1); }); });