Skip to content

Commit

Permalink
✅ test: add test for the pluginService
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 12, 2023
1 parent 4b2a33c commit 781510a
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 0 deletions.
125 changes: 125 additions & 0 deletions src/services/__tests__/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,128 @@ exports[`PluginService > convertOpenAPIToPluginSchema > can convert OpenAPI v3.1
},
]
`;

exports[`PluginService > getPluginManifest > support OpenAPI manifest > should get plugin manifest 1`] = `
{
"$schema": "../node_modules/@lobehub/chat-plugin-sdk/schema.json",
"api": [
{
"description": "Read Course Segments",
"name": "read_course_segments_course_segments__get",
"parameters": {
"properties": {},
"type": "object",
},
},
{
"description": "Read Problem Set Item",
"name": "read_problem_set_item_problem_set__problem_set_id___question_number__get",
"parameters": {
"properties": {
"problem_set_id": {
"title": "Problem Set Id",
"type": "integer",
},
"question_number": {
"title": "Question Number",
"type": "integer",
},
},
"required": [
"problem_set_id",
"question_number",
],
"type": "object",
},
},
{
"description": "Read Random Problem Set Items",
"name": "read_random_problem_set_items_problem_set_random__problem_set_id___n_items__get",
"parameters": {
"properties": {
"n_items": {
"title": "N Items",
"type": "integer",
},
"problem_set_id": {
"title": "Problem Set Id",
"type": "integer",
},
},
"required": [
"problem_set_id",
"n_items",
],
"type": "object",
},
},
{
"description": "Read Range Of Problem Set Items",
"name": "read_range_of_problem_set_items_problem_set_range__problem_set_id___start___end__get",
"parameters": {
"properties": {
"end": {
"title": "End",
"type": "integer",
},
"problem_set_id": {
"title": "Problem Set Id",
"type": "integer",
},
"start": {
"title": "Start",
"type": "integer",
},
},
"required": [
"problem_set_id",
"start",
"end",
],
"type": "object",
},
},
{
"description": "Read User Id",
"name": "read_user_id_user__get",
"parameters": {
"properties": {},
"type": "object",
},
},
],
"author": "LobeHub",
"createAt": "2023-08-12",
"homepage": "https://github.com/lobehub/chat-plugin-realtime-weather",
"identifier": "realtime-weather",
"meta": {
"avatar": "🌈",
"description": "Get realtime weather information",
"tags": [
"weather",
"realtime",
],
"title": "Realtime Weather",
},
"openapi": "http://fake-url.com/openapiUrl.json",
"settings": {
"properties": {
"HTTPBearer": {
"description": "HTTPBearer Bearer token",
"format": "password",
"title": "HTTPBearer",
"type": "string",
},
},
"required": [
"HTTPBearer",
],
"type": "object",
},
"ui": {
"height": 310,
"url": "https://realtime-weather.chat-plugin.lobehub.com/iframe",
},
"version": "1",
}
`;
151 changes: 151 additions & 0 deletions src/services/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,83 @@ describe('PluginService', () => {
expect(e).toEqual(new TypeError('fetchError'));
}
});

describe('support OpenAPI manifest', () => {
it('should get plugin manifest', async () => {
const manifestUrl = 'http://fake-url.com/manifest.json';
const openapiUrl = 'http://fake-url.com/openapiUrl.json';

const fakeManifest = {
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
api: [],
openapi: openapiUrl,
author: 'LobeHub',
createAt: '2023-08-12',
homepage: 'https://github.com/lobehub/chat-plugin-realtime-weather',
identifier: 'realtime-weather',
meta: {
avatar: '🌈',
tags: ['weather', 'realtime'],
title: 'Realtime Weather',
description: 'Get realtime weather information',
},
ui: {
url: 'https://realtime-weather.chat-plugin.lobehub.com/iframe',
height: 310,
},
version: '1',
};

global.fetch = vi.fn((url) =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(url === openapiUrl ? openAPIV3 : fakeManifest),
}),
) as any;

const manifest = await pluginService.getPluginManifest(manifestUrl);

expect(manifest).toMatchSnapshot();
});

it('should return error on openAPIInvalid', async () => {
const openapiUrl = 'http://fake-url.com/openapiUrl.json';
const manifestUrl = 'http://fake-url.com/manifest.json';
const fakeManifest = {
$schema: '../node_modules/@lobehub/chat-plugin-sdk/schema.json',
api: [],
openapi: openapiUrl,
author: 'LobeHub',
createAt: '2023-08-12',
homepage: 'https://github.com/lobehub/chat-plugin-realtime-weather',
identifier: 'realtime-weather',
meta: {
avatar: '🌈',
tags: ['weather', 'realtime'],
title: 'Realtime Weather',
description: 'Get realtime weather information',
},
ui: {
url: 'https://realtime-weather.chat-plugin.lobehub.com/iframe',
height: 310,
},
version: '1',
};

global.fetch = vi.fn((url) =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(url === openapiUrl ? [] : fakeManifest),
}),
) as any;

try {
await pluginService.getPluginManifest(manifestUrl);
} catch (e) {
expect(e).toEqual(new TypeError('openAPIInvalid'));
}
});
});
});

describe('installPlugin', () => {
Expand Down Expand Up @@ -395,5 +472,79 @@ describe('PluginService', () => {
type: 'object',
});
});

it('can convert OpenAPI Basic Auth to settings', async () => {
// 假设的 OpenAPI 配置示例,需要根据实际情况调整
const OpenAPI_Basic_Auth = {
components: {
securitySchemes: { basicAuth: { type: 'http', scheme: 'basic' } },
},
};

const plugins = await pluginService.convertAuthToSettingsSchema(OpenAPI_Basic_Auth);

expect(plugins).toEqual({
properties: {
basicAuth: {
format: 'password',
description: 'Basic authentication credentials',
type: 'string',
},
},
type: 'object',
required: ['basicAuth'],
});
});

it('can convert OpenAPI OAuth2 to settings', async () => {
const OpenAPI_OAuth2 = {
components: { securitySchemes: { oauth2: { type: 'oauth2' } } },
};

const plugins = await pluginService.convertAuthToSettingsSchema(OpenAPI_OAuth2);

expect(plugins).toEqual({
properties: {
oauth2_clientId: {
description: 'Client ID for OAuth2',
type: 'string',
},
oauth2_clientSecret: {
description: 'Client Secret for OAuth2',
format: 'password',
type: 'string',
},
oauth2_accessToken: {
description: 'Access token for OAuth2',
format: 'password',
type: 'string',
},
},
type: 'object',
required: ['oauth2_clientId', 'oauth2_clientSecret', 'oauth2_accessToken'],
});
});

it('should handle cases where securitySchemes is undefined', async () => {
const openApiWithoutSecuritySchemes = {};
const plugins = await pluginService.convertAuthToSettingsSchema(
openApiWithoutSecuritySchemes,
);

expect(plugins).toEqual({
properties: {},
type: 'object',
});
});

it('should handle cases where components is undefined', async () => {
const openApiWithoutComponents = { paths: {} };
const plugins = await pluginService.convertAuthToSettingsSchema(openApiWithoutComponents);

expect(plugins).toEqual({
properties: {},
type: 'object',
});
});
});
});

0 comments on commit 781510a

Please sign in to comment.