Auto-fix formatting issues
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
* and prevent regressions.
|
* and prevent regressions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { strict as assert } from 'assert';
|
import { strict as assert } from "assert";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to generate safe filename
|
* Helper function to generate safe filename
|
||||||
@@ -15,9 +15,9 @@ import { strict as assert } from 'assert';
|
|||||||
*/
|
*/
|
||||||
function generateSafeFilename(name) {
|
function generateSafeFilename(name) {
|
||||||
return name
|
return name
|
||||||
.replace(/[<>:"/\\|?*]/g, '_')
|
.replace(/[<>:"/\\|?*]/g, "_")
|
||||||
.replace(/\s+/g, '_')
|
.replace(/\s+/g, "_")
|
||||||
.replace(/\.+$/g, '')
|
.replace(/\.+$/g, "")
|
||||||
.slice(0, 255);
|
.slice(0, 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,50 +35,70 @@ function getSafeAppName(name) {
|
|||||||
export function testGetSafeAppName() {
|
export function testGetSafeAppName() {
|
||||||
const tests = [
|
const tests = [
|
||||||
// Basic cases
|
// Basic cases
|
||||||
{ input: 'MyApp', expected: 'myapp', description: 'Simple name' },
|
{ input: "MyApp", expected: "myapp", description: "Simple name" },
|
||||||
{ input: 'My App', expected: 'my_app', description: 'Name with space' },
|
{ input: "My App", expected: "my_app", description: "Name with space" },
|
||||||
{ input: 'my-app', expected: 'my-app', description: 'Name with hyphen' },
|
{ input: "my-app", expected: "my-app", description: "Name with hyphen" },
|
||||||
|
|
||||||
// Chinese characters
|
// Chinese characters
|
||||||
{ input: '我的应用', expected: '我的应用', description: 'Chinese name' },
|
{ input: "我的应用", expected: "我的应用", description: "Chinese name" },
|
||||||
{ input: '我的 App', expected: '我的_app', description: 'Mixed Chinese and English' },
|
{
|
||||||
|
input: "我的 App",
|
||||||
|
expected: "我的_app",
|
||||||
|
description: "Mixed Chinese and English",
|
||||||
|
},
|
||||||
|
|
||||||
// Special characters
|
// Special characters
|
||||||
{ input: 'App@2024', expected: 'app@2024', description: 'Special character @ (preserved)' },
|
{
|
||||||
{ input: 'My/App', expected: 'my_app', description: 'Forward slash' },
|
input: "App@2024",
|
||||||
{ input: 'My\\App', expected: 'my_app', description: 'Backslash' },
|
expected: "app@2024",
|
||||||
{ input: 'App:Name', expected: 'app_name', description: 'Colon' },
|
description: "Special character @ (preserved)",
|
||||||
{ input: 'App*Name', expected: 'app_name', description: 'Asterisk' },
|
},
|
||||||
{ input: 'App?Name', expected: 'app_name', description: 'Question mark' },
|
{ input: "My/App", expected: "my_app", description: "Forward slash" },
|
||||||
{ input: 'App"Name', expected: 'app_name', description: 'Double quote' },
|
{ input: "My\\App", expected: "my_app", description: "Backslash" },
|
||||||
{ input: 'App<Name>', expected: 'app_name_', description: 'Angle brackets' },
|
{ input: "App:Name", expected: "app_name", description: "Colon" },
|
||||||
{ input: 'App|Name', expected: 'app_name', description: 'Pipe' },
|
{ input: "App*Name", expected: "app_name", description: "Asterisk" },
|
||||||
|
{ input: "App?Name", expected: "app_name", description: "Question mark" },
|
||||||
|
{ input: 'App"Name', expected: "app_name", description: "Double quote" },
|
||||||
|
{
|
||||||
|
input: "App<Name>",
|
||||||
|
expected: "app_name_",
|
||||||
|
description: "Angle brackets",
|
||||||
|
},
|
||||||
|
{ input: "App|Name", expected: "app_name", description: "Pipe" },
|
||||||
|
|
||||||
// Edge cases
|
// Edge cases
|
||||||
{ input: 'APP', expected: 'app', description: 'All uppercase' },
|
{ input: "APP", expected: "app", description: "All uppercase" },
|
||||||
{ input: 'a', expected: 'a', description: 'Single character' },
|
{ input: "a", expected: "a", description: "Single character" },
|
||||||
{ input: '123', expected: '123', description: 'Numbers only' },
|
{ input: "123", expected: "123", description: "Numbers only" },
|
||||||
{ input: ' App ', expected: '_app_', description: 'Leading/trailing spaces (collapsed)' },
|
{
|
||||||
{ input: 'App...', expected: 'app', description: 'Trailing dots' },
|
input: " App ",
|
||||||
|
expected: "_app_",
|
||||||
|
description: "Leading/trailing spaces (collapsed)",
|
||||||
|
},
|
||||||
|
{ input: "App...", expected: "app", description: "Trailing dots" },
|
||||||
|
|
||||||
// Long names
|
// Long names
|
||||||
{
|
{
|
||||||
input: 'A'.repeat(300),
|
input: "A".repeat(300),
|
||||||
expected: 'a'.repeat(255),
|
expected: "a".repeat(255),
|
||||||
description: 'Very long name (should truncate to 255)',
|
description: "Very long name (should truncate to 255)",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
let passed = 0;
|
let passed = 0;
|
||||||
let failed = 0;
|
let failed = 0;
|
||||||
|
|
||||||
console.log('\n🧪 Testing getSafeAppName()');
|
console.log("\n🧪 Testing getSafeAppName()");
|
||||||
console.log('─'.repeat(50));
|
console.log("─".repeat(50));
|
||||||
|
|
||||||
tests.forEach((test, index) => {
|
tests.forEach((test, index) => {
|
||||||
try {
|
try {
|
||||||
const result = getSafeAppName(test.input);
|
const result = getSafeAppName(test.input);
|
||||||
assert.equal(result, test.expected, `Expected "${test.expected}", got "${result}"`);
|
assert.equal(
|
||||||
|
result,
|
||||||
|
test.expected,
|
||||||
|
`Expected "${test.expected}", got "${result}"`,
|
||||||
|
);
|
||||||
console.log(` ✓ Test ${index + 1}: ${test.description}`);
|
console.log(` ✓ Test ${index + 1}: ${test.description}`);
|
||||||
passed++;
|
passed++;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -90,7 +110,7 @@ export function testGetSafeAppName() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('─'.repeat(50));
|
console.log("─".repeat(50));
|
||||||
console.log(`Results: ${passed} passed, ${failed} failed\n`);
|
console.log(`Results: ${passed} passed, ${failed} failed\n`);
|
||||||
|
|
||||||
return failed === 0;
|
return failed === 0;
|
||||||
@@ -100,44 +120,44 @@ export function testGetSafeAppName() {
|
|||||||
* Test suite for download error notification (browser environment simulation)
|
* Test suite for download error notification (browser environment simulation)
|
||||||
*/
|
*/
|
||||||
export function testDownloadErrorNotification() {
|
export function testDownloadErrorNotification() {
|
||||||
console.log('\n🧪 Testing showDownloadError() Logic');
|
console.log("\n🧪 Testing showDownloadError() Logic");
|
||||||
console.log('─'.repeat(50));
|
console.log("─".repeat(50));
|
||||||
|
|
||||||
const tests = [
|
const tests = [
|
||||||
{
|
{
|
||||||
name: 'Chinese language detection',
|
name: "Chinese language detection",
|
||||||
language: 'zh-CN',
|
language: "zh-CN",
|
||||||
filename: 'test.pdf',
|
filename: "test.pdf",
|
||||||
expectedTitle: '下载错误',
|
expectedTitle: "下载错误",
|
||||||
expectedBody: '下载失败: test.pdf',
|
expectedBody: "下载失败: test.pdf",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'English language detection',
|
name: "English language detection",
|
||||||
language: 'en-US',
|
language: "en-US",
|
||||||
filename: 'document.docx',
|
filename: "document.docx",
|
||||||
expectedTitle: 'Download Error',
|
expectedTitle: "Download Error",
|
||||||
expectedBody: 'Download failed: document.docx',
|
expectedBody: "Download failed: document.docx",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Traditional Chinese',
|
name: "Traditional Chinese",
|
||||||
language: 'zh-TW',
|
language: "zh-TW",
|
||||||
filename: 'file.zip',
|
filename: "file.zip",
|
||||||
expectedTitle: '下载错误',
|
expectedTitle: "下载错误",
|
||||||
expectedBody: '下载失败: file.zip',
|
expectedBody: "下载失败: file.zip",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Hong Kong Chinese',
|
name: "Hong Kong Chinese",
|
||||||
language: 'zh-HK',
|
language: "zh-HK",
|
||||||
filename: 'image.png',
|
filename: "image.png",
|
||||||
expectedTitle: '下载错误',
|
expectedTitle: "下载错误",
|
||||||
expectedBody: '下载失败: image.png',
|
expectedBody: "下载失败: image.png",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Special characters in filename',
|
name: "Special characters in filename",
|
||||||
language: 'en-US',
|
language: "en-US",
|
||||||
filename: 'my file (1).pdf',
|
filename: "my file (1).pdf",
|
||||||
expectedTitle: 'Download Error',
|
expectedTitle: "Download Error",
|
||||||
expectedBody: 'Download failed: my file (1).pdf',
|
expectedBody: "Download failed: my file (1).pdf",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -149,18 +169,22 @@ export function testDownloadErrorNotification() {
|
|||||||
// Simulate language detection
|
// Simulate language detection
|
||||||
const isChineseLanguage = (lang) =>
|
const isChineseLanguage = (lang) =>
|
||||||
lang &&
|
lang &&
|
||||||
(lang.startsWith('zh') ||
|
(lang.startsWith("zh") ||
|
||||||
lang.includes('CN') ||
|
lang.includes("CN") ||
|
||||||
lang.includes('TW') ||
|
lang.includes("TW") ||
|
||||||
lang.includes('HK'));
|
lang.includes("HK"));
|
||||||
|
|
||||||
const isChinese = isChineseLanguage(test.language);
|
const isChinese = isChineseLanguage(test.language);
|
||||||
const title = isChinese ? '下载错误' : 'Download Error';
|
const title = isChinese ? "下载错误" : "Download Error";
|
||||||
const body = isChinese
|
const body = isChinese
|
||||||
? `下载失败: ${test.filename}`
|
? `下载失败: ${test.filename}`
|
||||||
: `Download failed: ${test.filename}`;
|
: `Download failed: ${test.filename}`;
|
||||||
|
|
||||||
assert.equal(title, test.expectedTitle, `Title mismatch for ${test.name}`);
|
assert.equal(
|
||||||
|
title,
|
||||||
|
test.expectedTitle,
|
||||||
|
`Title mismatch for ${test.name}`,
|
||||||
|
);
|
||||||
assert.equal(body, test.expectedBody, `Body mismatch for ${test.name}`);
|
assert.equal(body, test.expectedBody, `Body mismatch for ${test.name}`);
|
||||||
|
|
||||||
console.log(` ✓ Test ${index + 1}: ${test.name}`);
|
console.log(` ✓ Test ${index + 1}: ${test.name}`);
|
||||||
@@ -175,7 +199,7 @@ export function testDownloadErrorNotification() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('─'.repeat(50));
|
console.log("─".repeat(50));
|
||||||
console.log(`Results: ${passed} passed, ${failed} failed\n`);
|
console.log(`Results: ${passed} passed, ${failed} failed\n`);
|
||||||
|
|
||||||
return failed === 0;
|
return failed === 0;
|
||||||
@@ -185,20 +209,20 @@ export function testDownloadErrorNotification() {
|
|||||||
* Run all tests
|
* Run all tests
|
||||||
*/
|
*/
|
||||||
export async function runHelperTests() {
|
export async function runHelperTests() {
|
||||||
console.log('\n📦 Helper Functions Unit Tests');
|
console.log("\n📦 Helper Functions Unit Tests");
|
||||||
console.log('='.repeat(50));
|
console.log("=".repeat(50));
|
||||||
|
|
||||||
const results = [];
|
const results = [];
|
||||||
|
|
||||||
// Test getSafeAppName
|
// Test getSafeAppName
|
||||||
results.push({
|
results.push({
|
||||||
name: 'getSafeAppName()',
|
name: "getSafeAppName()",
|
||||||
passed: testGetSafeAppName(),
|
passed: testGetSafeAppName(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test download error notification
|
// Test download error notification
|
||||||
results.push({
|
results.push({
|
||||||
name: 'showDownloadError() Logic',
|
name: "showDownloadError() Logic",
|
||||||
passed: testDownloadErrorNotification(),
|
passed: testDownloadErrorNotification(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -207,13 +231,13 @@ export async function runHelperTests() {
|
|||||||
const passedCount = results.filter((r) => r.passed).length;
|
const passedCount = results.filter((r) => r.passed).length;
|
||||||
const totalCount = results.length;
|
const totalCount = results.length;
|
||||||
|
|
||||||
console.log('\n📊 Helper Tests Summary');
|
console.log("\n📊 Helper Tests Summary");
|
||||||
console.log('='.repeat(50));
|
console.log("=".repeat(50));
|
||||||
results.forEach((result) => {
|
results.forEach((result) => {
|
||||||
const icon = result.passed ? '✅' : '❌';
|
const icon = result.passed ? "✅" : "❌";
|
||||||
console.log(`${icon} ${result.name}`);
|
console.log(`${icon} ${result.name}`);
|
||||||
});
|
});
|
||||||
console.log('='.repeat(50));
|
console.log("=".repeat(50));
|
||||||
console.log(`Total: ${passedCount}/${totalCount} test suites passed\n`);
|
console.log(`Total: ${passedCount}/${totalCount} test suites passed\n`);
|
||||||
|
|
||||||
return allPassed;
|
return allPassed;
|
||||||
@@ -226,7 +250,7 @@ if (import.meta.url === `file://${process.argv[1]}`) {
|
|||||||
process.exit(success ? 0 : 1);
|
process.exit(success ? 0 : 1);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error('Test execution failed:', error);
|
console.error("Test execution failed:", error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user