| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #!/usr/bin/env node
- 'use strict';
- const assert = require('assert/strict');
- const fs = require('fs');
- const os = require('os');
- const path = require('path');
- async function main() {
- const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-message-archive-'));
- const previousArchive = process.env.QIWEI_MESSAGE_ARCHIVE_ENABLED;
- const previousMemory = process.env.QIWEI_AGENT_MEMORY_ENABLED;
- const previousRoot = process.env.QIWEI_MESSAGES_DIR;
- try {
- process.env.QIWEI_MESSAGES_DIR = tempRoot;
- delete process.env.QIWEI_MESSAGE_ARCHIVE_ENABLED;
- delete process.env.QIWEI_AGENT_MEMORY_ENABLED;
- require('../mcp/src/core/credentials').setActiveQiweiContext({ userId: 'archive-smoke-user' });
- const archive = require('../mcp/src/core/message-archive');
- const accountDir = archive.accountDir();
- assert.equal(archive.archiveEnabled(), false);
- assert.equal(archive.memoryEnabled(), false);
- await archive.appendChatRecord({ wxid: 'contact/unsafe', content: '默认不落盘', dir: 'in' });
- assert.equal(fs.existsSync(path.join(tempRoot, 'chat')), false);
- process.env.QIWEI_MESSAGE_ARCHIVE_ENABLED = '1';
- await archive.appendChatRecord({
- wxid: 'contact/unsafe', messageId: 'm-1', externalId: 'e-1', dir: 'in', senderType: 'customer',
- content: '需要下周安排一次产品演示', createdAt: '2026-08-03T10:00:00.000Z', source: 'smoke',
- });
- await archive.appendGroupRecord({
- roomId: 'room:demo', messageId: 'g-1', seq: 1, msgType: 1, senderId: 'member-1',
- senderName: '测试成员', self: false, content: '群内测试消息', timestamp: '2026-08-03T10:01:00.000Z',
- });
- await archive.drainQueues();
- const chatFile = path.join(tempRoot, 'chat', accountDir, 'contact_unsafe', '2026-08-03.json');
- const groupFile = path.join(tempRoot, 'group', accountDir, 'room_demo', '2026-08-03.json');
- assert.equal(JSON.parse(fs.readFileSync(chatFile, 'utf8')).content, '需要下周安排一次产品演示');
- assert.equal(JSON.parse(fs.readFileSync(groupFile, 'utf8')).roomId, 'room:demo');
- assert.deepEqual(archive.ensureMemory('contact-1', '刘总'), { enabled: false, dir: '', created: [] });
- process.env.QIWEI_AGENT_MEMORY_ENABLED = '1';
- const memory = archive.ensureMemory('contact-1', '刘总');
- assert.equal(memory.enabled, true);
- assert.deepEqual(memory.created.sort(), [...archive.MEMORY_FILES].sort());
- const templates = archive.MEMORY_FILES.map(name => fs.readFileSync(path.join(memory.dir, name), 'utf8')).join('\n');
- assert.match(templates, /当前目标与需求/);
- assert.doesNotMatch(templates, /房源|购房|看房|小牛/);
- assert.match(archive.memoryIndexText('contact-1'), /用户记忆索引/);
- assert.equal(archive.memoryRelBase('contact/1'), `messages/memory/${accountDir}/contact_1`);
- await archive.appendChatRecord({
- wxid: 'contact/unsafe', messageId: 'm-dup-1', externalId: 'e-dup-1', dir: 'in', senderType: 'customer',
- content: '需要下周安排一次产品演示', createdAt: '2026-08-03T10:00:00.000Z', source: 'smoke-dup',
- });
- await archive.drainQueues();
- const chatLines = fs.readFileSync(chatFile, 'utf8').split(/\r?\n/).filter(Boolean);
- assert.equal(chatLines.length, 1);
- process.stdout.write(`${JSON.stringify({ status: 'ok', checks: 15, archiveDefault: 'off', memoryDefault: 'off' }, null, 2)}\n`);
- } finally {
- if (previousArchive === undefined) delete process.env.QIWEI_MESSAGE_ARCHIVE_ENABLED;
- else process.env.QIWEI_MESSAGE_ARCHIVE_ENABLED = previousArchive;
- if (previousMemory === undefined) delete process.env.QIWEI_AGENT_MEMORY_ENABLED;
- else process.env.QIWEI_AGENT_MEMORY_ENABLED = previousMemory;
- if (previousRoot === undefined) delete process.env.QIWEI_MESSAGES_DIR;
- else process.env.QIWEI_MESSAGES_DIR = previousRoot;
- fs.rmSync(tempRoot, { recursive: true, force: true });
- }
- }
- main().catch(error => {
- process.stderr.write(`${error.stack || error.message}\n`);
- process.exitCode = 1;
- });
|