Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions apps/keeper/test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import path from 'path';
import { parseAsyncAPIDocumentFromFile } from '../src/utils.js';

const validAsyncapiPath = path.resolve(__dirname, './__fixtures__/asyncapi-message-validation.yml');
const invalidAsyncapiPath = path.resolve(__dirname, './__fixtures__/non-existent-file.yml');

describe('parseAsyncAPIDocumentFromFile', () => {
describe('Input Validation', () => {
test('should throw error if asyncapiFilepath is null', async () => {
await expect(parseAsyncAPIDocumentFromFile(null)).rejects.toThrow(
'Invalid "asyncapiFilepath" parameter: must be a non-empty string'
);
});

test('should throw error if asyncapiFilepath is undefined', async () => {
await expect(parseAsyncAPIDocumentFromFile(undefined)).rejects.toThrow(
'Invalid "asyncapiFilepath" parameter: must be a non-empty string'
);
});

test('should throw error if asyncapiFilepath is empty string', async () => {
await expect(parseAsyncAPIDocumentFromFile('')).rejects.toThrow(
'Invalid "asyncapiFilepath" parameter: must be a non-empty string'
);
});

test('should throw error if asyncapiFilepath is whitespace only', async () => {
await expect(parseAsyncAPIDocumentFromFile(' ')).rejects.toThrow(
'Invalid "asyncapiFilepath" parameter: must be a non-empty string'
);
});

test('should throw error if asyncapiFilepath is a number', async () => {
await expect(parseAsyncAPIDocumentFromFile(123)).rejects.toThrow(
'Invalid "asyncapiFilepath" parameter: must be a non-empty string'
);
});

test('should throw error if asyncapiFilepath is an object', async () => {
await expect(parseAsyncAPIDocumentFromFile({ path: 'test.yml' })).rejects.toThrow(
'Invalid "asyncapiFilepath" parameter: must be a non-empty string'
);
});

test('should throw error if asyncapiFilepath is an array', async () => {
await expect(parseAsyncAPIDocumentFromFile(['test.yml'])).rejects.toThrow(
'Invalid "asyncapiFilepath" parameter: must be a non-empty string'
);
});
});

describe('Error Wrapping', () => {
test('should wrap parser errors with "Failed to parse AsyncAPI document" message', async () => {
await expect(parseAsyncAPIDocumentFromFile(invalidAsyncapiPath)).rejects.toThrow(
/Failed to parse AsyncAPI document/
);
});

test('should include original error details in wrapped message', async () => {
await expect(parseAsyncAPIDocumentFromFile(invalidAsyncapiPath)).rejects.toThrow(
expect.objectContaining({
message: expect.stringMatching(/Failed to parse AsyncAPI document/)
})
);
});
});

describe('Happy Path', () => {
test('should return parsed document for valid file path', async () => {
const document = await parseAsyncAPIDocumentFromFile(validAsyncapiPath);
expect(document).toBeDefined();
expect(document.info()).toBeDefined();
expect(document.info().title()).toBe('WebSocket Client');
});

test('should return document with correct version', async () => {
const document = await parseAsyncAPIDocumentFromFile(validAsyncapiPath);
expect(document.version()).toBe('3.0.0');
});

test('should return document with channels', async () => {
const document = await parseAsyncAPIDocumentFromFile(validAsyncapiPath);
const channels = document.channels();
expect(channels).toBeDefined();
expect(channels.all().length).toBeGreaterThan(0);
});
});
});