fix: 增加@所有人 完善mac发送清理与端口回退

This commit is contained in:
Wxw-Gu committed 2026-11-12 16:00:45 +08:00
1 parent c50f8861a4
commit aca103cb96
9 files changed
+737 -173

No files matched your search

@@ -173,6 +173,53 @@ describe('PersonalWechatSendPage on macOS', () => {
)
})
it('still starts the runtime when a target survives cleanup, and names it', async () => {
const onNotice = vi.fn()
cleanupResidualProcesses.mockResolvedValueOnce({
stopped: [],
remaining: [
{
pid: 404,
name: 'frida-helper',
state: 'UE',
uninterruptible: true,
targets: ['frida_helper', 'port_4290'],
confirmationToken: 'process-identity-404'
}
],
runtimeStopped: false
})
render(<PersonalWechatSendPage onNotice={onNotice} />)
await screen.findByText('运行环境已就绪')
fireEvent.click(screen.getByRole('button', { name: '清理残留以及微信进程' }))
fireEvent.click(await screen.findByRole('button', { name: '确认结束进程' }))
await waitFor(() => expect(startMacRuntime).toHaveBeenCalledOnce())
expect(onNotice).toHaveBeenCalledWith(
'0 个进程已退出,1 个系统无法结束(frida-helper);正在改用可用端口启动发送组件…'
)
})
it('reports the fallback port when the runtime comes up on a spare one', async () => {
listResidualProcesses.mockResolvedValue([])
startMacRuntime.mockResolvedValueOnce({
lifecycle: 'online',
port: 4291,
portFallbackFrom: 4290
})
const onNotice = vi.fn()
render(<PersonalWechatSendPage onNotice={onNotice} />)
await screen.findByText('运行环境已就绪')
fireEvent.click(screen.getByRole('button', { name: '清理残留以及微信进程' }))
await waitFor(() => expect(startMacRuntime).toHaveBeenCalledOnce())
expect(onNotice).toHaveBeenCalledWith(
'发送 Host 已在 4291 端口启动。4290 已被占用,已改用 4291 端口。'
)
})
it('uses the same cleanup button to start the runtime when no residual process exists', async () => {
listResidualProcesses.mockResolvedValue([])
const onNotice = vi.fn()
+17
View File
@@ -225,6 +225,23 @@ describe('POST /api/v1/wechat/send', () => {
expect(invalid.status).toBe(400)
})
it('透传群聊 @ 全体目标', async () => {
const response = await post({
to: 'fixture-room@chatroom',
isGroup: true,
type: 'text',
text: '@所有人 请查看这条测试消息',
at: { 'notify@all': '所有人' }
})
expect(response.status).toBe(200)
expect(lastAction().content).toEqual({
type: 'text',
text: '@所有人 请查看这条测试消息',
at: { 'notify@all': '所有人' }
})
})
it('拒绝格式错误的群聊 @ 成员映射', async () => {
const response = await post({
to: 'fixture-room@chatroom',
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import {
cleanupMacWechatResidualProcesses,
isTraceMemoFridaHelper,
inspectMacWechatResidualProcesses,
readPids,
type MacWechatResidualCleanupDependencies
@@ -13,8 +14,12 @@ function createDependencies(
platform: 'darwin',
listWechatPids: vi.fn(async () => [101]),
listHostPids: vi.fn(async () => []),
listFridaHelperPids: vi.fn(async () => []),
listPortPids: vi.fn(async () => [202]),
processName: vi.fn(async (pid) => (pid === 101 ? 'WeChat' : 'tm-wechat-host')),
processCommand: vi.fn(async () => ''),
processState: vi.fn(async () => ''),
processCwd: vi.fn(async () => ''),
processIdentity: vi.fn(async (pid) => 'identity-' + pid),
signal: vi.fn(),
wait: vi.fn(async () => undefined),
@@ -64,6 +69,98 @@ describe('macOS WeChat residual cleanup', () => {
])
})
it('lists Frida helpers that retain the configured listener', async () => {
const dependencies = createDependencies({
listWechatPids: vi.fn(async () => []),
listHostPids: vi.fn(async () => []),
listFridaHelperPids: vi.fn(async () => [404]),
listPortPids: vi.fn(async () => [404]),
processName: vi.fn(async () => '(frida-helper)'),
processCommand: vi.fn(async () => '(frida-helper)'),
processCwd: vi.fn(async () => '/Applications/TraceMemo.app/Contents/Resources/runtime/darwin-arm64'),
processState: vi.fn(async () => 'UE')
})
await expect(inspectMacWechatResidualProcesses(dependencies)).resolves.toEqual([
{
pid: 404,
name: '(frida-helper)',
state: 'UE',
uninterruptible: true,
targets: ['frida_helper', 'port_4290'],
confirmationToken: 'identity-404'
}
])
})
it('escalates a helper in kernel wait state and reports it while the system retains it', async () => {
const dependencies = createDependencies({
listWechatPids: vi.fn(async () => []),
listHostPids: vi.fn(async () => []),
listFridaHelperPids: vi.fn(async () => [404]),
listPortPids: vi.fn(async () => [404]),
processName: vi.fn(async () => '(frida-helper)'),
processCommand: vi.fn(async () => '(frida-helper)'),
processCwd: vi.fn(async () => '/Applications/TraceMemo.app/Contents/Resources/runtime/darwin-arm64'),
processState: vi.fn(async () => 'UE')
})
const targets = await inspectMacWechatResidualProcesses(dependencies)
const result = await cleanupMacWechatResidualProcesses(
targets.map(({ pid, confirmationToken }) => ({ pid, confirmationToken })),
dependencies
)
expect(dependencies.signal).toHaveBeenCalledWith(404, 'SIGTERM')
expect(dependencies.signal).toHaveBeenCalledWith(404, 'SIGKILL')
expect(dependencies.wait).not.toHaveBeenCalled()
expect(result.stopped).toEqual([])
expect(result.remaining).toMatchObject([{ pid: 404, state: 'UE', targets: ['frida_helper', 'port_4290'] }])
})
it('uses the process working directory to associate an orphan helper with TraceMemo', async () => {
const dependencies = createDependencies({
listWechatPids: vi.fn(async () => []),
listHostPids: vi.fn(async () => []),
listFridaHelperPids: vi.fn(async () => [405]),
listPortPids: vi.fn(async () => []),
processCommand: vi.fn(async () => '(frida-helper)'),
processCwd: vi.fn(async () => '/Applications/TraceMemo.app/Contents/Resources/runtime/darwin-arm64')
})
await expect(inspectMacWechatResidualProcesses(dependencies)).resolves.toEqual([
{
pid: 405,
name: 'tm-wechat-host',
targets: ['frida_helper'],
confirmationToken: 'identity-405'
}
])
})
it('does not list unrelated Frida helpers', async () => {
const dependencies = createDependencies({
listWechatPids: vi.fn(async () => []),
listHostPids: vi.fn(async () => []),
listFridaHelperPids: vi.fn(async () => [405]),
listPortPids: vi.fn(async () => []),
processCommand: vi.fn(async () => '/opt/debug-tool/frida-helper'),
processCwd: vi.fn(async () => '/opt/debug-tool')
})
await expect(inspectMacWechatResidualProcesses(dependencies)).resolves.toEqual([])
})
it('identifies an orphan helper by its TraceMemo runtime path', () => {
expect(
isTraceMemoFridaHelper(
'(frida-helper)',
'/Applications/TraceMemo.app/Contents/Resources/runtime/darwin-arm64'
)
).toBe(true)
expect(isTraceMemoFridaHelper('/opt/debug-tool/frida-helper', '/opt/debug-tool')).toBe(false)
})
it('does not signal a PID whose process identity changed after confirmation', async () => {
const dependencies = createDependencies({
listWechatPids: vi.fn(async () => []),
@@ -108,6 +205,39 @@ describe('macOS WeChat residual cleanup', () => {
expect(result.remaining.map((target) => target.pid)).toEqual([202])
})
it('waits for a TraceMemo Host to finish normal teardown before signaling its helper', async () => {
const sentSignals: Array<[number, NodeJS.Signals]> = []
const dependencies = createDependencies({
listWechatPids: vi.fn(async () => []),
listHostPids: vi.fn(async () => [303]),
listFridaHelperPids: vi.fn(async () => [404]),
listPortPids: vi.fn(async () => [404]),
processName: vi.fn(async (pid) => (pid === 303 ? 'tm-wechat-host' : '(frida-helper)')),
processCommand: vi.fn(async (pid) =>
pid === 303
? '/Applications/TraceMemo.app/Contents/Resources/runtime/darwin-arm64/tm-wechat-host'
: '(frida-helper)'
),
processCwd: vi.fn(async (pid) =>
pid === 303 ? '' : '/Applications/TraceMemo.app/Contents/Resources/runtime/darwin-arm64'
),
processIdentity: vi.fn(async (pid) => 'identity-' + pid),
signal: vi.fn((pid, signal) => sentSignals.push([pid, signal])),
wait: vi.fn(async () => undefined)
})
const targets = await inspectMacWechatResidualProcesses(dependencies)
await cleanupMacWechatResidualProcesses(
targets.map(({ pid, confirmationToken }) => ({ pid, confirmationToken })),
dependencies
)
expect(sentSignals.findIndex(([pid]) => pid === 303)).toBeLessThan(
sentSignals.findIndex(([pid]) => pid === 404)
)
expect(sentSignals[0]).toEqual([303, 'SIGTERM'])
})
it('reports only identity-matched targets before signaling so the caller can stop its own runtime safely', async () => {
const dependencies = createDependencies({
listWechatPids: vi.fn(async () => []),
@@ -157,6 +287,8 @@ describe('macOS WeChat residual cleanup', () => {
onTargetsConfirmed
)
expect(identityReads).toBeGreaterThan(1)
expect(dependencies.signal).not.toHaveBeenCalled()
expect(onTargetsConfirmed).not.toHaveBeenCalled()
expect(result.stopped).toEqual([])
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import {
deriveMacWechatSenderStatus,
formatMacWechatHostStartupFailure,
MacWechatRuntimeManager,
resolveMacWechatSendMechanism
} from '../../src/main/services/mac-wechat-runtime-manager'
@@ -96,6 +97,11 @@ class ConcurrentStartManager extends MacWechatRuntimeManager {
}
describe('deriveMacWechatSenderStatus', () => {
it('shows the Native Host startup diagnostic instead of only exit code 1', () => {
expect(formatMacWechatHostStartupFailure(1, 'bind 127.0.0.1:4290 failed: Address already in use\n'))
.toBe('发送运行时启动失败(进程退出码 1):bind 127.0.0.1:4290 failed: Address already in use')
})
it('shares one in-flight start promise with concurrent status reads', async () => {
const manager = new ConcurrentStartManager()