feat: 完善多账号连接诊断与聊天媒体导出

- 新增微信账号发现、环境诊断和分步数据库连接引导
- 支持按账号安全保存数据库密钥及快速切换账号
- 完善 WCDB 历史消息分片读取和分页状态提示
- 支持导出图片、视频和语音,提供原图优先及缩略图回退
- 更新安装指引、兼容版本说明和相关自动化测试
This commit is contained in:
Wxw-Gu
2026-08-03 10:43:14 +08:00
parent 224308f0e0
commit 08e1294e5d
37 changed files with 2011 additions and 255 deletions
+69
View File
@@ -0,0 +1,69 @@
import fs from 'fs-extra'
import os from 'os'
import path from 'path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const mocked = vi.hoisted(() => ({
userData: `${process.env.TEMP || process.env.TMP || '.'}/wxe-account-discovery-tests`
}))
vi.mock('electron', () => ({
app: { getPath: () => mocked.userData }
}))
import { discoverAccounts } from '../../src/main/services/account-discovery'
describe('account discovery', () => {
let root: string
const keyStore = {
getStatus: vi.fn(async (accountRoot: string) => ({
saved: accountRoot.endsWith('account-b'),
encryptionAvailable: true
}))
}
beforeEach(async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), 'wxe-accounts-'))
await Promise.all(
['account-a', 'account-b', 'account-c'].map((name) =>
fs.ensureDir(path.join(root, name, 'db_storage'))
)
)
await fs.ensureDir(path.join(root, 'Backup'))
})
afterEach(async () => {
await fs.remove(root)
await fs.remove(mocked.userData)
})
it('rejects an invalid Backup directory without continuing', async () => {
const result = await discoverAccounts(path.join(root, 'Backup'), keyStore as never)
expect(result.success).toBe(false)
expect(result.accounts).toEqual([])
})
it('lists every direct account and never preselects one from a root directory', async () => {
const result = await discoverAccounts(root, keyStore as never)
expect(result.success).toBe(true)
expect(result.accounts.map((account) => account.directoryName).sort()).toEqual([
'account-a',
'account-b',
'account-c'
])
expect(result.preselectedAccountId).toBeUndefined()
expect(
result.accounts.find((account) => account.directoryName === 'account-b')?.hasSavedDbKey
).toBe(true)
})
it('preselects a directly selected account directory while retaining its card', async () => {
const accountRoot = path.join(root, 'account-c')
const result = await discoverAccounts(accountRoot, keyStore as never)
expect(result.success).toBe(true)
expect(result.accounts).toHaveLength(1)
expect(result.accounts[0].accountRoot).toBe(accountRoot)
expect(result.preselectedAccountId).toBe(result.accounts[0].id)
expect(result.accounts[0].selectedByInput).toBe(true)
})
})
+26
View File
@@ -0,0 +1,26 @@
import { describe, expect, it } from 'vitest'
import { buildSafeDiagnosticSummary } from '../../src/shared/connection-diagnostics'
describe('connection diagnostics', () => {
it('contains useful versions and readiness without secrets or full account paths', () => {
const summary = buildSafeDiagnosticSummary({
platform: 'win32',
osVersion: 'Windows 11 fixture',
appVersion: 'v2.1.6',
wechatVersion: '4.1.9.57',
dataStructureVersion: '微信 4.xWCDB',
dataDirectoryDetected: true,
autoDetectSupported: true,
wechatRunning: true,
accountIdentified: true,
dbConnected: false,
encryptionAvailable: true
})
expect(summary).toContain('WechatExplorer: v2.1.6')
expect(summary).toContain('微信客户端: 4.1.9.57')
expect(summary).not.toContain('0123456789abcdef')
expect(summary).not.toContain('C:\\Users\\fixture\\xwechat_files\\wxid_secret')
expect(summary).not.toContain('wxid_')
})
})
+62
View File
@@ -0,0 +1,62 @@
import { describe, expect, it } from 'vitest'
import { renderExportPage } from '../../src/main/export-html-template'
import { getImageExportAttempts } from '../../src/shared/export-media'
import type { Message } from '../../src/shared/types'
const baseMessage = (overrides: Partial<Message>): Message => ({
id: 'fixture-message',
from: 'fixture',
type: '文本',
datetime: '2026-08-01 10:00:00',
content: '',
isSender: false,
...overrides
})
describe('export media', () => {
it('always attempts the original before an explicitly enabled thumbnail fallback', () => {
const first = getImageExportAttempts({ preferOriginal: true, fallbackThumbnail: true })
const repeated = getImageExportAttempts({ preferOriginal: true, fallbackThumbnail: true })
expect(first).toEqual([
{ allowThumbnail: false, preferThumbnail: false, fallback: false },
{ allowThumbnail: true, preferThumbnail: true, fallback: true }
])
expect(repeated).toEqual(first)
})
it('renders movable relative audio and video assets plus accurate missing-media details', () => {
const html = renderExportPage('脱敏导出', [
baseMessage({ id: 'voice', type: '语音', voiceDataUrl: 'voices/voice_1.wav' }),
baseMessage({
id: 'video',
type: '视频',
exportMediaType: 'video',
exportMediaUrl: 'media/video_2.mp4'
}),
baseMessage({
id: 'missing',
type: '语音',
exportMediaError: '语音文件缺失:本地未找到语音数据'
})
])
expect(html).toContain(
'audio class="audio" controls preload="metadata" src="voices/voice_1.wav"'
)
expect(html).toContain('video class="media-image" controls src="media/video_2.mp4"')
expect(html).toContain('语音文件缺失:本地未找到语音数据')
expect(html).not.toMatch(/(?:src|href)="[A-Za-z]:\\/)
})
it('renders explicit and keyboard-accessible lightbox closing controls', () => {
const html = renderExportPage('图片预览', [
baseMessage({ id: 'image', type: '图片', exportMediaUrl: 'media/image.jpg' })
])
expect(html).toContain('aria-label="关闭图片预览"')
expect(html).toContain("closeButton.addEventListener('click',closeLightbox)")
expect(html).toContain('if(event.target===box)closeLightbox()')
expect(html).toContain("if(event.key==='Escape')closeLightbox()")
})
})
+10
View File
@@ -20,4 +20,14 @@ describe('message pagination', () => {
)
expect(merged.map((message) => message.id)).toEqual(['oldest', 'overlap', 'latest'])
})
it('keeps cross-year pages continuous through the earliest fixture record', () => {
const page2025 = [makeMessage('2025', 1_735_689_600), makeMessage('2026', 1_767_225_600)]
const page2017 = [makeMessage('2017', 1_483_228_800), makeMessage('2025', 1_735_689_600)]
const merged = mergeMessagePages(page2017, page2025)
expect(merged.map((message) => message.id)).toEqual(['2017', '2025', '2026'])
expect(new Set(merged.map((message) => message.id)).size).toBe(merged.length)
})
})
+45
View File
@@ -0,0 +1,45 @@
import { describe, expect, it, vi } from 'vitest'
import { Wcdb4Client, type Wcdb4Message } from '../../src/main/wcdb4-client'
const message = (id: string, year: number): Wcdb4Message => ({
mesLocalID: id,
serverId: `server-${id}`,
mesDes: 0,
messageType: '1',
msgCreateTime: String(Math.floor(Date.UTC(year, 0, 1) / 1000)),
msgContent: `fixture-${year}`,
raw: {}
})
describe('WCDB message shard pagination', () => {
it('merges cursor and all-store rows for a bounded cross-year page', async () => {
const cursor = vi.fn(async () => [message('2025', 2025)])
const tableScan = vi.fn(async () => [message('2017', 2017), message('2025', 2025)])
const client = Object.assign(Object.create(Wcdb4Client.prototype), {
wcdbGetMessageTableStats: vi.fn(),
wcdbExecQuery: vi.fn(),
getMessagesByCursorAsync: cursor,
getMessagesByTableScanAsync: tableScan
}) as Wcdb4Client
const result = await client.getMessagesAsync(
'fixture@chatroom',
undefined,
Math.floor(Date.UTC(2026, 0, 1) / 1000),
{ limit: 20 }
)
expect(tableScan).toHaveBeenCalledOnce()
expect(result.map((item) => item.msgContent)).toEqual(['fixture-2017', 'fixture-2025'])
})
it('reports an unsupported shard query instead of claiming history ended', async () => {
const client = Object.assign(Object.create(Wcdb4Client.prototype), {
getMessagesByCursorAsync: vi.fn(async () => [])
}) as Wcdb4Client
await expect(
client.getMessagesAsync('fixture@chatroom', undefined, 1_767_225_600, { limit: 20 })
).rejects.toThrow('无法检查历史消息分片')
})
})