feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
160
frontend/packages/arch/bot-http/__tests__/api-error.test.ts
Normal file
160
frontend/packages/arch/bot-http/__tests__/api-error.test.ts
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { type AxiosError, AxiosHeaders } from 'axios';
|
||||
import { logger } from '@coze-arch/logger';
|
||||
|
||||
import {
|
||||
reportHttpError,
|
||||
ReportEventNames,
|
||||
ApiError,
|
||||
isApiError,
|
||||
} from '../src/api-error';
|
||||
|
||||
vi.mock('@coze-arch/logger', () => ({
|
||||
logger: {
|
||||
info: vi.fn(),
|
||||
persist: {
|
||||
error: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
describe('reportHttpError', () => {
|
||||
const error: AxiosError = {
|
||||
response: {
|
||||
data: {
|
||||
code: '500',
|
||||
msg: 'Internal Server Error',
|
||||
},
|
||||
status: 500,
|
||||
statusText: '',
|
||||
config: {
|
||||
headers: new AxiosHeaders(),
|
||||
},
|
||||
headers: {
|
||||
'x-tt-logid': '1234567890',
|
||||
},
|
||||
},
|
||||
config: {
|
||||
method: 'GET',
|
||||
headers: new AxiosHeaders(),
|
||||
url: '/users',
|
||||
},
|
||||
message: 'Request failed with status code 500',
|
||||
name: 'AxiosError',
|
||||
isAxiosError: true,
|
||||
toJSON: () => ({}),
|
||||
};
|
||||
it('if no response data, should report http error', () => {
|
||||
const eventName = ReportEventNames.ApiError;
|
||||
const noResponseError: AxiosError = {
|
||||
response: {
|
||||
data: {
|
||||
code: '',
|
||||
},
|
||||
status: 500,
|
||||
statusText: '',
|
||||
config: {
|
||||
headers: new AxiosHeaders(),
|
||||
},
|
||||
headers: {
|
||||
'x-tt-logid': '1234567890',
|
||||
},
|
||||
},
|
||||
config: {
|
||||
method: 'GET',
|
||||
headers: new AxiosHeaders(),
|
||||
url: '/users',
|
||||
},
|
||||
message: 'Request failed with status code 500',
|
||||
name: 'AxiosError',
|
||||
isAxiosError: true,
|
||||
toJSON: () => ({}),
|
||||
};
|
||||
|
||||
reportHttpError(eventName, noResponseError);
|
||||
|
||||
expect(logger.persist.error).toBeCalledWith({
|
||||
eventName,
|
||||
error: noResponseError,
|
||||
meta: {
|
||||
message: error.message,
|
||||
name: error.name,
|
||||
httpStatusCode: '500',
|
||||
httpMethod: 'GET',
|
||||
urlPath: '/users',
|
||||
logId: '1234567890',
|
||||
customErrorCode: '',
|
||||
customErrorMsg: '',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should report http error', () => {
|
||||
const eventName = ReportEventNames.ApiError;
|
||||
|
||||
reportHttpError(eventName, error);
|
||||
|
||||
expect(logger.persist.error).toHaveBeenCalledWith({
|
||||
eventName,
|
||||
error,
|
||||
meta: {
|
||||
message: error.message,
|
||||
name: error.name,
|
||||
httpStatusCode: '500',
|
||||
httpMethod: 'GET',
|
||||
urlPath: '/users',
|
||||
logId: '1234567890',
|
||||
customErrorCode: '500',
|
||||
customErrorMsg: 'Internal Server Error',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle error when reporting http catch', () => {
|
||||
const eventName = ReportEventNames.ApiError;
|
||||
|
||||
(logger.persist.error as any).mockImplementation(() => {
|
||||
throw new Error('Failed to persist error');
|
||||
});
|
||||
|
||||
expect(() => {
|
||||
reportHttpError(eventName, error);
|
||||
}).toThrowError('Failed to persist error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isApiError', () => {
|
||||
it('should return true if error is an instance of ApiError', () => {
|
||||
const error = new ApiError('500', 'Internal Server Error', {
|
||||
data: {},
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
headers: new AxiosHeaders(),
|
||||
config: {
|
||||
headers: new AxiosHeaders(),
|
||||
},
|
||||
});
|
||||
const result = isApiError(error);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if error is not an instance of ApiError', () => {
|
||||
const error = new Error('OtherError');
|
||||
const result = isApiError(error);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
249
frontend/packages/arch/bot-http/__tests__/axios.test.ts
Normal file
249
frontend/packages/arch/bot-http/__tests__/axios.test.ts
Normal file
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
import { AxiosError, isAxiosError } from 'axios';
|
||||
import { redirect } from '@coze-arch/web-context';
|
||||
|
||||
import { emitAPIErrorEvent } from '../src/eventbus';
|
||||
import { axiosInstance } from '../src/axios'; // your import path
|
||||
import { ApiError, reportHttpError, ReportEventNames } from '../src/api-error';
|
||||
|
||||
// This sets the mock adapter on the default instance
|
||||
const mock = new MockAdapter(axiosInstance);
|
||||
|
||||
vi.mock('@coze-arch/logger', () => ({
|
||||
logger: {
|
||||
info: vi.fn(),
|
||||
persist: {
|
||||
error: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../src/eventbus', () => ({
|
||||
emitAPIErrorEvent: vi.fn(),
|
||||
APIErrorEvent: {
|
||||
UNAUTHORIZED: 'unauthorized',
|
||||
COUNTRY_RESTRICTED: 'countryRestricted',
|
||||
COZE_TOKEN_INSUFFICIENT: 'cozeTokenInsufficient',
|
||||
},
|
||||
}));
|
||||
vi.mock('../src/api-error', async () => {
|
||||
const actual = (await vi.importActual('../src/api-error')) as any;
|
||||
|
||||
return {
|
||||
...actual,
|
||||
reportHttpError: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('@coze-arch/web-context', () => ({
|
||||
redirect: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('axiosInstance', () => {
|
||||
beforeEach(() => {
|
||||
mock.reset();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
it('should fetch users', async () => {
|
||||
// Mock any GET request to /users
|
||||
// arguments for reply are (status, data, headers)
|
||||
mock.onGet('/users').reply(200, {
|
||||
code: 0,
|
||||
data: { users: [{ id: 1, name: 'John Smith' }] },
|
||||
});
|
||||
|
||||
const response = await axiosInstance.get('/users');
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.data.data.users[0].id).toBe(1);
|
||||
});
|
||||
|
||||
it('should throw api errors if code not equal to zero', async () => {
|
||||
mock.onGet('/users').reply(200, {
|
||||
code: 1,
|
||||
msg: 'fake error',
|
||||
});
|
||||
|
||||
await expect(() => axiosInstance.get('/users')).rejects.toThrowError(
|
||||
ApiError,
|
||||
);
|
||||
expect(reportHttpError).toBeCalledWith(
|
||||
ReportEventNames.ApiError,
|
||||
expect.any(ApiError),
|
||||
);
|
||||
});
|
||||
|
||||
it('should emit special events when not login', async () => {
|
||||
mock.onGet('/users').reply(200, {
|
||||
// 700012006 => not login
|
||||
code: 700012006,
|
||||
msg: 'fake error',
|
||||
});
|
||||
|
||||
await expect(axiosInstance.get('/users')).rejects.toThrow(ApiError);
|
||||
expect(emitAPIErrorEvent).toBeCalledWith(
|
||||
'unauthorized',
|
||||
expect.any(ApiError),
|
||||
);
|
||||
expect(reportHttpError).toBeCalledWith(
|
||||
ReportEventNames.ApiError,
|
||||
expect.any(ApiError),
|
||||
);
|
||||
|
||||
mock.onGet('/users2').reply(200, {
|
||||
// 700012015 => COUNTRY_RESTRICTED
|
||||
code: 700012015,
|
||||
msg: 'fake error',
|
||||
});
|
||||
|
||||
await expect(() => axiosInstance.get('/users2')).rejects.toThrowError(
|
||||
ApiError,
|
||||
);
|
||||
expect(emitAPIErrorEvent).toBeCalledWith(
|
||||
'countryRestricted',
|
||||
expect.any(ApiError),
|
||||
);
|
||||
expect(reportHttpError).toBeCalledWith(
|
||||
ReportEventNames.ApiError,
|
||||
expect.any(ApiError),
|
||||
);
|
||||
|
||||
mock.onGet('/users4').reply(200, {
|
||||
// 702082020 => COZE_TOKEN_INSUFFICIENT
|
||||
code: 702082020,
|
||||
msg: 'fake error',
|
||||
});
|
||||
|
||||
await expect(axiosInstance.get('/users4')).rejects.toThrow(ApiError);
|
||||
expect(emitAPIErrorEvent).toBeCalledWith(
|
||||
'cozeTokenInsufficient',
|
||||
expect.any(ApiError),
|
||||
);
|
||||
expect(reportHttpError).toBeCalledWith(
|
||||
ReportEventNames.ApiError,
|
||||
expect.any(ApiError),
|
||||
);
|
||||
|
||||
mock.onGet('/users5').reply(200, {
|
||||
// 702095072 => COZE_TOKEN_INSUFFICIENT
|
||||
code: 702095072,
|
||||
msg: 'fake error',
|
||||
});
|
||||
|
||||
await expect(axiosInstance.get('/users5')).rejects.toThrow(ApiError);
|
||||
expect(emitAPIErrorEvent).toBeCalledWith(
|
||||
'cozeTokenInsufficient',
|
||||
expect.any(ApiError),
|
||||
);
|
||||
expect(reportHttpError).toBeCalledWith(
|
||||
ReportEventNames.ApiError,
|
||||
expect.any(ApiError),
|
||||
);
|
||||
});
|
||||
|
||||
it('should logger error when network error', async () => {
|
||||
mock.onGet('/users3').networkError();
|
||||
try {
|
||||
await expect(() => axiosInstance.get('/users3')).rejects.toThrow(Error);
|
||||
} catch (error) {
|
||||
expect(isAxiosError(error)).toBe(true);
|
||||
expect(reportHttpError).toBeCalledWith(
|
||||
ReportEventNames.NetworkError,
|
||||
expect.any(AxiosError),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('should handle unauthorized response', async () => {
|
||||
mock.onGet('/users').reply(401, {
|
||||
code: 401,
|
||||
data: {
|
||||
redirect_uri: '/login',
|
||||
},
|
||||
message: 'Unauthorized',
|
||||
});
|
||||
|
||||
await expect(() => axiosInstance.get('/users')).rejects.toThrowError(Error);
|
||||
expect(reportHttpError).toBeCalledWith(
|
||||
ReportEventNames.NetworkError,
|
||||
expect.any(Error),
|
||||
);
|
||||
|
||||
expect(redirect).toBeCalledWith('/login');
|
||||
});
|
||||
|
||||
it('should set content-type header for post request', async () => {
|
||||
mock.onPost('/api/mock').reply(200, {
|
||||
code: 0,
|
||||
data: {},
|
||||
});
|
||||
const response = await axiosInstance.post('/api/mock');
|
||||
expect(response.config.headers['Content-Type']).toBe('application/json');
|
||||
expect(response.config.data).toBe(JSON.stringify({}));
|
||||
});
|
||||
it('should set content-type header for get request', async () => {
|
||||
mock.onGet('/api/mock').reply(200, {
|
||||
code: 0,
|
||||
data: {},
|
||||
});
|
||||
const response = await axiosInstance.request({
|
||||
url: '/api/mock',
|
||||
method: 'GET',
|
||||
});
|
||||
expect(response.config.headers['Content-Type']).toBe('application/json');
|
||||
});
|
||||
it("won't override exist data", async () => {
|
||||
mock.onPost('/api/mock').reply(200, {
|
||||
code: 0,
|
||||
data: {},
|
||||
});
|
||||
const response = await axiosInstance.post('/api/mock', { data: '1' });
|
||||
expect(response.config.headers['Content-Type']).toBe('application/json');
|
||||
expect(response.config.data).toBe(JSON.stringify({ data: '1' }));
|
||||
});
|
||||
it("won't override exist content-type header", async () => {
|
||||
mock.onPost('/api/mock').reply(200, {
|
||||
code: 0,
|
||||
data: {},
|
||||
});
|
||||
const response = await axiosInstance.post('/api/mock', undefined, {
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
},
|
||||
});
|
||||
expect(response.config.headers['Content-Type']).toBe('text/plain');
|
||||
});
|
||||
it('should set handle object header', async () => {
|
||||
mock.onPost('/api/mock').reply(200, {
|
||||
code: 0,
|
||||
data: {},
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.request.use(config => {
|
||||
// @ts-expect-error just for test
|
||||
config.headers = { 'Content-Type': 'application/json' };
|
||||
return config;
|
||||
});
|
||||
const response = await axiosInstance.request({
|
||||
url: '/api/mock',
|
||||
method: 'POST',
|
||||
});
|
||||
expect(response.config.headers['Content-Type']).toBe('application/json');
|
||||
});
|
||||
});
|
||||
90
frontend/packages/arch/bot-http/__tests__/eventbus.test.ts
Normal file
90
frontend/packages/arch/bot-http/__tests__/eventbus.test.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
APIErrorEvent,
|
||||
clearAPIErrorEvent,
|
||||
emitAPIErrorEvent,
|
||||
handleAPIErrorEvent,
|
||||
removeAPIErrorEvent,
|
||||
startAPIErrorEvent,
|
||||
stopAPIErrorEvent,
|
||||
} from '../src/eventbus';
|
||||
|
||||
const mockEmit = vi.fn();
|
||||
const mockOn = vi.fn();
|
||||
const mockOff = vi.fn();
|
||||
const mockStart = vi.fn();
|
||||
const mockStop = vi.fn();
|
||||
const mockClear = vi.fn();
|
||||
|
||||
vi.mock('@coze-arch/web-context', () => ({
|
||||
GlobalEventBus: class MockGlobalEventBus {
|
||||
static create() {
|
||||
return new MockGlobalEventBus();
|
||||
}
|
||||
emit() {
|
||||
mockEmit();
|
||||
}
|
||||
on() {
|
||||
mockOn();
|
||||
}
|
||||
off() {
|
||||
mockOff();
|
||||
}
|
||||
start() {
|
||||
mockStart();
|
||||
}
|
||||
stop() {
|
||||
mockStop();
|
||||
}
|
||||
clear() {
|
||||
mockClear();
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
describe('eventbus', () => {
|
||||
test('emitAPIErrorEvent', () => {
|
||||
emitAPIErrorEvent(APIErrorEvent.COUNTRY_RESTRICTED);
|
||||
expect(mockEmit).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('handleAPIErrorEvent', () => {
|
||||
handleAPIErrorEvent(APIErrorEvent.COUNTRY_RESTRICTED, vi.fn());
|
||||
expect(mockOn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('removeAPIErrorEvent', () => {
|
||||
removeAPIErrorEvent(APIErrorEvent.COUNTRY_RESTRICTED, vi.fn());
|
||||
expect(mockOff).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('stopAPIErrorEvent', () => {
|
||||
stopAPIErrorEvent();
|
||||
expect(mockStop).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('startAPIErrorEvent', () => {
|
||||
startAPIErrorEvent();
|
||||
expect(mockStart).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('clearAPIErrorEvent', () => {
|
||||
clearAPIErrorEvent();
|
||||
expect(mockClear).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import MockAdapter from 'axios-mock-adapter';
|
||||
|
||||
vi.mock('@coze-arch/logger', () => ({
|
||||
logger: {
|
||||
info: vi.fn(),
|
||||
persist: {
|
||||
error: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../src/eventbus', () => ({
|
||||
emitAPIErrorEvent: vi.fn(),
|
||||
APIErrorEvent: {
|
||||
UNAUTHORIZED: 'unauthorized',
|
||||
COUNTRY_RESTRICTED: 'countryRestricted',
|
||||
COZE_TOKEN_INSUFFICIENT: 'cozeTokenInsufficient',
|
||||
},
|
||||
}));
|
||||
vi.mock('../src/api-error', async () => {
|
||||
const actual = (await vi.importActual('../src/api-error')) as any;
|
||||
|
||||
return {
|
||||
...actual,
|
||||
reportHttpError: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('@coze-arch/web-context', () => ({
|
||||
redirect: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('globalRequestInterceptor', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should run intercept logic', async () => {
|
||||
const { addGlobalRequestInterceptor, axiosInstance } =
|
||||
await vi.importActual('../src/axios');
|
||||
// This sets the mock adapter on the default instance
|
||||
const mock = new MockAdapter(axiosInstance);
|
||||
|
||||
addGlobalRequestInterceptor(config => {
|
||||
config.headers.set('x-tt-foo', 'bar');
|
||||
return config;
|
||||
});
|
||||
|
||||
mock.onGet('/users').reply(200, {
|
||||
code: 0,
|
||||
data: { users: [{ id: 1, name: 'John Smith' }] },
|
||||
});
|
||||
|
||||
const response = await axiosInstance.get('/users');
|
||||
|
||||
expect(response.config.headers['x-tt-foo']).toEqual('bar');
|
||||
});
|
||||
|
||||
it('run extra interceptor logic', async () => {
|
||||
const { addGlobalResponseInterceptor, axiosInstance } =
|
||||
await vi.importActual('../src/axios');
|
||||
const mock = new MockAdapter(axiosInstance);
|
||||
const removeInterceptor = addGlobalResponseInterceptor(obj => {
|
||||
obj.data.data.oh = 2;
|
||||
return obj;
|
||||
});
|
||||
|
||||
mock.onGet('/oh').reply(200, {
|
||||
code: 0,
|
||||
data: { oh: 1 },
|
||||
});
|
||||
const response = await axiosInstance.get('/oh');
|
||||
expect(response.data.data.oh).toBe(2);
|
||||
|
||||
removeInterceptor();
|
||||
const response2 = await axiosInstance.get('/oh');
|
||||
expect(response2.data.data.oh).toBe(1);
|
||||
});
|
||||
|
||||
it('should support remove interceptors', async () => {
|
||||
const {
|
||||
addGlobalRequestInterceptor,
|
||||
removeGlobalRequestInterceptor,
|
||||
axiosInstance,
|
||||
} = await vi.importActual('../src/axios');
|
||||
// This sets the mock adapter on the default instance
|
||||
|
||||
const mock = new MockAdapter(axiosInstance);
|
||||
const id = addGlobalRequestInterceptor(config => {
|
||||
config.headers.set('x-tt-foo', 'bar');
|
||||
console.log('wfe', 'wefe');
|
||||
return config;
|
||||
});
|
||||
removeGlobalRequestInterceptor(id);
|
||||
|
||||
mock.onGet('/users').reply(200, {
|
||||
code: 0,
|
||||
data: { users: [{ id: 1, name: 'John Smith' }] },
|
||||
});
|
||||
|
||||
const response = await axiosInstance.get('/users');
|
||||
|
||||
expect(response.config.headers['x-tt-foo']).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user