Files
lik 76197b838f feat(ai): 等待气泡展示后台处理进度
- AI 聊天用工具/子代理/推理帧推断等待阶段,提示已完成步数,子代理输出不再并入主回答
- 下单弹层去掉 01-04 编号分组标题
- 同步首页设计规范与 UI 测试断言,补充首页品牌图
2026-11-18 22:31:58 +08:00

246 lines
10 KiB
JavaScript

const { test } = require('node:test')
const assert = require('node:assert/strict')
const fs = require('node:fs')
const path = require('node:path')
const vm = require('node:vm')
const { withDuplicateNameHints } = require('../utils/booking-profiles.js')
const root = path.resolve(__dirname, '..')
function createHospitalSelector() {
let definition
let hidden = 0
vm.runInNewContext(fs.readFileSync(path.join(root, 'components/hospital-select/index.js'), 'utf8'), {
Component(value) { definition = value },
wx: { getWindowInfo: () => ({ windowHeight: 800 }), hideKeyboard() { hidden++ } }
})
const events = []
const instance = {
data: { ...definition.data, disabled: false, options: [], value: '' },
setData(value) { Object.assign(this.data, value) },
triggerEvent(name, detail) { events.push({ name, detail }) }
}
Object.assign(instance, definition.methods)
return { instance, events, hidden: () => hidden }
}
const hospitals = [
{ _id: 'a', name: '北京医院东院区', address: '东城区', entityName: '北京医院', pinyin: 'beijingyiyuan', pinyinFL: 'bjyy', nicknames: ['北医'] },
{ _id: 'b', name: '天津医院', address: '河西区', pinyin: 'tianjinyiyuan', pinyinFL: 'tjyy' }
]
test('医院搜索支持名称、主体、拼音、首字母及别名,不截断长名称', () => {
const { instance: c } = createHospitalSelector()
c.data.options = hospitals
c.onOpen()
assert.equal(c.data.filtered.length, 2)
for (const value of ['北京', '北京医院', 'BEIJING', ' bj yy ', '北医']) {
c.onKeywordInput({ detail: { value } })
assert.equal(c.data.filtered.length, 1)
assert.equal(c.data.filtered[0].name, hospitals[0].name)
}
c.onKeywordInput({ detail: { value: '不存在' } })
assert.equal(c.data.filtered.length, 0)
})
test('医院选择回传原记录,关闭后重开重置搜索及键盘', () => {
const { instance: c, events, hidden } = createHospitalSelector()
c.data.options = hospitals
c.onOpen()
assert.equal(c.data.panelHeight, 656)
c.onKeyboardHeightChange({ detail: { height: 320 } })
assert.equal(c.data.panelHeight, 480)
assert.equal(c.data.keyboardHeight, 320)
c.onSelect({ currentTarget: { dataset: { index: 1 } } })
assert.equal(c.data.open, false)
const change = events.find(event => event.name === 'change').detail
assert.equal(change.name, hospitals[1].name)
assert.equal(change.hospital, hospitals[1])
assert.equal(change.manual, false)
assert.equal(hidden(), 1)
c.onKeyboardHeightChange({ detail: { height: 320 } })
assert.equal(c.data.keyboardHeight, 0)
c.onOpen()
assert.equal(c.data.keyword, '')
assert.equal(c.data.filtered.length, 2)
assert.equal(c.data.panelHeight, 656)
})
test('手填沿用关键词、拒绝纯空格,取消可返回列表', () => {
const { instance: c, events } = createHospitalSelector()
c.onOpen()
c.onKeywordInput({ detail: { value: ' 新医院 ' } })
c.onStartManual()
assert.equal(c.data.manualName, '新医院')
c.onManualNameInput({ detail: { value: ' ' } })
c.onConfirmManual()
assert.equal(events.filter(event => event.name === 'change').length, 0)
c.onCancelManual()
assert.equal(c.data.manualEntry, false)
assert.equal(c.data.keyboardHeight, 0)
assert.equal(c.data.panelHeight, 656)
c.onStartManual()
c.onConfirmManual()
const change = events.find(event => event.name === 'change').detail
assert.equal(change.manual, true)
assert.equal(change.hospital, null)
assert.equal(change.name, '新医院')
})
test('禁用时不能打开医院选择;异常键盘高度不会产生负面板高度', () => {
const { instance: c } = createHospitalSelector()
c.data.disabled = true
c.onOpen()
assert.equal(c.data.open, false)
c.data.disabled = false
c.onOpen()
c.onKeyboardHeightChange({ detail: { height: -10 } })
assert.equal(c.data.keyboardHeight, 0)
c.onKeyboardHeightChange({ detail: { height: 1000 } })
assert.equal(c.data.panelHeight, 0)
})
test('同名就诊人补展示提示,不改 ID、姓名、电话或输入数据', () => {
const profiles = [
{ _id: '1', name: '李明', mobile: '13800001111', birth: '1960-01-01' },
{ _id: '2', name: '李明', mobile: '13800002222', birth: '1990-01-01' },
{ _id: '3', name: '王芳', mobile: '13800003333' }
]
const before = JSON.stringify(profiles)
const result = withDuplicateNameHints(profiles)
assert.equal(result[0].nameHint, '1960-01-01 · 尾号1111')
assert.equal(result[1].nameHint, '1990-01-01 · 尾号2222')
assert.equal(result[2].nameHint, '')
result.forEach((profile, index) => {
const { nameHint, ...fields } = profile
assert.deepEqual(fields, profiles[index])
})
assert.equal(JSON.stringify(profiles), before)
assert.deepEqual(withDuplicateNameHints([]), [])
})
test('两个预约入口仍按服务数据定价,协议、字段与提交入口保留', () => {
for (const page of ['itemorder', 'itemdetail']) {
const js = fs.readFileSync(path.join(root, `pages/escort/${page}.js`), 'utf8')
const wxml = fs.readFileSync(path.join(root, `pages/escort/${page}.wxml`), 'utf8')
assert.match(js, /totalPrice: service.price/)
assert.match(js, /patientName: (?:profile|first).name/)
assert.match(wxml, /bindtap="submitOrder"/)
assert.match(wxml, /bindtap="onToggleAgreement"/)
assert.match(wxml, /catchtap="onOpenAgreement"/)
assert.match(wxml, /bindchange="onHospitalChange"/)
assert.doesNotMatch(wxml, /<text class="order-label">就诊人姓名<\/text>/)
assert.match(wxml, /费用仅含人工服务费,不含就医、药品及快递费用/)
}
const home = fs.readFileSync(path.join(root, 'pages/home/index.wxml'), 'utf8')
assert.ok(home.indexOf('class="training-card"') > home.indexOf('class="quick-list"'))
assert.ok(home.indexOf('class="training-card"') < home.indexOf('class="page-footer"'))
assert.doesNotMatch(home, /全程1对1陪同/)
})
test('浅色页面恢复深色导航文字', () => {
let colors
const context = { module: { exports: {} }, console, wx: { setNavigationBarColor(value) { colors = value } } }
vm.runInNewContext(fs.readFileSync(path.join(root, 'utils/navigation.js'), 'utf8'), context)
context.module.exports.restoreLightNavigation()
assert.equal(colors.frontColor, '#000000')
assert.equal(colors.backgroundColor, '#FAF8F5')
})
function createBookingPage(page, overrides = {}) {
let definition
const service = {
id: 1, title: '全天陪诊', price: '498.00', areas: ['北京市'], notices: ['请提前预约'],
booking: { advanceDays: 1, sessions: [{ start: '08:00' }] }
}
const app = {
globalData: { user: null },
loadServices: async () => [service],
loadDisclaimer: async () => null,
loadHospitals: async () => [],
getHospitalsByProvince: () => [],
waitForAuth: async () => false,
...overrides
}
vm.runInNewContext(fs.readFileSync(path.join(root, `pages/escort/${page}.js`), 'utf8'), {
Page(value) { definition = value },
getApp: () => app,
require(name) {
if (name === '../../utils/api.js') return {}
if (name === '../../utils/auth.js') return { ensureLogin: async () => false }
return require(path.resolve(root, 'pages/escort', name))
},
wx: { setNavigationBarTitle() {}, getSystemInfoSync: () => ({ windowWidth: 375, windowHeight: 800 }) },
console: { error() {}, log() {} }
})
const instance = {
...definition,
data: JSON.parse(JSON.stringify(definition.data)),
serviceId: 1,
setData(value, callback) {
Object.assign(this.data, value)
if (callback) callback()
}
}
return { instance, app }
}
for (const page of ['itemorder', 'itemdetail']) {
test(`${page}:正常初始化显示服务、价格及预约配置`, async () => {
const { instance } = createBookingPage(page)
await instance.initializePage()
assert.equal(instance.data.service.id, 1)
assert.equal(instance.data.totalPrice, '498.00')
assert.equal(instance.data.pageLoading, false)
assert.equal(instance.data.pageError, '')
assert.equal(instance.data.timeSlots[0], '08:00')
assert.equal(instance.data.timeSlots.at(-1), '23:30')
})
test(`${page}:请求或配置处理异常退出加载,重试可恢复`, async () => {
for (const loadServices of [
async () => { throw new Error('request:fail') },
async () => ({ invalid: true }),
async () => null,
async () => []
]) {
const { instance, app } = createBookingPage(page, { loadServices })
await instance.initializePage()
assert.equal(instance.data.pageLoading, false)
assert.ok(instance.data.pageError)
app.loadServices = async () => [{ id: 1, title: '全天陪诊', price: '498.00' }]
await instance.initializePage()
assert.equal(instance.data.pageError, '')
assert.equal(instance.data.service.id, 1)
}
})
test(`${page}:医院或协议加载失败不阻断服务正文`, async () => {
const { instance } = createBookingPage(page, {
loadDisclaimer: async () => { throw new Error('disclaimer unavailable') },
loadHospitals: async () => { throw new Error('hospitals unavailable') }
})
await instance.initializePage()
assert.equal(instance.data.service.id, 1)
assert.equal(instance.data.pageError, '')
assert.equal(instance.data.disclaimerLoading, false)
assert.equal(instance.data.disclaimerError, true)
})
test(`${page}:加载组件已注册,正文与加载/错误状态互斥`, () => {
const config = JSON.parse(fs.readFileSync(path.join(root, `pages/escort/${page}.json`), 'utf8'))
const wxml = fs.readFileSync(path.join(root, `pages/escort/${page}.wxml`), 'utf8')
assert.equal(config.usingComponents['t-loading'], 'tdesign-miniprogram/loading/loading')
assert.match(wxml, /wx:if="{{!pageLoading && !pageError && service.id}}"/)
assert.match(wxml, /bindtap="retryLoad"/)
})
}
test('立即预约:登录初始化失败仍保留可用表单', async () => {
const { instance } = createBookingPage('itemorder', {
waitForAuth: async () => { throw new Error('auth unavailable') }
})
await instance.initializePage()
assert.equal(instance.data.pageError, '')
assert.equal(instance.data.service.id, 1)
assert.equal(instance.data.pageLoading, false)
})