refactor(品牌工具页): 抽取 formatDateTime 与 uploadPathsToJava 公共工具
- 新增 shared/utils/datetime.ts:13 个品牌页逐字重复的 formatDateTime 收敛为一处; 语义不同的 3 个变体(toLocaleString / 字符串切片 / 支持时间戳)保留不动 - 新增 shared/utils/upload-to-java.ts:10 个品牌页的上传循环收敛,api 依赖注入便于单测; 保留 uploadOss(品牌Tab)与 returnEmptyWhenUnavailable(跟价)两处行为差异 - 补 datetime / upload-to-java 单测 10 例 净减约 327 行;vue-tsc 构建与 690 个前端单测全通过
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { formatDateTime } from '../src/shared/utils/datetime.ts'
|
||||
|
||||
// 用本地时间构造再往返 ISO,期望值由同一本地时区还原,避免依赖运行环境的时区。
|
||||
test('formatDateTime 空值返回 -', () => {
|
||||
assert.equal(formatDateTime(undefined), '-')
|
||||
assert.equal(formatDateTime(''), '-')
|
||||
})
|
||||
|
||||
test('formatDateTime 无法解析时原样返回', () => {
|
||||
assert.equal(formatDateTime('not-a-date'), 'not-a-date')
|
||||
})
|
||||
|
||||
test('formatDateTime 按 YYYY-MM-DD HH:mm:ss 补零(个位月/日/时/分/秒)', () => {
|
||||
const local = new Date(2026, 0, 5, 9, 7, 3)
|
||||
assert.equal(formatDateTime(local.toISOString()), '2026-01-05 09:07:03')
|
||||
})
|
||||
|
||||
test('formatDateTime 年末边界', () => {
|
||||
const local = new Date(2026, 11, 31, 23, 59, 59)
|
||||
assert.equal(formatDateTime(local.toISOString()), '2026-12-31 23:59:59')
|
||||
})
|
||||
@@ -0,0 +1,70 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { uploadPathsToJava } from '../src/shared/utils/upload-to-java.ts'
|
||||
import type { PywebviewApi } from '../src/shared/bridges/pywebview.ts'
|
||||
|
||||
function fakeApi(
|
||||
upload: (
|
||||
filePath: string,
|
||||
relativePath?: string,
|
||||
uploadOss?: boolean,
|
||||
) => Promise<{ success: boolean; data?: never; error?: string; message?: string }>,
|
||||
) {
|
||||
return { upload_file_to_java: upload } as unknown as PywebviewApi
|
||||
}
|
||||
|
||||
function okData(filePath: string) {
|
||||
return { fileKey: `k:${filePath}`, originalFilename: filePath, localPath: '', size: 1 }
|
||||
}
|
||||
|
||||
test('uploadPathsToJava 逐个上传并返回结果列表', async () => {
|
||||
const calls: Array<[string, string | undefined, boolean | undefined]> = []
|
||||
const api = fakeApi(async (filePath, relativePath, uploadOss) => {
|
||||
calls.push([filePath, relativePath, uploadOss])
|
||||
return { success: true, data: okData(filePath) as never }
|
||||
})
|
||||
|
||||
const result = await uploadPathsToJava(api, ['/a.xlsx', { absolutePath: '/b.xlsx', relativePath: 'sub/b.xlsx' }])
|
||||
|
||||
assert.equal(result.length, 2)
|
||||
assert.deepEqual(calls, [
|
||||
['/a.xlsx', undefined, undefined],
|
||||
['/b.xlsx', 'sub/b.xlsx', undefined],
|
||||
])
|
||||
})
|
||||
|
||||
test('uploadPathsToJava 透传 uploadOss 参数', async () => {
|
||||
let seen: boolean | undefined
|
||||
const api = fakeApi(async (filePath, _relativePath, uploadOss) => {
|
||||
seen = uploadOss
|
||||
return { success: true, data: okData(filePath) as never }
|
||||
})
|
||||
|
||||
await uploadPathsToJava(api, ['/a.xlsx'], { uploadOss: true })
|
||||
|
||||
assert.equal(seen, true)
|
||||
})
|
||||
|
||||
test('uploadPathsToJava 任一失败即整体抛错并带上桥返回的文案', async () => {
|
||||
const api = fakeApi(async (filePath) =>
|
||||
filePath === '/bad.xlsx'
|
||||
? { success: false, error: '磁盘读取失败' }
|
||||
: { success: true, data: okData(filePath) as never },
|
||||
)
|
||||
|
||||
await assert.rejects(() => uploadPathsToJava(api, ['/ok.xlsx', '/bad.xlsx']), /磁盘读取失败/)
|
||||
})
|
||||
|
||||
test('uploadPathsToJava 失败无文案时回落到「上传失败:路径」', async () => {
|
||||
const api = fakeApi(async () => ({ success: false }))
|
||||
|
||||
await assert.rejects(() => uploadPathsToJava(api, ['/x.xlsx']), /上传失败:\/x\.xlsx/)
|
||||
})
|
||||
|
||||
test('uploadPathsToJava 缺上传桥默认抛错', async () => {
|
||||
await assert.rejects(() => uploadPathsToJava(undefined, ['/a.xlsx']), /未提供文件上传能力/)
|
||||
})
|
||||
|
||||
test('uploadPathsToJava 缺上传桥时 returnEmptyWhenUnavailable 返回空数组', async () => {
|
||||
assert.deepEqual(await uploadPathsToJava(undefined, ['/a.xlsx'], { returnEmptyWhenUnavailable: true }), [])
|
||||
})
|
||||
Reference in New Issue
Block a user