Files
WechatExplorer/tests/unit/chat-service.test.ts
Wxw-Gu 0a3d930298 fix: 修复媒体导出与 HTML 聊天档案体验
- 修复打包版图片、语音解析不可用,内置 FFmpeg 与必要运行时依赖
- 修复 HTML 导出原图查找、缩略图回退及增量档案媒体解析问题
- 修复本人昵称在软件和 HTML 导出中显示为微信号的问题,并兼容旧档案昵称迁移
- 修复 HTML 语音播放器超出消息气泡的问题
- 优化 HTML 双向滚动加载,大量消息时最多渲染 240 条,避免页面卡顿
- 移除图片解密页面中手动配置 FFmpeg 的相关提示
- 补充图片解密、语音运行时、打包资源和 HTML 导出相关测试
2026-08-04 17:03:07 +08:00

105 lines
3.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import type { WechatDb } from '../../src/main/wechat-db'
import {
closeChatDbForQuit,
getSelfAccountInfoAsync,
isReady,
listContactsAsync,
setChatDb
} from '../../src/main/services/chat-service'
describe('chat service contacts', () => {
afterEach(() => setChatDb(null))
it('hydrates display names before returning macOS-style session ids', async () => {
const session = {
username: '57101206391@chatroom',
nickname: '57101206391@chatroom'
}
const getSessionsAsync = vi.fn(async (options: { hydrateDisplayNames?: boolean }) => {
if (options.hydrateDisplayNames) session.nickname = '测试群聊'
return [session]
})
const fakeDb = {
close: vi.fn(),
md5: () => 'fixture-md5',
getAllGroupContacts: () => ({ fixture: session.nickname }),
getUserList: () => [
{
m_nsUsrName: session.username,
nickname: session.nickname
}
],
getWcdb4Client: () => ({ getSessionsAsync })
} as unknown as WechatDb
setChatDb(fakeDb)
const contacts = await listContactsAsync()
expect(getSessionsAsync).toHaveBeenCalledWith({
hydrateDisplayNames: true,
hydrateStatuses: true
})
expect(contacts[0]?.m_nsNickName).toBe('测试群聊')
})
it('hydrates the current account nickname before returning self info', async () => {
const session = { username: 'a969409112', nickname: 'a969409112' }
const client = {
getSessionsAsync: vi.fn(async () => {
session.nickname = '濑岛田井卫'
return [session]
}),
getAccountRoot: () => '/fixture/a969409112_d784',
getMyUsernameCandidates: () => ['a969409112'],
getUsernameByMd5: () => undefined,
md5: () => 'fixture-md5',
getMyAvatarUrl: () => undefined
}
const fakeDb = {
close: vi.fn(),
md5: () => 'fixture-md5',
getAllGroupContacts: () => ({}),
getUserList: () => [
{
m_nsUsrName: session.username,
nickname: session.nickname
}
],
getWcdb4Client: () => client
} as unknown as WechatDb
setChatDb(fakeDb)
const info = await getSelfAccountInfoAsync()
expect(client.getSessionsAsync).toHaveBeenCalledWith({ hydrateDisplayNames: true })
expect(info).toMatchObject({ wxid: 'a969409112', nickname: '濑岛田井卫' })
})
it('detaches the database immediately and awaits native cleanup on quit', async () => {
let finishClose: ((value: boolean) => void) | undefined
const closeAsync = vi.fn(
() =>
new Promise<boolean>((resolve) => {
finishClose = resolve
})
)
const fakeDb = {
close: vi.fn(),
closeAsync
} as unknown as WechatDb
setChatDb(fakeDb)
const closing = closeChatDbForQuit()
expect(isReady()).toBe(false)
expect(closeAsync).toHaveBeenCalledOnce()
finishClose?.(true)
await expect(closing).resolves.toBe(true)
const lateDb = { close: vi.fn() } as unknown as WechatDb
expect(setChatDb(lateDb)).toBe(false)
expect(lateDb.close).toHaveBeenCalledOnce()
})
})