Skip to content

Commit 2af1824

Browse files
committed
feat: Update tests for asyncapi protocol
1 parent 1e0465f commit 2af1824

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

src/apitypes/async/async.utils.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ export { dump, getCustomTags, resolveApiAudience } from '../../utils/apihubSpeci
3030
* @returns Protocol string (e.g., 'kafka', 'amqp', 'mqtt') or 'unknown'
3131
*/
3232
export function extractProtocol(channel: AsyncAPIV3.ChannelObject): AsyncProtocol {
33-
if (isObject(channel.servers)) {
34-
for (const server of Object.values(channel.servers as AsyncAPIV3.ServerObject[])) {
35-
if (isServerObject(server) && server.protocol) {
36-
const {protocol} = server
37-
return ASYNC_SUPPORTED_PROTOCOLS.includes(protocol) ? protocol as AsyncProtocol : 'unknown'
38-
}
33+
if (!isObject(channel.servers)) {
34+
return 'unknown'
35+
}
36+
for (const server of Object.values(channel.servers as AsyncAPIV3.ServerObject[])) {
37+
if (isServerObject(server) && server.protocol) {
38+
const { protocol } = server
39+
return ASYNC_SUPPORTED_PROTOCOLS.includes(protocol) ? protocol as AsyncProtocol : 'unknown'
3940
}
4041
}
4142

@@ -59,7 +60,7 @@ export function determineOperationAction(operationData: any): AsyncOperationActi
5960
}
6061

6162
function isServerObject(server: AsyncAPIV3.ServerObject | AsyncAPIV3.ReferenceObject): server is AsyncAPIV3.ServerObject {
62-
return server && typeof server === 'object' && 'protocol' in server
63+
return isObject(server) && 'protocol' in server
6364
}
6465

6566
function isTagObject(item: AsyncAPIV3.TagObject | AsyncAPIV3.ReferenceObject): item is AsyncAPIV3.TagObject {

test/async.operation.test.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { describe, expect, test } from '@jest/globals'
17+
import { describe, expect, it, test } from '@jest/globals'
1818
import * as fs from 'fs/promises'
1919
import * as path from 'path'
2020
import YAML from 'js-yaml'
@@ -93,26 +93,53 @@ describe('AsyncAPI 3.0 Operation Tests', () => {
9393
})
9494

9595
describe('protocol', () => {
96-
it('unit unique values', () => {
97-
const data = [
98-
[{
99-
title: 'channel1',
100-
servers: [{
101-
protocol: 'amqp',
102-
}],
103-
} as AsyncAPIV3.ChannelObject, 'result1'],
104-
]
105-
data.forEach(([channel, expected]) => {
106-
const result = extractProtocol(channel as AsyncAPIV3.ChannelObject)
107-
expect(result).toBe(expected)
108-
})
96+
it('should uses the (first) server protocol when supported', () => {
97+
const channel = {
98+
title: 'channel1',
99+
servers: [
100+
{ protocol: 'amqp' },
101+
{ protocol: 'kafka' },
102+
],
103+
} as unknown as AsyncAPIV3.ChannelObject
104+
105+
expect(extractProtocol(channel)).toBe('amqp')
106+
})
107+
108+
it('should returns unknown for unsupported protocol', () => {
109+
const channel = {
110+
title: 'channel1',
111+
servers: [
112+
{ protocol: 'mqtt' },
113+
{ protocol: 'amqp' },
114+
],
115+
} as unknown as AsyncAPIV3.ChannelObject
116+
117+
expect(extractProtocol(channel)).toBe('unknown')
118+
})
119+
120+
it('should returns first server with protocol', () => {
121+
const channel = {
122+
title: 'channel1',
123+
servers: [
124+
{ $ref: '#/servers/amqp1' },
125+
{ protocol: 'amqp' },
126+
],
127+
} as unknown as AsyncAPIV3.ChannelObject
128+
129+
expect(extractProtocol(channel)).toBe('unknown')
130+
})
131+
132+
it('should returns unknown when servers are missing or empty', () => {
133+
expect(extractProtocol({ title: 'no-servers' } as unknown as AsyncAPIV3.ChannelObject)).toBe('unknown')
134+
expect(extractProtocol({ title: 'empty-servers', servers: [] } as unknown as AsyncAPIV3.ChannelObject)).toBe('unknown')
109135
})
110136

111137
it('e2e', async () => {
112138
const result = await buildPackage('asyncapi/operations/single-operation')
113139
const operations = Array.from(result.operations.values())
114140
const [operation] = operations
115-
expect(operation.metadata.protocol).toBe('protocol')
141+
// In test spec, channel's first server is amqp.
142+
expect(operation.metadata.protocol).toBe('amqp')
116143
})
117144
})
118145

0 commit comments

Comments
 (0)