Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix chatbot and add regression test #82

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion server/src/test/websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
} from "../../../shared/common";
import { handleChatMessage } from "../websocketServer/handleChatMessage";
import { executeGameOperation } from "./helpers";
import { delay } from "../utils";
import { getSendBotMessagesRunnable } from "../websocketServer/bots/sendBotMessage";

const getTestSession = E.provideService(
DBConnection.pipe(
Expand Down Expand Up @@ -103,7 +105,7 @@ describe("gamestate tests", () => {
expect(postTestTotalCards).toEqual(11);
});

it("only buy only the cards paying for cards are removed from cardsInPlay", async () => {
it("only the cards used for purchase are removed from cardsInPlay", async () => {
// Fetch initial game state
const startingGameState = await setUpGame();

Expand Down Expand Up @@ -193,6 +195,10 @@ describe("gamestate tests", () => {
});

describe("chat tests", () => {
beforeEach(async () => {
await resetAndSeedDatabase();
});

it("send chat message", async () => {
const initialGamestate = await E.runPromise(getTestSession);

Expand Down Expand Up @@ -237,4 +243,100 @@ describe("chat tests", () => {
testMsg.chatMessage
);
});

it("can add bot and have converstion with bot", async () => {
const initialGameState = await E.runPromise(getTestSession);
const addBotPlayerRunnable = pipe(
executeGameOperation({
effect: SupportedEffects.addLivePlayer,
userId: testUser1.userId,
lastGameState: initialGameState,
}),
E.flatMap((gamestate) =>
executeGameOperation({
effect: SupportedEffects.addBotPlayer,
userId: testUser1.userId,
lastGameState: gamestate,
})
)
);

const testMsg1 = {
mutationIndex: initialGameState.mutation_index,
authToken: testUser1.authToken,
effect: SupportedEffects.sendChatMessage,
room: initialGameState.room,
cardName: undefined,
userId: testUser1.userId,
chatMessage: "test chat message 1",
} as ClientPayload;

const testMsg2 = {
...testMsg1,
chatMessage: "test chat message 2",
} as ClientPayload;

const sendChatMessage1 = DBConnection.pipe(
E.flatMap((connection) => connection.pool),
E.flatMap((pool) =>
E.all({
pool: E.succeed(pool),
msg: E.succeed(testMsg1),
})
),
E.flatMap(({ msg, pool }) =>
handleChatMessage({
msg,
pool,
userInfo: {
userId: testUser1.userId,
username: testUser1.username,
},
})
),
E.flatMap(safeParseChatLog)
);

const sendChatMessage2 = DBConnection.pipe(
E.flatMap((connection) => connection.pool),
E.flatMap((pool) =>
E.all({
pool: E.succeed(pool),
msg: E.succeed(testMsg2),
})
),
E.flatMap(({ msg, pool }) =>
handleChatMessage({
msg,
pool,
userInfo: {
userId: testUser1.userId,
username: testUser1.username,
},
})
),
E.flatMap(safeParseChatLog)
);

const sendChatMessage1Runnable = E.provideService(
sendChatMessage1,
DBConnection,
DBConnectionTest
);

const sendBotMessagesRunnable = getSendBotMessagesRunnable(testMsg1, []);

const sendChatMessage2Runnable = E.provideService(
sendChatMessage2,
DBConnection,
DBConnectionTest
);

await E.runPromise(addBotPlayerRunnable);
await E.runPromise(sendChatMessage1Runnable);
await E.runPromise(sendBotMessagesRunnable);
const resultLog = await E.runPromise(sendChatMessage2Runnable);
expect(resultLog.some((msg) => msg.username.match(/_bot_/))).toEqual(true);
expect(resultLog.length).toEqual(3);
});
});
29 changes: 11 additions & 18 deletions server/src/websocketServer/bots/sendBotMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,17 @@ export function getSendBotMessagesRunnable(
) {
const sendBotMessages = DBConnection.pipe(
E.flatMap((connection) => connection.pool),
E.flatMap((pool) =>
pipe(
E.all({
msg: parseJSONToClientMsg(msg),
pool: E.succeed(pool),
}),
E.flatMap(({ pool, msg }) => {
if (msg.chatMessage) {
return sendBotMessage(msg, roomConnections, pool);
}
return E.succeed(E.unit);
}),
E.catchAll((e) => {
console.log(e);
return E.succeed(E.unit);
})
)
)
E.flatMap((pool) => {
if (msg?.chatMessage) {
return sendBotMessage(msg, roomConnections, pool);
}
return E.succeed(E.unit);
}),

E.catchAll((e) => {
console.log(e);
return E.succeed(E.unit);
})
);

return E.provide(sendBotMessages, DBConnectionLive);
Expand Down
Loading