|
| 1 | +import { afterEach, expect, mock, spyOn, test } from "bun:test" |
| 2 | +import { enterpriseSpamProbability, ENTERPRISE_SPAM_MODEL, ENTERPRISE_SPAM_URL } from "./enterprise-spam" |
| 3 | +import { handleEnterpriseRequest } from "./enterprise" |
| 4 | + |
| 5 | +const submission = { |
| 6 | + name: "Fake Visitor", |
| 7 | + role: "Engineering lead", |
| 8 | + company: "Example Test Company", |
| 9 | + email: "fake-visitor@example.test", |
| 10 | + message: "We need SSO for 200 developers.", |
| 11 | +} |
| 12 | + |
| 13 | +afterEach(() => mock.restore()) |
| 14 | + |
| 15 | +const score = (probability: unknown) => |
| 16 | + Response.json({ model: ENTERPRISE_SPAM_MODEL, answers: { spam: { type: "noul", noul: probability } } }) |
| 17 | + |
| 18 | +test("scores a submission through the System One gateway", async () => { |
| 19 | + const requests: Request[] = [] |
| 20 | + // The form handler passes the whole submission, phone included; the classifier must not receive it. |
| 21 | + const withPhone = { ...submission, phone: "+1 555 0100" } |
| 22 | + const probability = await enterpriseSpamProbability(withPhone, { |
| 23 | + apiKey: "test-key", |
| 24 | + fetch: async (request) => { |
| 25 | + requests.push(request) |
| 26 | + return score(0.97) |
| 27 | + }, |
| 28 | + }) |
| 29 | + |
| 30 | + expect(probability).toBe(0.97) |
| 31 | + expect(requests).toHaveLength(1) |
| 32 | + expect(requests[0].url).toBe(ENTERPRISE_SPAM_URL) |
| 33 | + expect(requests[0].headers.get("authorization")).toBe("Bearer test-key") |
| 34 | + const body = await requests[0].json() |
| 35 | + expect(body.model).toBe(ENTERPRISE_SPAM_MODEL) |
| 36 | + expect(body.state.submission).toMatchObject({ email: submission.email, message: submission.message }) |
| 37 | + expect(body.state.submission).not.toHaveProperty("phone") |
| 38 | + expect(body.questions.spam.type).toBe("noul") |
| 39 | +}) |
| 40 | + |
| 41 | +test("returns no score when the gateway fails, times out, or answers without a probability", async () => { |
| 42 | + const options = { apiKey: "test-key" } |
| 43 | + const failures = [ |
| 44 | + async () => new Response("nope", { status: 401 }), |
| 45 | + async () => { |
| 46 | + throw new Error("network") |
| 47 | + }, |
| 48 | + async () => score("high"), |
| 49 | + async () => score(1.5), |
| 50 | + async () => Response.json({ answers: {} }), |
| 51 | + (request: Request) => |
| 52 | + new Promise<Response>((_, reject) => |
| 53 | + request.signal.addEventListener("abort", () => reject(request.signal.reason)), |
| 54 | + ), |
| 55 | + ] |
| 56 | + for (const fetch of failures) { |
| 57 | + expect(await enterpriseSpamProbability(submission, { ...options, timeoutMs: 20, fetch })).toBeUndefined() |
| 58 | + } |
| 59 | +}) |
| 60 | + |
| 61 | +const submit = async (fetch: (request: Request) => Promise<Response>) => { |
| 62 | + const leads: Record<string, unknown>[] = [] |
| 63 | + const response = await handleEnterpriseRequest( |
| 64 | + new Request("http://localhost/api/enterprise", { |
| 65 | + method: "POST", |
| 66 | + headers: { "Content-Type": "application/json" }, |
| 67 | + body: JSON.stringify({ ...submission, alias: "", captcha: "token" }), |
| 68 | + }), |
| 69 | + { |
| 70 | + captcha: { secret: "test-secret", fetch: async () => Response.json({ success: true }) }, |
| 71 | + spam: { apiKey: "test-key", fetch }, |
| 72 | + salesforceSubmit: async (payload) => { |
| 73 | + leads.push(payload) |
| 74 | + return true |
| 75 | + }, |
| 76 | + notifications: false, |
| 77 | + }, |
| 78 | + ) |
| 79 | + return { status: response.status, body: await response.json(), leads } |
| 80 | +} |
| 81 | + |
| 82 | +test("confident spam gets the normal success response, never reaches Salesforce, and is logged", async () => { |
| 83 | + const warn = spyOn(console, "warn").mockImplementation(() => {}) |
| 84 | + const result = await submit(async () => score(0.95)) |
| 85 | + expect(result.status).toBe(200) |
| 86 | + expect(result.body).toMatchObject({ success: true }) |
| 87 | + expect(result.leads).toHaveLength(0) |
| 88 | + expect(warn).toHaveBeenCalledWith("Enterprise inquiry dropped as spam", { |
| 89 | + email: submission.email, |
| 90 | + probability: 0.95, |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +test("uncertain, missing, or failed scores still create the lead", async () => { |
| 95 | + spyOn(console, "error").mockImplementation(() => {}) |
| 96 | + for (const fetch of [ |
| 97 | + async () => score(0.6), |
| 98 | + async () => Response.json({ answers: {} }), |
| 99 | + async () => { |
| 100 | + throw new Error("classifier down") |
| 101 | + }, |
| 102 | + ]) { |
| 103 | + const result = await submit(fetch) |
| 104 | + expect(result.status).toBe(200) |
| 105 | + expect(result.leads).toHaveLength(1) |
| 106 | + } |
| 107 | +}) |
0 commit comments