chore: replace all cn comments of fe to en version by volc api (#320)
This commit is contained in:
@@ -23,65 +23,65 @@ describe('generatePdfAssetsUrl', () => {
|
||||
const originalRegion = global.REGION;
|
||||
|
||||
beforeEach(() => {
|
||||
// 重置模拟
|
||||
// Reset simulation
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// 恢复原始 REGION 值
|
||||
// Restore original REGION value
|
||||
global.REGION = originalRegion;
|
||||
});
|
||||
|
||||
it('应该为 cmaps 生成正确的 URL(中国区域)', () => {
|
||||
// 设置区域为中国
|
||||
// Set the region to China
|
||||
global.REGION = 'cn';
|
||||
|
||||
const url = generatePdfAssetsUrl('cmaps');
|
||||
|
||||
// 验证 URL 格式
|
||||
// Verify URL format
|
||||
expect(url).toContain('//lf-cdn.coze.cn/obj/unpkg');
|
||||
expect(url).toContain(pkg.name.replace(/^@/, ''));
|
||||
expect(url).toContain('lib/cmaps/');
|
||||
});
|
||||
|
||||
it('应该为 pdf.worker 生成正确的 URL(中国区域)', () => {
|
||||
// 设置区域为中国
|
||||
// Set the region to China
|
||||
global.REGION = 'cn';
|
||||
|
||||
const url = generatePdfAssetsUrl('pdf.worker');
|
||||
|
||||
// 验证 URL 格式
|
||||
// Verify URL format
|
||||
expect(url).toContain('//lf-cdn.coze.cn/obj/unpkg');
|
||||
expect(url).toContain(pkg.name.replace(/^@/, ''));
|
||||
expect(url).toContain('lib/worker.js');
|
||||
});
|
||||
|
||||
it('应该为 cmaps 生成正确的 URL(国际区域)', () => {
|
||||
// 设置区域为国际
|
||||
// Set the region to International
|
||||
global.REGION = 'va';
|
||||
|
||||
const url = generatePdfAssetsUrl('cmaps');
|
||||
|
||||
// 验证 URL 格式
|
||||
// Verify URL format
|
||||
expect(url).toContain('//sf-cdn.coze.com/obj/unpkg-va');
|
||||
expect(url).toContain(pkg.name.replace(/^@/, ''));
|
||||
expect(url).toContain('lib/cmaps/');
|
||||
});
|
||||
|
||||
it('应该为 pdf.worker 生成正确的 URL(国际区域)', () => {
|
||||
// 设置区域为国际
|
||||
// Set the region to International
|
||||
global.REGION = 'va';
|
||||
|
||||
const url = generatePdfAssetsUrl('pdf.worker');
|
||||
|
||||
// 验证 URL 格式
|
||||
// Verify URL format
|
||||
expect(url).toContain('//sf-cdn.coze.com/obj/unpkg-va');
|
||||
expect(url).toContain(pkg.name.replace(/^@/, ''));
|
||||
expect(url).toContain('lib/worker.js');
|
||||
});
|
||||
|
||||
it('应该在传入无效资源类型时抛出错误', () => {
|
||||
// 使用类型断言来测试错误情况
|
||||
// Use type assertions to test error conditions
|
||||
expect(() => generatePdfAssetsUrl('invalid' as any)).toThrow(
|
||||
'目前只支持引用 cmaps 与 pdf.worker 文件',
|
||||
);
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
// 模拟 pdfjs-dist 模块
|
||||
// Emulate pdfjs-dist module
|
||||
vi.mock('pdfjs-dist', () => ({
|
||||
getDocument: vi.fn(),
|
||||
}));
|
||||
|
||||
// 模拟 generate-assets 和 init-pdfjs-dist 模块
|
||||
// Emulate generate-assets and init-pdfjs-dist modules
|
||||
vi.mock('../src/generate-assets', () => ({
|
||||
generatePdfAssetsUrl: vi.fn(),
|
||||
}));
|
||||
@@ -30,7 +30,7 @@ vi.mock('../src/init-pdfjs-dist', () => ({
|
||||
initPdfJsWorker: vi.fn(),
|
||||
}));
|
||||
|
||||
// 导入被测试的模块
|
||||
// Import the tested module
|
||||
import {
|
||||
generatePdfAssetsUrl,
|
||||
initPdfJsWorker,
|
||||
@@ -39,11 +39,11 @@ import {
|
||||
|
||||
describe('pdfjs-shadow index', () => {
|
||||
it('应该导出所有必要的函数和类型', () => {
|
||||
// 验证导出的函数
|
||||
// Validate the derived function
|
||||
expect(typeof generatePdfAssetsUrl).toBe('function');
|
||||
expect(typeof initPdfJsWorker).toBe('function');
|
||||
|
||||
// 验证从 pdfjs-dist 重新导出的函数和类型
|
||||
// Verify functions and types re-exported from pdfjs-dist
|
||||
expect(getDocument).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,19 +16,19 @@
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
// 模拟 pdfjs-dist 模块
|
||||
// Emulate pdfjs-dist module
|
||||
vi.mock('pdfjs-dist', () => ({
|
||||
GlobalWorkerOptions: {
|
||||
workerSrc: '',
|
||||
},
|
||||
}));
|
||||
|
||||
// 模拟 generate-assets 模块
|
||||
// Simulate generate-assets module
|
||||
vi.mock('../src/generate-assets', () => ({
|
||||
generatePdfAssetsUrl: vi.fn().mockReturnValue('mocked-worker-url'),
|
||||
}));
|
||||
|
||||
// 导入被测试的模块
|
||||
// Import the tested module
|
||||
import { GlobalWorkerOptions } from 'pdfjs-dist';
|
||||
|
||||
import { initPdfJsWorker } from '../src/init-pdfjs-dist';
|
||||
@@ -36,43 +36,43 @@ import { generatePdfAssetsUrl } from '../src/generate-assets';
|
||||
|
||||
describe('initPdfJsWorker', () => {
|
||||
beforeEach(() => {
|
||||
// 每个测试前重置 GlobalWorkerOptions.workerSrc
|
||||
// Reset GlobalWorkerOptions.workerSrc before each test
|
||||
GlobalWorkerOptions.workerSrc = '';
|
||||
// 清除所有模拟函数的调用记录
|
||||
// Clear all call records for simulated functions
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// 每个测试后重置模拟
|
||||
// Reset simulation after each test
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it('应该设置 GlobalWorkerOptions.workerSrc 当它为空时', () => {
|
||||
// 确保 workerSrc 初始为空
|
||||
// Make sure workerSrc is initially empty
|
||||
expect(GlobalWorkerOptions.workerSrc).toBe('');
|
||||
|
||||
// 调用初始化函数
|
||||
// Invoke initialization function
|
||||
initPdfJsWorker();
|
||||
|
||||
// 验证 generatePdfAssetsUrl 被调用,且参数正确
|
||||
// Verify generatePdfAssetsUrl is called and the parameters are correct
|
||||
expect(generatePdfAssetsUrl).toHaveBeenCalledTimes(1);
|
||||
expect(generatePdfAssetsUrl).toHaveBeenCalledWith('pdf.worker');
|
||||
|
||||
// 验证 workerSrc 被正确设置
|
||||
// Verify that workerSrc is set correctly
|
||||
expect(GlobalWorkerOptions.workerSrc).toBe('mocked-worker-url');
|
||||
});
|
||||
|
||||
it('不应该重新设置 GlobalWorkerOptions.workerSrc 当它已经有值时', () => {
|
||||
// 预先设置 workerSrc
|
||||
// Pre-set workerSrc
|
||||
GlobalWorkerOptions.workerSrc = 'existing-worker-url';
|
||||
|
||||
// 调用初始化函数
|
||||
// Invoke initialization function
|
||||
initPdfJsWorker();
|
||||
|
||||
// 验证 generatePdfAssetsUrl 没有被调用
|
||||
// Verify generatePdfAssetsUrl not called
|
||||
expect(generatePdfAssetsUrl).not.toHaveBeenCalled();
|
||||
|
||||
// 验证 workerSrc 保持不变
|
||||
// Verify that workerSrc remains unchanged
|
||||
expect(GlobalWorkerOptions.workerSrc).toBe('existing-worker-url');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,24 +19,24 @@ import fs from 'fs/promises';
|
||||
|
||||
import { OUTPUT_DIR } from './const';
|
||||
|
||||
// 复制目录的函数
|
||||
// Function to copy directory
|
||||
const copyDir = async (src: string, dest: string) => {
|
||||
// 读取目录下所有文件/文件夹
|
||||
// Read all files/folders in the directory
|
||||
const entries = await fs.readdir(src, { withFileTypes: true });
|
||||
|
||||
// 创建目标目录
|
||||
// Create target directory
|
||||
await fs.mkdir(dest, { recursive: true });
|
||||
|
||||
// 遍历所有文件/文件夹
|
||||
// Iterate through all files/folders
|
||||
for (const entry of entries) {
|
||||
const srcPath = path.join(src, entry.name);
|
||||
const destPath = path.join(dest, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
// 如果是文件夹则递归复制
|
||||
// If it is a folder, copy it recursively.
|
||||
await copyDir(srcPath, destPath);
|
||||
} else {
|
||||
// 如果是文件则直接复制
|
||||
// If it is a file, copy it directly.
|
||||
await fs.copyFile(srcPath, destPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,12 @@ import pkg from '../package.json';
|
||||
|
||||
type AssetsType = 'cmaps' | 'pdf.worker';
|
||||
|
||||
// 这里需要写 bnpm 已经发布的版本
|
||||
// Here you need to write the version that bnpm has released.
|
||||
//
|
||||
const DEFAULT_VERSION = '0.1.0-alpha.x6e892414ec';
|
||||
|
||||
/**
|
||||
* 该方法用于生产 unpkg 环境的 worker & cmaps 链接,注意并非 pdfjs 原生方法
|
||||
* This method is used to produce the worker & cmaps link of the unpkg environment. Note that it is not a native method of pdfjs
|
||||
*/
|
||||
export const generatePdfAssetsUrl = (assets: AssetsType) => {
|
||||
const { name } = pkg;
|
||||
|
||||
@@ -19,7 +19,7 @@ import { GlobalWorkerOptions } from 'pdfjs-dist';
|
||||
import { generatePdfAssetsUrl } from './generate-assets';
|
||||
|
||||
/**
|
||||
* 该方法用于初始化 pdfjs-dist 的 workerSrc 参数,可重复调用
|
||||
* This method is used to initialize the workerSrc parameter of pdfjs-dist, which can be called repeatedly
|
||||
*/
|
||||
export const initPdfJsWorker = () => {
|
||||
if (!GlobalWorkerOptions.workerSrc) {
|
||||
|
||||
Reference in New Issue
Block a user