Skip to content

Commit

Permalink
add tests for v3 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushmau5 committed Dec 4, 2024
1 parent 811eceb commit 5454a91
Show file tree
Hide file tree
Showing 5 changed files with 792 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function diff(
config: Config = {}
): AsyncAPIDiff {
if (incompatibleDocuments(firstDocument, secondDocument)) {
throw new TypeError('Incompatible documents');
throw new TypeError('diff between different AsyncAPI version is not allowed');
}

const standard = getStandardFromVersion(firstDocument);
Expand Down
96 changes: 96 additions & 0 deletions test/fixtures/main.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,102 @@ export const diffOutput = {
],
};

export const diffOutputV3 = {
changes: [
{
action: 'edit',
after: '1.1',
before: '1.0',
path: '/components/operations/sendUserSignUp/channel/servers/0/protocolVersion',
type: 'unclassified',
},
{
action: 'edit',
after: 'rabbitmq.in.mycompany.com:5673',
before: 'rabbitmq.in.mycompany.com:5672',
path: '/components/operations/sendUserSignUp/channel/servers/0/host',
type: 'unclassified',
},
{
action: 'edit',
after: 'users.{userid}',
before: 'users.{userId}',
path: '/components/operations/sendUserSignUp/channel/address',
type: 'unclassified',
},
{
action: 'edit',
after: '1.1',
before: '1.0',
path: '/operations/sendUserSignUp/channel/servers/0/protocolVersion',
type: 'unclassified',
},
{
action: 'edit',
after: 'rabbitmq.in.mycompany.com:5673',
before: 'rabbitmq.in.mycompany.com:5672',
path: '/operations/sendUserSignUp/channel/servers/0/host',
type: 'unclassified',
},
{
action: 'edit',
after: 'users.{userid}',
before: 'users.{userId}',
path: '/operations/sendUserSignUp/channel/address',
type: 'unclassified',
},
{
action: 'edit',
after: 'A short description',
before: 'A longer description',
path: '/operations/sendUserSignUp/description',
type: 'non-breaking',
},
{
action: 'edit',
after: '1.1',
before: '1.0',
path: '/channels/user/servers/0/protocolVersion',
type: 'unclassified',
},
{
action: 'edit',
after: 'rabbitmq.in.mycompany.com:5673',
before: 'rabbitmq.in.mycompany.com:5672',
path: '/channels/user/servers/0/host',
type: 'unclassified',
},
{
action: 'edit',
after: 'users.{userid}',
before: 'users.{userId}',
path: '/channels/user/address',
type: 'breaking',
},
{
action: 'edit',
after: '1.1',
before: '1.0',
path: '/servers/production/protocolVersion',
type: 'breaking',
},
{
action: 'edit',
after: 'rabbitmq.in.mycompany.com:5673',
before: 'rabbitmq.in.mycompany.com:5672',
path: '/servers/production/host',
type: 'breaking',
},
{
action: 'edit',
after: 'World',
before: 'Hello',
path: '/info/contact/name',
type: 'non-breaking',
},
],
};

export const overrides = {
'/channels/*': {
add: 'breaking',
Expand Down
42 changes: 41 additions & 1 deletion test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
arrayChanges,
YAMLArrayChanges,
MarkdownArrayChanges,
diffOutputV3,
} from './fixtures/main.fixtures';

const parser = new Parser();

describe('main function', () => {
test('runs the diff function', async () => {
test('runs the diff function for spec v2', async () => {
const firstSpecDocument = readFileSync(
resolve('./test/spec/asyncapi.yml'),
'utf-8'
Expand All @@ -43,6 +44,45 @@ describe('main function', () => {
expect(output.nonBreaking()).toEqual(nonBreakingChanges);
});

test('throws on incompatible documents', async () => {
const firstSpecDocument = readFileSync(
resolve('./test/spec/asyncapi.yml'),
'utf-8'
);
const secondSpecDocument = readFileSync(
resolve('./test/spec/asyncapi-v3.yml'),
'utf-8'
);
const firstDocument = await parser.parse(firstSpecDocument);
const secondDocument = await parser.parse(secondSpecDocument);

expect(() => diff(
firstDocument.document?.json(),
secondDocument.document?.json()
)).toThrowError(
new TypeError('diff between different AsyncAPI version is not allowed')
);
});

test('runs the diff function for spec v3', async () => {
const firstSpecDocument = readFileSync(
resolve('./test/spec/asyncapi-v3.yml'),
'utf-8'
);
const secondSpecDocument = readFileSync(
resolve('./test/spec/asyncapi-v3-diff.yml'),
'utf-8'
);
const firstDocument = await parser.parse(firstSpecDocument);
const secondDocument = await parser.parse(secondSpecDocument);
const output = diff(
firstDocument.document?.json(),
secondDocument.document?.json()
);
expect(output).toBeInstanceOf(AsyncAPIDiff);
expect(output.getOutput()).toEqual(diffOutputV3);
});

test('runs the diff function with empty spec', () => {
const firstSpec = {asyncapi: '2.1.0'};
const secondSpec = {asyncapi: '2.1.0'};
Expand Down
Loading

0 comments on commit 5454a91

Please sign in to comment.