Files
WechatExplorer/tests/unit/query-agent-conversation-memory.test.ts
T
Wxw-Gu ab3b3731b7 feat: 重构问问微信并完善知识库增量检索
统一问问微信与 Agent Hub 的查询链路
完善查询覆盖度、新鲜度和部分结果表达,避免索引滞后产生错误结论
基于会话实现真正的增量追新与历史补齐
支持后台同步、取消恢复、重启续传以及同步期间继续查询
优化知识库跨会话检索、同步状态、进度展示和侧栏布局
2026-09-11 17:44:48 +08:00

61 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
CONVERSATION_MEMORY_ANSWER_MAX_CHARS,
CONVERSATION_MEMORY_MAX_TURNS,
CONVERSATION_MEMORY_QUESTION_MAX_CHARS,
CONVERSATION_MEMORY_TTL_MS,
QueryAgentConversationMemory
} from '../../src/main/services/query-agent-conversation-memory'
describe('QueryAgentConversationMemory', () => {
it('只保留最近 N 轮,且有明确长度边界', () => {
const memory = new QueryAgentConversationMemory()
memory.record('k', 'q1', 'a1')
memory.record('k', 'q2', 'a2')
memory.record('k', 'q3', 'a3')
const history = memory.history('k')
expect(history).toHaveLength(CONVERSATION_MEMORY_MAX_TURNS)
expect(history.map((turn) => turn.question)).toEqual(['q2', 'q3'])
})
it('超长问题与回答会被截断', () => {
const memory = new QueryAgentConversationMemory()
memory.record('k', 'q'.repeat(500), 'a'.repeat(2000))
const [turn] = memory.history('k')
expect(turn.question.length).toBe(CONVERSATION_MEMORY_QUESTION_MAX_CHARS + 1)
expect(turn.answer.length).toBe(CONVERSATION_MEMORY_ANSWER_MAX_CHARS + 1)
})
it('超过 TTL 后自动失效', () => {
let now = 1_000
const memory = new QueryAgentConversationMemory(() => now)
memory.record('k', 'q', 'a')
expect(memory.history('k')).toHaveLength(1)
now += CONVERSATION_MEMORY_TTL_MS + 1
expect(memory.history('k')).toEqual([])
})
it('按会话隔离,forget 只清目标会话', () => {
const memory = new QueryAgentConversationMemory()
memory.record('a', 'q-a', 'a-a')
memory.record('b', 'q-b', 'a-b')
memory.forget('a')
expect(memory.history('a')).toEqual([])
expect(memory.history('b')).toHaveLength(1)
memory.forget()
expect(memory.history('b')).toEqual([])
})
it('空白内容不进入记忆', () => {
const memory = new QueryAgentConversationMemory()
memory.record('k', ' ', 'a')
memory.record('k', 'q', ' ')
expect(memory.history('k')).toEqual([])
})
})