import { expect, it, vi } from 'vitest' import { defineComponent } from 'vue' import { mount, shallowMount, flushPromises } from '@vue/test-utils' import ElementPlus from 'element-plus' import Preview from '@/components/BusinessAssistant/AssistantNotificationPreview.vue' import { assistantApplyDraftChanges, assistantFieldLocked } from '@/utils/assistant-draft-edit' import { notificationFieldVisible } from '@/utils/assistant-notification' import Draft from '@/components/BusinessAssistant/AssistantDraft.vue' vi.mock('@/components/BusinessAssistant/AssistantField.vue', () => ({ default: { template: '' } })) const plan = () => ({ recipientVersion: 'synthetic', scope: '全部有效用户', recipientCount: 55, channel: 'INBOX', title: '模拟说明', content: '请使用问题反馈功能。\n这是第二行。', notice: '确认后才发送站内消息', recipients: Array.from({ length: 55 }, (_, i) => ({ id: i + 1, name: `模拟用户${i + 1}`, account: `account${i + 1}` })) }) it('renders full scope, channel, body and actual count and pages all recipients without truncation', async () => { const wrapper = mount(Preview, { props: { plan: plan() }, global: { plugins: [ElementPlus] } }) await flushPromises() expect(wrapper.text()).toContain('全部有效用户 · 55 人') expect(wrapper.text()).toContain('站内消息') expect(wrapper.find('.notification-body').text()).toContain('这是第二行。') expect(wrapper.findAll('tbody tr')).toHaveLength(50) await wrapper.find('.btn-next').trigger('click') await flushPromises() expect(wrapper.findAll('tbody tr')).toHaveLength(5) expect(wrapper.find('tbody').text()).toContain('模拟用户55') expect(wrapper.findAll('thead th').map(node => node.text())).toEqual(['姓名', '登录账号']) wrapper.unmount() }) it('search narrows only review display, resets pagination, and never changes send audience', async () => { const data = plan() data.emailRequired = true; data.channel = 'BOTH'; data.recipients[54].email = 'last@example.invalid' const wrapper = mount(Preview, { props: { plan: data }, global: { plugins: [ElementPlus] } }) await wrapper.find('.btn-next').trigger('click') await wrapper.find('input').setValue('last@example.invalid') await flushPromises() expect(wrapper.findAll('tbody tr')).toHaveLength(1) expect(wrapper.find('tbody').text()).toContain('模拟用户55') expect(wrapper.text()).toContain('搜索不改变发送范围') expect(data.recipients).toHaveLength(55) expect(wrapper.emitted('execute')).toBeUndefined() await wrapper.find('input').setValue('nonexistent') expect(wrapper.text()).toContain('没有匹配的接收人') await wrapper.setProps({ plan: { ...plan(), recipientVersion: 'new-version' } }) expect(wrapper.find('input').element.value).toBe('') expect(wrapper.findAll('tbody tr')).toHaveLength(50) wrapper.unmount() }) it('recipient mode hides unrelated pickers and keeps inactive cells locked in a mixed draft', () => { expect(notificationFieldVisible({ key: 'userIds' }, [{ recipientMode: 'ALL' }])).toBe(false) expect(notificationFieldVisible({ key: 'roleIds' }, [{ recipientMode: 'ROLES' }])).toBe(true) expect(notificationFieldVisible({ key: 'userIds' }, [{}, { recipientMode: 'ALL' }])).toBe(true) expect(assistantFieldLocked('notification.send', { recipientMode: 'ALL' }, { key: 'userIds' })).toBe(true) expect(assistantFieldLocked('notification.send', { recipientMode: 'ROLES' }, { key: 'roleIds' })).toBe(false) }) it('editing scope, channel or body clears stale full recipient preview and derived version', () => { const fields = ['recipientMode', 'userIds', 'roleIds', 'channel', 'title', 'content'].map(key => ({ key })) const source = { _rowId: 'one', recipientMode: 'USERS', userIds: [7], _notificationPreview: plan(), recipientVersion: 'old', recipientSummary: 'old', deliveryNotice: 'old' } for (const [key, value] of [['recipientMode', 'ROLES'], ['content', 'changed'], ['channel', 'BOTH']]) { const [changed] = assistantApplyDraftChanges([source], { rowId: 'one', key, value }, { action: 'notification.send', fields }) expect(changed._notificationPreview).toBeUndefined() expect(changed.recipientVersion).toBeUndefined() if (key === 'recipientMode') expect(changed.userIds).toBeUndefined() } expect(source._notificationPreview).toBeDefined() }) it('the main confirmation card embeds the full authoritative plan and omits unused recipient pickers', () => { const column = defineComponent({ props: ['label'], template: '{{ label }}' }) const data = plan(), rows = [{ _rowId: 'one', recipientMode: 'ALL', _notificationPreview: data }] const wrapper = shallowMount(Draft, { props: { draft: { id: 'synthetic', action: 'notification.send', status: 'WAIT_CONFIRM', fields: [{ key: 'recipientMode', label: '接收范围' }, { key: 'userIds', label: '指定接收人' }, { key: 'roleIds', label: '接收角色' }] }, rows, action: { allowed: true } }, global: { stubs: { ElTable: { template: '
' }, ElTableColumn: column } } }) expect(wrapper.findComponent(Preview).props('plan').recipients).toHaveLength(55) const headers = wrapper.findAllComponents(column).map(node => node.props('label')) expect(headers).toContain('接收范围') expect(headers).not.toContain('指定接收人') expect(headers).not.toContain('接收角色') wrapper.unmount() })