cmvr-wms/scripts/projectdev/vue3-global-components-regression.cjs

121 lines
7.5 KiB
JavaScript
Raw Normal View History

const assert = require('node:assert/strict')
const { chromium, frontend: base, backend, username, password } = require('./vue3-test-runtime.cjs')
;(async () => {
const login = await (await fetch(backend + '/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password, code: '', uuid: '' }) })).json()
assert.equal(login.code, 200)
const browser = await chromium.launch()
try {
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } })
await context.addCookies([{ name: 'Admin-Token', value: login.token, url: base }])
const page = await context.newPage(), errors = [], mutations = [], uploads = []
page.on('pageerror', error => errors.push(error.message))
await page.route('**/dev-api/**', route => {
const req = route.request(), url = new URL(req.url())
if (url.pathname === '/dev-api/unlockscreen') {
mutations.push(url.pathname)
return route.fulfill({ json: req.postDataJSON().password === 'test-unlock' ? { code: 200 } : { code: 500, msg: '密码错误' } })
}
if (url.pathname === '/dev-api/common/upload') {
uploads.push(url.pathname)
return route.fulfill({ json: { code: 500, msg: 'mock upload rejection' } })
}
if (url.pathname === '/dev-api/logout') {
mutations.push(url.pathname)
return route.fulfill({ json: { code: 500, msg: 'mock logout rejection' } })
}
if (!['GET', 'HEAD'].includes(req.method())) return route.abort('blockedbyclient')
return route.continue()
})
await page.goto(base + '/procurement/details')
await page.getByRole('button', { name: '新增采购批次', exact: true }).click()
const name = page.locator('.batch-form .el-form-item').filter({ hasText: '名称' }).locator('input')
await name.fill('LOCK-UNSAVED')
const uid = await page.locator('.purchase-sheet').evaluate(el => el.__vueParentComponent.uid)
await page.locator('.avatar-wrapper').hover()
await page.getByRole('menuitem', { name: '锁定屏幕', exact: true }).click()
await page.locator('.lock-container').waitFor()
assert.equal(new URL(page.url()).pathname, '/procurement/details')
assert.equal(await page.locator('.el-message-box:visible').count(), 0)
await page.locator('.lock-footer a').click()
await page.getByText('退出失败,请重试,系统仍保持锁定', { exact: true }).waitFor()
assert.equal(await page.locator('.lock-container').count(), 1)
await page.locator('.lock-input').fill('wrong')
await page.locator('.unlock-btn').click()
await page.locator('.error-msg').waitFor()
assert.equal(await page.locator('.lock-container').count(), 1)
await page.keyboard.press('Escape')
assert.equal(await page.locator('.lock-container').count(), 1)
await page.locator('.lock-input').fill('test-unlock')
await page.locator('.unlock-btn').click()
await page.locator('.lock-container').waitFor({ state: 'detached' })
assert.equal(await name.inputValue(), 'LOCK-UNSAVED')
assert.equal(await page.locator('.purchase-sheet').evaluate(el => el.__vueParentComponent.uid), uid)
console.log('PASS lock overlay preserves draft/component; incorrect password and Escape cannot unlock')
const uploadResults = await page.evaluate(async () => {
const { createApp, h, nextTick, ElementPlus } = await import('/tests/browser-runtime.js')
const results = []
for (const name of ['FileUpload', 'ImageUpload']) {
const { default: Component } = await import('/src/components/' + name + '/index.vue')
const emitted = [], host = document.createElement('div')
document.body.append(host)
const app = createApp({ render: () => h(Component, { drag: true, 'onUpdate:modelValue': value => emitted.push(value) }) }).use(ElementPlus)
let closes = 0
app.config.globalProperties.$modal = { loading() {}, closeLoading() { closes++ }, msgError() {} }
app.mount(host); await nextTick(); await nextTick()
const component = app._instance.subTree.component
const vm = component.setupState
const image = name === 'ImageUpload'
vm.handleBeforeUpload({ name: image ? 'one.PNG' : 'one.PDF', type: image ? 'image/png' : 'application/pdf', size: 4, uid: 1 })
vm.handleBeforeUpload({ name: image ? 'two.PNG' : 'two.PDF', type: image ? 'image/png' : 'application/pdf', size: 4, uid: 2 })
vm.handleUploadError(new Error('mock network error'), { uid: 1 })
vm.handleUploadSuccess({ code: 200, fileName: '/profile/mock-success.' + (image ? 'png' : 'pdf') }, { uid: 2 })
await nextTick()
results.push({ name, pending: vm.number, emitted, closes, count: vm.fileList.length })
app.unmount(); host.remove()
}
return results
})
for (const result of uploadResults) {
assert.equal(result.pending, 0, result.name)
assert.equal(result.count, 1, result.name)
assert.equal(result.emitted.length, 1, result.name)
assert.match(result.emitted[0], /mock-success/)
assert.equal(result.closes, 1, result.name)
}
const editor = await page.evaluate(async () => {
const { createApp, h, nextTick, ElementPlus } = await import('/tests/browser-runtime.js')
const { default: Editor } = await import('/src/components/Editor/index.vue')
const host = document.createElement('div'); document.body.append(host)
const messages = [], app = createApp(Editor).use(ElementPlus)
app.config.globalProperties.$modal = { msgError: message => messages.push(message) }
app.mount(host); await nextTick(); await new Promise(resolve => setTimeout(resolve, 100))
const vm = app._instance.setupState
await vm.insertImage(new File(['bad'], 'bad.txt', { type: 'text/plain' }))
await vm.insertImage(new File([new Uint8Array(6 * 1024 * 1024)], 'large.png', { type: 'image/png' }))
await vm.insertImage(new File(['mock'], 'valid.png', { type: 'image/png' }))
const quill = vm.quillEditorRef.getQuill()
quill.selection.savedRange = null
vm.handleUploadSuccess({ code: 200, fileName: '/profile/mock.png' })
await nextTick()
const inserted = host.querySelectorAll('.ql-editor img').length
app.unmount(); host.remove()
const icon = document.querySelector('.desktop-viewport').__vueParentComponent.appContext.components.Watermelon
const iconHost = document.createElement('div'); document.body.append(iconHost)
const iconApp = createApp({ render: () => h(icon) }); iconApp.mount(iconHost)
for (let i = 0; i < 30 && !iconHost.querySelector('svg path'); i++) await new Promise(resolve => setTimeout(resolve, 50))
const fallbackIcon = !!iconHost.querySelector('svg path')
iconApp.unmount(); iconHost.remove()
return { messages, inserted, fallbackIcon }
})
assert.equal(uploads.length, 1, 'invalid pasted files must not start requests')
assert.equal(editor.messages.length, 3)
assert.equal(editor.inserted, 1, 'image insertion tolerates an absent saved selection')
assert.equal(editor.fallbackIcon, true, 'uncommon dynamic icons remain available')
assert.deepEqual(errors, [])
assert.equal(mutations.length, 3)
console.log('PASS mixed upload failure/success settles counters and keeps successful attachments; all business writes blocked')
console.log('PASS editor paste validation/rejection/empty cursor and lazy dynamic icon fallback')
} finally { await browser.close() }
})().catch(error => { console.error(error); process.exitCode = 1 })