feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* 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, beforeEach, vi } from 'vitest';
|
||||
|
||||
import { localStorageService } from '../../src/core';
|
||||
|
||||
const LOCAL_STORAGE_KEY = '__coz_biz_cache__';
|
||||
|
||||
describe('LocalStorageService', () => {
|
||||
beforeEach(() => {
|
||||
// 清除 localStorage
|
||||
localStorage.clear();
|
||||
// 重置 userId
|
||||
localStorageService.setUserId(undefined);
|
||||
});
|
||||
|
||||
describe('永久存储', () => {
|
||||
const permanentKey = 'workflow-toolbar-role-onboarding-hidden';
|
||||
|
||||
it('应该能正确设置和获取永久存储的值', () => {
|
||||
const value = 'test-value';
|
||||
|
||||
localStorageService.setValue(permanentKey, value);
|
||||
expect(localStorageService.getValue(permanentKey)).toBe(value);
|
||||
});
|
||||
|
||||
it('应该能正确删除永久存储的值', () => {
|
||||
localStorageService.setValue(permanentKey, 'test-value');
|
||||
localStorageService.setValue(permanentKey, undefined);
|
||||
expect(localStorageService.getValue(permanentKey)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('应该将数据持久化到 localStorage', async () => {
|
||||
const value = 'test-value';
|
||||
|
||||
localStorageService.setValue(permanentKey, value);
|
||||
|
||||
// 等待 throttle
|
||||
await new Promise(resolve => setTimeout(resolve, 400));
|
||||
|
||||
const storedData = JSON.parse(
|
||||
localStorage.getItem(LOCAL_STORAGE_KEY) || '{}',
|
||||
);
|
||||
expect(storedData.permanent?.[permanentKey]).toBe(value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('用户相关存储', () => {
|
||||
const userId = 'test-user-id';
|
||||
const userBindKey = 'coachmark';
|
||||
|
||||
beforeEach(() => {
|
||||
localStorageService.setUserId(userId);
|
||||
});
|
||||
|
||||
it('应该能正确设置和获取用户相关的值', () => {
|
||||
const value = 'test-value';
|
||||
|
||||
localStorageService.setValue(userBindKey, value);
|
||||
expect(localStorageService.getValue(userBindKey)).toBe(value);
|
||||
});
|
||||
|
||||
it('在没有设置 userId 时不应该设置用户相关的值', () => {
|
||||
vi.stubGlobal('IS_DEV_MODE', false);
|
||||
localStorageService.setUserId(undefined);
|
||||
|
||||
localStorageService.setValue(userBindKey, 'test-value');
|
||||
expect(localStorageService.getValue(userBindKey)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('切换用户时应该能访问对应用户的数据', () => {
|
||||
const value1 = 'user1-value';
|
||||
const value2 = 'user2-value';
|
||||
const userId2 = 'test-user-id-2';
|
||||
|
||||
// 第一个用户的数据
|
||||
localStorageService.setValue(userBindKey, value1);
|
||||
|
||||
// 切换到第二个用户
|
||||
localStorageService.setUserId(userId2);
|
||||
localStorageService.setValue(userBindKey, value2);
|
||||
expect(localStorageService.getValue(userBindKey)).toBe(value2);
|
||||
|
||||
// 切回第一个用户
|
||||
localStorageService.setUserId(userId);
|
||||
expect(localStorageService.getValue(userBindKey)).toBe(value1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('事件监听', () => {
|
||||
const permanentKey = 'workflow-toolbar-role-onboarding-hidden';
|
||||
|
||||
it('值变化时应该触发 change 事件', async () => {
|
||||
const value = 'test-value';
|
||||
const changeHandler = vi.fn();
|
||||
|
||||
localStorageService.on('change', changeHandler);
|
||||
localStorageService.setValue(permanentKey, value);
|
||||
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
expect(changeHandler).toHaveBeenCalled();
|
||||
|
||||
localStorageService.off('change', changeHandler);
|
||||
});
|
||||
|
||||
it('设置 userId 时应该触发 setUserId 事件', () => {
|
||||
const userId = 'test-user-id';
|
||||
const setUserIdHandler = vi.fn();
|
||||
|
||||
localStorageService.on('setUserId', setUserIdHandler);
|
||||
localStorageService.setUserId(userId);
|
||||
|
||||
expect(setUserIdHandler).toHaveBeenCalledWith(userId);
|
||||
|
||||
localStorageService.off('setUserId', setUserIdHandler);
|
||||
});
|
||||
});
|
||||
|
||||
describe('异步获取值', () => {
|
||||
const permanentKey = 'workflow-toolbar-role-onboarding-hidden';
|
||||
|
||||
it('对于非用户绑定的值应该直接返回', async () => {
|
||||
const value = 'test-value';
|
||||
|
||||
localStorageService.setValue(permanentKey, value);
|
||||
const result = await localStorageService.getValueSync(permanentKey);
|
||||
expect(result).toBe(value);
|
||||
}, 10000);
|
||||
|
||||
it('对于用户绑定的值,应该等待 userId 设置后再返回', async () => {
|
||||
const userId = 'test-user-id';
|
||||
const value = 'test-value';
|
||||
|
||||
// 先设置值
|
||||
localStorageService.setUserId(userId);
|
||||
localStorageService.setValue('coachmark', value);
|
||||
localStorageService.setUserId(undefined);
|
||||
|
||||
// 异步获取值
|
||||
const valuePromise = localStorageService.getValueSync('coachmark');
|
||||
|
||||
// 设置 userId
|
||||
setTimeout(() => {
|
||||
localStorageService.setUserId(userId);
|
||||
}, 0);
|
||||
|
||||
const result = await valuePromise;
|
||||
expect(result).toBe(value);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* 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, beforeEach } from 'vitest';
|
||||
import { renderHook, act } from '@testing-library/react';
|
||||
|
||||
import { useValue } from '../../src/hooks/use-value';
|
||||
import { localStorageService } from '../../src/core';
|
||||
|
||||
describe('useValue', () => {
|
||||
const permanentKey = 'workflow-toolbar-role-onboarding-hidden';
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
localStorageService.setUserId(undefined);
|
||||
});
|
||||
|
||||
it('应该返回存储的值', async () => {
|
||||
const value = 'test-value';
|
||||
localStorageService.setValue(permanentKey, value);
|
||||
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
const { result } = renderHook(() => useValue(permanentKey));
|
||||
expect(result.current).toBe(value);
|
||||
});
|
||||
|
||||
it('当值改变时应该更新', async () => {
|
||||
const { result } = renderHook(() => useValue(permanentKey));
|
||||
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
await act(async () => {
|
||||
localStorageService.setValue(permanentKey, 'new-value');
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
});
|
||||
|
||||
expect(result.current).toBe('new-value');
|
||||
});
|
||||
|
||||
it('当值被删除时应该返回 undefined', async () => {
|
||||
localStorageService.setValue(permanentKey, 'test-value');
|
||||
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
const { result } = renderHook(() => useValue(permanentKey));
|
||||
|
||||
await act(async () => {
|
||||
localStorageService.setValue(permanentKey, undefined);
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
});
|
||||
|
||||
expect(result.current).toBeUndefined();
|
||||
});
|
||||
|
||||
it('卸载时应该清理事件监听', async () => {
|
||||
const { unmount } = renderHook(() => useValue(permanentKey));
|
||||
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
unmount();
|
||||
|
||||
// 确保不会触发已卸载组件的状态更新
|
||||
localStorageService.setValue(permanentKey, 'new-value');
|
||||
// 如果事件监听没有被清理,这里会抛出 React 警告
|
||||
});
|
||||
|
||||
describe('用户相关的值', () => {
|
||||
// 使用一个确定绑定了用户的 key
|
||||
const userBindKey = 'coachmark' as const;
|
||||
const userId = 'test-user-id';
|
||||
|
||||
beforeEach(() => {
|
||||
localStorageService.setUserId(userId);
|
||||
});
|
||||
|
||||
it('应该返回当前用户的值', async () => {
|
||||
localStorageService.setValue(userBindKey, 'user-value');
|
||||
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
const { result } = renderHook(() => useValue(userBindKey));
|
||||
expect(result.current).toBe('user-value');
|
||||
});
|
||||
|
||||
it('切换用户时应该更新值', async () => {
|
||||
const userId2 = 'test-user-id-2';
|
||||
localStorageService.setValue(userBindKey, 'user1-value');
|
||||
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
|
||||
const { result } = renderHook(() => useValue(userBindKey));
|
||||
expect(result.current).toBe('user1-value');
|
||||
|
||||
await act(async () => {
|
||||
localStorageService.setUserId(userId2);
|
||||
localStorageService.setValue(userBindKey, 'user2-value');
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
});
|
||||
|
||||
expect(result.current).toBe('user2-value');
|
||||
|
||||
await act(async () => {
|
||||
localStorageService.setUserId(userId);
|
||||
// 等待事件触发
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
});
|
||||
|
||||
expect(result.current).toBe('user1-value');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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 } from 'vitest';
|
||||
|
||||
import { paseLocalStorageValue, filterCacheData } from '../../src/utils/parse';
|
||||
import { type LocalStorageCacheData } from '../../src/types';
|
||||
|
||||
describe('解析工具函数', () => {
|
||||
describe('paseLocalStorageValue', () => {
|
||||
it('应该返回空对象当输入为 null', () => {
|
||||
expect(paseLocalStorageValue(null)).toEqual({});
|
||||
});
|
||||
|
||||
it('应该返回空对象当输入不是有效的 JSON', () => {
|
||||
expect(paseLocalStorageValue('invalid json')).toEqual({});
|
||||
});
|
||||
|
||||
it('应该返回空对象当输入的 JSON 不符合缓存数据格式', () => {
|
||||
expect(paseLocalStorageValue('{"invalid": "data"}')).toEqual({
|
||||
invalid: 'data',
|
||||
});
|
||||
});
|
||||
|
||||
it('应该正确解析永久缓存数据', () => {
|
||||
const data = {
|
||||
permanent: {
|
||||
'workspace-spaceId': 'test-space-id',
|
||||
},
|
||||
};
|
||||
expect(paseLocalStorageValue(JSON.stringify(data))).toEqual(data);
|
||||
});
|
||||
|
||||
it('应该正确解析用户相关缓存数据', () => {
|
||||
const data = {
|
||||
userRelated: {
|
||||
'user-1': {
|
||||
'workspace-spaceId': 'test-space-id',
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(paseLocalStorageValue(JSON.stringify(data))).toEqual(data);
|
||||
});
|
||||
|
||||
it('应该正确解析同时包含永久和用户相关的缓存数据', () => {
|
||||
const data = {
|
||||
permanent: {
|
||||
'workspace-spaceId': 'test-space-id',
|
||||
},
|
||||
userRelated: {
|
||||
'user-1': {
|
||||
'workspace-subMenu': 'test-menu',
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(paseLocalStorageValue(JSON.stringify(data))).toEqual(data);
|
||||
});
|
||||
|
||||
it('应该返回空对象当永久缓存数据格式无效', () => {
|
||||
const data = {
|
||||
permanent: {
|
||||
key: 123, // 应该是字符串
|
||||
},
|
||||
};
|
||||
expect(paseLocalStorageValue(JSON.stringify(data))).toEqual({});
|
||||
});
|
||||
|
||||
it('应该返回空对象当用户相关缓存数据格式无效', () => {
|
||||
const data = {
|
||||
userRelated: {
|
||||
'user-1': {
|
||||
key: 123, // 应该是字符串
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(paseLocalStorageValue(JSON.stringify(data))).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('filterCacheData', () => {
|
||||
it('应该过滤掉永久缓存中的无效键', () => {
|
||||
const data: LocalStorageCacheData = {
|
||||
permanent: {
|
||||
'workspace-spaceId': 'valid-value',
|
||||
'invalid-key': 'invalid-value',
|
||||
},
|
||||
};
|
||||
|
||||
const filtered = filterCacheData(data);
|
||||
expect(filtered.permanent?.['workspace-spaceId']).toBe('valid-value');
|
||||
expect(filtered.permanent?.['invalid-key']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('应该过滤掉用户相关缓存中的无效键', () => {
|
||||
const data: LocalStorageCacheData = {
|
||||
userRelated: {
|
||||
'user-1': {
|
||||
'workspace-spaceId': 'valid-value',
|
||||
'invalid-key': 'invalid-value',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const filtered = filterCacheData(data);
|
||||
expect(filtered.userRelated?.['user-1']?.['workspace-spaceId']).toBe(
|
||||
'valid-value',
|
||||
);
|
||||
expect(filtered.userRelated?.['user-1']?.['invalid-key']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('应该保持用户 ID 不变', () => {
|
||||
const data: LocalStorageCacheData = {
|
||||
userRelated: {
|
||||
'user-1': {
|
||||
'workspace-spaceId': 'value-1',
|
||||
},
|
||||
'user-2': {
|
||||
'workspace-spaceId': 'value-2',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const filtered = filterCacheData(data);
|
||||
expect(Object.keys(filtered.userRelated || {})).toEqual([
|
||||
'user-1',
|
||||
'user-2',
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user