From 574a96050bd6f514d4da73ff5c21fe0cb8a91bfb Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 24 Nov 2025 17:45:49 +0100 Subject: [PATCH] Add utility tests for paths, prefixes, and sleep --- src/utils.test.ts | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/utils.test.ts diff --git a/src/utils.test.ts b/src/utils.test.ts new file mode 100644 index 000000000..e84015ff1 --- /dev/null +++ b/src/utils.test.ts @@ -0,0 +1,65 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { describe, expect, it, vi } from "vitest"; +import { + assertProvider, + ensureDir, + normalizeE164, + normalizePath, + sleep, + toWhatsappJid, + withWhatsAppPrefix, +} from "./utils.js"; + +describe("normalizePath", () => { + it("adds leading slash when missing", () => { + expect(normalizePath("foo")).toBe("/foo"); + }); + + it("keeps existing slash", () => { + expect(normalizePath("/bar")).toBe("/bar"); + }); +}); + +describe("withWhatsAppPrefix", () => { + it("adds whatsapp prefix", () => { + expect(withWhatsAppPrefix("+1555")).toBe("whatsapp:+1555"); + }); + + it("leaves prefixed intact", () => { + expect(withWhatsAppPrefix("whatsapp:+1555")).toBe("whatsapp:+1555"); + }); +}); + +describe("ensureDir", () => { + it("creates nested directory", async () => { + const tmp = await fs.promises.mkdtemp(path.join(os.tmpdir(), "warelay-test-")); + const target = path.join(tmp, "nested", "dir"); + await ensureDir(target); + expect(fs.existsSync(target)).toBe(true); + }); +}); + +describe("sleep", () => { + it("resolves after delay using fake timers", async () => { + vi.useFakeTimers(); + const promise = sleep(1000); + vi.advanceTimersByTime(1000); + await expect(promise).resolves.toBeUndefined(); + vi.useRealTimers(); + }); +}); + +describe("assertProvider", () => { + it("throws for invalid provider", () => { + expect(() => assertProvider("bad" as string)).toThrow(); + }); +}); + +describe("normalizeE164 & toWhatsappJid", () => { + it("strips formatting and prefixes", () => { + expect(normalizeE164("whatsapp:(555) 123-4567")).toBe("+5551234567"); + expect(toWhatsappJid("whatsapp:+555 123 4567")).toBe("5551234567@s.whatsapp.net"); + }); +});