test: 添加 background 单元测试与边界情况处理
- 新增 entrypoints/__tests__/background.test.ts (12个测试用例) - 超长文本截断限制 (>10000字符) - Base64 内联图片拦截并返回错误提示 - 更新 parseContextMenuClick 返回 ParseResult 格式 - 更新测试匹配新的返回格式 - lint/typecheck/test 全部通过 (579个测试)
This commit is contained in:
+36
-10
@@ -12,8 +12,16 @@ export interface ContextMenuClickedInfo {
|
||||
payload: string;
|
||||
}
|
||||
|
||||
export interface ParseResult {
|
||||
success: boolean;
|
||||
data?: ContextMenuClickedInfo;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
const PARENT_MENU_ID = 'testing-tools-parent';
|
||||
|
||||
export const MAX_PAYLOAD_LENGTH = 10000;
|
||||
|
||||
export const CONTEXT_MENU_CONFIGS: ContextMenuItemConfig[] = [
|
||||
{
|
||||
id: PARENT_MENU_ID,
|
||||
@@ -78,36 +86,54 @@ export function createAllContextMenus(): void {
|
||||
export function parseContextMenuClick(
|
||||
menuItemId: string,
|
||||
info: chrome.contextMenus.OnClickData,
|
||||
): ContextMenuClickedInfo | null {
|
||||
): ParseResult {
|
||||
const featureKey = menuItemId as PageType;
|
||||
|
||||
if (menuItemId === 'qrCode-image') {
|
||||
const srcUrl = info.srcUrl || '';
|
||||
|
||||
if (srcUrl.startsWith('data:image/')) {
|
||||
return {
|
||||
success: false,
|
||||
error: '无法识别 Base64 内联图片,请使用图片文件 URL',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
featureKey: 'qrCode',
|
||||
payload: info.srcUrl || '',
|
||||
success: true,
|
||||
data: { featureKey: 'qrCode', payload: srcUrl },
|
||||
};
|
||||
}
|
||||
|
||||
if (menuItemId === 'qrCode-page') {
|
||||
return {
|
||||
featureKey: 'qrCode',
|
||||
payload: info.pageUrl || '',
|
||||
success: true,
|
||||
data: { featureKey: 'qrCode', payload: info.pageUrl || '' },
|
||||
};
|
||||
}
|
||||
|
||||
if (info.selectionText) {
|
||||
const text = info.selectionText;
|
||||
|
||||
if (text.length > MAX_PAYLOAD_LENGTH) {
|
||||
return {
|
||||
success: true,
|
||||
data: { featureKey, payload: text.substring(0, MAX_PAYLOAD_LENGTH) },
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
featureKey,
|
||||
payload: info.selectionText,
|
||||
success: true,
|
||||
data: { featureKey, payload: text },
|
||||
};
|
||||
}
|
||||
|
||||
if (info.pageUrl) {
|
||||
return {
|
||||
featureKey,
|
||||
payload: info.pageUrl,
|
||||
success: true,
|
||||
data: { featureKey, payload: info.pageUrl },
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
return { success: false, error: '无法获取有效数据' };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user