pipeline.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { after, before, beforeEach, test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { prepareTestDatabase } from '../helpers/testServer.js';
  4. import { applyTestConfig } from '../helpers/testConfig.js';
  5. import type { SupportMessageEvent } from '../../src/domain/messageNormalizer.js';
  6. import type { ChatwootClient } from '../../src/clients/chatwootClient.js';
  7. import type { FlowiseClient, FlowiseResult } from '../../src/clients/flowiseClient.js';
  8. const dbUrl = prepareTestDatabase('test-pipeline');
  9. const { processMessageEvent, buildFlowisePayload } = await import(
  10. '../../src/domain/conversationPipeline.js'
  11. );
  12. const { db, disconnectDb } = await import('../../src/store/db.js');
  13. const { claimMessage } = await import('../../src/store/idempotencyStore.js');
  14. /** Records every Chatwoot side effect instead of performing it. */
  15. class FakeChatwoot {
  16. sentMessages: { conversationId: number; content: string }[] = [];
  17. privateNotes: { conversationId: number; content: string }[] = [];
  18. labels: { conversationId: number; label: string }[] = [];
  19. attributes: { conversationId: number; attrs: Record<string, unknown> }[] = [];
  20. unassigned: number[] = [];
  21. statusUpdates: { conversationId: number; status: string }[] = [];
  22. conversation: Record<string, unknown> = { id: 1311, labels: [], custom_attributes: {} };
  23. async getConversation(): Promise<Record<string, unknown>> {
  24. return this.conversation;
  25. }
  26. async addLabel(conversationId: number, label: string): Promise<void> {
  27. this.labels.push({ conversationId, label });
  28. }
  29. async setCustomAttributes(conversationId: number, attrs: Record<string, unknown>): Promise<void> {
  30. this.attributes.push({ conversationId, attrs });
  31. const existing = (this.conversation.custom_attributes ?? {}) as Record<string, unknown>;
  32. this.conversation.custom_attributes = { ...existing, ...attrs };
  33. }
  34. async unassignConversation(conversationId: number): Promise<boolean> {
  35. this.unassigned.push(conversationId);
  36. return true;
  37. }
  38. async setConversationStatus(conversationId: number, status: 'open' | 'resolved' | 'pending'): Promise<boolean> {
  39. this.statusUpdates.push({ conversationId, status });
  40. return true;
  41. }
  42. async sendOutgoingMessage(conversationId: number, content: string): Promise<unknown> {
  43. this.sentMessages.push({ conversationId, content });
  44. return {};
  45. }
  46. async sendPrivateNote(conversationId: number, content: string): Promise<unknown> {
  47. this.privateNotes.push({ conversationId, content });
  48. return {};
  49. }
  50. }
  51. class FakeFlowise {
  52. calls: unknown[] = [];
  53. constructor(private readonly result: FlowiseResult | Error) {}
  54. async predict(payload: unknown): Promise<FlowiseResult> {
  55. this.calls.push(payload);
  56. if (this.result instanceof Error) throw this.result;
  57. return this.result;
  58. }
  59. }
  60. function evt(overrides: Partial<SupportMessageEvent> = {}): SupportMessageEvent {
  61. return {
  62. source: 'chatwoot',
  63. channel: 'email',
  64. conversationId: 1311,
  65. messageId: `m-${Math.random().toString(36).slice(2)}`,
  66. inboxId: 1,
  67. content: 'Czy macie gaz R134a?',
  68. subject: 'Pytanie',
  69. messageCreatedAt: new Date().toISOString(),
  70. senderEmail: 'klient@example.com',
  71. senderName: 'Klient Testowy',
  72. senderId: '332',
  73. labels: [],
  74. customAttributes: {},
  75. additionalAttributes: {},
  76. attachmentCount: 0,
  77. attachments: [],
  78. formMail: null,
  79. needsConversationFetch: false,
  80. ...overrides,
  81. };
  82. }
  83. const deps = (chatwoot: FakeChatwoot, flowise: FakeFlowise) => ({
  84. chatwoot: chatwoot as unknown as ChatwootClient,
  85. flowise: flowise as unknown as FlowiseClient,
  86. });
  87. before(() => applyTestConfig({ DATABASE_URL: dbUrl }));
  88. beforeEach(async () => {
  89. await db().ticket.deleteMany({});
  90. await db().auditEvent.deleteMany({});
  91. await db().processedMessage.deleteMany({});
  92. });
  93. after(async () => {
  94. await disconnectDb();
  95. });
  96. test('a normal question is answered and marked replied', async () => {
  97. const chatwoot = new FakeChatwoot();
  98. const flowise = new FakeFlowise({ type: 'reply', text: 'Tak, mamy.', actions: null });
  99. const event = evt();
  100. await claimMessage(event.source, event.messageId, event.conversationId);
  101. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  102. assert.equal(outcome.action, 'reply');
  103. assert.equal(chatwoot.sentMessages.length, 1);
  104. assert.equal(chatwoot.sentMessages[0]?.content, 'Tak, mamy.');
  105. const row = await db().processedMessage.findUnique({
  106. where: { source_messageId: { source: 'chatwoot', messageId: event.messageId } },
  107. });
  108. assert.equal(row?.status, 'replied');
  109. });
  110. test('a conversation already in ticket mode never reaches Flowise', async () => {
  111. const chatwoot = new FakeChatwoot();
  112. const flowise = new FakeFlowise({ type: 'reply', text: 'nie powinno wyjść', actions: null });
  113. const event = evt({ labels: ['ticket'] });
  114. await claimMessage(event.source, event.messageId, event.conversationId);
  115. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  116. assert.deepEqual(outcome, { action: 'skipped', reason: 'ticket_mode' });
  117. assert.equal(flowise.calls.length, 0);
  118. assert.equal(chatwoot.sentMessages.length, 0);
  119. });
  120. test('a ticket_number custom attribute also stops the AI', async () => {
  121. const chatwoot = new FakeChatwoot();
  122. const flowise = new FakeFlowise({ type: 'reply', text: 'x', actions: null });
  123. const event = evt({ customAttributes: { ticket_number: 'EKS-20260820-1311' } });
  124. await claimMessage(event.source, event.messageId, event.conversationId);
  125. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  126. assert.equal(outcome.action, 'skipped');
  127. assert.equal(flowise.calls.length, 0);
  128. });
  129. test('spam is blocked before Flowise and recorded', async () => {
  130. const chatwoot = new FakeChatwoot();
  131. const flowise = new FakeFlowise({ type: 'reply', text: 'x', actions: null });
  132. const event = evt({ senderEmail: 'mailer-daemon@example.com' });
  133. await claimMessage(event.source, event.messageId, event.conversationId);
  134. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  135. assert.equal(outcome.action, 'spam');
  136. assert.equal(flowise.calls.length, 0, 'the spam gate must sit in front of the LLM');
  137. const row = await db().processedMessage.findUnique({
  138. where: { source_messageId: { source: 'chatwoot', messageId: event.messageId } },
  139. });
  140. assert.equal(row?.status, 'spam');
  141. });
  142. test('configured AgentBot guard skips conversations assigned to the legacy user account', async () => {
  143. applyTestConfig({
  144. DATABASE_URL: dbUrl,
  145. CHATWOOT_REQUIRE_AGENT_BOT_ASSIGNMENT: 'true',
  146. CHATWOOT_REQUIRED_AGENT_BOT_ID: '1',
  147. });
  148. const chatwoot = new FakeChatwoot();
  149. chatwoot.conversation = {
  150. id: 1311,
  151. labels: [],
  152. custom_attributes: {},
  153. assignee_agent_bot_id: null,
  154. meta: { assignee_type: 'User', assignee: { id: 2, name: 'KlimBot' } },
  155. };
  156. const flowise = new FakeFlowise({ type: 'reply', text: 'nie powinno wyjść', actions: null });
  157. const event = evt();
  158. await claimMessage(event.source, event.messageId, event.conversationId);
  159. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  160. assert.deepEqual(outcome, { action: 'skipped', reason: 'not_assigned_to_bot' });
  161. assert.equal(flowise.calls.length, 0);
  162. assert.equal(chatwoot.sentMessages.length, 0);
  163. applyTestConfig({ DATABASE_URL: dbUrl });
  164. });
  165. test('configured AgentBot guard allows conversations assigned to the Chatwoot AgentBot', async () => {
  166. applyTestConfig({
  167. DATABASE_URL: dbUrl,
  168. CHATWOOT_REQUIRE_AGENT_BOT_ASSIGNMENT: 'true',
  169. CHATWOOT_REQUIRED_AGENT_BOT_ID: '1',
  170. });
  171. const chatwoot = new FakeChatwoot();
  172. chatwoot.conversation = {
  173. id: 1311,
  174. labels: [],
  175. custom_attributes: {},
  176. assignee_agent_bot_id: 1,
  177. meta: { assignee_type: 'AgentBot', assignee: { id: 1, name: 'Agent' } },
  178. };
  179. const flowise = new FakeFlowise({ type: 'reply', text: 'OK od bota', actions: null });
  180. const event = evt();
  181. await claimMessage(event.source, event.messageId, event.conversationId);
  182. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  183. assert.equal(outcome.action, 'reply');
  184. assert.equal(flowise.calls.length, 1);
  185. assert.equal(chatwoot.sentMessages[0]?.content, 'OK od bota');
  186. applyTestConfig({ DATABASE_URL: dbUrl });
  187. });
  188. test('a handoff action creates a ticket, labels it and replies with the number', async () => {
  189. const chatwoot = new FakeChatwoot();
  190. const flowise = new FakeFlowise({
  191. type: 'handoff',
  192. text: 'Przekazuję sprawę do supportu.',
  193. actions: [{ type: 'handoff' }],
  194. });
  195. const event = evt();
  196. await claimMessage(event.source, event.messageId, event.conversationId);
  197. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  198. assert.equal(outcome.action, 'handoff');
  199. assert.match((outcome as { ticketNumber: string }).ticketNumber, /^EKS-\d{8}-1311$/);
  200. assert.ok(chatwoot.labels.some((l) => l.label === 'ticket'));
  201. assert.ok(chatwoot.attributes.some((a) => 'ticket_number' in a.attrs));
  202. assert.deepEqual(chatwoot.unassigned, [1311]);
  203. assert.deepEqual(chatwoot.statusUpdates, [{ conversationId: 1311, status: 'open' }]);
  204. assert.equal(chatwoot.sentMessages.length, 1);
  205. assert.match(chatwoot.sentMessages[0]?.content ?? '', /EKS-\d{8}-1311/);
  206. });
  207. test('two handoffs for one conversation reuse the same ticket number', async () => {
  208. const chatwoot = new FakeChatwoot();
  209. const first = evt({ messageId: 'h1' });
  210. const second = evt({ messageId: 'h2' });
  211. await claimMessage(first.source, first.messageId, first.conversationId);
  212. await claimMessage(second.source, second.messageId, second.conversationId);
  213. const flowise = new FakeFlowise({ type: 'handoff', text: 'ok', actions: [{ type: 'handoff' }] });
  214. const a = await processMessageEvent(first, deps(chatwoot, flowise));
  215. // The second message would normally be stopped by the ticket-mode check; call
  216. // createTicket directly to prove the idempotency of the ticket itself.
  217. const { createTicket } = await import('../../src/domain/ticketService.js');
  218. const b = await createTicket(1311, 'retry', chatwoot as unknown as ChatwootClient);
  219. assert.equal((a as { ticketNumber: string }).ticketNumber, b.ticketNumber);
  220. assert.equal(b.status, 'existing');
  221. assert.equal(await db().ticket.count({ where: { conversationId: 1311 } }), 1);
  222. });
  223. test('an empty Flowise answer is recorded as failed and nothing is sent', async () => {
  224. const chatwoot = new FakeChatwoot();
  225. const flowise = new FakeFlowise({ type: 'unknown', text: null, actions: null });
  226. const event = evt();
  227. await claimMessage(event.source, event.messageId, event.conversationId);
  228. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  229. assert.equal(outcome.action, 'no_reply');
  230. assert.equal(chatwoot.sentMessages.length, 0);
  231. const row = await db().processedMessage.findUnique({
  232. where: { source_messageId: { source: 'chatwoot', messageId: event.messageId } },
  233. });
  234. assert.equal(row?.status, 'failed');
  235. });
  236. test('the conversation is fetched when the webhook omitted labels', async () => {
  237. const chatwoot = new FakeChatwoot();
  238. chatwoot.conversation = { id: 1311, labels: ['ticket'], custom_attributes: {} };
  239. const flowise = new FakeFlowise({ type: 'reply', text: 'x', actions: null });
  240. const event = evt({ needsConversationFetch: true, labels: [] });
  241. await claimMessage(event.source, event.messageId, event.conversationId);
  242. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  243. assert.equal(outcome.action, 'skipped');
  244. assert.equal(flowise.calls.length, 0);
  245. });
  246. test('the Flowise payload keeps the contract the custom tools depend on', async () => {
  247. const payload = await buildFlowisePayload(evt());
  248. assert.match(payload.question, /\[CONTACT_INFO\]/);
  249. assert.match(payload.question, /contact_email: klient@example\.com/);
  250. assert.match(payload.question, /Czy macie gaz R134a\?/);
  251. assert.equal(payload.overrideConfig.sessionId, 'chatwoot:1311');
  252. assert.equal(payload.overrideConfig.conversationId, 1311);
  253. assert.equal(payload.overrideConfig.inboxId, 1);
  254. assert.equal(payload.overrideConfig.tenantId, 'easyklima');
  255. assert.equal(payload.overrideConfig.channel, 'email');
  256. assert.equal(payload.metadata.source, 'chatwoot');
  257. assert.equal(payload.metadata.messageType, 'incoming');
  258. });
  259. test('a significant attachment creates ticket handoff before Flowise', async () => {
  260. const chatwoot = new FakeChatwoot();
  261. const flowise = new FakeFlowise({ type: 'reply', text: 'nie powinno wyjść', actions: null });
  262. const event = evt({
  263. messageId: 'att-1',
  264. attachmentCount: 1,
  265. attachments: [{ fileName: 'faktura.pdf', contentType: 'application/pdf', fileSize: 100_000, inline: false, contentId: null }],
  266. });
  267. await claimMessage(event.source, event.messageId, event.conversationId);
  268. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  269. assert.equal(outcome.action, 'handoff');
  270. assert.equal(flowise.calls.length, 0);
  271. assert.ok(chatwoot.labels.some((l) => l.label === 'attachment'));
  272. assert.equal(chatwoot.privateNotes.length, 1);
  273. });
  274. test('form-mail mapping uses customer email in Flowise payload and records attributes', async () => {
  275. const chatwoot = new FakeChatwoot();
  276. const flowise = new FakeFlowise({ type: 'reply', text: 'OK', actions: null });
  277. const event = evt({
  278. messageId: 'form-1',
  279. senderEmail: 'wordpress@easyklima.com',
  280. formMail: {
  281. customerEmail: 'anna@example.com',
  282. customerName: 'Anna',
  283. phone: '+48123123123',
  284. technicalSender: 'wordpress@easyklima.com',
  285. formSource: 'Formularz kontaktowy',
  286. },
  287. });
  288. event.senderEmail = event.formMail?.customerEmail ?? event.senderEmail;
  289. event.senderName = event.formMail?.customerName ?? event.senderName;
  290. await claimMessage(event.source, event.messageId, event.conversationId);
  291. await processMessageEvent(event, deps(chatwoot, flowise));
  292. const payload = flowise.calls[0] as { question: string };
  293. assert.match(payload.question, /contact_email: anna@example\.com/);
  294. assert.ok(chatwoot.attributes.some((a) => a.attrs.form_mail === true));
  295. assert.ok(chatwoot.labels.some((l) => l.label === 'www-form'));
  296. });
  297. test('Flowise timeout/error is marked ai-error without customer reply', async () => {
  298. const chatwoot = new FakeChatwoot();
  299. const flowise = new FakeFlowise(new Error('upstream timeout'));
  300. const event = evt({ messageId: 'ai-fail-1' });
  301. await claimMessage(event.source, event.messageId, event.conversationId);
  302. const outcome = await processMessageEvent(event, deps(chatwoot, flowise));
  303. assert.deepEqual(outcome, { action: 'ai_error', code: 'FLOWISE_TIMEOUT' });
  304. assert.equal(chatwoot.sentMessages.length, 0);
  305. assert.ok(chatwoot.labels.some((l) => l.label === 'ai-error'));
  306. assert.equal(chatwoot.privateNotes.length, 1);
  307. });