|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { after, before, beforeEach, describe, it } from 'node:test'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { CacheManager } from '../../build/utils/cache.js'; |
| 5 | +import { SourceMethod, SourceType } from '../../build/utils/source.js'; |
| 6 | +import { MockHttpServer } from './mock-server.js'; |
| 7 | +import { clearCache, setupCacheWithSources } from './test-utils.js'; |
| 8 | + |
| 9 | +describe('CacheManager - OpenAPI File-based Sources', () => { |
| 10 | + const fixturesPath = join(process.cwd(), 'example', 'api', 'fixtures'); |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + clearCache(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('OpenAPI JSON Schema File', () => { |
| 17 | + it('should load and cache OpenAPI schema from JSON file', async () => { |
| 18 | + await setupCacheWithSources([ |
| 19 | + { |
| 20 | + name: 'TestOpenAPIJSON', |
| 21 | + type: SourceType.API, |
| 22 | + path: join(fixturesPath, 'json', 'openapi-schema.json') |
| 23 | + } |
| 24 | + ]); |
| 25 | + |
| 26 | + const docs = await CacheManager.getDocs('TestOpenAPIJSON'); |
| 27 | + |
| 28 | + assert.strictEqual(docs.length, 1, 'Should have one cache entry'); |
| 29 | + assert.strictEqual(docs[0].name, 'TestOpenAPIJSON', 'Cache entry name should match'); |
| 30 | + assert.ok(docs[0].resources.length > 0, 'Should have resources'); |
| 31 | + |
| 32 | + const getUsersEndpoint = docs[0].resources.find(r => r.name === 'GET /users'); |
| 33 | + assert.ok(getUsersEndpoint, 'Should have GET /users endpoint'); |
| 34 | + assert.strictEqual(getUsersEndpoint.type, 'GET', 'Should have correct resource type'); |
| 35 | + assert.strictEqual(getUsersEndpoint.description, 'Get all users', 'Should have correct summary'); |
| 36 | + |
| 37 | + const postUsersEndpoint = docs[0].resources.find(r => r.name === 'POST /users'); |
| 38 | + assert.ok(postUsersEndpoint, 'Should have POST /users endpoint'); |
| 39 | + assert.strictEqual(postUsersEndpoint.type, 'POST', 'Should have correct resource type'); |
| 40 | + |
| 41 | + const getUserByIdEndpoint = docs[0].resources.find(r => r.name === 'GET /users/{id}'); |
| 42 | + assert.ok(getUserByIdEndpoint, 'Should have GET /users/{id} endpoint'); |
| 43 | + |
| 44 | + const putUserEndpoint = docs[0].resources.find(r => r.name === 'PUT /users/{id}'); |
| 45 | + assert.ok(putUserEndpoint, 'Should have PUT /users/{id} endpoint'); |
| 46 | + |
| 47 | + const deleteUserEndpoint = docs[0].resources.find(r => r.name === 'DELETE /users/{id}'); |
| 48 | + assert.ok(deleteUserEndpoint, 'Should have DELETE /users/{id} endpoint'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should retrieve specific endpoint details from OpenAPI JSON schema', async () => { |
| 52 | + await setupCacheWithSources([ |
| 53 | + { |
| 54 | + name: 'TestOpenAPIJSON', |
| 55 | + type: SourceType.API, |
| 56 | + path: join(fixturesPath, 'json', 'openapi-schema.json') |
| 57 | + } |
| 58 | + ]); |
| 59 | + |
| 60 | + const details = await CacheManager.getDetails('POST /users'); |
| 61 | + |
| 62 | + assert.ok(details.length > 0, 'Should find POST /users endpoint details'); |
| 63 | + assert.strictEqual(details[0].resources[0].name, 'POST /users', 'Should match endpoint name'); |
| 64 | + |
| 65 | + const postResource = details[0].resources[0]; |
| 66 | + assert.ok(postResource.schema, 'Should have schema'); |
| 67 | + assert.strictEqual(typeof postResource.schema, 'string', 'Schema should be a string'); |
| 68 | + |
| 69 | + const schema = JSON.parse(postResource.schema); |
| 70 | + assert.ok(schema.openapi, 'Should have OpenAPI version'); |
| 71 | + assert.ok(schema.paths['/users'].post, 'Should have POST /users endpoint in schema'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('OpenAPI YAML Schema File', () => { |
| 76 | + it('should load and cache OpenAPI schema from YAML file', async () => { |
| 77 | + await setupCacheWithSources([ |
| 78 | + { |
| 79 | + name: 'TestOpenAPIYAML', |
| 80 | + type: SourceType.API, |
| 81 | + path: join(fixturesPath, 'yaml', 'openapi-schema.yaml') |
| 82 | + } |
| 83 | + ]); |
| 84 | + |
| 85 | + const docs = await CacheManager.getDocs('TestOpenAPIYAML'); |
| 86 | + |
| 87 | + assert.strictEqual(docs.length, 1, 'Should have one cache entry'); |
| 88 | + assert.strictEqual(docs[0].name, 'TestOpenAPIYAML', 'Cache entry name should match'); |
| 89 | + assert.ok(docs[0].resources.length > 0, 'Should have resources'); |
| 90 | + |
| 91 | + const getUsersEndpoint = docs[0].resources.find(r => r.name === 'GET /users'); |
| 92 | + assert.ok(getUsersEndpoint, 'Should have GET /users endpoint'); |
| 93 | + |
| 94 | + const postUsersEndpoint = docs[0].resources.find(r => r.name === 'POST /users'); |
| 95 | + assert.ok(postUsersEndpoint, 'Should have POST /users endpoint'); |
| 96 | + |
| 97 | + const getUserByIdEndpoint = docs[0].resources.find(r => r.name === 'GET /users/{id}'); |
| 98 | + assert.ok(getUserByIdEndpoint, 'Should have GET /users/{id} endpoint'); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('CacheManager - OpenAPI URL-based Sources', () => { |
| 104 | + let mockServer: MockHttpServer; |
| 105 | + const fixturesPath = join(process.cwd(), 'example', 'api', 'fixtures'); |
| 106 | + const TEST_PORT = 8765; |
| 107 | + |
| 108 | + beforeEach(() => { |
| 109 | + clearCache(); |
| 110 | + }); |
| 111 | + |
| 112 | + before(async () => { |
| 113 | + mockServer = new MockHttpServer({ |
| 114 | + port: TEST_PORT, |
| 115 | + openApiJsonPath: join(fixturesPath, 'json', 'openapi-schema.json'), |
| 116 | + openApiYamlPath: join(fixturesPath, 'yaml', 'openapi-schema.yaml') |
| 117 | + }); |
| 118 | + await mockServer.start(); |
| 119 | + }); |
| 120 | + |
| 121 | + after(async () => { |
| 122 | + await mockServer.stop(); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('OpenAPI URL Source - JSON', () => { |
| 126 | + it('should load and cache OpenAPI schema from JSON URL', async () => { |
| 127 | + await setupCacheWithSources([ |
| 128 | + { |
| 129 | + name: 'TestOpenAPIURLJSON', |
| 130 | + type: SourceType.API, |
| 131 | + method: SourceMethod.GET, |
| 132 | + url: mockServer.getUrl('/openapi.json') |
| 133 | + } |
| 134 | + ]); |
| 135 | + |
| 136 | + const docs = await CacheManager.getDocs('TestOpenAPIURLJSON'); |
| 137 | + |
| 138 | + assert.strictEqual(docs.length, 1, 'Should have one cache entry'); |
| 139 | + assert.strictEqual(docs[0].name, 'TestOpenAPIURLJSON', 'Cache entry name should match'); |
| 140 | + assert.ok(docs[0].resources.length > 0, 'Should have resources'); |
| 141 | + |
| 142 | + const getUsersEndpoint = docs[0].resources.find(r => r.name === 'GET /users'); |
| 143 | + assert.ok(getUsersEndpoint, 'Should have GET /users endpoint'); |
| 144 | + |
| 145 | + const postUsersEndpoint = docs[0].resources.find(r => r.name === 'POST /users'); |
| 146 | + assert.ok(postUsersEndpoint, 'Should have POST /users endpoint'); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('OpenAPI URL Source - YAML', () => { |
| 151 | + it('should load and cache OpenAPI schema from YAML URL', async () => { |
| 152 | + await setupCacheWithSources([ |
| 153 | + { |
| 154 | + name: 'TestOpenAPIURLYAML', |
| 155 | + type: SourceType.API, |
| 156 | + method: SourceMethod.GET, |
| 157 | + url: mockServer.getUrl('/openapi.yaml') |
| 158 | + } |
| 159 | + ]); |
| 160 | + |
| 161 | + const docs = await CacheManager.getDocs('TestOpenAPIURLYAML'); |
| 162 | + |
| 163 | + assert.strictEqual(docs.length, 1, 'Should have one cache entry'); |
| 164 | + assert.strictEqual(docs[0].name, 'TestOpenAPIURLYAML', 'Cache entry name should match'); |
| 165 | + assert.ok(docs[0].resources.length > 0, 'Should have resources'); |
| 166 | + |
| 167 | + const getUsersEndpoint = docs[0].resources.find(r => r.name === 'GET /users'); |
| 168 | + assert.ok(getUsersEndpoint, 'Should have GET /users endpoint'); |
| 169 | + |
| 170 | + const postUsersEndpoint = docs[0].resources.find(r => r.name === 'POST /users'); |
| 171 | + assert.ok(postUsersEndpoint, 'Should have POST /users endpoint'); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments