feat: 完善mac发送逻辑

This commit is contained in:
Wxw-Gu committed 2026-11-09 11:19:59 +08:00
1 parent 05b881d79e
commit f47af8024f
13 files changed
+1048 -46

No files matched your search

@@ -1,4 +1,4 @@
import { render, screen } from '@testing-library/react'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PersonalWechatSendPage } from '../../src/renderer/src/features/settings/pages/PersonalWechatSendPage'
@@ -12,6 +12,10 @@ vi.mock('../../src/renderer/src/utils/runtime-environment', () => ({
const getCapability = vi.fn()
const getSenderStatus = vi.fn()
const rebindSender = vi.fn()
const listResidualProcesses = vi.fn()
const cleanupResidualProcesses = vi.fn()
const startMacRuntime = vi.fn()
const getMacLicenseStatus = vi.fn()
const senderStatus = {
state: 'online' as const,
@@ -21,7 +25,7 @@ const senderStatus = {
wechatRunning: true,
wechatPid: 4668,
boundWechatPid: 4668,
endpoint: '127.0.0.1:4290',
endpoint: 'http://127.0.0.1:4290',
endpointReady: true,
runtimeReady: true,
attachReady: true,
@@ -53,29 +57,54 @@ describe('PersonalWechatSendPage on macOS', () => {
getCapability.mockReset().mockResolvedValue(capability)
getSenderStatus.mockReset().mockResolvedValue(senderStatus)
rebindSender.mockReset().mockResolvedValue(senderStatus)
listResidualProcesses.mockReset().mockResolvedValue([
{
pid: 202,
name: 'tm-wechat-host',
targets: ['port_4290'],
confirmationToken: 'process-identity-202'
}
])
cleanupResidualProcesses.mockReset().mockResolvedValue({
stopped: [
{
pid: 202,
name: 'tm-wechat-host',
targets: ['port_4290'],
confirmationToken: 'process-identity-202'
}
],
remaining: [],
runtimeStopped: true
})
startMacRuntime.mockReset().mockResolvedValue({ lifecycle: 'online' })
getMacLicenseStatus.mockReset().mockResolvedValue({
deviceId: null,
state: 'unlicensed',
authorized: false,
source: 'none',
features: [],
lastCheckedAt: null,
licenseExpiresAt: null,
tokenExpiresAt: null,
message: '当前设备暂无发送能力授权'
})
Object.defineProperty(window, 'api', {
configurable: true,
value: {
getPersonalWechatSendCapability: getCapability,
getPersonalWechatSenderStatus: getSenderStatus,
rebindPersonalWechatSender: rebindSender,
listMacWechatResidualProcesses: listResidualProcesses,
cleanupMacWechatResidualProcesses: cleanupResidualProcesses,
startMacSendRuntime: startMacRuntime,
// 发送能力授权卡所需的两个入口。页面在 Native 档位会调用它们,
// 缺 mock 会让 refresh 抛错、整页停在错误态。
getMacSendRuntimeAvailability: vi.fn(async () => ({
state: 'available',
message: '微信发送组件可用'
})),
getMacLicenseStatus: vi.fn(async () => ({
deviceId: null,
state: 'unlicensed',
authorized: false,
source: 'none',
features: [],
lastCheckedAt: null,
licenseExpiresAt: null,
tokenExpiresAt: null,
message: '当前设备暂无发送能力授权'
})),
getMacLicenseStatus,
refreshMacLicense: vi.fn()
}
})
@@ -87,8 +116,142 @@ describe('PersonalWechatSendPage on macOS', () => {
// 页面重构后「就绪」的落点从 SetupGuide 尾部文案变成了「微信发送环境」卡片的状态标签;
// 断言改到新位置,语义仍是“运行时准备好了”。
expect(await screen.findByText('运行环境已就绪')).toBeVisible()
expect(
screen.getByText('当前发送能力占用端口为 4290(TM_WECHAT_HOST_PORT)。')
).toBeVisible()
expect(await screen.findByText('已被占用:tm-wechat-host(PID 202)')).toBeVisible()
expect(screen.getAllByText('已就绪')).not.toHaveLength(0)
expect(screen.queryByText('查看支持的微信版本')).not.toBeInTheDocument()
expect(screen.queryByText('高级诊断')).not.toBeInTheDocument()
})
it('shows the actual runtime port when it uses a configured fallback instead of 4290', async () => {
const fallbackPortStatus = { ...senderStatus, endpoint: 'http://127.0.0.1:4291' }
getCapability.mockResolvedValueOnce({ ...capability, senderStatus: fallbackPortStatus })
getSenderStatus.mockResolvedValueOnce(fallbackPortStatus)
listResidualProcesses.mockResolvedValueOnce([])
render(<PersonalWechatSendPage onNotice={vi.fn()} />)
expect(
await screen.findByText(
'当前发送能力占用端口为 4291(TM_WECHAT_HOST_PORT)。'
)
).toBeVisible()
expect(screen.getByText('未被占用')).toBeVisible()
})
it('shows confirmed cleanup targets before ending processes', async () => {
const onNotice = vi.fn()
render(<PersonalWechatSendPage onNotice={onNotice} />)
await screen.findByText('运行环境已就绪')
fireEvent.click(screen.getByRole('button', { name: '清理残留以及微信进程' }))
expect(await screen.findByRole('heading', { name: '清理微信残留进程?' })).toBeVisible()
expect(screen.getByText('tm-wechat-host')).toBeVisible()
expect(screen.getByText(/PID 202.*4290 端口/)).toBeVisible()
expect(cleanupResidualProcesses).not.toHaveBeenCalled()
})
it('cleans only the process the user confirmed and refreshes the runtime state', async () => {
const onNotice = vi.fn()
render(<PersonalWechatSendPage onNotice={onNotice} />)
await screen.findByText('运行环境已就绪')
fireEvent.click(screen.getByRole('button', { name: '清理残留以及微信进程' }))
fireEvent.click(await screen.findByRole('button', { name: '确认结束进程' }))
await waitFor(() => {
expect(cleanupResidualProcesses).toHaveBeenCalledWith([
{ pid: 202, confirmationToken: 'process-identity-202' }
])
})
expect(onNotice).toHaveBeenCalledWith('已结束 1 个残留进程;发送环境状态已刷新。')
expect(await screen.findByRole('button', { name: '重新启动发送组件' })).toBeVisible()
fireEvent.click(screen.getByRole('button', { name: '重新启动发送组件' }))
await waitFor(() => expect(startMacRuntime).toHaveBeenCalledOnce())
})
it('shows that WeChat must be reopened and rebound after its process is cleaned', async () => {
listResidualProcesses.mockResolvedValueOnce([
{
pid: 4668,
name: 'WeChat',
targets: ['wechat'],
confirmationToken: 'wechat-process-4668'
}
])
cleanupResidualProcesses.mockResolvedValueOnce({
stopped: [
{
pid: 4668,
name: 'WeChat',
targets: ['wechat'],
confirmationToken: 'wechat-process-4668'
}
],
remaining: [],
runtimeStopped: false
})
rebindSender.mockResolvedValueOnce({
...senderStatus,
state: 'stopped',
wechatRunning: true,
wechatPid: undefined,
boundWechatPid: undefined,
canSend: false
})
const stoppedStatus = {
...senderStatus,
state: 'stopped',
wechatRunning: false,
wechatPid: undefined,
boundWechatPid: undefined,
attachReady: false,
baseAddressReady: false,
canSend: false,
canSendText: false,
canSendImage: false,
canSendVoice: false
}
getSenderStatus.mockReset().mockResolvedValueOnce(senderStatus).mockResolvedValue(stoppedStatus)
getMacLicenseStatus.mockResolvedValue({
deviceId: 'mac_test',
state: 'valid',
authorized: true,
source: 'online',
features: ['wechat'],
lastCheckedAt: new Date().toISOString(),
licenseExpiresAt: null,
tokenExpiresAt: new Date(Date.now() + 60_000).toISOString(),
message: '设备已授权'
})
const onNotice = vi.fn()
render(<PersonalWechatSendPage onNotice={onNotice} />)
await screen.findByText('运行环境已就绪')
fireEvent.click(screen.getByRole('button', { name: '清理残留以及微信进程' }))
fireEvent.click(await screen.findByRole('button', { name: '确认结束进程' }))
expect(
await screen.findByText(
'微信进程已清理。请重新打开微信,然后点击“绑定微信”;绑定完成后此提示会自动消失。'
)
).toBeVisible()
fireEvent.click(screen.getByRole('button', { name: '绑定微信' }))
await waitFor(() => expect(rebindSender).toHaveBeenCalledOnce())
expect(
screen.getByText(
'微信进程已清理。请重新打开微信,然后点击“绑定微信”;绑定完成后此提示会自动消失。'
)
).toBeVisible()
fireEvent.click(screen.getByRole('button', { name: '绑定微信' }))
await waitFor(() => expect(rebindSender).toHaveBeenCalledTimes(2))
expect(
screen.queryByText(
'微信进程已清理。请重新打开微信,然后点击“绑定微信”;绑定完成后此提示会自动消失。'
)
).not.toBeInTheDocument()
})
})
@@ -97,6 +97,63 @@ describe('personal WeChat unavailable product state', () => {
expect(screen.queryByText(/manifest|dylib|runtime missing|文件缺失/i)).not.toBeInTheDocument()
})
it('describes a ready runtime separately from pending send capability initialization', () => {
const boundStatus: PersonalWechatSenderStatus = {
...unavailableStatus,
state: 'hook_not_ready',
wechatRunning: true,
wechatPid: 4668,
boundWechatPid: 4668,
endpointReady: true,
runtimeReady: true,
attachReady: true,
baseAddressReady: true,
message: '已绑定,等待发送 hook 就绪'
}
render(
<PersonalWechatSetupGuide
senderStatus={boundStatus}
binding={false}
detecting={false}
sessionBound
showBindAction={false}
/>
)
expect(screen.getByText('微信已绑定,正在等待发送能力完成初始化…')).toBeVisible()
expect(screen.queryByText('正在等待发送运行时完成初始化…')).not.toBeInTheDocument()
})
it('does not say the runtime is still initializing when it is ready but message capabilities are pending', () => {
const boundButInitializing: PersonalWechatSenderStatus = {
...unavailableStatus,
state: 'hook_not_ready',
wechatRunning: true,
wechatPid: 4668,
boundWechatPid: 4668,
endpointReady: true,
runtimeReady: true,
attachReady: true,
baseAddressReady: true,
message: '已绑定,等待发送 hook 就绪'
}
render(
<PersonalWechatSetupGuide
senderStatus={boundButInitializing}
binding={false}
detecting={false}
sessionBound
showBindAction={false}
showBindingInstructionsInInitialization
/>
)
expect(screen.getByText('微信已绑定,正在等待发送能力完成初始化…')).toBeVisible()
expect(screen.queryByText('正在等待发送运行时完成初始化…')).not.toBeInTheDocument()
})
it('shows the disabled binding flow on the settings page', async () => {
render(<PersonalWechatSendPage onNotice={vi.fn()} />)
@@ -206,12 +263,17 @@ describe('personal WeChat unavailable product state', () => {
const copyButton = await screen.findByRole('button', { name: '复制完整设备 ID' })
fireEvent.click(copyButton)
await waitFor(() => expect(navigator.clipboard.writeText).toHaveBeenCalledWith(deviceId))
expect(await screen.findByText('授权已过期')).toBeVisible()
expect(await screen.findByText('授权状态需刷新')).toBeVisible()
expect(
screen.getByText('当前授权状态与到期日不一致,请点击「重新检测授权」更新状态。')
).toBeVisible()
fireEvent.click(screen.getByRole('button', { name: '重新检测授权' }))
expect(await screen.findByRole('button', { name: '检测中…' })).toBeDisabled()
expect(copyButton).toBeEnabled()
resolveRefresh?.({ ...licenseStatus, deviceId, state: 'valid', authorized: true })
expect(await screen.findByText('设备已授权')).toBeVisible()
expect(screen.queryByText('授权状态需刷新')).not.toBeInTheDocument()
})
it('does not query or render License when the send runtime is unavailable', async () => {