import { describe, expect, it } from "vitest"; import { readFileSync, statSync } from "node:fs"; import { gzipSync } from "node:zlib"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; const here = dirname(fileURLToPath(import.meta.url)); const sdkPath = join(here, "..", "public", "embed", "marketing.js"); describe("marketing.js SDK", () => { it("is checked into apps/web/public/embed/marketing.js", () => { expect(statSync(sdkPath).isFile()).toBe(true); }); it("stays under the 3KB gzipped budget", () => { const raw = readFileSync(sdkPath); const gz = gzipSync(raw); expect(gz.byteLength).toBeLessThan(3 * 1024); }); it("stays under the 5KB raw budget", () => { const raw = readFileSync(sdkPath); expect(raw.byteLength).toBeLessThan(5 * 1024); }); it("references the capture endpoint path", () => { const text = readFileSync(sdkPath, "utf8"); expect(text).toContain("/api/leads/capture"); }); it("exposes the toolctrlMarketing global with the documented surface", () => { const text = readFileSync(sdkPath, "utf8"); expect(text).toContain("toolctrlMarketing"); expect(text).toContain("captureLead"); expect(text).toContain("bindFormById"); expect(text).toContain("getAttribution"); }); it("auto-captures the documented UTM/click-id parameters", () => { const text = readFileSync(sdkPath, "utf8"); for (const k of ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content", "gclid", "fbclid"]) { expect(text).toContain(k); } }); it("persists first-touch attribution in localStorage", () => { const text = readFileSync(sdkPath, "utf8"); expect(text).toContain("localStorage"); expect(text).toContain("__tcm_attr"); }); it("includes a honeypot field name compatible with the capture endpoint", () => { const text = readFileSync(sdkPath, "utf8"); expect(text).toContain("website_url"); }); });