outbound-callback-smoke-test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. const assert = require('assert/strict');
  3. const fs = require('fs');
  4. const os = require('os');
  5. const path = require('path');
  6. const { AgentWorkbenchDb } = require('../mcp/src/core/agent-workbench-db');
  7. const { __testing } = require('../mcp/src/dashboard/agent-service');
  8. async function main() {
  9. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-outbound-callback-'));
  10. const db = new AgentWorkbenchDb(path.join(dir, 'outbound.db'), {
  11. globalPaused: false,
  12. defaultMode: 'review',
  13. });
  14. const emitted = [];
  15. const target = {
  16. config: { selfUserId: 'staff-1', allowedSenders: ['customer-1'] },
  17. db,
  18. service: { emit: (...args) => emitted.push(args) },
  19. qiwei: {
  20. async syncMessages(seq, limit) {
  21. assert.equal(seq, 19);
  22. assert.equal(limit, 10);
  23. return {
  24. syncMsgList: [{
  25. seq: 21,
  26. msgType: 2,
  27. senderId: 'staff-1',
  28. receiverId: 'customer-1',
  29. msgData: { content: '手机端补采消息' },
  30. msgUniqueIdentifier: 'outbound-synced-1',
  31. timestamp: 1_785_495_167,
  32. }],
  33. };
  34. },
  35. },
  36. };
  37. try {
  38. const direct = await __testing.ingestMessageForWorkbench(target, {
  39. seq: 10,
  40. msgType: 2,
  41. senderId: 'staff-1',
  42. receiverId: 'customer-1',
  43. msgData: { content: '电脑端直接消息' },
  44. msgUniqueIdentifier: 'outbound-direct-1',
  45. timestamp: 1_785_495_100,
  46. }, 'relay_callback');
  47. assert.equal(direct.status, 'outbound_recorded');
  48. const synced = await __testing.ingestMessageForWorkbench(target, {
  49. seq: 20,
  50. msgType: 2001,
  51. senderId: 'staff-1',
  52. receiverId: 'customer-1',
  53. msgUniqueIdentifier: 'outbound-notification-1',
  54. timestamp: 1_785_495_162,
  55. msgData: {},
  56. }, 'relay_callback');
  57. assert.equal(synced.status, 'outbound_recorded');
  58. const conversation = db.getConversationByContactId('customer-1');
  59. const messages = db.listMessages(conversation.id, 20);
  60. assert.deepEqual(messages.map(item => [item.direction, item.sender_type, item.content]), [
  61. ['outbound', 'human', '电脑端直接消息'],
  62. ['outbound', 'human', '手机端补采消息'],
  63. ]);
  64. assert.equal(emitted.length, 2);
  65. process.stdout.write('[ok] outbound callbacks are captured without triggering Agent\n');
  66. } finally {
  67. db.close();
  68. fs.rmSync(dir, { recursive: true, force: true });
  69. }
  70. }
  71. main().catch(error => {
  72. console.error(error);
  73. process.exitCode = 1;
  74. });