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, it, expect, vi, beforeEach } from 'vitest';
import { RemoteWebWorker, register } from '../src/index';
// 获取模拟函数
const mockCreateObjectURL = vi.mocked(URL.createObjectURL);
describe('RemoteWebWorker', () => {
beforeEach(() => {
// 清除模拟调用记录
vi.clearAllMocks();
});
describe('constructor', () => {
it('应该直接使用本地 URL', () => {
const localUrl = 'worker.js';
const options = { type: 'module' };
new RemoteWebWorker(localUrl, options);
expect(mockCreateObjectURL).not.toHaveBeenCalled();
});
it('应该直接使用同源的远程 URL', () => {
const sameOriginUrl = 'https://example.com/worker.js';
const options = { type: 'module' };
new RemoteWebWorker(sameOriginUrl, options);
expect(mockCreateObjectURL).not.toHaveBeenCalled();
});
it('应该直接使用 blob URL', () => {
const blobUrl = 'blob:https://example.com/worker.js';
const options = { type: 'module' };
new RemoteWebWorker(blobUrl, options);
expect(mockCreateObjectURL).not.toHaveBeenCalled();
});
it('应该为跨域 URL 创建 Blob 并使用 URL.createObjectURL', () => {
const crossOriginUrl = 'https://other-domain.com/worker.js';
const options = { type: 'module' };
new RemoteWebWorker(crossOriginUrl, options);
expect(mockCreateObjectURL).toHaveBeenCalledTimes(1);
// 验证创建的 Blob 内容
const blobArg = mockCreateObjectURL.mock.calls[0][0];
expect(blobArg).toBeInstanceOf(Blob);
// 由于 Blob 的内容无法直接访问,我们只能验证它被创建了
});
it('应该处理非字符串 URL', () => {
const nonStringUrl = {
toString: () => 'https://other-domain.com/worker.js',
};
const options = { type: 'module' };
new RemoteWebWorker(nonStringUrl as any, options);
expect(mockCreateObjectURL).toHaveBeenCalledTimes(1);
});
});
});
describe('register', () => {
it('应该将全局 Worker 替换为 RemoteWebWorker', () => {
// 创建一个模拟的全局对象
const mockGlobal = {
worker() {
/* 空函数 */
},
};
// 将 worker 属性重命名为 Worker以便测试 register 函数
Object.defineProperty(mockGlobal, 'Worker', {
get() {
return this.worker;
},
set(value) {
this.worker = value;
},
enumerable: true,
configurable: true,
});
register(mockGlobal as any);
expect(mockGlobal.worker).toBe(RemoteWebWorker);
});
it('当全局对象未定义时不应该抛出错误', () => {
expect(() => register(undefined as any)).not.toThrow();
});
});

View File

@@ -0,0 +1,54 @@
/*
* 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';
// 定义一个模拟的 Worker 类
class MockWorker {
constructor(
public scriptURL: string,
public options: any,
) {}
// 添加 Worker 接口所需的方法
terminate(): void {
// 空实现
}
postMessage(): void {
// 空实现
}
onmessage = null;
onmessageerror = null;
}
// 全局模拟
global.Worker = MockWorker as any;
global.URL = {
createObjectURL: vi.fn().mockReturnValue('blob:mocked-object-url'),
} as any;
global.Blob = class MockBlob {
constructor(
public array: any[],
public options: any,
) {}
} as any;
global.location = {
origin: 'https://example.com',
} as any;