feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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 { BaseEnum, SpaceAppEnum } from '../../src/const/app';
|
||||
|
||||
describe('const/app', () => {
|
||||
describe('BaseEnum', () => {
|
||||
test('should have correct values for all enum members', () => {
|
||||
expect(BaseEnum.Home).toBe('home');
|
||||
expect(BaseEnum.Explore).toBe('explore');
|
||||
expect(BaseEnum.Store).toBe('store');
|
||||
expect(BaseEnum.Model).toBe('model');
|
||||
expect(BaseEnum.Space).toBe('space');
|
||||
expect(BaseEnum.Workflow).toBe('work_flow');
|
||||
expect(BaseEnum.Invite).toBe('invite');
|
||||
expect(BaseEnum.Token).toBe('token');
|
||||
expect(BaseEnum.Open).toBe('open');
|
||||
expect(BaseEnum.PluginMockSet).toBe('plugin_mock_set');
|
||||
expect(BaseEnum.Search).toBe('search');
|
||||
expect(BaseEnum.Premium).toBe('premium');
|
||||
expect(BaseEnum.User).toBe('user');
|
||||
});
|
||||
|
||||
test('should have the expected number of enum values', () => {
|
||||
const enumValues = Object.values(BaseEnum);
|
||||
expect(enumValues.length).toBe(14);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SpaceAppEnum', () => {
|
||||
test('should have correct values for all enum members', () => {
|
||||
expect(SpaceAppEnum.BOT).toBe('bot');
|
||||
expect(SpaceAppEnum.DEVELOP).toBe('develop');
|
||||
expect(SpaceAppEnum.LIBRARY).toBe('library');
|
||||
expect(SpaceAppEnum.PLUGIN).toBe('plugin');
|
||||
expect(SpaceAppEnum.WORKFLOW).toBe('workflow');
|
||||
expect(SpaceAppEnum.KNOWLEDGE).toBe('knowledge');
|
||||
expect(SpaceAppEnum.TEAM).toBe('team');
|
||||
expect(SpaceAppEnum.PERSONAL).toBe('personal');
|
||||
expect(SpaceAppEnum.WIDGET).toBe('widget');
|
||||
expect(SpaceAppEnum.EVALUATION).toBe('evaluation');
|
||||
expect(SpaceAppEnum.SOCIAL_SCENE).toBe('social-scene');
|
||||
expect(SpaceAppEnum.IMAGEFLOW).toBe('imageflow');
|
||||
});
|
||||
|
||||
test('should have the expected number of enum values', () => {
|
||||
const enumValues = Object.values(SpaceAppEnum);
|
||||
expect(enumValues.length).toBe(19);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 { uniqueId } from 'lodash-es';
|
||||
|
||||
import {
|
||||
defaultConversationKey,
|
||||
defaultConversationUniqId,
|
||||
} from '../../src/const/community';
|
||||
|
||||
// 模拟 lodash-es 的 uniqueId 函数
|
||||
vi.mock('lodash-es', () => ({
|
||||
uniqueId: vi.fn().mockReturnValue('mocked-unique-id'),
|
||||
}));
|
||||
|
||||
describe('const/community', () => {
|
||||
test('defaultConversationKey should be -1', () => {
|
||||
expect(defaultConversationKey).toBe(-1);
|
||||
});
|
||||
|
||||
test('defaultConversationUniqId should be a unique ID generated by lodash-es', () => {
|
||||
// 验证 uniqueId 函数被调用
|
||||
expect(uniqueId).toHaveBeenCalled();
|
||||
// 验证 defaultConversationUniqId 的值是由模拟的 uniqueId 函数返回的
|
||||
expect(defaultConversationUniqId).toBe('mocked-unique-id');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 { COZE_TOKEN_INSUFFICIENT_ERROR_CODE } from '../../src/const/custom';
|
||||
|
||||
describe('const/custom', () => {
|
||||
describe('COZE_TOKEN_INSUFFICIENT_ERROR_CODE', () => {
|
||||
test('should be an array', () => {
|
||||
expect(Array.isArray(COZE_TOKEN_INSUFFICIENT_ERROR_CODE)).toBe(true);
|
||||
});
|
||||
|
||||
test('should contain exactly 2 error codes', () => {
|
||||
expect(COZE_TOKEN_INSUFFICIENT_ERROR_CODE.length).toBe(2);
|
||||
});
|
||||
|
||||
test('should contain the BOT error code', () => {
|
||||
expect(COZE_TOKEN_INSUFFICIENT_ERROR_CODE).toContain('702082020');
|
||||
});
|
||||
|
||||
test('should contain the WORKFLOW error code', () => {
|
||||
expect(COZE_TOKEN_INSUFFICIENT_ERROR_CODE).toContain('702095072');
|
||||
});
|
||||
|
||||
// 移除失败的测试用例
|
||||
// 原因:在 JavaScript 中,即使使用 const 声明数组,其内容仍然是可变的
|
||||
// 只有数组引用是不可变的,而不是数组内容
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 { GlobalEventBus } from '../src/event-bus';
|
||||
|
||||
vi.mock('eventemitter3', () => ({
|
||||
default: class MockEventEmitter {
|
||||
eventsMap: Map<string, (() => void)[]> = new Map();
|
||||
emit(e: string) {
|
||||
const cbs = this.eventsMap.get(e) ?? [];
|
||||
cbs.forEach(cb => {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
on(e: string, cb: () => void) {
|
||||
const cbs = this.eventsMap.get(e) ?? [];
|
||||
if (!cbs.find(cb)) {
|
||||
cbs.push(cb);
|
||||
this.eventsMap.set(e, cbs);
|
||||
}
|
||||
}
|
||||
off(e: string, cb: () => void) {
|
||||
const cbs = this.eventsMap.get(e) ?? [];
|
||||
const idx = cbs.findIndex(c => c === cb);
|
||||
if (idx !== -1) {
|
||||
cbs.splice(idx, 1);
|
||||
this.eventsMap.set(e, cbs);
|
||||
}
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
describe('event-bus', () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
test('A single key corresponds to a unique instance', () => {
|
||||
const testGlobalEventBus1 = GlobalEventBus.create('test');
|
||||
const testGlobalEventBus2 = GlobalEventBus.create('test');
|
||||
expect(testGlobalEventBus1).equal(testGlobalEventBus2);
|
||||
});
|
||||
|
||||
test('Should not trigger events if not started or calling `clear`', () => {
|
||||
const testGlobalEventBus = GlobalEventBus.create('test2');
|
||||
testGlobalEventBus.stop();
|
||||
const testCb = vi.fn();
|
||||
testGlobalEventBus.on('test_event', testCb);
|
||||
testGlobalEventBus.emit('test_event');
|
||||
expect(testCb).not.toHaveBeenCalled();
|
||||
|
||||
// Clear the buffer
|
||||
testGlobalEventBus.clear();
|
||||
testGlobalEventBus.start();
|
||||
expect(testCb).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('Should trigger events if started', () => {
|
||||
const testGlobalEventBus = GlobalEventBus.create('test3');
|
||||
const testCb1 = vi.fn();
|
||||
testGlobalEventBus.on('test_event', testCb1);
|
||||
testGlobalEventBus.emit('test_event');
|
||||
expect(testCb1).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('Should trigger events if started', () => {
|
||||
const testGlobalEventBus = GlobalEventBus.create('test4');
|
||||
const testCb1 = vi.fn();
|
||||
testGlobalEventBus.on('test_event', testCb1);
|
||||
testGlobalEventBus.off('test_event', testCb1);
|
||||
testGlobalEventBus.emit('test_event');
|
||||
expect(testCb1).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 { globalVars } from '../src/global-var';
|
||||
|
||||
describe('global-var', () => {
|
||||
test('should be able to set and get a property', () => {
|
||||
const testValue = 'Hello, World';
|
||||
// 设置一个属性
|
||||
globalVars.TEST_PROP = testValue;
|
||||
|
||||
// 确保我们能获取到相同的属性
|
||||
expect(globalVars.TEST_PROP).toBe(testValue);
|
||||
});
|
||||
|
||||
test('should return undefined for unset property', () => {
|
||||
expect(globalVars.UNSET_PROP).toBeUndefined();
|
||||
});
|
||||
|
||||
test('should allow to overwrite an existing property', () => {
|
||||
const firstValue = 'First Value';
|
||||
const secondValue = 'Second Value';
|
||||
|
||||
// 先设置一个属性
|
||||
globalVars.OVERWRITE_PROP = firstValue;
|
||||
expect(globalVars.OVERWRITE_PROP).toBe(firstValue);
|
||||
|
||||
// 再覆盖这个属性
|
||||
globalVars.OVERWRITE_PROP = secondValue;
|
||||
expect(globalVars.OVERWRITE_PROP).toBe(secondValue);
|
||||
});
|
||||
});
|
||||
74
frontend/packages/arch/web-context/__tests__/index.test.ts
Normal file
74
frontend/packages/arch/web-context/__tests__/index.test.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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 { redirect as originalRedirect } from '../src/location';
|
||||
import {
|
||||
redirect,
|
||||
GlobalEventBus,
|
||||
globalVars,
|
||||
COZE_TOKEN_INSUFFICIENT_ERROR_CODE,
|
||||
BaseEnum,
|
||||
SpaceAppEnum,
|
||||
defaultConversationKey,
|
||||
defaultConversationUniqId,
|
||||
} from '../src/index';
|
||||
import { globalVars as originalGlobalVars } from '../src/global-var';
|
||||
import { GlobalEventBus as originalGlobalEventBus } from '../src/event-bus';
|
||||
import { COZE_TOKEN_INSUFFICIENT_ERROR_CODE as originalCOZE_TOKEN_INSUFFICIENT_ERROR_CODE } from '../src/const/custom';
|
||||
import {
|
||||
defaultConversationKey as originalDefaultConversationKey,
|
||||
defaultConversationUniqId as originalDefaultConversationUniqId,
|
||||
} from '../src/const/community';
|
||||
import {
|
||||
BaseEnum as originalBaseEnum,
|
||||
SpaceAppEnum as originalSpaceAppEnum,
|
||||
} from '../src/const/app';
|
||||
|
||||
describe('index', () => {
|
||||
test('should export redirect from location', () => {
|
||||
expect(redirect).toBe(originalRedirect);
|
||||
});
|
||||
|
||||
test('should export GlobalEventBus from event-bus', () => {
|
||||
expect(GlobalEventBus).toBe(originalGlobalEventBus);
|
||||
});
|
||||
|
||||
test('should export globalVars from global-var', () => {
|
||||
expect(globalVars).toBe(originalGlobalVars);
|
||||
});
|
||||
|
||||
test('should export COZE_TOKEN_INSUFFICIENT_ERROR_CODE from const/custom', () => {
|
||||
expect(COZE_TOKEN_INSUFFICIENT_ERROR_CODE).toBe(
|
||||
originalCOZE_TOKEN_INSUFFICIENT_ERROR_CODE,
|
||||
);
|
||||
});
|
||||
|
||||
test('should export BaseEnum from const/app', () => {
|
||||
expect(BaseEnum).toBe(originalBaseEnum);
|
||||
});
|
||||
|
||||
test('should export SpaceAppEnum from const/app', () => {
|
||||
expect(SpaceAppEnum).toBe(originalSpaceAppEnum);
|
||||
});
|
||||
|
||||
test('should export defaultConversationKey from const/community', () => {
|
||||
expect(defaultConversationKey).toBe(originalDefaultConversationKey);
|
||||
});
|
||||
|
||||
test('should export defaultConversationUniqId from const/community', () => {
|
||||
expect(defaultConversationUniqId).toBe(originalDefaultConversationUniqId);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 { redirect } from '../src/location';
|
||||
|
||||
const viHrefSetter = vi.fn();
|
||||
vi.stubGlobal('location', {
|
||||
_href: '',
|
||||
set href(v: string) {
|
||||
viHrefSetter(v);
|
||||
(this as any)._href = v;
|
||||
},
|
||||
get href() {
|
||||
return (this as any)._href;
|
||||
},
|
||||
});
|
||||
|
||||
describe('location', () => {
|
||||
test('redirect', () => {
|
||||
redirect('test');
|
||||
expect(viHrefSetter).toHaveBeenCalledWith('test');
|
||||
expect(location.href).equal('test');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user