更新
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
const assert = require('node:assert/strict')
|
||||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const test = require('node:test')
|
||||
const { parse, compileScript, compileTemplate } = require('@vue/compiler-sfc')
|
||||
const ts = require('typescript')
|
||||
const vue = require('vue')
|
||||
|
||||
const filename = path.join(__dirname, '../src/views/consumer/prescription/components/PrescriptionOrderTimeDialog.vue')
|
||||
const source = fs.readFileSync(filename, 'utf8')
|
||||
const { descriptor, errors } = parse(source, { filename })
|
||||
assert.deepEqual(errors, [])
|
||||
const script = compileScript(descriptor, { id: 'order-time-test' })
|
||||
const template = compileTemplate({
|
||||
source: descriptor.template.content,
|
||||
filename,
|
||||
id: 'order-time-test',
|
||||
compilerOptions: { bindingMetadata: script.bindings }
|
||||
})
|
||||
assert.deepEqual(template.errors, [])
|
||||
const compiled = ts.transpileModule(script.content, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }
|
||||
}).outputText
|
||||
|
||||
function dialog(save = async () => ({})) {
|
||||
const calls = []
|
||||
const events = []
|
||||
const module = { exports: {} }
|
||||
const mockRequire = (name) => {
|
||||
if (name === 'vue') return vue
|
||||
if (name === '@/api/tcm') return {
|
||||
prescriptionOrderEditTime: async (payload) => {
|
||||
calls.push(payload)
|
||||
return save(payload)
|
||||
}
|
||||
}
|
||||
if (name === '@/utils/feedback') return { default: { msgSuccess() {} } }
|
||||
throw new Error(`Unexpected dependency: ${name}`)
|
||||
}
|
||||
new Function('require', 'module', 'exports', compiled)(mockRequire, module, module.exports)
|
||||
const state = module.exports.default.setup({}, {
|
||||
expose() {},
|
||||
emit: (...event) => events.push(event)
|
||||
})
|
||||
state.formRef.value = { validate: async () => true, clearValidate() {} }
|
||||
return { state, calls, events }
|
||||
}
|
||||
|
||||
test('opening and saving preserves creation time seconds and submits only the selected order', async () => {
|
||||
const { state, calls, events } = dialog()
|
||||
state.open({ id: 42, order_no: 'RX42', create_time: '2026-09-09 11:12:37' })
|
||||
assert.equal(state.form.create_time, '2026-09-09 11:12:37')
|
||||
state.form.create_time = '2026-08-31 09:08:07'
|
||||
await state.submit()
|
||||
assert.deepEqual(calls, [{ id: 42, create_time: '2026-08-31 09:08:07' }])
|
||||
assert.deepEqual(events, [['saved', 42]])
|
||||
assert.equal(state.visible.value, false)
|
||||
})
|
||||
|
||||
test('legacy seconds, milliseconds and strings populate the same local picker time', () => {
|
||||
const { state } = dialog()
|
||||
const date = new Date(2026, 8, 9, 11, 12, 37)
|
||||
for (const value of [date.getTime() / 1000, String(date.getTime() / 1000), date.getTime(), '2026-09-09T11:12:37']) {
|
||||
state.open({ id: 1, create_time: value })
|
||||
assert.equal(state.form.create_time, '2026-09-09 11:12:37')
|
||||
}
|
||||
state.open({ id: 1, create_time: '2026-09-09 11:12' })
|
||||
assert.equal(state.form.create_time, '2026-09-09 11:12:00')
|
||||
state.open({ id: 1, create_time: 0 })
|
||||
assert.equal(state.form.create_time, '')
|
||||
})
|
||||
|
||||
test('validation failures do not send requests, and failed saves retain editable input', async () => {
|
||||
const { state, calls, events } = dialog(async () => { throw new Error('denied') })
|
||||
state.open({ id: 9, create_time: '2026-09-09 11:12:37' })
|
||||
state.formRef.value.validate = async () => { throw new Error('required') }
|
||||
await state.submit()
|
||||
assert.equal(calls.length, 0)
|
||||
state.formRef.value.validate = async () => true
|
||||
await state.submit()
|
||||
assert.equal(state.visible.value, true)
|
||||
assert.equal(state.submitting.value, false)
|
||||
assert.equal(state.form.create_time, '2026-09-09 11:12:37')
|
||||
assert.deepEqual(events, [])
|
||||
})
|
||||
|
||||
test('a pending request cannot submit twice or switch its target order', async () => {
|
||||
let finish
|
||||
const pending = new Promise((resolve) => { finish = resolve })
|
||||
const { state, calls, events } = dialog(() => pending)
|
||||
state.open({ id: 7, create_time: '2026-09-09 11:12:37' })
|
||||
const saving = state.submit()
|
||||
await state.submit()
|
||||
await new Promise((resolve) => setImmediate(resolve))
|
||||
state.open({ id: 8, create_time: '2026-09-08 00:00:00' })
|
||||
await state.submit()
|
||||
assert.equal(calls.length, 1)
|
||||
assert.equal(state.form.id, 7)
|
||||
finish({})
|
||||
await saving
|
||||
assert.deepEqual(events, [['saved', 7]])
|
||||
})
|
||||
@@ -0,0 +1,154 @@
|
||||
const assert = require('node:assert/strict')
|
||||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const test = require('node:test')
|
||||
const { parse, compileScript, compileTemplate, compileStyle } = require('@vue/compiler-sfc')
|
||||
const ts = require('typescript')
|
||||
const vue = require('vue')
|
||||
|
||||
const filename = path.join(__dirname, '../src/views/first_visit/wecom_promotion/index.vue')
|
||||
const { descriptor, errors } = parse(fs.readFileSync(filename, 'utf8'), { filename })
|
||||
assert.deepEqual(errors, [])
|
||||
const script = compileScript(descriptor, { id: 'member-sync-test' })
|
||||
const template = compileTemplate({ source: descriptor.template.content, filename, id: 'member-sync-test', compilerOptions: { bindingMetadata: script.bindings } })
|
||||
assert.deepEqual(template.errors, [])
|
||||
assert.deepEqual(compileStyle({ source: descriptor.styles[0].content, filename, id: 'member-sync-test', scoped: true, preprocessLang: 'scss' }).errors, [])
|
||||
|
||||
function loadModule(source, mockRequire) {
|
||||
const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
|
||||
const module = { exports: {} }
|
||||
new Function('require', 'module', 'exports', compiled)(mockRequire, module, module.exports)
|
||||
return module.exports
|
||||
}
|
||||
|
||||
const automation = loadModule(fs.readFileSync(path.join(path.dirname(filename), 'components/promotion-automation.ts'), 'utf8'), require)
|
||||
const member = (id, enabled = 1) => ({ id, admin_id: id, userid: `member-${id}`, name: `医助 ${id}`, enabled, reception_available: enabled === 1, is_in_remote_range: true })
|
||||
const pool = (id = 1) => ({ id, name: `方案 ${id}`, status: 1, can_operate: true, can_manage_access: true, member_admin_ids: [1, 2], member_rules: [member(1), member(2, 0)], official_link: { range_userids: ['member-1', 'member-2'] }, dispatch_sync: { status: 1 } })
|
||||
|
||||
function page(api = {}, pools = [pool()]) {
|
||||
const messages = []
|
||||
const emitMessage = (type, value) => messages.push({ type, message: typeof value === 'string' ? value : value.message })
|
||||
const ElMessage = (value) => emitMessage(value.type, value)
|
||||
for (const type of ['success', 'warning', 'error']) ElMessage[type] = (value) => emitMessage(type, value)
|
||||
const instance = loadModule(script.content, (name) => {
|
||||
if (name === 'vue') return { ...vue, onMounted() {} }
|
||||
if (name === 'element-plus') return { ElMessage, ElMessageBox: {} }
|
||||
if (name === '@element-plus/icons-vue') return {}
|
||||
if (name === '@/api/first_visit') return { wecomPromotionOverview: async () => ({ pools }), ...api }
|
||||
if (name.endsWith('.vue')) return {}
|
||||
if (name === './components/promotion-automation') return automation
|
||||
throw new Error(`Unexpected dependency: ${name}`)
|
||||
}).default.setup({}, { expose() {} })
|
||||
Object.assign(instance.overview, { pools })
|
||||
instance.selectedPoolId.value = pools[0]?.id
|
||||
return { state: instance, messages }
|
||||
}
|
||||
|
||||
test('disabled local member still in remote snapshot is explicitly pending removal', () => {
|
||||
const { state } = page()
|
||||
assert.equal(state.routeStatus(member(2, 0)).label, '待移出(仍在企微)')
|
||||
assert.equal(state.routeStatus({ ...member(1), is_in_remote_range: '0' }).label, '待加入企微')
|
||||
assert.match(state.selectedSyncState.value.description, /当前计划:医助 1;上次企微确认:医助 1、医助 2/)
|
||||
state.overview.pools[0].dispatch_sync = { status: 3, last_error: '可信 IP 校验失败' }
|
||||
assert.match(state.selectedSyncState.value.description, /可信 IP 校验失败/)
|
||||
})
|
||||
|
||||
test('single toggles never infer remote success from an empty error or planned dispatch', async () => {
|
||||
const calls = []
|
||||
const { state, messages } = page({ wecomPromotionToggleMember: async (payload) => { calls.push(payload); return { sync_error: '', dispatch: { queued: true } } } })
|
||||
await state.handleMemberToggle(member(2), false)
|
||||
assert.deepEqual(calls, [{ id: 2, status: 0 }])
|
||||
assert.equal(messages.at(-1).type, 'warning')
|
||||
assert.match(messages.at(-1).message, /尚未确认同步/)
|
||||
assert.doesNotMatch(messages.at(-1).message, /已确认同步|已重新计算/)
|
||||
assert.match(state.operationResultText(state.operationResults.value[0]), /尚未确认同步/)
|
||||
})
|
||||
|
||||
test('single rule save reports explicit remote confirmation and preserves precise failures', async () => {
|
||||
const { state, messages } = page({ wecomPromotionSaveMember: async () => ({ sync_status: 'failed', sync_error: '企微成员范围不一致' }) })
|
||||
Object.assign(state.memberForm, { id: 2, active_range: [] })
|
||||
await state.saveMemberRule()
|
||||
assert.equal(messages.at(-1).type, 'warning')
|
||||
assert.match(messages.at(-1).message, /企微成员范围不一致/)
|
||||
state.notifySavedResult('本地已保存', { sync_status: 'synced' })
|
||||
assert.equal(messages.at(-1).type, 'success')
|
||||
assert.match(messages.at(-1).message, /企微成员范围已确认同步/)
|
||||
})
|
||||
|
||||
test('pool saves with queued work retain a warning instead of implying official link completion', async () => {
|
||||
const { state, messages } = page({ wecomPromotionSavePool: async () => ({ id: 1, sync_status: 'pending', sync_queued: true, sync_error: '' }) })
|
||||
Object.assign(state.poolForm, { id: 1, name: '方案 1', member_admin_ids: [1] })
|
||||
await state.savePool()
|
||||
assert.equal(state.poolDialogVisible.value, false)
|
||||
assert.equal(messages.at(-1).type, 'warning')
|
||||
assert.match(messages.at(-1).message, /尚未确认同步/)
|
||||
assert.equal(state.operationResults.value[0].sync_queued, true)
|
||||
})
|
||||
|
||||
test('manual retry targets only its pool and does not import remote links', async () => {
|
||||
const calls = []
|
||||
const { state, messages } = page({ wecomPromotionSyncMemberRange: async (payload) => { calls.push(payload); return { pool_id: 1, sync_status: 'synced', sync_error: '', sync_queued: false } } })
|
||||
await state.syncMemberRange(1)
|
||||
assert.deepEqual(calls, [{ pool_id: 1 }])
|
||||
assert.equal(state.operationResults.value[0].sync_status, 'synced')
|
||||
assert.equal(state.syncingPoolId.value, 0)
|
||||
assert.equal(messages.at(-1).type, 'success')
|
||||
state.overview.pools[0].can_operate = false
|
||||
await state.syncMemberRange(1)
|
||||
assert.equal(calls.length, 1)
|
||||
})
|
||||
|
||||
test('batch sync awaits all queued successful pools with at most two concurrent requests', async () => {
|
||||
const pending = []
|
||||
let active = 0
|
||||
let maximum = 0
|
||||
const { state } = page({ wecomPromotionSyncMemberRange: ({ pool_id }) => new Promise((resolve) => {
|
||||
active++
|
||||
maximum = Math.max(maximum, active)
|
||||
pending.push({ id: pool_id, finish: (result) => { active--; resolve({ pool_id, ...result }) } })
|
||||
}) })
|
||||
const results = [1, 2, 3].map((id) => ({ id, name: `方案 ${id}`, success: true, sync_queued: true }))
|
||||
results.push({ id: 4, name: '保存失败方案', success: false, sync_queued: true, error: '无权限' })
|
||||
const saving = state.syncBatchResults(results)
|
||||
assert.deepEqual(pending.map((item) => item.id), [1, 2])
|
||||
pending[0].finish({ sync_status: 'synced', sync_error: '' })
|
||||
await new Promise((resolve) => setImmediate(resolve))
|
||||
assert.deepEqual(pending.map((item) => item.id), [1, 2, 3])
|
||||
assert.equal(state.batchSyncProgress.completed, 1)
|
||||
pending[1].finish({ sync_status: 'failed', sync_error: '范围校验失败' })
|
||||
pending[2].finish({ sync_status: 'pending', sync_error: '' })
|
||||
await saving
|
||||
assert.equal(maximum, 2)
|
||||
assert.equal(state.batchSyncProgress.completed, 3)
|
||||
assert.deepEqual(state.operationResults.value.map((item) => item.sync_status), ['synced', 'failed', 'pending', undefined])
|
||||
assert.match(state.operationResultText(state.operationResults.value[1]), /范围校验失败/)
|
||||
assert.match(state.operationResultText(state.operationResults.value[3]), /本地保存失败:无权限/)
|
||||
})
|
||||
|
||||
test('batch save retains partial failures and never reports queued work as remote success', async () => {
|
||||
const pools = [pool(1), pool(2), pool(3)]
|
||||
const syncedIds = []
|
||||
const { state, messages } = page({
|
||||
wecomPromotionBatchUpdatePools: async () => ({ updated: 2, failed: 1, member_updated: 2, results: [
|
||||
{ id: 1, name: '方案 1', success: true, sync_queued: true },
|
||||
{ id: 2, name: '方案 2', success: true, sync_queued: true },
|
||||
{ id: 3, name: '方案 3', success: false, error: '保存失败' }
|
||||
] }),
|
||||
wecomPromotionSyncMemberRange: async ({ pool_id }) => {
|
||||
syncedIds.push(pool_id)
|
||||
if (pool_id === 2) throw '企微请求超时'
|
||||
return { pool_id, sync_status: 'synced', sync_error: '', sync_queued: false }
|
||||
}
|
||||
}, pools)
|
||||
Object.assign(state.batchConfigApply, { member_status: true })
|
||||
Object.assign(state.batchConfigForm, { pool_ids: [1, 2, 3], member_admin_ids: [2], member_status: 0 })
|
||||
state.batchConfigDialogVisible.value = true
|
||||
await state.saveBatchConfig()
|
||||
assert.deepEqual(syncedIds, [1, 2])
|
||||
assert.equal(state.savingBatchConfig.value, false)
|
||||
assert.equal(state.batchConfigDialogVisible.value, false)
|
||||
assert.deepEqual(state.selectedPoolIds.value, [2, 3])
|
||||
assert.match(messages.at(-1).message, /企微已确认同步 1 个,1 个尚未确认同步;1 个本地保存失败/)
|
||||
assert.equal(messages.at(-1).type, 'warning')
|
||||
assert.match(state.operationResults.value[1].sync_error, /企微请求超时/)
|
||||
})
|
||||
Reference in New Issue
Block a user