Skip to content

Commit

Permalink
assign the appropriate message source based on the client (BOT vs AGENT)
Browse files Browse the repository at this point in the history
J=CLIP-1341

TEST=manual,auto
tested via test-site using aws connect agent handoff and verified that the messages show up with the right sources.
added and ran unit tests.
  • Loading branch information
anguyen-yext2 committed Jul 19, 2024
1 parent 93df9d0 commit 7c713c9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
28 changes: 23 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/chat-headless/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-headless",
"version": "0.10.1",
"version": "0.11.0",
"description": "A state manager library powered by Redux for Yext Chat integrations",
"main": "./dist/commonjs/src/index.js",
"module": "./dist/esm/src/index.mjs",
Expand Down Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
"@yext/analytics": "^0.6.3",
"@yext/chat-core": "^0.8.2"
"@yext/chat-core": "^0.9.0"
},
"devDependencies": {
"@babel/preset-env": "^7.21.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/chat-headless/src/ChatHeadlessImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ChatHeadlessImpl implements ChatHeadless {

client.on("message", (data: string) => {
this.addMessage({
source: MessageSource.BOT,
source: client === this.botClient ? MessageSource.BOT : MessageSource.AGENT,
text: data,
timestamp: new Date().toISOString(),
});
Expand Down
6 changes: 5 additions & 1 deletion packages/chat-headless/tests/chatheadless.clients.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Message, MessageResponse } from "@yext/chat-core";
import { Message, MessageResponse, MessageSource } from "@yext/chat-core";
import { provideChatHeadless } from "../src/HeadlessProvider";
import { ChatEventClient, ChatHttpClient, HeadlessConfig } from "../src/models";
import * as analyticsLib from "@yext/analytics";
Expand Down Expand Up @@ -85,17 +85,20 @@ it("handoff http client between event client", async () => {
bot: botClient,
agent: agentClient,
});
// const expectedMessageSources: MessageSource[] = []

//http client handoff to event client
await headless.getNextMessage();
expect(botClient.getNextMessage).toHaveBeenCalledTimes(1);
expect(agentClient.processMessage).toHaveBeenCalledTimes(0);
expect(agentClient.init).toHaveBeenCalledTimes(1);
expect(headless.state.conversation.messages.at(-1)?.source).toEqual(MessageSource.BOT);

//event client handle next user message
await headless.getNextMessage("user message 1");
expect(botClient.getNextMessage).toHaveBeenCalledTimes(1);
expect(agentClient.processMessage).toHaveBeenCalledTimes(1);
expect(headless.state.conversation.messages.at(-1)?.source).toEqual(MessageSource.AGENT);

//event client handoff to http client
callbacks["close"]?.forEach((cb) => cb());
Expand All @@ -106,6 +109,7 @@ it("handoff http client between event client", async () => {
await headless.getNextMessage("user message 2");
expect(botClient.getNextMessage).toHaveBeenCalledTimes(2);
expect(agentClient.processMessage).toHaveBeenCalledTimes(1);
expect(headless.state.conversation.messages.at(-1)?.source).toEqual(MessageSource.BOT);
});

it("update state on events from event client", async () => {
Expand Down

0 comments on commit 7c713c9

Please sign in to comment.