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

@@ -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()