Skip to content

Commit

Permalink
✅ test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 16, 2023
1 parent ab40a72 commit ad2788e
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@vitest/coverage-v8": "latest",
"@vitest/coverage-v8": "0.34.6",
"babel-plugin-antd-style": "^1.0.4",
"clean-pkg-json": "^1",
"commitlint": "^18",
Expand All @@ -115,7 +115,7 @@
"stylelint": "^15",
"ts-json-schema-generator": "^1.4.0",
"typescript": "^5",
"vitest": "latest"
"vitest": "0.34.6"
},
"peerDependencies": {
"react": ">=18",
Expand Down
64 changes: 64 additions & 0 deletions tests/error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
PluginErrorType,
createErrorResponse,
getPluginErrorStatus,
} from '@lobehub/chat-plugin-sdk';
import { describe, expect, it } from 'vitest';

describe('getPluginErrorStatus', () => {
it('should return 404 for not found errors', () => {
expect(getPluginErrorStatus(PluginErrorType.PluginApiNotFound)).toBe(404);
expect(getPluginErrorStatus(PluginErrorType.PluginMetaNotFound)).toBe(404);
expect(getPluginErrorStatus(PluginErrorType.PluginManifestNotFound)).toBe(404);
});

it('should return 490 for invalid plugin-related errors', () => {
expect(getPluginErrorStatus(PluginErrorType.PluginMetaInvalid)).toBe(490);
expect(getPluginErrorStatus(PluginErrorType.PluginMarketIndexInvalid)).toBe(490);
expect(getPluginErrorStatus(PluginErrorType.PluginManifestInvalid)).toBe(490);
});

it('should return 590 for PluginMarketIndexNotFound', () => {
expect(getPluginErrorStatus(PluginErrorType.PluginMarketIndexNotFound)).toBe(590);
});

it('should return 422 for user input related errors', () => {
expect(getPluginErrorStatus(PluginErrorType.PluginApiParamsError)).toBe(422);
expect(getPluginErrorStatus(PluginErrorType.PluginSettingsInvalid)).toBe(422);
});

it('should return the errorType itself if it is a number', () => {
expect(getPluginErrorStatus(400)).toBe(400);
expect(getPluginErrorStatus(500)).toBe(500);
});

it('should return 500 for unknown error types', () => {
expect(getPluginErrorStatus('SomeUnknownError')).toBe(500);
});
});

describe('createErrorResponse', () => {
it('should create an error response with the correct status and body', async () => {
const errorType = PluginErrorType.PluginApiNotFound;
const body = { message: 'Plugin API not found' };
const response = createErrorResponse(errorType, body);
expect(response.status).toBe(404);
expect(await response.text()).toEqual(JSON.stringify({ body, errorType }));
});

it('should handle string error types and use default 500 status', async () => {
const errorType = 'UnknownError';
const body = { message: 'An unknown error occurred' };
const response = createErrorResponse(errorType, body);
expect(response.status).toBe(500);
expect(await response.text()).toEqual(JSON.stringify({ body, errorType }));
});

it('should handle numeric error types and use the number as status', async () => {
const errorType = 400;
const body = { message: 'Bad request' };
const response = createErrorResponse(errorType, body);
expect(response.status).toBe(errorType);
expect(await response.text()).toEqual(JSON.stringify({ body, errorType: 400 }));
});
});
76 changes: 76 additions & 0 deletions tests/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
LOBE_PLUGIN_SETTINGS,
createHeadersWithPluginSettings,
getPluginSettingsFromHeaders,
getPluginSettingsFromRequest,
} from '@lobehub/chat-plugin-sdk';
import { describe, expect, it } from 'vitest';

describe('getPluginSettingsFromRequest', () => {
it('should return undefined if header is not set', () => {
const mockRequest = new Request('https://example.com', { headers: {} });
const settings = getPluginSettingsFromRequest(mockRequest);
expect(settings).toBeUndefined();
});

it('should parse settings if header is valid JSON', () => {
const mockRequest = new Request('https://example.com', {
headers: { [LOBE_PLUGIN_SETTINGS]: JSON.stringify({ key: 'value' }) },
});
const settings = getPluginSettingsFromRequest<{ key: string }>(mockRequest);
expect(settings).toEqual({ key: 'value' });
});

it('should return raw string if header is not valid JSON', () => {
const mockRequest = new Request('https://example.com', {
headers: { [LOBE_PLUGIN_SETTINGS]: 'not json' },
});
const settings = getPluginSettingsFromRequest(mockRequest);
expect(settings).toBe('not json');
});
});

describe('getPluginSettingsFromHeaders', () => {
it('should return undefined if header is not set', () => {
const settings = getPluginSettingsFromHeaders({});
expect(settings).toBeUndefined();
});

it('should parse settings if header is valid JSON', () => {
const settings = getPluginSettingsFromHeaders({
[LOBE_PLUGIN_SETTINGS]: JSON.stringify({ key: 'value' }),
});
expect(settings).toEqual({ key: 'value' });
});

it('should return raw string if header is not valid JSON', () => {
const settings = getPluginSettingsFromHeaders({
[LOBE_PLUGIN_SETTINGS]: 'not json',
});
expect(settings).toBe('not json');
});
});

describe('createHeadersWithPluginSettings', () => {
it('should create headers with JSON stringified settings', () => {
const settings = { key: 'value' };
const headers = createHeadersWithPluginSettings(settings);
expect(headers[LOBE_PLUGIN_SETTINGS]).toBe(JSON.stringify(settings));
});

it('should create headers with string settings', () => {
const settings = 'string setting';
const headers = createHeadersWithPluginSettings(settings);
expect(headers[LOBE_PLUGIN_SETTINGS]).toBe(settings);
});

it('should merge provided headers with settings', () => {
const settings = { key: 'value' };
const customHeaders = { 'Custom-Header': 'custom' };
const headers = createHeadersWithPluginSettings(settings, customHeaders);
expect(headers).toEqual({
'Custom-Header': 'custom',
[LOBE_PLUGIN_SETTINGS]: JSON.stringify(settings),
});
});
});
1 change: 0 additions & 1 deletion tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"skipLibCheck": true,
"strict": true,
"paths": {
"@/*": [".src/*"],
"@lobehub/chat-plugin-sdk": ["./src"],
"@lobehub/chat-plugin-sdk/client": ["./src/client"],
"@lobehub/chat-plugin-sdk/openapi": ["./src/openapi"]
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
},
"types": ["vitest/globals"]
},
"exclude": ["client.ts", "tests"],
"exclude": ["client.ts", "tests", "openapi.ts"],
"include": ["src", "typings", "*.ts"]
}

0 comments on commit ad2788e

Please sign in to comment.