feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
/*
* 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, expect, it, vi } from 'vitest';
import { renderHook, act } from '@testing-library/react-hooks';
import { OAuthStatus } from '@coze-arch/bot-api/plugin_develop';
import { useAuthForApiTool } from '@/hooks/auth/use-auth-for-api-tool';
vi.mock('@coze-arch/logger', () => ({
logger: {
error: vi.fn(),
},
createLoggerWith: vi.fn(),
}));
vi.mock('@coze-studio/bot-plugin-store', () => ({
usePluginStore: vi.fn().mockReturnValue({
pluginInfo: {
plugin_id: 'plugin_id',
canEdit: !0,
},
}),
}));
vi.mock('@coze-arch/bot-api', () => ({
PluginDevelopApi: {
RevokeAuthToken: vi
.fn()
.mockRejectedValueOnce(new Error('mockRejectedValue'))
.mockResolvedValue(0),
GetOAuthStatus: vi
.fn()
.mockResolvedValueOnce({
is_oauth: !0,
status: OAuthStatus.Authorized,
content: 'content',
})
.mockResolvedValueOnce({
is_oauth: !0,
status: OAuthStatus.Unauthorized,
content: 'content',
})
.mockResolvedValueOnce({
is_oauth: !!0,
})
.mockResolvedValueOnce({
is_oauth: !0,
status: OAuthStatus.Unauthorized,
content: 'content',
}),
},
}));
vi.stubGlobal('open', vi.fn());
describe('useAuthForApiTool', () => {
it('useAuthForApiTool (三方服务 & 已授权 => 取消授权失败 => 取消授权成功) is pass', async () => {
const { result, waitForNextUpdate } = renderHook(() => useAuthForApiTool());
await waitForNextUpdate();
expect(result.current.needAuth).toBe(!0);
expect(result.current.isHasAuth).toBe(!0);
try {
await act(async () => await result.current.doCancelOauth());
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
expect(result.current.isHasAuth).toBe(!0);
await act(async () => await result.current.doCancelOauth());
expect(result.current.isHasAuth).toBe(!!0);
});
it('useAuthForApiTool (非三方服务 & 无需授权) is pass', async () => {
const { result, waitForNextUpdate } = renderHook(() => useAuthForApiTool());
await waitForNextUpdate();
expect(result.current.needAuth).toBe(!!0);
expect(result.current.isHasAuth).toBe(!!0);
});
it('useAuthForApiTool (三方服务 & 未授权 => 去授权) is pass', async () => {
const { result, waitForNextUpdate } = renderHook(() => useAuthForApiTool());
await waitForNextUpdate();
expect(result.current.needAuth).toBe(!0);
expect(result.current.isHasAuth).toBe(!!0);
act(() => result.current.doOauth());
});
});

View File

@@ -0,0 +1,111 @@
/*
* 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 { vi } from 'vitest';
vi.mock('@coze-arch/logger', () => ({
useErrorHandler: vi.fn().mockReturnValue(vi.fn()),
logger: {
error: vi.fn(),
info: vi.fn(),
},
reporter: {
tracer: vi.fn().mockReturnValue(vi.fn()),
event: vi.fn(),
errorEvent: vi.fn(),
},
}));
vi.mock('@coze-studio/bot-plugin-store', () => ({
usePluginStore: vi.fn().mockReturnValue({
pluginInfo: {
plugin_id: 'plugin_id',
canEdit: !0,
},
}),
}));
vi.mock('@coze-arch/i18n', () => ({
I18n: {
t: (key, params = {}) => `Translated: ${key} ${JSON.stringify(params)}`,
},
}));
vi.mock('@coze-arch/bot-tea', () => ({
sendTeaEvent: vi.fn(),
EVENT_NAMES: {
use_mockset_front: 'use_mockset_front',
del_mockset_front: 'del_mockset_front',
},
ParamsTypeDefine: {},
PluginMockDataGenerateMode: {
MANUAL: 0, // 手动创建
RANDOM: 1, // 随机生成
LLM: 2,
},
}));
vi.mock('@coze-arch/bot-hooks', () => ({
SceneType: {
BOT__VIEW__WORKFLOW: 'botViewWorkflow',
/** bot 详情页查看 workflow或新建 workflow 但未发布,点击返回 */
WORKFLOW__BACK__BOT: 'workflowBackBot',
/** bot 详情页创建 workflow在 workflow 发布后返回 */
WORKFLOW_PUBLISHED__BACK__BOT: 'workflowPublishedBackBot',
/** bot 详情页进入 mock data 页面 */
BOT__TO__PLUGIN_MOCK_DATA: 'botToPluginMockData',
/** workflow 详情页进入 mock data 页面 */
WORKFLOW__TO__PLUGIN_MOCK_DATA: 'workflowToPluginMockData',
/** mock set 页进入 mock data 页面 */
PLUGIN_MOCK_SET__TO__PLUGIN_MOCK_DATA: 'pluginMockSetToPluginMockData',
/** bot 详情页进入 knowledge 页面 */
BOT__VIEW__KNOWLEDGE: 'botViewKnowledge',
/** knowledge 页面点击退出返回 bot 详情页(未点击添加) */
KNOWLEDGE__BACK__BOT: 'knowledgeBackBot',
/** knowledge 页面点击返回 bot 详情页,并添加到 bot */
KNOWLEDGE__ADD_TO__BOT: 'knowledgeAddToBot',
},
usePageJumpService: vi.fn().mockReturnValue({
jump: vi.fn(),
}),
}));
vi.mock('@coze-arch/bot-studio-store', () => ({
useSpaceStore: {
getState: vi.fn().mockReturnValue({
getSpaceId: vi.fn().mockReturnValue('spaceId'),
}),
},
}));
vi.mock('@coze-arch/report-events', () => ({
REPORT_EVENTS: {
pluginIdeInitTrace: 'pluginIdeInitTrace',
pluginIdeInit: 'pluginIdeInit',
},
}));
vi.mock('@coze-arch/bot-error', () => ({
CustomError: vi.fn(),
}));
vi.mock('react-router-dom', () => ({
useNavigate: vi.fn().mockReturnValue(vi.fn()),
useParams: vi.fn().mockReturnValue({
space_id: 'space_id',
plugin_id: 'plugin_id',
}),
}));

View File

@@ -0,0 +1,62 @@
/*
* 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, expect, it, vi } from 'vitest';
import { getEnv } from '@/util/get-env';
describe('getEnv function', () => {
it('should return "cn-boe" when not in production', () => {
vi.stubGlobal('IS_PROD', undefined);
// 不设置IS_PROD默认为非生产环境
const env = getEnv();
expect(env).toBe('cn-boe');
});
it('should return "cn-release" when in production, not overseas, and is release version', () => {
vi.stubGlobal('IS_PROD', true); // 设置为生产环境
vi.stubGlobal('IS_OVERSEA', false); // 不是海外
vi.stubGlobal('IS_RELEASE_VERSION', true); // 是发布版本
const env = getEnv();
expect(env).toBe('cn-release');
});
it('should return "cn-inhouse" when in production, not overseas, and is not release version', () => {
vi.stubGlobal('IS_PROD', true); // 设置为生产环境
vi.stubGlobal('IS_OVERSEA', false); // 不是海外
vi.stubGlobal('IS_RELEASE_VERSION', false); // 不是发布版本
const env = getEnv();
expect(env).toBe('cn-inhouse');
});
it('should return "oversea-release" when in production, overseas, and is release version', () => {
vi.stubGlobal('IS_PROD', true); // 设置为生产环境
vi.stubGlobal('IS_OVERSEA', true); // 是海外
vi.stubGlobal('IS_RELEASE_VERSION', true); // 是发布版本
const env = getEnv();
expect(env).toBe('oversea-release');
});
it('should return "oversea-inhouse" when in production, overseas, and is not release version', () => {
vi.stubGlobal('IS_PROD', true); // 设置为生产环境
vi.stubGlobal('IS_OVERSEA', true); // 是海外
vi.stubGlobal('IS_RELEASE_VERSION', false); // 不是发布版本
const env = getEnv();
expect(env).toBe('oversea-inhouse');
});
});