-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters