import { findSessionOfIncomingCall, getSessionsCollection, } from "../sessions.getter"; import { getFlowsCollection } from "../../flows"; import { getPhoneNumbersCollection } from "../../phone_numbers"; import { createFlow, createPhoneNumber, createSession, } from "../../../test-utils/factories"; import { faker } from "@faker-js/faker"; import { ObjectId } from "mongodb"; describe("sessions", () => { describe("findSessionOfIncomingCall()", () => { it("returns session by incoming phone number", async () => { const to = "+972500000000"; const from = "+972540000000"; const flow = createFlow({ conversationSettings: { interruptions: { enableInterruptionDetection: true, interruptionWindowSeconds: 60, interruptionThresholdSeconds: 10, interruptionInstruction: "I am hearing background noise that makes it hard for me to focus on what you are saying. Please try to move to a quieter place or reduce the noise so I can better assist you.", }, silence: { enableSilenceDetection: true, firstWarningSilenceSeconds: 5, firstWarningInstruction: "I have not heard you for a few seconds. If you are still on the line, please say something so we can continue.", secondWarningSilenceSeconds: 10, secondWarningInstruction: "I still have not heard anything from you. If you do not respond in the next few seconds, I will have to end this call.", disconnectSilenceSeconds: 30, }, }, }); const phoneNumberData = createPhoneNumber({ phone_number: to, flow_id: flow._id, }); const json = { [faker.lorem.word()]: faker.lorem.sentence() }; const session = createSession({ flow_id: flow._id, phone_numbers: [{ phoneNumber: from, gender: "male", name: "yakov" }], json, }); await getFlowsCollection().insertOne(flow); await getPhoneNumbersCollection().insertOne(phoneNumberData); await getSessionsCollection().insertOne(session); const result = await findSessionOfIncomingCall(from, to); expect(result).not.toBeNull(); expect(result?.phone_numbers[0].phoneNumber).toEqual(from); expect(result?.flow_id).toEqual(flow._id); }); it("works with different phone number formats (with/without plus)", async () => { const toInDB = "972500000000"; const fromInDB = "+972540000000"; const flow = createFlow(); const phoneNumberData = createPhoneNumber({ phone_number: toInDB, flow_id: flow._id, }); const session = createSession({ flow_id: flow._id, phone_numbers: [ { phoneNumber: fromInDB, gender: "female", name: "sara" }, ], }); await getFlowsCollection().insertOne(flow); await getPhoneNumbersCollection().insertOne(phoneNumberData); await getSessionsCollection().insertOne(session); // Search with from WITH plus (matches + in DB), to WITH plus (matches without + in DB) const result = await findSessionOfIncomingCall( "+972540000000", "+972500000000", ); expect(result).not.toBeNull(); expect(result?.phone_numbers[0].phoneNumber).toEqual(fromInDB); }); it("works when flow_id is string in phone_numbers and ObjectId in session", async () => { const to = "+972501111111"; const from = "+972541111111"; const flowId = new ObjectId(); // Store flow_id as string in phone_numbers const phoneNumberData = createPhoneNumber({ phone_number: to, flow_id: flowId.toHexString() as any, }); const session = createSession({ flow_id: flowId, // stored as ObjectId phone_numbers: [{ phoneNumber: from, gender: "male", name: "isaac" }], }); await getPhoneNumbersCollection().insertOne(phoneNumberData); await getSessionsCollection().insertOne(session); const result = await findSessionOfIncomingCall(from, to); expect(result).not.toBeNull(); expect(result?.flow_id?.toString()).toEqual(flowId.toHexString()); }); it("works when flow_id is string in session", async () => { const to = "+972502222222"; const from = "+972542222222"; const flowId = new ObjectId(); const phoneNumberData = createPhoneNumber({ phone_number: to, flow_id: flowId, }); const session = createSession({ flow_id: flowId.toHexString(), // stored as string phone_numbers: [{ phoneNumber: from, gender: "female", name: "rivka" }], }); await getPhoneNumbersCollection().insertOne(phoneNumberData); await getSessionsCollection().insertOne(session); const result = await findSessionOfIncomingCall(from, to); expect(result).not.toBeNull(); expect(result?.flow_id).toEqual(flowId.toHexString()); }); it("returns null if receiver phone number is not found", async () => { const result = await findSessionOfIncomingCall( "any-from", "non-existent-to", ); expect(result).toBeNull(); }); }); });