You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
5.3 KiB
70 lines
5.3 KiB
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import vm from 'node:vm'
|
|
function component(path) {
|
|
const source = fs.readFileSync(new URL('../' + path, import.meta.url), 'utf8')
|
|
const script = source.match(/<script>([\s\S]*?)<\/script>/)[1].replace(/^\s*import .*$/gm, '').replace('export default', 'result =')
|
|
const sandbox = { SupplyMemberBrief: {}, SupplyAuthDialog: {}, requireSupplyMember: async () => true, loadSupplySession: async () => ({ registered: true }), uni: { setNavigationBarTitle() {}, showModal() {}, navigateTo() {} }, base: {}, uSearch: {}, uLoadmore: {}, uTabs: {}, uLine: {}, uAvatar: {}, uLoadingPage: {}, console }
|
|
vm.createContext(sandbox); vm.runInContext(script, sandbox)
|
|
return sandbox.result
|
|
}
|
|
const chat = component('packages/chat/chatWindow.vue')
|
|
function instance(api) {
|
|
const c = { ...chat.data(), $store: { state: { vuex_user: { id: 2 } } }, $u: { api }, $nextTick: f => f() }
|
|
for (const [k, f] of Object.entries(chat.methods)) c[k] = f.bind(c)
|
|
return c
|
|
}
|
|
test('before binding a demand request includes demand ID even with no recipient', () => {
|
|
const c = instance({}); c.supplyDemandId = 8; c.theirUserId = 0
|
|
assert.equal(c.contextParams().supply_demand_id, 8)
|
|
c.dialogueId = 12; const params = c.contextParams()
|
|
assert.equal(params.dialogue_id, 12); assert.equal('to_user_id' in params, false)
|
|
})
|
|
test('failed send retains text and does not manufacture a successful message', async () => {
|
|
const c = instance({ supplyDemandSendMessage: async () => false }); c.newMessage = '咨询'; c.supplyDemandId = 1
|
|
await c.sendMessage(); assert.equal(c.newMessage, '咨询'); assert.equal(c.messages.length, 0); assert.equal(c.sending, false)
|
|
})
|
|
test('server conversation context replaces stale owner info after handover', async () => {
|
|
const c = instance({ supplyDemandMessageList: async () => ({ dialogue_id: 10, supplyDemand: { id: 9, is_managed: false }, other_profile: { id: 1, name: '发布人', company_name: '公司' }, message: { data: [{ id: 1, user_id: 2, created_at: '2026-10-16 13:00:00', content: '咨询' }], current_page: 1, last_page: 1 } }) })
|
|
c.dialogueId = 10; c.theirUserId = 0; await c.fetchMessages()
|
|
assert.equal(c.theirUserId, 1); assert.equal(c.supplyDemand.id, 9); assert.equal(c.messages[0].from, 'me')
|
|
})
|
|
test('deleted or unassociated demand removes context and cannot provide a detail link', async () => {
|
|
const c = instance({ supplyDemandMessageList: async () => ({ dialogue_id: 10, supplyDemand: null, message: { data: [], current_page: 1, last_page: 1 } }) })
|
|
c.dialogueId = 10; c.supplyDemand = { id: 9 }; c.supplyDemandId = 9; await c.fetchMessages()
|
|
assert.equal(c.supplyDemand, null); assert.equal(c.supplyDemandId, '')
|
|
})
|
|
test('refresh merges by message ID and does not duplicate existing messages', async () => {
|
|
const c = instance({ supplyDemandMessageList: async () => ({ dialogue_id: 10, supplyDemand: null, message: { data: [{ id: 2, user_id: 1, created_at: '2026-10-16 14:00:00' }, { id: 1, user_id: 2, created_at: '2026-10-16 13:00:00' }], current_page: 1, last_page: 1 } }) })
|
|
c.dialogueId = 10; c.messages = [{ id: 1 }]; await c.fetchMessages(1, false, true)
|
|
assert.equal(c.messages.length, 2); assert.equal(c.messages[0].id, 1); assert.equal(c.messages[1].id, 2)
|
|
})
|
|
test('conversation route names decode Chinese and preserve malformed percent text', async () => {
|
|
const c = instance({}); let title
|
|
c.fetchMessages = () => {}; c.fetchSupplyDemand = () => {}
|
|
// Capture the actual initial navigation title, before any message response exists.
|
|
const original = fs.readFileSync(new URL('../packages/chat/chatWindow.vue', import.meta.url), 'utf8')
|
|
const script = original.match(/<script>([\s\S]*?)<\/script>/)[1].replace(/^\s*import .*$/gm, '').replace('export default', 'result =')
|
|
const sandbox = { SupplyMemberBrief: {}, SupplyAuthDialog: {}, requireSupplyMember: async () => true, uni: { setNavigationBarTitle: value => { title = value.title } }, console }
|
|
vm.createContext(sandbox); vm.runInContext(script, sandbox)
|
|
await sandbox.result.onLoad.call(c, { userId: '1', userName: encodeURIComponent('刘燕') })
|
|
assert.equal(title, '与 刘燕 的对话')
|
|
assert.equal(c.decodeRouteName('刘燕'), '刘燕')
|
|
assert.equal(c.decodeRouteName('完成率100%'), '完成率100%')
|
|
assert.equal(c.decodeRouteName('A%26B%3FC'), 'A&B?C')
|
|
})
|
|
|
|
test('returning to supply and my posts reloads page one and queues a refresh during loading', async () => {
|
|
for (const [path, reload, fetch] of [['packages/supply/index.vue','reloadList','fetchList'],['packages/supply/my-posts.vue','reloadPosts','fetchMyPosts']]) {
|
|
const definition = component(path)
|
|
const c = { ...definition.data(), identityReady: true, page: 3, list: [{ id: 1 }], $u: { api: { supplyDemandList: async params => { assert.equal(params.page, 1); return { supplyDemands: { data: [{ id: 9 }], current_page: 1, last_page: 1 } } } } } }
|
|
for (const [key, method] of Object.entries(definition.methods)) c[key] = method.bind(c)
|
|
c[reload](); await new Promise(resolve => setTimeout(resolve, 0))
|
|
assert.equal(c.page, 1); assert.equal(c.list[0].id, 9)
|
|
c.loading = true; c[reload](); assert.equal(c.reloadPending, true)
|
|
c.loading = false; await c[fetch](); await new Promise(resolve => setTimeout(resolve, 0))
|
|
assert.equal(c.reloadPending, false); assert.equal(c.list.length, 1)
|
|
}
|
|
})
|