mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-13 10:09:02 +08:00
Feature/add test to nodejs sdk (#31)
This commit is contained in:
parent
f5b2271c8c
commit
3117619ef3
5
sdks/nodejs-client/babel.config.json
Normal file
5
sdks/nodejs-client/babel.config.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"presets": [
|
||||||
|
"@babel/preset-env"
|
||||||
|
]
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
|
||||||
const BASE_URL = 'https://api.dify.ai/v1'
|
export const BASE_URL = 'https://api.dify.ai/v1'
|
||||||
|
|
||||||
const routes = {
|
export const routes = {
|
||||||
application: {
|
application: {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: () => `/parameters`
|
url: () => `/parameters`
|
||||||
|
66
sdks/nodejs-client/index.test.js
Normal file
66
sdks/nodejs-client/index.test.js
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { DifyClient, BASE_URL, routes } from ".";
|
||||||
|
|
||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
|
jest.mock('axios')
|
||||||
|
|
||||||
|
describe('Client', () => {
|
||||||
|
let difyClient
|
||||||
|
beforeEach(() => {
|
||||||
|
difyClient = new DifyClient('test')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('should create a client', () => {
|
||||||
|
expect(difyClient).toBeDefined();
|
||||||
|
})
|
||||||
|
// test updateApiKey
|
||||||
|
test('should update the api key', () => {
|
||||||
|
difyClient.updateApiKey('test2');
|
||||||
|
expect(difyClient.apiKey).toBe('test2');
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Send Requests', () => {
|
||||||
|
let difyClient
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
difyClient = new DifyClient('test')
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.resetAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should make a successful request to the application parameter', async () => {
|
||||||
|
const method = 'GET'
|
||||||
|
const endpoint = routes.application.url
|
||||||
|
const expectedResponse = { data: 'response' }
|
||||||
|
axios.mockResolvedValue(expectedResponse)
|
||||||
|
|
||||||
|
await difyClient.sendRequest(method, endpoint)
|
||||||
|
|
||||||
|
expect(axios).toHaveBeenCalledWith({
|
||||||
|
method,
|
||||||
|
url: `${BASE_URL}${endpoint}`,
|
||||||
|
data: null,
|
||||||
|
params: null,
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${difyClient.apiKey}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
responseType: 'json',
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle errors from the API', async () => {
|
||||||
|
const method = 'GET'
|
||||||
|
const endpoint = '/test-endpoint'
|
||||||
|
const errorMessage = 'Request failed with status code 404'
|
||||||
|
axios.mockRejectedValue(new Error(errorMessage))
|
||||||
|
|
||||||
|
await expect(difyClient.sendRequest(method, endpoint)).rejects.toThrow(
|
||||||
|
errorMessage
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
@ -14,7 +14,21 @@
|
|||||||
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
|
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"test": "jest"
|
||||||
|
},
|
||||||
|
"jest": {
|
||||||
|
"transform": {
|
||||||
|
"^.+\\.[t|j]sx?$": "babel-jest"
|
||||||
|
}
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.3.5"
|
"axios": "^1.3.5"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/core": "^7.21.8",
|
||||||
|
"@babel/preset-env": "^7.21.5",
|
||||||
|
"babel-jest": "^29.5.0",
|
||||||
|
"jest": "^29.5.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user