Files
aishangjia-uniapp/tests/check-ai-house-location-response.js
T

76 lines
2.4 KiB
JavaScript

const assert = require('node:assert/strict')
const fs = require('node:fs')
const path = require('node:path')
const pagePath = path.resolve(__dirname, '../pages/ai-house/index.vue')
const source = fs.readFileSync(pagePath, 'utf8')
const scriptMatch = source.match(/<script>([\s\S]*?)<\/script>/)
assert.ok(scriptMatch, '未找到 AI 找房页面脚本')
const componentScript = scriptMatch[1]
.replace(/^\s*import .*$/gm, '')
.replace('export default {', 'return {')
const component = new Function(
'HouseAiConfigApi',
'HouseAiChatApi',
'storage',
'USER_ID',
'apiUrl',
componentScript
)({}, {}, {}, 'userId', 'http://localhost')
function createPageVm() {
const vm = component.data()
Object.entries(component.methods).forEach(([name, method]) => {
vm[name] = method.bind(vm)
})
vm.scrollToBottom = () => {}
return vm
}
function receiveLocationResponse(fieldName, locationCards) {
const vm = createPageVm()
const response = {
type: 'house_ai_result',
answer: '已按已维护的地点资料筛选,以下地点可供参考。'
}
response[fieldName] = locationCards
vm.handleSocketMessage(JSON.stringify(response))
return vm.messages.find((message) => message.type === 'location')
}
const locationCard = {
location_id: 101,
city: '南宁市',
location_name: '五象航洋城',
parent_location_name: '五象新区',
tags: ['停车方便'],
knowledge_items: [{
topic: 'parking',
title: '停车',
parking_available: true,
parking_fee: '月租 300 元'
}]
}
const locationMessage = receiveLocationResponse('location_cards', JSON.stringify([locationCard]))
assert.ok(locationMessage, '地点咨询回包应创建地点卡片消息')
assert.ok(Array.isArray(locationMessage.locationCards), '地点卡片应转换为数组')
assert.equal(locationMessage.locationCards[0].locationId, 101)
assert.equal(locationMessage.locationCards[0].locationName, '五象航洋城')
assert.equal(locationMessage.locationCards[0].knowledgeItems[0].parkingAvailable, true)
const backendLocationMessage = receiveLocationResponse('locationCards', [{
locationId: 102,
city: '南宁市',
locationName: '会展航洋城',
knowledgeItems: []
}])
assert.ok(backendLocationMessage, '后端驼峰地点卡片回包应保持兼容')
assert.equal(backendLocationMessage.locationCards[0].locationName, '会展航洋城')
console.log('地点咨询回包能够创建可渲染的地点卡片消息')