Files
zyt/server/tests/WecomPromotionAutomationUiTest.mjs
T
2026-09-01 11:26:47 +08:00

67 lines
4.6 KiB
JavaScript

import assert from 'node:assert/strict'
import fs from 'node:fs'
import ts from '../../admin/node_modules/typescript/lib/typescript.js'
const source = fs.readFileSync(new URL('../../admin/src/views/first_visit/wecom_promotion/components/promotion-automation.ts', import.meta.url), 'utf8')
const formSource = fs.readFileSync(new URL('../../admin/src/views/first_visit/wecom_promotion/components/PromotionAutomationForm.vue', import.meta.url), 'utf8')
const pageSource = fs.readFileSync(new URL('../../admin/src/views/first_visit/wecom_promotion/index.vue', import.meta.url), 'utf8')
const compiled = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext } }).outputText
const { defaultAutomationConfig, cloneAutomationConfig, serializeAutomationConfig, validateAutomationConfig, validateCustomTagName, validateWelcomeMessage, welcomeScheduleOverlap, previewTemplate } = await import(`data:text/javascript;base64,${Buffer.from(compiled).toString('base64')}`)
const defaults = defaultAutomationConfig()
assert.equal(validateAutomationConfig(defaults, [1]), '')
const configured = {
...defaults,
reception_mode: 'scheduled',
reception_schedule: [{ weekdays: [1], start: '22:00', end: '02:00', member_admin_ids: [1], member_userids: ['must-not-submit'] }],
backup_member_admin_ids: [2], backup_userids: ['must-not-submit'],
welcome_mode: 'channel', welcome: { text: '您好,{customer_name}', attachments: [] }
}
assert.equal(validateAutomationConfig(configured, [1]), '')
const copy = cloneAutomationConfig(configured)
assert.equal(copy.backup_userids, undefined)
assert.equal(copy.reception_schedule[0].member_userids, undefined)
assert.deepEqual(
Object.fromEntries(Object.entries(serializeAutomationConfig({
...copy,
tags_enabled: true,
remark_enabled: false,
description_enabled: true,
welcome_schedule_enabled: false
})).filter(([key]) => key.endsWith('_enabled'))),
{ tags_enabled: 1, remark_enabled: 0, description_enabled: 1, welcome_schedule_enabled: 0 }
)
copy.reception_schedule[0].weekdays.push(2)
assert.deepEqual(configured.reception_schedule[0].weekdays, [1])
assert.match(validateAutomationConfig({ ...configured, backup_member_admin_ids: [] }, [1]), /备用/)
assert.match(validateAutomationConfig({ ...configured, backup_member_admin_ids: [1] }, [1]), /重复/)
assert.match(validateAutomationConfig({ ...configured, reception_schedule: [{ ...copy.reception_schedule[0], member_admin_ids: [3] }] }, [1]), /主接待/)
assert.match(validateWelcomeMessage({ text: '😀'.repeat(1001), attachments: [] }, ''), /4000/)
assert.match(validateWelcomeMessage({ text: '', attachments: [] }, ''), /正文或添加附件/)
assert.match(validateWelcomeMessage({ text: 'hello', attachments: [{ msgtype: 'link', link: { title: 'test', url: 'javascript:alert(1)', desc: '' } }] }, ''), /HTTP/)
assert.match(validateAutomationConfig({ ...defaults, tags_enabled: true }, [1]), /标签/)
assert.equal(validateAutomationConfig({ ...defaults, tags_enabled: true, tag_ids: ['tag1'] }, [1]), '')
assert.match(validateAutomationConfig({ ...defaults, tags_enabled: true, tag_ids: ['tag1', 'tag2'] }, [1]), /只能选择一个/)
assert.match(validateAutomationConfig({ ...defaults, tags_enabled: false, tag_ids: ['tag1', 'tag2'] }, [1]), /只能选择一个/)
assert.deepEqual(cloneAutomationConfig({ ...defaults, tag_ids: ['tag1', 'tag2'] }).tag_ids, ['tag1', 'tag2'])
assert.match(validateCustomTagName(' '), /名称/)
assert.match(validateCustomTagName('名称\n换行'), /控制字符/)
assert.match(validateCustomTagName('名称\u200b'), /不可见/)
assert.match(validateCustomTagName('标'.repeat(31)), /30/)
assert.equal(validateCustomTagName('标'.repeat(30)), '')
assert.equal(validateCustomTagName(' 官网咨询 '), '')
assert.equal(welcomeScheduleOverlap([
{ weekdays: [7], start: '22:00', end: '02:00' },
{ weekdays: [1], start: '01:00', end: '03:00' }
]), true)
assert.equal(welcomeScheduleOverlap([
{ weekdays: [7], start: '22:00', end: '02:00' },
{ weekdays: [1], start: '02:00', end: '03:00' }
]), false)
assert.match(previewTemplate('{customer_name}-{employee_name}-{add_time}', '小陈'), /^张女士-小陈-\d{4}-\d{2}-\d{2}$/)
assert.equal(Array.from(previewTemplate('王'.repeat(30), '小陈', 20)).length, 20)
assert.match(formSource, /不会写入企微获客链接详情中的“欢迎语\/客户标签”配置/)
assert.match(formSource, /客户添加回调中立即发送渠道欢迎语并添加标签/)
assert.match(pageSource, /result\?\.automation_saved !== true/)
console.log('WECOM_PROMOTION_AUTOMATION_UI_OK')