build: 新增单元测试框架
All checks were successful
deploy to server / build-and-deploy (push) Successful in 2m55s

- 使用nuxt test与vitest建立单元测试框架
- 创建部分单元测试
This commit is contained in:
2025-10-27 14:16:18 +08:00
8 changed files with 577 additions and 17 deletions

View File

@ -0,0 +1,66 @@
import { expect, test, describe } from 'vitest';
/**
* 单元测试: isObject
*/
describe('isObject', () => {
test('identify plain object', () => {
expect(isObject({})).toBe(true);
expect(isObject({ key: 'value' })).toBe(true);
});
test('identify null as non objevt', () => {
expect(isObject(null)).toBe(false);
});
test('identify non-object types', () => {
expect(isObject(undefined)).toBe(false);
expect(isObject(42)).toBe(false);
expect(isObject('string')).toBe(false);
expect(isObject(Symbol('sym'))).toBe(false);
expect(isObject(true)).toBe(false);
expect(isObject(() => {})).toBe(false);
});
test('identify arrays as objects', () => {
expect(isObject([])).toBe(true);
});
test('identify narrowed object type', () => {
const value: unknown = { id: 1 };
if (isObject<{ id: number }>(value)) {
expect(value.id).toBe(1);
} else {
throw new Error('Type narrowing failed');
}
});
});
/**
* 单元测试: isArrayOfObject
*/
describe('isArrayOfObject', () => {
test('identify array of plain objects', () => {
const arr = [{ id: 1 }, { name: 'Alice' }];
expect(isArrayOfObject<{ id?: number; name?: string }>(arr)).toBe(true);
});
test('identify array containing non-objects', () => {
expect(isArrayOfObject([1, 2, 3])).toBe(false);
expect(isArrayOfObject([{ id: 1 }, null])).toBe(false);
expect(isArrayOfObject([{ id: 1 }, 'string'])).toBe(false);
});
test('identify non-array types', () => {
expect(isArrayOfObject(null)).toBe(false);
expect(isArrayOfObject({})).toBe(false);
expect(isArrayOfObject(42)).toBe(false);
});
test('identify empty array as array of objects', () => {
expect(isArrayOfObject([])).toBe(true);
});
test('identify narrowed array of object type', () => {
const data: unknown = [{ id: 1 }, { id: 2 }];
if (isArrayOfObject<{ id: number }>(data)) {
// TS 能识别为 { id: number }[]
expect(data[0].id).toBe(1);
expect(data[1].id).toBe(2);
} else {
throw new Error('Type guard failed');
}
});
});

View File

@ -0,0 +1,75 @@
import { describe, expect, test } from 'vitest';
/**
* 单元测试: converters
*/
describe('converters', () => {
test('convert product item', () => {
const item = {
id: 1,
name: 'Hydraulic Pump',
summary: 'High efficiency',
description: 'Detailed description',
type: 'pump',
};
const result = converters.products(item);
expect(result).toEqual({
id: 1,
type: 'product',
title: 'Hydraulic Pump',
summary: 'High efficiency',
});
});
test('convert solution item', () => {
const item = {
id: 1,
title: 'Solution A',
summary: 'Effective solution',
content: 'Detailed content',
type: 'Type A',
};
const result = converters.solutions(item);
expect(result).toEqual({
id: 1,
type: 'solution',
title: 'Solution A',
summary: 'Effective solution',
});
});
test('convert question item', () => {
const item = {
id: 1,
title: 'How to use product?',
content:
'This is a detailed explanation of how to use the product effectively.',
products: ['Product A'],
product_types: ['Type A'],
};
const result = converters.questions(item);
expect(result).toEqual({
id: 1,
title: 'How to use product?',
summary:
'This is a detailed explanation of how to use the product effectively....',
type: 'question',
});
});
test('convert product document item', () => {
const item = {
id: 1,
title: 'User Manual',
products: ['Product A'],
product_types: ['Type A'],
};
const result = converters.product_documents(item);
expect(result).toEqual({
id: 1,
title: 'User Manual',
summary: undefined,
type: 'document',
});
});
});

46
app/utils/file.test.ts Normal file
View File

@ -0,0 +1,46 @@
import { expect, test, describe } from 'vitest';
/**
* 单元测试: formatFileSize
*/
describe('formatFileSize', () => {
test('format bytes correctly', () => {
expect(formatFileSize(500)).toBe('500.00 B');
});
test('format kilobytes correctly', () => {
expect(formatFileSize(2048)).toBe('2.00 KB');
});
test('format megabytes correctly', () => {
expect(formatFileSize(5 * 1024 * 1024)).toBe('5.00 MB');
});
});
/**
* 单元测试: getFileExtension
*/
describe('getFileExtension', () => {
test('extract extension from filename', () => {
expect(getFileExtension('document.pdf')).toBe('pdf');
});
test('handle filenames without extension', () => {
expect(getFileExtension('README')).toBe('');
});
test('handle multiple dots in filename', () => {
expect(getFileExtension('archive.tar.gz')).toBe('gz');
});
test('handle empty filename', () => {
expect(getFileExtension('')).toBe('');
});
});
/**
* 单元测试: formatFileExtension
*/
describe('formatFileExtension', () => {
test('format extension without dot', () => {
expect(formatFileExtension('txt')).toBe('TXT');
});
test('format extension with dot', () => {
expect(formatFileExtension('.jpg')).toBe('JPG');
});
});

View File

@ -11,7 +11,9 @@ export function formatFileSize(sizeInBytes: number): string {
}
export function getFileExtension(filename: string): string {
return filename.split('.').pop() || '';
const lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex === -1) return ''; // 找不到拓展名
return filename.slice(lastDotIndex + 1);
}
export function formatFileExtension(ext: string): string {