Files
SupplierSystem/server/tests/assistantChatRetention.test.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

86 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import {
ASSISTANT_CHAT_RETENTION_DEFAULT_DAYS,
resolveAssistantChatRetention,
} from '../src/services/assistantChatRetentionService';
import { stableToolCallKey } from '../src/mcp/orderEntryAgentHost';
/**
* 助手聊天记录保留策略(2026-09-22):建表后一直没有任何清理逻辑(对比 operation_logs 有月度清理),
* 且用户没有删除入口。默认保留 180 天,可用 ASSISTANT_CHAT_RETENTION_DAYS 调整/关闭。
*/
describe('resolveAssistantChatRetention', () => {
it('未配置 → 默认保留 180 天', () => {
expect(resolveAssistantChatRetention(undefined)).toEqual({
enabled: true,
days: ASSISTANT_CHAT_RETENTION_DEFAULT_DAYS,
});
expect(resolveAssistantChatRetention('')).toEqual({
enabled: true,
days: ASSISTANT_CHAT_RETENTION_DEFAULT_DAYS,
});
});
it('正整数按天,且封顶 3650', () => {
expect(resolveAssistantChatRetention('30')).toEqual({ enabled: true, days: 30 });
expect(resolveAssistantChatRetention('99999')).toEqual({ enabled: true, days: 3650 });
});
it('0 / 负数 / 非法值 → 关闭清理(不能因手滑清空全部历史)', () => {
expect(resolveAssistantChatRetention('0')).toEqual({ enabled: false });
expect(resolveAssistantChatRetention('-1')).toEqual({ enabled: false });
expect(resolveAssistantChatRetention('abc')).toEqual({ enabled: false });
});
});
describe('stableToolCallKey(工具去重键)', () => {
it('对象键顺序不影响去重键', () => {
expect(stableToolCallKey('list_orders', { b: 2, a: 1 })).toBe(
stableToolCallKey('list_orders', { a: 1, b: 2 })
);
});
it('嵌套对象 / 数组同样稳定,且忽略 undefined', () => {
expect(
stableToolCallKey('prepare_order_edit', { items: [{ sku: 'S1', qty: 2 }], order_code: 'O1', note: undefined })
).toBe(stableToolCallKey('prepare_order_edit', { order_code: 'O1', items: [{ qty: 2, sku: 'S1' }] }));
});
it('参数不同 → 键不同(不能误去重)', () => {
expect(stableToolCallKey('list_orders', { a: 1 })).not.toBe(stableToolCallKey('list_orders', { a: 2 }));
expect(stableToolCallKey('list_orders', { a: 1 })).not.toBe(stableToolCallKey('list_skus', { a: 1 }));
});
});
describe('接线棘轮', () => {
const read = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
it('定时任务在每日 04:00 清理过期助手会话', () => {
const src = read('src/services/scheduledJobs.ts');
expect(src).toContain('runDailyAssistantChatPurge');
expect(src).toContain('await runDailyAssistantChatPurge();');
expect(src).toContain('purgeExpiredAssistantChatsForCurrentTenant');
});
it('提供删除会话接口(仅自己的会话)', () => {
const route = read('src/routes/orderEntryAgent.ts');
expect(route).toContain("router.delete('/conversations/:id'");
const service = read('src/services/assistantChatLogService.ts');
expect(service).toContain('deleteAssistantConversation');
expect(service).toContain('WHERE id = $1::uuid AND user_id = $2');
});
it('预览生成失败会留日志(不再静默退化)', () => {
const src = read('src/capabilities/registry.ts');
expect(src).toContain('[assistant] 预览生成失败,回退通用预览');
});
it('对话去重使用稳定键(不再 JSON.stringify(args))', () => {
const src = read('src/mcp/orderEntryAgentHost.ts');
expect(src).toContain('callKey: stableToolCallKey(name, args)');
expect(src).not.toMatch(/callKey = `\$\{name\}:\$\{JSON\.stringify\(args\)\}`/);
});
});