import { AI_REVIEW_SUBJECT, buildWhatsAppUrl, isServiceOrCasePath, type WhatsAppContexts, whatsAppSubjectForPath, } from "./whatsapp"; const contexts: WhatsAppContexts = { services: { "criacao-de-sites": "Sites profissionais" }, cases: { "sev-modaintima": "SEV Moda Íntima" }, }; describe("whatsAppSubjectForPath", () => { it("cites the service on /servicos/", () => { expect(whatsAppSubjectForPath("/servicos/criacao-de-sites", contexts)).toBe( "Sites profissionais", ); }); it("cites the case on /cases/", () => { expect(whatsAppSubjectForPath("/cases/sev-modaintima", contexts)).toBe( "um projeto parecido com o da SEV Moda Íntima", ); }); it("cites the AI review on /ia", () => { expect(whatsAppSubjectForPath("/ia", contexts)).toBe(AI_REVIEW_SUBJECT); expect(whatsAppSubjectForPath("/ia/", undefined)).toBe(AI_REVIEW_SUBJECT); }); it("ignores a trailing slash", () => { expect( whatsAppSubjectForPath("/servicos/criacao-de-sites/", contexts), ).toBe("Sites profissionais"); }); it.each([ "/", "/servicos", "/cases", "/curriculo", "/privacidade", "/servicos/inexistente", "/cases/inexistente", "/servicos/criacao-de-sites/extra", // nomes que existem em todo objeto não podem virar assunto "/servicos/constructor", "/cases/__proto__", "/cases/toString", ])("falls back to a generic message on %s", (pathname) => { expect(whatsAppSubjectForPath(pathname, contexts)).toBeUndefined(); }); it("treats an empty or blank name as missing, so the message never reads 'sobre .'", () => { const blank: WhatsAppContexts = { services: { vazio: "", espacos: " " }, cases: { vazio: "", espacos: " " }, }; for (const pathname of [ "/servicos/vazio", "/servicos/espacos", "/cases/vazio", "/cases/espacos", ]) { expect(whatsAppSubjectForPath(pathname, blank)).toBeUndefined(); } }); it("is generic without contexts, without a pathname or outside the router", () => { expect( whatsAppSubjectForPath("/servicos/criacao-de-sites"), ).toBeUndefined(); expect(whatsAppSubjectForPath(null, contexts)).toBeUndefined(); expect(whatsAppSubjectForPath(undefined, contexts)).toBeUndefined(); }); }); describe("isServiceOrCasePath", () => { it.each([ ["/servicos/criacao-de-sites", true], ["/servicos/qualquer-coisa/", true], ["/cases/sev-modaintima", true], ["/", false], ["/servicos", false], ["/cases", false], ["/ia", false], ["/servicos/a/b", false], [null, false], [undefined, false], ])("%s -> %s", (pathname, expected) => { expect(isServiceOrCasePath(pathname)).toBe(expected); }); }); describe("buildWhatsAppUrl", () => { it("encodes the message in the text parameter of a wa.me link", () => { const url = new URL(buildWhatsAppUrl("Olá & tudo? #1")); expect(url.origin).toBe("https://wa.me"); expect(url.pathname).toMatch(/^\/\d{12,13}$/); expect(url.searchParams.get("text")).toBe("Olá & tudo? #1"); }); });