|
| 1 | +import getSendingStats from "../getSendingStats"; |
| 2 | +import { client } from "../../../client"; |
| 3 | + |
| 4 | +jest.mock("../../../client", () => ({ |
| 5 | + client: { |
| 6 | + stats: { |
| 7 | + get: jest.fn(), |
| 8 | + byDomain: jest.fn(), |
| 9 | + byCategory: jest.fn(), |
| 10 | + byEmailServiceProvider: jest.fn(), |
| 11 | + byDate: jest.fn(), |
| 12 | + }, |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +const mockStats = { |
| 17 | + delivery_count: 100, |
| 18 | + delivery_rate: 0.95, |
| 19 | + bounce_count: 5, |
| 20 | + bounce_rate: 0.05, |
| 21 | + open_count: 80, |
| 22 | + open_rate: 0.8, |
| 23 | + click_count: 50, |
| 24 | + click_rate: 0.5, |
| 25 | + spam_count: 2, |
| 26 | + spam_rate: 0.02, |
| 27 | +}; |
| 28 | + |
| 29 | +const originalEnv = { ...process.env }; |
| 30 | + |
| 31 | +describe("getSendingStats", () => { |
| 32 | + beforeEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | + Object.assign(process.env, { |
| 35 | + MAILTRAP_ACCOUNT_ID: "12345", |
| 36 | + }); |
| 37 | + (client as any).stats.get.mockResolvedValue(mockStats); |
| 38 | + (client as any).stats.byDomain.mockResolvedValue([ |
| 39 | + { name: "sending_domain_id", value: 3938, stats: mockStats }, |
| 40 | + ]); |
| 41 | + (client as any).stats.byDate.mockResolvedValue([ |
| 42 | + { name: "date", value: "2025-01-01", stats: mockStats }, |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + Object.assign(process.env, originalEnv); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return aggregated stats successfully", async () => { |
| 51 | + const result = await getSendingStats({ |
| 52 | + start_date: "2025-01-01", |
| 53 | + end_date: "2025-01-31", |
| 54 | + }); |
| 55 | + |
| 56 | + expect((client as any).stats.get).toHaveBeenCalledWith({ |
| 57 | + start_date: "2025-01-01", |
| 58 | + end_date: "2025-01-31", |
| 59 | + }); |
| 60 | + expect(result.content).toHaveLength(1); |
| 61 | + expect(result.content[0].type).toBe("text"); |
| 62 | + expect(result.content[0].text).toContain("Sending stats (aggregated)"); |
| 63 | + expect(result.content[0].text).toContain("2025-01-01 to 2025-01-31"); |
| 64 | + expect(result.content[0].text).toContain("Delivery: 100 (95.00%)"); |
| 65 | + expect(result.content[0].text).toContain("Bounces: 5 (5.00%)"); |
| 66 | + expect(result.content[0].text).toContain("Opens: 80 (80.00%)"); |
| 67 | + expect(result.content[0].text).toContain("Clicks: 50 (50.00%)"); |
| 68 | + expect(result.content[0].text).toContain("Spam: 2 (2.00%)"); |
| 69 | + expect(result.isError).toBeUndefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should return stats by domain when breakdown is by_domain", async () => { |
| 73 | + const result = await getSendingStats({ |
| 74 | + start_date: "2025-01-01", |
| 75 | + end_date: "2025-01-31", |
| 76 | + breakdown: "by_domain", |
| 77 | + }); |
| 78 | + |
| 79 | + expect((client as any).stats.byDomain).toHaveBeenCalledWith({ |
| 80 | + start_date: "2025-01-01", |
| 81 | + end_date: "2025-01-31", |
| 82 | + }); |
| 83 | + expect(result.content[0].text).toContain("by domain"); |
| 84 | + expect(result.content[0].text).toContain("3938:"); |
| 85 | + expect(result.content[0].text).toContain("Delivery: 100 (95.00%)"); |
| 86 | + expect(result.isError).toBeUndefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should return stats by date when breakdown is by_date", async () => { |
| 90 | + const result = await getSendingStats({ |
| 91 | + start_date: "2025-01-01", |
| 92 | + end_date: "2025-01-31", |
| 93 | + breakdown: "by_date", |
| 94 | + }); |
| 95 | + |
| 96 | + expect((client as any).stats.byDate).toHaveBeenCalledWith({ |
| 97 | + start_date: "2025-01-01", |
| 98 | + end_date: "2025-01-31", |
| 99 | + }); |
| 100 | + expect(result.content[0].text).toContain("by date"); |
| 101 | + expect(result.content[0].text).toContain("2025-01-01:"); |
| 102 | + expect(result.isError).toBeUndefined(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should pass optional filters to the API", async () => { |
| 106 | + await getSendingStats({ |
| 107 | + start_date: "2025-01-01", |
| 108 | + end_date: "2025-01-31", |
| 109 | + sending_domain_ids: [1, 2], |
| 110 | + sending_streams: ["transactional"], |
| 111 | + categories: ["Welcome"], |
| 112 | + email_service_providers: ["Google"], |
| 113 | + }); |
| 114 | + |
| 115 | + expect((client as any).stats.get).toHaveBeenCalledWith({ |
| 116 | + start_date: "2025-01-01", |
| 117 | + end_date: "2025-01-31", |
| 118 | + sending_domain_ids: [1, 2], |
| 119 | + sending_streams: ["transactional"], |
| 120 | + categories: ["Welcome"], |
| 121 | + email_service_providers: ["Google"], |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should return error when MAILTRAP_ACCOUNT_ID is missing", async () => { |
| 126 | + delete process.env.MAILTRAP_ACCOUNT_ID; |
| 127 | + |
| 128 | + const result = await getSendingStats({ |
| 129 | + start_date: "2025-01-01", |
| 130 | + end_date: "2025-01-31", |
| 131 | + }); |
| 132 | + |
| 133 | + expect(result.isError).toBe(true); |
| 134 | + expect(result.content[0].text).toContain( |
| 135 | + "MAILTRAP_ACCOUNT_ID environment variable is required for sending stats" |
| 136 | + ); |
| 137 | + expect((client as any).stats.get).not.toHaveBeenCalled(); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should return error when MAILTRAP_ACCOUNT_ID is invalid", async () => { |
| 141 | + process.env.MAILTRAP_ACCOUNT_ID = "not-a-number"; |
| 142 | + |
| 143 | + const result = await getSendingStats({ |
| 144 | + start_date: "2025-01-01", |
| 145 | + end_date: "2025-01-31", |
| 146 | + }); |
| 147 | + |
| 148 | + expect(result.isError).toBe(true); |
| 149 | + expect(result.content[0].text).toContain( |
| 150 | + "MAILTRAP_ACCOUNT_ID environment variable is required for sending stats" |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should return error when client is null", async () => { |
| 155 | + jest.doMock("../../../client", () => ({ client: null })); |
| 156 | + jest.resetModules(); |
| 157 | + const { default: getSendingStatsWithNullClient } = await import( |
| 158 | + "../getSendingStats" |
| 159 | + ); |
| 160 | + const result = await getSendingStatsWithNullClient({ |
| 161 | + start_date: "2025-01-01", |
| 162 | + end_date: "2025-01-31", |
| 163 | + }); |
| 164 | + expect(result.isError).toBe(true); |
| 165 | + expect(result.content[0].text).toContain("MAILTRAP_API_TOKEN"); |
| 166 | + jest.resetModules(); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should handle API errors", async () => { |
| 170 | + (client as any).stats.get.mockRejectedValue( |
| 171 | + new Error("Rate limit exceeded") |
| 172 | + ); |
| 173 | + |
| 174 | + const result = await getSendingStats({ |
| 175 | + start_date: "2025-01-01", |
| 176 | + end_date: "2025-01-31", |
| 177 | + }); |
| 178 | + |
| 179 | + expect(result.isError).toBe(true); |
| 180 | + expect(result.content[0].text).toContain("Failed to get sending stats"); |
| 181 | + expect(result.content[0].text).toContain("Rate limit exceeded"); |
| 182 | + }); |
| 183 | + |
| 184 | + it("should call byCategory when breakdown is by_category", async () => { |
| 185 | + (client as any).stats.byCategory.mockResolvedValue([ |
| 186 | + { name: "category", value: "Welcome", stats: mockStats }, |
| 187 | + ]); |
| 188 | + |
| 189 | + const result = await getSendingStats({ |
| 190 | + start_date: "2025-01-01", |
| 191 | + end_date: "2025-01-31", |
| 192 | + breakdown: "by_category", |
| 193 | + }); |
| 194 | + |
| 195 | + expect((client as any).stats.byCategory).toHaveBeenCalledWith({ |
| 196 | + start_date: "2025-01-01", |
| 197 | + end_date: "2025-01-31", |
| 198 | + }); |
| 199 | + expect(result.content[0].text).toContain("Welcome:"); |
| 200 | + }); |
| 201 | + |
| 202 | + it("should call byEmailServiceProvider when breakdown is by_email_service_provider", async () => { |
| 203 | + (client as any).stats.byEmailServiceProvider.mockResolvedValue([ |
| 204 | + { name: "email_service_provider", value: "Google", stats: mockStats }, |
| 205 | + ]); |
| 206 | + |
| 207 | + const result = await getSendingStats({ |
| 208 | + start_date: "2025-01-01", |
| 209 | + end_date: "2025-01-31", |
| 210 | + breakdown: "by_email_service_provider", |
| 211 | + }); |
| 212 | + |
| 213 | + expect((client as any).stats.byEmailServiceProvider).toHaveBeenCalledWith({ |
| 214 | + start_date: "2025-01-01", |
| 215 | + end_date: "2025-01-31", |
| 216 | + }); |
| 217 | + expect(result.content[0].text).toContain("Google:"); |
| 218 | + }); |
| 219 | +}); |
0 commit comments