import { addDays, countCallsByPhoneInRange, range, startOfDay, getCallsCollection, } from "../calls.getters"; import { createIncomingCallDoc, createOutGoingCallDoc, } from "../../../test-utils/factories"; describe("callStats.utils", () => { describe("countCallsByPhoneInRange", () => { it("counts outgoing calls for a phone within a date range", async () => { const phone = "+972500000001"; const baseDate = new Date("2023-01-10T10:00:00Z"); const outgoingInside = createOutGoingCallDoc({ customerPhoneNumber: phone, createdAt: addDays(baseDate, 0), }); const outgoingOutsideBefore = createOutGoingCallDoc({ customerPhoneNumber: phone, createdAt: addDays(baseDate, -2), }); const outgoingOutsideAfter = createOutGoingCallDoc({ customerPhoneNumber: phone, createdAt: addDays(baseDate, 2), }); await getCallsCollection().insertMany([ { ...outgoingInside, updatedAt: outgoingInside.createdAt, env: "test" }, { ...outgoingOutsideBefore, updatedAt: outgoingOutsideBefore.createdAt, env: "test", }, { ...outgoingOutsideAfter, updatedAt: outgoingOutsideAfter.createdAt, env: "test", }, ]); const since = addDays(baseDate, -1); const until = addDays(baseDate, 1); const count = await countCallsByPhoneInRange( phone, { isOutgoingCall: true }, { since, until }, ); expect(count).toBe(1); }); it("filters by incoming/outgoing flags", async () => { const phone = "+972500000002"; const createdAt = new Date("2023-02-01T12:00:00Z"); const outgoing = createOutGoingCallDoc({ customerPhoneNumber: phone, createdAt, }); const incoming = createIncomingCallDoc({ customerPhoneNumber: phone, createdAt, }); await getCallsCollection().insertMany([ { ...outgoing, updatedAt: outgoing.createdAt, env: "test" }, { ...incoming, updatedAt: incoming.createdAt, env: "test" }, ]); const outgoingCount = await countCallsByPhoneInRange( phone, { isOutgoingCall: true }, range.between(addDays(createdAt, -1), addDays(createdAt, 1)), ); const incomingCount = await countCallsByPhoneInRange( phone, { isIncomingCall: true }, range.between(addDays(createdAt, -1), addDays(createdAt, 1)), ); expect(outgoingCount).toBe(1); expect(incomingCount).toBe(1); }); }); describe("date helpers", () => { it("startOfDay normalizes to midnight", () => { const d = new Date("2023-03-10T15:30:45.000Z"); const s = startOfDay(d); expect(s.getUTCHours()).toBe(0); expect(s.getUTCMinutes()).toBe(0); expect(s.getUTCSeconds()).toBe(0); expect(s.getUTCMilliseconds()).toBe(0); }); it("addDays shifts the date by given days", () => { const d = new Date("2023-03-10T00:00:00.000Z"); expect(addDays(d, 1).getUTCDate()).toBe(11); expect(addDays(d, -1).getUTCDate()).toBe(9); }); it("range helpers produce expected bounds", () => { const base = new Date("2023-03-15T10:00:00.000Z"); const betweenRange = range.between(addDays(base, -1), addDays(base, 1)); expect(betweenRange.since).toEqual(addDays(base, -1)); expect(betweenRange.until).toEqual(addDays(base, 1)); const yesterdayRange = range.yesterday(); expect(yesterdayRange.since).toBeInstanceOf(Date); expect(yesterdayRange.until).toBeInstanceOf(Date); const todayFullRange = range.todayFull(); expect(todayFullRange.since).toBeInstanceOf(Date); expect(todayFullRange.until).toBeInstanceOf(Date); const startToNowRange = range.startOfDayToNow(base); expect(startToNowRange.since).toEqual(startOfDay(base)); expect(startToNowRange.until).toBeInstanceOf(Date); }); }); });