- Expand batch upload list area and optimize preview image layout - Improve upload speed tracking and display (server-side & client-side) - Reduce network detection log noise - Optimize Docker build process (caching, env validation) - Add shadow environment deployment support
121 lines
4.3 KiB
TypeScript
121 lines
4.3 KiB
TypeScript
/**
|
||
* R44修复:JSON 解析异常处理测试
|
||
*
|
||
* 测试内容:
|
||
* - 验证 parseDisplayImages 函数能正确处理各种 JSON 格式
|
||
* - 验证异常情况下能安全降级
|
||
*/
|
||
|
||
// 复用 API 路由中的 parseDisplayImages 函数
|
||
function parseDisplayImages(displayImages: any): string[] {
|
||
if (!displayImages) return []
|
||
|
||
if (Array.isArray(displayImages)) {
|
||
return displayImages.filter((img: any) => typeof img === 'string' && img.trim().length > 0)
|
||
}
|
||
|
||
if (typeof displayImages === 'string') {
|
||
try {
|
||
const parsed = JSON.parse(displayImages)
|
||
if (Array.isArray(parsed)) {
|
||
return parsed.filter((img: any) => typeof img === 'string' && img.trim().length > 0)
|
||
}
|
||
} catch (e) {
|
||
// JSON 解析失败,降级处理
|
||
}
|
||
return displayImages.trim() ? [displayImages] : []
|
||
}
|
||
|
||
return []
|
||
}
|
||
|
||
describe('R44: JSON 解析异常处理', () => {
|
||
describe('parseDisplayImages', () => {
|
||
it('应该处理 null 和 undefined', () => {
|
||
expect(parseDisplayImages(null)).toEqual([])
|
||
expect(parseDisplayImages(undefined)).toEqual([])
|
||
})
|
||
|
||
it('应该处理空字符串', () => {
|
||
expect(parseDisplayImages('')).toEqual([])
|
||
expect(parseDisplayImages(' ')).toEqual([])
|
||
})
|
||
|
||
it('应该处理数组类型输入', () => {
|
||
const input = ['url1', 'url2', 'url3']
|
||
expect(parseDisplayImages(input)).toEqual(input)
|
||
})
|
||
|
||
it('应该过滤数组中的空字符串', () => {
|
||
const input = ['url1', '', 'url2', ' ', 'url3']
|
||
expect(parseDisplayImages(input)).toEqual(['url1', 'url2', 'url3'])
|
||
})
|
||
|
||
it('应该过滤数组中的非字符串值', () => {
|
||
const input = ['url1', 123, null, undefined, 'url2', { url: 'test' }]
|
||
expect(parseDisplayImages(input)).toEqual(['url1', 'url2'])
|
||
})
|
||
|
||
it('应该解析有效的 JSON 字符串', () => {
|
||
const input = '["url1", "url2", "url3"]'
|
||
expect(parseDisplayImages(input)).toEqual(['url1', 'url2', 'url3'])
|
||
})
|
||
|
||
it('应该处理单值 JSON 字符串', () => {
|
||
// JSON 字符串 "single-url" 解析为字符串 'single-url',不是数组
|
||
// 函数降级为原始字符串作为单值
|
||
expect(parseDisplayImages('"single-url"')).toEqual(['"single-url"'])
|
||
expect(parseDisplayImages('single-url')).toEqual(['single-url'])
|
||
})
|
||
|
||
it('应该安全处理无效的 JSON 字符串', () => {
|
||
// 无效 JSON,降级为单值数组
|
||
expect(parseDisplayImages('not-valid-json')).toEqual(['not-valid-json'])
|
||
expect(parseDisplayImages('{invalid-json}')).toEqual(['{invalid-json}'])
|
||
})
|
||
|
||
it('应该处理不完整的 JSON 数组', () => {
|
||
// 不完整的 JSON,降级为原始字符串
|
||
expect(parseDisplayImages('["url1", "url2"')).toEqual(['["url1", "url2"'])
|
||
expect(parseDisplayImages('url1", "url2"]')).toEqual(['url1", "url2"]'])
|
||
})
|
||
|
||
it('应该处理 JSON 对象(非数组)', () => {
|
||
// JSON 对象会被解析,但不是数组,降级为原始字符串
|
||
expect(parseDisplayImages('{"key": "value"}')).toEqual(['{"key": "value"}'])
|
||
})
|
||
|
||
it('应该处理包含空格的 JSON', () => {
|
||
const input = ' ["url1", "url2"] '
|
||
expect(parseDisplayImages(input)).toEqual(['url1', 'url2'])
|
||
})
|
||
|
||
it('应该处理嵌套数组(只取第一层)', () => {
|
||
const input = '[["url1", "url2"], ["url3"]]'
|
||
// 第一层元素不是字符串,应该被过滤掉
|
||
expect(parseDisplayImages(input)).toEqual([])
|
||
})
|
||
|
||
it('应该处理 URL 编码的 JSON', () => {
|
||
// 模拟 encodeURIComponent 后的 JSON
|
||
const jsonStr = JSON.stringify(['url1', 'url2'])
|
||
const encoded = encodeURIComponent(jsonStr)
|
||
const decoded = decodeURIComponent(encoded)
|
||
expect(parseDisplayImages(decoded)).toEqual(['url1', 'url2'])
|
||
})
|
||
|
||
it('应该处理包含特殊字符的 URL', () => {
|
||
const input = '["http://example.com/image.jpg?size=large&quality=high", "https://test.com/photo(1).jpg"]'
|
||
expect(parseDisplayImages(input)).toEqual([
|
||
'http://example.com/image.jpg?size=large&quality=high',
|
||
'https://test.com/photo(1).jpg',
|
||
])
|
||
})
|
||
|
||
it('应该处理混合类型的数组(只保留字符串)', () => {
|
||
const input = ['string1', '', 123, true, null, undefined, 'string2']
|
||
expect(parseDisplayImages(input)).toEqual(['string1', 'string2'])
|
||
})
|
||
})
|
||
})
|