Spaces:
Sleeping
Sleeping
Download apps/api/src/network/sms.service.spec.ts from Benaah/MicProtocol: direct link, hf CLI and curl.
- Browser
- Download file 7.96 kB
-
https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/apps/api/src/network/sms.service.spec.ts
- Command line
-
hf download hf://spaces/Benaah/MicProtocol/apps/api/src/network/sms.service.spec.ts
-
curl -L -o sms.service.spec.ts https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/apps/api/src/network/sms.service.spec.ts
7.96 kB
| import { Test, TestingModule } from "@nestjs/testing"; | |
| import { HttpService } from "@nestjs/axios"; | |
| import { ConfigService } from "@nestjs/config"; | |
| import { of, throwError } from "rxjs"; | |
| import { SmsService } from "./sms.service"; | |
| import { PrismaService } from "../prisma/prisma.service"; | |
| describe("SmsService", () => { | |
| let service: SmsService; | |
| let httpService: HttpService; | |
| let configService: ConfigService; | |
| const mockPrisma = { | |
| guestPublicProfile: { | |
| findFirst: jest.fn(), | |
| }, | |
| networkBookingRequest: { | |
| findMany: jest.fn(), | |
| }, | |
| profileView: { | |
| count: jest.fn(), | |
| }, | |
| }; | |
| const mockHttpService = { | |
| post: jest.fn(), | |
| }; | |
| const configValues: Record<string, any> = { | |
| TWILIO_ENABLED: false, | |
| TWILIO_ACCOUNT_SID: "mock-sid", | |
| TWILIO_AUTH_TOKEN: "mock-token", | |
| TWILIO_PHONE_NUMBER: "+1234567890", | |
| WHATSAPP_ENABLED: false, | |
| AFRICAS_TALKING_ENABLED: false, | |
| AFRICAS_TALKING_API_KEY: "mock-at-key", | |
| AFRICAS_TALKING_USERNAME: "sandbox", | |
| AFRICAS_TALKING_FROM: "MicProtocol", | |
| }; | |
| const mockConfigService = { | |
| get: jest.fn((key: string) => configValues[key]), | |
| }; | |
| beforeEach(async () => { | |
| // Reset config defaults | |
| configValues.TWILIO_ENABLED = false; | |
| configValues.WHATSAPP_ENABLED = false; | |
| configValues.AFRICAS_TALKING_ENABLED = false; | |
| configValues.AFRICAS_TALKING_USERNAME = "sandbox"; | |
| configValues.AFRICAS_TALKING_API_KEY = "mock-at-key"; | |
| configValues.AFRICAS_TALKING_FROM = "MicProtocol"; | |
| const module: TestingModule = await Test.createTestingModule({ | |
| providers: [ | |
| SmsService, | |
| { provide: HttpService, useValue: mockHttpService }, | |
| { provide: ConfigService, useValue: mockConfigService }, | |
| { provide: PrismaService, useValue: mockPrisma }, | |
| ], | |
| }).compile(); | |
| service = module.get<SmsService>(SmsService); | |
| httpService = module.get<HttpService>(HttpService); | |
| configService = module.get<ConfigService>(ConfigService); | |
| jest.clearAllMocks(); | |
| }); | |
| it("should be defined", () => { | |
| expect(service).toBeDefined(); | |
| }); | |
| describe("detectLanguage", () => { | |
| it("should map country codes to correct languages", () => { | |
| expect(service.detectLanguage("+254712345678")).toBe("sw"); // Kenya | |
| expect(service.detectLanguage("+2348012345678")).toBe("yo"); // Nigeria | |
| expect(service.detectLanguage("+27123456789")).toBe("zu"); // South Africa | |
| expect(service.detectLanguage("+11234567890")).toBe("en"); // USA (fallback) | |
| }); | |
| }); | |
| describe("sendSMSViaAfricasTalking", () => { | |
| it("should use sandbox URL when username is sandbox", async () => { | |
| // Setup SmsService with Africa's Talking enabled | |
| configValues.AFRICAS_TALKING_ENABLED = true; | |
| configValues.AFRICAS_TALKING_USERNAME = "sandbox"; | |
| // Re-instantiate to apply constructor values | |
| service = new SmsService(httpService, configService, mockPrisma as any); | |
| const responseData = { | |
| SMSMessageData: { | |
| Recipients: [{ messageId: "at-message-id-1" }], | |
| }, | |
| }; | |
| const mockResponse = { | |
| data: responseData, | |
| status: 200, | |
| statusText: "OK", | |
| headers: {}, | |
| config: {} as any, | |
| }; | |
| mockHttpService.post.mockReturnValue(of(mockResponse)); | |
| const result = await service.sendBookingRequest("+254700000000", { | |
| podcastName: "Test Podcast", | |
| message: "Hello there", | |
| actionUrl: "http://localhost/test", | |
| }); | |
| expect(result.success).toBe(true); | |
| expect(result.channel).toBe("sms"); | |
| expect(result.messageId).toBe("at-message-id-1"); | |
| expect(mockHttpService.post).toHaveBeenCalledWith( | |
| "https://api.sandbox.africastalking.com/version1/messaging", | |
| expect.any(URLSearchParams), | |
| expect.objectContaining({ | |
| headers: { | |
| apiKey: "mock-at-key", | |
| Accept: "application/json", | |
| }, | |
| }) | |
| ); | |
| // Verify parameters sent in body | |
| const postArgs = mockHttpService.post.mock.calls[0]; | |
| const params = postArgs[1] as URLSearchParams; | |
| expect(params.get("username")).toBe("sandbox"); | |
| expect(params.get("to")).toBe("+254700000000"); | |
| expect(params.get("from")).toBe("MicProtocol"); | |
| }); | |
| it("should use live URL when username is not sandbox", async () => { | |
| configValues.AFRICAS_TALKING_ENABLED = true; | |
| configValues.AFRICAS_TALKING_USERNAME = "micprotocol"; | |
| service = new SmsService(httpService, configService, mockPrisma as any); | |
| const responseData = { | |
| SMSMessageData: { | |
| Recipients: [{ messageId: "at-live-message-id" }], | |
| }, | |
| }; | |
| const mockResponse = { | |
| data: responseData, | |
| status: 200, | |
| statusText: "OK", | |
| headers: {}, | |
| config: {} as any, | |
| }; | |
| mockHttpService.post.mockReturnValue(of(mockResponse)); | |
| const result = await service.sendBookingRequest("+254700000000", { | |
| podcastName: "Test Podcast", | |
| message: "Hello there", | |
| actionUrl: "http://localhost/test", | |
| }); | |
| expect(result.success).toBe(true); | |
| expect(result.messageId).toBe("at-live-message-id"); | |
| expect(mockHttpService.post).toHaveBeenCalledWith( | |
| "https://api.africastalking.com/version1/messaging", | |
| expect.any(URLSearchParams), | |
| expect.any(Object) | |
| ); | |
| const postArgs = mockHttpService.post.mock.calls[0]; | |
| const params = postArgs[1] as URLSearchParams; | |
| expect(params.get("username")).toBe("micprotocol"); | |
| }); | |
| it("should omit from parameter if AFRICAS_TALKING_FROM is not set", async () => { | |
| configValues.AFRICAS_TALKING_ENABLED = true; | |
| configValues.AFRICAS_TALKING_USERNAME = "micprotocol"; | |
| configValues.AFRICAS_TALKING_FROM = undefined; | |
| service = new SmsService(httpService, configService, mockPrisma as any); | |
| const responseData = { | |
| SMSMessageData: { | |
| Recipients: [{ messageId: "at-no-from-id" }], | |
| }, | |
| }; | |
| const mockResponse = { | |
| data: responseData, | |
| status: 200, | |
| statusText: "OK", | |
| headers: {}, | |
| config: {} as any, | |
| }; | |
| mockHttpService.post.mockReturnValue(of(mockResponse)); | |
| await service.sendBookingRequest("+254700000000", { | |
| podcastName: "Test Podcast", | |
| message: "Hello there", | |
| actionUrl: "http://localhost/test", | |
| }); | |
| const postArgs = mockHttpService.post.mock.calls[0]; | |
| const params = postArgs[1] as URLSearchParams; | |
| expect(params.has("from")).toBe(false); | |
| }); | |
| it("should handle HTTP errors gracefully and fail the send request", async () => { | |
| configValues.AFRICAS_TALKING_ENABLED = true; | |
| configValues.AFRICAS_TALKING_USERNAME = "micprotocol"; | |
| service = new SmsService(httpService, configService, mockPrisma as any); | |
| mockHttpService.post.mockReturnValue(throwError(() => new Error("Network error"))); | |
| const result = await service.sendBookingRequest("+254700000000", { | |
| podcastName: "Test Podcast", | |
| message: "Hello there", | |
| actionUrl: "http://localhost/test", | |
| }); | |
| expect(result.success).toBe(false); | |
| expect(result.channel).toBe("none"); | |
| }); | |
| it("should use mock fallback if api key is missing", async () => { | |
| configValues.AFRICAS_TALKING_ENABLED = true; | |
| configValues.AFRICAS_TALKING_API_KEY = undefined; | |
| service = new SmsService(httpService, configService, mockPrisma as any); | |
| const result = await service.sendBookingRequest("+254700000000", { | |
| podcastName: "Test Podcast", | |
| message: "Hello there", | |
| actionUrl: "http://localhost/test", | |
| }); | |
| expect(result.success).toBe(true); | |
| expect(result.channel).toBe("sms"); | |
| expect(result.messageId).toContain("mock_at_"); | |
| expect(mockHttpService.post).not.toHaveBeenCalled(); | |
| }); | |
| }); | |
| }); | |