Skip to content

Commit 304f2e4

Browse files
committed
refactor: update test spec and schema discovery tests
1 parent 9afd6fa commit 304f2e4

2 files changed

Lines changed: 204 additions & 17 deletions

File tree

tests/fixtures/test_spec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
openapi: 3.0.0
22
info:
3-
title: Test API from File
3+
title: Test API from YAML
44
version: 1.0.0
55
servers:
66
- url: https://api.example.com
77
paths:
8-
/test:
8+
/yaml-test:
99
get:
10-
operationId: getTest
11-
summary: Test operation
10+
operationId: getYamlTest
11+
summary: YAML test endpoint
1212
responses:
1313
'200':
1414
description: Success

tests/schema_discovery.test.ts

Lines changed: 200 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -609,23 +609,14 @@ describe('OCP Schema Discovery', () => {
609609
expect(apiSpec.tools.length).toBe(1);
610610
});
611611

612-
test('load spec from absolute path (YAML)', async () => {
613-
const absolutePath = `${__dirname}/fixtures/test_spec.yaml`;
614-
const apiSpec = await discovery.discoverApi(absolutePath);
615-
616-
expect(apiSpec.title).toBe('Test API from File');
617-
expect(apiSpec.version).toBe('1.0.0');
618-
expect(apiSpec.base_url).toBe('https://api.example.com');
619-
expect(apiSpec.tools.length).toBe(1);
620-
expect(apiSpec.tools[0].name).toBe('getTest');
621-
});
622-
623-
test('load spec from relative path (YAML)', async () => {
612+
test('load yaml file', async () => {
624613
const relativePath = './tests/fixtures/test_spec.yaml';
625614
const apiSpec = await discovery.discoverApi(relativePath);
626615

627-
expect(apiSpec.title).toBe('Test API from File');
616+
expect(apiSpec.title).toBe('Test API from YAML');
617+
expect(apiSpec.version).toBe('1.0.0');
628618
expect(apiSpec.tools.length).toBe(1);
619+
expect(apiSpec.tools[0].name).toBe('getYamlTest');
629620
});
630621

631622
test('error on file not found', async () => {
@@ -660,4 +651,200 @@ describe('OCP Schema Discovery', () => {
660651
expect(apiSpec1.tools.length).toBe(apiSpec2.tools.length);
661652
});
662653
});
654+
655+
describe('Resource Filtering', () => {
656+
test('discover api with include resources', async () => {
657+
// Mock OpenAPI spec with multiple resource paths
658+
const openapiSpecWithResources = {
659+
openapi: '3.0.0',
660+
info: {title: 'GitHub API', version: '3.0'},
661+
servers: [{url: 'https://api.github.com'}],
662+
paths: {
663+
'/repos/{owner}/{repo}': {
664+
get: {
665+
operationId: 'repos/get',
666+
summary: 'Get a repository',
667+
parameters: [
668+
{name: 'owner', in: 'path', required: true, schema: {type: 'string'}},
669+
{name: 'repo', in: 'path', required: true, schema: {type: 'string'}}
670+
],
671+
responses: {'200': {description: 'Repository details'}}
672+
}
673+
},
674+
'/user/repos': {
675+
get: {
676+
operationId: 'repos/listForAuthenticatedUser',
677+
summary: 'List user repositories',
678+
responses: {'200': {description: 'List of repositories'}}
679+
}
680+
},
681+
'/repos/{owner}/{repo}/issues': {
682+
get: {
683+
operationId: 'issues/listForRepo',
684+
summary: 'List repository issues',
685+
parameters: [
686+
{name: 'owner', in: 'path', required: true, schema: {type: 'string'}},
687+
{name: 'repo', in: 'path', required: true, schema: {type: 'string'}}
688+
],
689+
responses: {'200': {description: 'List of issues'}}
690+
}
691+
},
692+
'/orgs/{org}/members': {
693+
get: {
694+
operationId: 'orgs/listMembers',
695+
summary: 'List organization members',
696+
parameters: [
697+
{name: 'org', in: 'path', required: true, schema: {type: 'string'}}
698+
],
699+
responses: {'200': {description: 'List of members'}}
700+
}
701+
}
702+
}
703+
};
704+
705+
// Mock fetch response
706+
(global.fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue({
707+
ok: true,
708+
json: async () => openapiSpecWithResources,
709+
} as Response);
710+
711+
// Discover API with only repos resources (first segment matching)
712+
const apiSpec = await discovery.discoverApi(
713+
'https://api.github.com/openapi.json',
714+
undefined,
715+
['repos']
716+
);
717+
718+
// Should have 2 tools starting with /repos
719+
expect(apiSpec.tools.length).toBe(2);
720+
expect(apiSpec.tools.every(tool => tool.path.toLowerCase().startsWith('/repos'))).toBe(true);
721+
});
722+
723+
test('discover api with multiple include resources', async () => {
724+
// Mock OpenAPI spec with multiple resource paths
725+
const openapiSpecWithResources = {
726+
openapi: '3.0.0',
727+
info: {title: 'GitHub API', version: '3.0'},
728+
servers: [{url: 'https://api.github.com'}],
729+
paths: {
730+
'/repos/{owner}/{repo}': {
731+
get: {
732+
operationId: 'repos/get',
733+
summary: 'Get a repository',
734+
parameters: [
735+
{name: 'owner', in: 'path', required: true, schema: {type: 'string'}},
736+
{name: 'repo', in: 'path', required: true, schema: {type: 'string'}}
737+
],
738+
responses: {'200': {description: 'Repository details'}}
739+
}
740+
},
741+
'/user/repos': {
742+
get: {
743+
operationId: 'repos/listForAuthenticatedUser',
744+
summary: 'List user repositories',
745+
responses: {'200': {description: 'List of repositories'}}
746+
}
747+
},
748+
'/repos/{owner}/{repo}/issues': {
749+
get: {
750+
operationId: 'issues/listForRepo',
751+
summary: 'List repository issues',
752+
parameters: [
753+
{name: 'owner', in: 'path', required: true, schema: {type: 'string'}},
754+
{name: 'repo', in: 'path', required: true, schema: {type: 'string'}}
755+
],
756+
responses: {'200': {description: 'List of issues'}}
757+
}
758+
},
759+
'/orgs/{org}/members': {
760+
get: {
761+
operationId: 'orgs/listMembers',
762+
summary: 'List organization members',
763+
parameters: [
764+
{name: 'org', in: 'path', required: true, schema: {type: 'string'}}
765+
],
766+
responses: {'200': {description: 'List of members'}}
767+
}
768+
}
769+
}
770+
};
771+
772+
// Mock fetch response
773+
(global.fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue({
774+
ok: true,
775+
json: async () => openapiSpecWithResources,
776+
} as Response);
777+
778+
// Discover API with repos and orgs resources (first segment matching)
779+
const apiSpec = await discovery.discoverApi(
780+
'https://api.github.com/openapi.json',
781+
undefined,
782+
['repos', 'orgs']
783+
);
784+
785+
// Should have 3 tools (repos, repos/issues, orgs)
786+
expect(apiSpec.tools.length).toBe(3);
787+
});
788+
789+
test('discover api without include resources returns all tools', async () => {
790+
// Mock OpenAPI spec with multiple resource paths
791+
const openapiSpecWithResources = {
792+
openapi: '3.0.0',
793+
info: {title: 'GitHub API', version: '3.0'},
794+
servers: [{url: 'https://api.github.com'}],
795+
paths: {
796+
'/repos/{owner}/{repo}': {
797+
get: {
798+
operationId: 'repos/get',
799+
summary: 'Get a repository',
800+
parameters: [
801+
{name: 'owner', in: 'path', required: true, schema: {type: 'string'}},
802+
{name: 'repo', in: 'path', required: true, schema: {type: 'string'}}
803+
],
804+
responses: {'200': {description: 'Repository details'}}
805+
}
806+
},
807+
'/user/repos': {
808+
get: {
809+
operationId: 'repos/listForAuthenticatedUser',
810+
summary: 'List user repositories',
811+
responses: {'200': {description: 'List of repositories'}}
812+
}
813+
},
814+
'/repos/{owner}/{repo}/issues': {
815+
get: {
816+
operationId: 'issues/listForRepo',
817+
summary: 'List repository issues',
818+
parameters: [
819+
{name: 'owner', in: 'path', required: true, schema: {type: 'string'}},
820+
{name: 'repo', in: 'path', required: true, schema: {type: 'string'}}
821+
],
822+
responses: {'200': {description: 'List of issues'}}
823+
}
824+
},'/orgs/{org}/members': {
825+
get: {
826+
operationId: 'orgs/listMembers',
827+
summary: 'List organization members',
828+
parameters: [
829+
{name: 'org', in: 'path', required: true, schema: {type: 'string'}}
830+
],
831+
responses: {'200': {description: 'List of members'}}
832+
}
833+
}
834+
}
835+
};
836+
837+
// Mock fetch response
838+
(global.fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue({
839+
ok: true,
840+
json: async () => openapiSpecWithResources,
841+
} as Response);
842+
843+
// Discover API without filtering
844+
const apiSpec = await discovery.discoverApi('https://api.github.com/openapi.json');
845+
846+
// Should have all 4 tools
847+
expect(apiSpec.tools.length).toBe(4);
848+
});
849+
});
663850
});

0 commit comments

Comments
 (0)