feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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 { useParams } from 'react-router-dom';
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { messageReportEvent } from '@coze-arch/bot-utils';
|
||||
|
||||
import { useMessageReportEvent } from '../../src/bot/use-message-report-event';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@coze-arch/bot-utils', () => ({
|
||||
messageReportEvent: {
|
||||
start: vi.fn(),
|
||||
interrupt: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('react-router-dom', () => ({
|
||||
useParams: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('useMessageReportEvent', () => {
|
||||
const mockBotId = 'test-bot-id';
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(useParams as any).mockReturnValue({ bot_id: mockBotId });
|
||||
});
|
||||
|
||||
it('should start message report event with correct parameters', () => {
|
||||
renderHook(() => useMessageReportEvent());
|
||||
|
||||
expect(messageReportEvent.start).toHaveBeenCalledWith(mockBotId);
|
||||
});
|
||||
|
||||
it('should not start message report event when bot_id is not available', () => {
|
||||
(useParams as any).mockReturnValue({ bot_id: undefined });
|
||||
|
||||
renderHook(() => useMessageReportEvent());
|
||||
|
||||
expect(messageReportEvent.start).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should interrupt message report event on unmount', () => {
|
||||
const { unmount } = renderHook(() => useMessageReportEvent());
|
||||
|
||||
unmount();
|
||||
|
||||
expect(messageReportEvent.interrupt).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should restart message report event when bot_id changes', () => {
|
||||
const { rerender } = renderHook(() => useMessageReportEvent());
|
||||
|
||||
expect(messageReportEvent.start).toHaveBeenCalledTimes(1);
|
||||
expect(messageReportEvent.start).toHaveBeenCalledWith(mockBotId);
|
||||
|
||||
const newBotId = 'new-bot-id';
|
||||
(useParams as any).mockReturnValue({ bot_id: newBotId });
|
||||
|
||||
rerender();
|
||||
|
||||
expect(messageReportEvent.start).toHaveBeenCalledTimes(2);
|
||||
expect(messageReportEvent.start).toHaveBeenCalledWith(newBotId);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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 { describe, it, expect, vi } from 'vitest';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import {
|
||||
userStoreService,
|
||||
type UserInfo,
|
||||
type UserLabel,
|
||||
} from '@coze-studio/user-store';
|
||||
|
||||
import { useUserSenderInfo } from '../../src/bot/use-user-sender-info';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@coze-studio/user-store', () => ({
|
||||
userStoreService: {
|
||||
useUserLabel: vi.fn(),
|
||||
useUserInfo: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('useUserSenderInfo', () => {
|
||||
const mockUserLabel = {
|
||||
id: 'label-1',
|
||||
name: 'Test Label',
|
||||
} as unknown as UserLabel;
|
||||
const mockUserInfo: Partial<UserInfo> = {
|
||||
avatar_url: 'https://example.com/avatar.jpg',
|
||||
name: 'Test User',
|
||||
user_id_str: '12345',
|
||||
app_user_info: {
|
||||
user_unique_name: 'test_user',
|
||||
},
|
||||
};
|
||||
|
||||
it('should return null when userInfo is not available', () => {
|
||||
vi.mocked(userStoreService.useUserLabel).mockReturnValue(mockUserLabel);
|
||||
vi.mocked(userStoreService.useUserInfo).mockReturnValue(null);
|
||||
|
||||
const { result } = renderHook(() => useUserSenderInfo());
|
||||
|
||||
expect(result.current).toBeNull();
|
||||
});
|
||||
|
||||
it('should return formatted user sender info when userInfo is available', () => {
|
||||
vi.mocked(userStoreService.useUserLabel).mockReturnValue(mockUserLabel);
|
||||
vi.mocked(userStoreService.useUserInfo).mockReturnValue(
|
||||
mockUserInfo as UserInfo,
|
||||
);
|
||||
|
||||
const { result } = renderHook(() => useUserSenderInfo());
|
||||
|
||||
expect(result.current).toEqual({
|
||||
url: mockUserInfo.avatar_url,
|
||||
nickname: mockUserInfo.name,
|
||||
id: mockUserInfo.user_id_str,
|
||||
userUniqueName: mockUserInfo.app_user_info?.user_unique_name,
|
||||
userLabel: mockUserLabel,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle missing optional fields', () => {
|
||||
const partialUserInfo: Partial<UserInfo> = {
|
||||
user_id_str: '12345',
|
||||
app_user_info: {},
|
||||
};
|
||||
|
||||
vi.mocked(userStoreService.useUserLabel).mockReturnValue(mockUserLabel);
|
||||
vi.mocked(userStoreService.useUserInfo).mockReturnValue(
|
||||
partialUserInfo as UserInfo,
|
||||
);
|
||||
|
||||
const { result } = renderHook(() => useUserSenderInfo());
|
||||
|
||||
expect(result.current).toEqual({
|
||||
url: '',
|
||||
nickname: '',
|
||||
id: partialUserInfo.user_id_str,
|
||||
userUniqueName: '',
|
||||
userLabel: mockUserLabel,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user