156 lines
7.3 KiB
TypeScript
156 lines
7.3 KiB
TypeScript
// P2-10(后管 AC/user-notice「只把中文给用户看」口径)。
|
||
//
|
||
// 评审实录:`RecordsSoftwareVersionPage.vue`/`RecordsTutorialPage.vue` 的 catch 直接写
|
||
// `error instanceof Error ? error.message : '上传失败'`,而 `src/api/http.ts` 的响应拦截器
|
||
// **不做 sanitize**(只挑 401/403/4011)⇒ `Broken pipe`、`Request failed with status code 403`
|
||
// 这类英文异常原文整段上屏。收口方式沿用本仓既有出口 `components/load-failure-model`
|
||
// 的 `loadFailureMessage(error, 本页兜底句)`(去重页四条写路径同一写法)。
|
||
//
|
||
// 跑法:npm run test:mount(tests/mount/**,vitest+jsdom)。
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { flushPromises, mount } from '@vue/test-utils'
|
||
import { readFileSync } from 'node:fs'
|
||
import { dirname, resolve } from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
import RecordsSoftwareVersionPage from '@/pages/records/RecordsSoftwareVersionPage.vue'
|
||
import RecordsTutorialPage from '@/pages/records/RecordsTutorialPage.vue'
|
||
import { loadFailureMessage } from '@/components/load-failure-model'
|
||
|
||
const here = dirname(fileURLToPath(import.meta.url))
|
||
const root = resolve(here, '../..')
|
||
|
||
const versionApi = vi.hoisted(() => ({
|
||
fetchSoftwareVersions: vi.fn(),
|
||
uploadSoftwareVersion: vi.fn(),
|
||
deleteSoftwareVersions: vi.fn(),
|
||
}))
|
||
const tutorialApi = vi.hoisted(() => ({
|
||
fetchTutorialPackages: vi.fn(),
|
||
uploadTutorialPackage: vi.fn(),
|
||
deleteTutorialPackages: vi.fn(),
|
||
}))
|
||
const confirmMock = vi.hoisted(() => vi.fn())
|
||
const feedbackMock = vi.hoisted(() => vi.fn())
|
||
const messageMock = vi.hoisted(() => ({ success: vi.fn() }))
|
||
|
||
vi.mock('@/pages/records/version-api.ts', () => versionApi)
|
||
vi.mock('@/pages/records/tutorial-api.ts', () => tutorialApi)
|
||
vi.mock('@/components/admin-confirm', () => ({ openAdminConfirm: confirmMock }))
|
||
vi.mock('@/components/admin-feedback-ui', () => ({ showAdminFeedback: feedbackMock }))
|
||
vi.mock('element-plus', () => ({ ElMessage: messageMock }))
|
||
|
||
/** el-upload 的桩:把页面传进来的 on-change 回调顶出来,等价于用户选了一个 .zip。 */
|
||
const uploadStub = {
|
||
props: ['onChange'],
|
||
template: '<button class="pick-zip" @click="onChange({ raw: fake })">选择文件</button>',
|
||
data: () => ({ fake: new File([new Uint8Array(16)], 'pkg.zip', { type: 'application/zip' }) }),
|
||
}
|
||
|
||
const stubs = {
|
||
'el-dialog': { template: '<div><slot /><slot name="footer" /></div>' },
|
||
'el-form': { template: '<form><slot /></form>' },
|
||
'el-form-item': { template: '<div><slot /></div>' },
|
||
'el-input': {
|
||
props: ['modelValue'],
|
||
emits: ['update:modelValue'],
|
||
template: '<input :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" />',
|
||
},
|
||
'el-upload': uploadStub,
|
||
'el-button': { template: '<button><slot /></button>' },
|
||
'el-progress': true,
|
||
'el-alert': { props: ['title'], template: '<div class="upload-msg">{{ title }}</div>' },
|
||
}
|
||
|
||
/** 后端/驱动层原文:一句中文都没有,正是过去直接上屏的那种。 */
|
||
const RAW_ENGLISH = 'Broken pipe: java.io.IOException at sun.nio.ch.FileDispatcherImpl.write0'
|
||
|
||
function clickButtonByText(wrapper: ReturnType<typeof mount>, text: string, last = false) {
|
||
const matches = wrapper.findAll('button').filter((node) => node.text().trim() === text)
|
||
const button = last ? matches[matches.length - 1] : matches[0]
|
||
if (!button) throw new Error(`找不到文案为「${text}」的按钮`)
|
||
return button
|
||
}
|
||
|
||
async function submitZipUpload(component: typeof RecordsSoftwareVersionPage, versionPlaceholder: string, submitText: string, openText = '上传新版本') {
|
||
const wrapper = mount(component, { global: { stubs } })
|
||
await flushPromises()
|
||
|
||
clickButtonByText(wrapper, openText)
|
||
const versionInput = wrapper.find(`input[placeholder="${versionPlaceholder}"]`)
|
||
if (versionInput.exists()) await versionInput.setValue('9.9.9')
|
||
await wrapper.get('.pick-zip').trigger('click')
|
||
await clickButtonByText(wrapper, submitText, true).trigger('click')
|
||
await flushPromises()
|
||
return wrapper
|
||
}
|
||
|
||
describe('P2-10 后管上传/加载失败只把中文交给界面', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
versionApi.fetchSoftwareVersions.mockResolvedValue({ items: [] })
|
||
tutorialApi.fetchTutorialPackages.mockResolvedValue({ items: [] })
|
||
confirmMock.mockResolvedValue(true)
|
||
})
|
||
|
||
it('真实出口对照:英文原文一律被换成中文,原因段不拼半截句', () => {
|
||
expect(loadFailureMessage(new Error(RAW_ENGLISH), '上传失败')).toBe('上传失败')
|
||
expect(loadFailureMessage(new Error('分片会话不存在或已过期'), '上传失败'))
|
||
.toBe('上传失败:分片会话不存在或已过期')
|
||
})
|
||
|
||
it('安装包上传失败:界面文案不含英文原文,原文只进 console', async () => {
|
||
const warned = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||
versionApi.uploadSoftwareVersion.mockRejectedValue(new Error(RAW_ENGLISH))
|
||
|
||
const wrapper = await submitZipUpload(RecordsSoftwareVersionPage, '例如:1.0.0', '上传软件版本')
|
||
|
||
const shown = wrapper.get('.upload-msg').text()
|
||
expect(shown, '英文异常原文仍在上屏').not.toContain('Broken pipe')
|
||
expect(shown).toContain('上传失败')
|
||
expect(
|
||
warned.mock.calls.some((call) => String(call.join(' ')).includes(RAW_ENGLISH)),
|
||
'原文要留在 console,否则支持同学对着后端日志无从核对',
|
||
).toBe(true)
|
||
warned.mockRestore()
|
||
})
|
||
|
||
it('教程包上传失败:同一条中文出口,不得再直落 error.message', async () => {
|
||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||
tutorialApi.uploadTutorialPackage.mockRejectedValue(new Error(RAW_ENGLISH))
|
||
|
||
const wrapper = await submitZipUpload(RecordsTutorialPage, '例如:v2026.09 或留空', '上传教程包', '上传教程包')
|
||
|
||
const shown = wrapper.get('.upload-msg').text()
|
||
expect(shown, '英文异常原文仍在上屏').not.toContain('Broken pipe')
|
||
expect(shown).toContain('上传失败')
|
||
})
|
||
|
||
it('列表加载失败:LoadFailureBar 的文案也是中文', async () => {
|
||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||
versionApi.fetchSoftwareVersions.mockRejectedValue(new Error('Internal Server Error'))
|
||
|
||
const wrapper = mount(RecordsSoftwareVersionPage, { global: { stubs } })
|
||
await flushPromises()
|
||
|
||
const shown = wrapper.text()
|
||
expect(shown).not.toContain('Internal Server Error')
|
||
expect(shown).toContain('版本列表加载失败')
|
||
expect(feedbackMock).toHaveBeenCalled()
|
||
})
|
||
|
||
// 写法收口的台账:两页四条 catch(列表/删除×2/上传)都必须把错误交给同一个出口。
|
||
it('两页不得再留 error instanceof Error ? error.message 那套写法', () => {
|
||
for (const file of [
|
||
'src/pages/records/RecordsSoftwareVersionPage.vue',
|
||
'src/pages/records/RecordsTutorialPage.vue',
|
||
]) {
|
||
const source = readFileSync(resolve(root, file), 'utf-8')
|
||
expect(source, `${file} 仍直落 error.message`).not.toMatch(/error instanceof Error \? error\.message/)
|
||
expect(source.match(/loadFailureMessage\(error,/g)?.length ?? 0, `${file} 的失败出口没接上`)
|
||
.toBeGreaterThanOrEqual(4)
|
||
expect(source).toMatch(/import \{ loadFailureMessage \} from '@\/components\/load-failure-model'/)
|
||
}
|
||
})
|
||
})
|