Add vitest and unit coverage for provider helpers
This commit is contained in:
29
src/index.test.ts
Normal file
29
src/index.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { normalizeE164, toWhatsappJid, assertProvider } from "./index.js";
|
||||
|
||||
describe("normalizeE164", () => {
|
||||
it("strips whatsapp: prefix and whitespace", () => {
|
||||
expect(normalizeE164("whatsapp:+1 555 123 4567")).toBe("+15551234567");
|
||||
});
|
||||
|
||||
it("adds plus when missing", () => {
|
||||
expect(normalizeE164("1555123")).toBe("+1555123");
|
||||
});
|
||||
});
|
||||
|
||||
describe("toWhatsappJid", () => {
|
||||
it("converts E164 to jid", () => {
|
||||
expect(toWhatsappJid("+1 555 123 4567")).toBe("15551234567@s.whatsapp.net");
|
||||
});
|
||||
});
|
||||
|
||||
describe("assertProvider", () => {
|
||||
it("accepts valid providers", () => {
|
||||
expect(() => assertProvider("twilio")).not.toThrow();
|
||||
expect(() => assertProvider("web")).not.toThrow();
|
||||
});
|
||||
|
||||
it("throws on invalid provider", () => {
|
||||
expect(() => assertProvider("invalid" as string)).toThrow();
|
||||
});
|
||||
});
|
||||
13
src/index.ts
13
src/index.ts
@@ -5,6 +5,7 @@ import net from "node:net";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import process, { stdin as input, stdout as output } from "node:process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import readline from "node:readline/promises";
|
||||
import { promisify } from "node:util";
|
||||
import bodyParser from "body-parser";
|
||||
@@ -384,8 +385,7 @@ type Provider = "twilio" | "web";
|
||||
|
||||
function assertProvider(input: string): asserts input is Provider {
|
||||
if (input !== "twilio" && input !== "web") {
|
||||
console.error("Provider must be 'twilio' or 'web'");
|
||||
process.exit(1);
|
||||
throw new Error("Provider must be 'twilio' or 'web'");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1818,4 +1818,11 @@ program
|
||||
await waitForever();
|
||||
});
|
||||
|
||||
program.parseAsync(process.argv);
|
||||
export { normalizeE164, toWhatsappJid, assertProvider };
|
||||
|
||||
const isMain =
|
||||
process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
|
||||
|
||||
if (isMain) {
|
||||
program.parseAsync(process.argv);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user