|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +// A click trailing its impression past the device ceiling is refused outright: |
| 4 | +// no ad_clicks row, no redirect to the advertiser. Recording it as invalid and |
| 5 | +// still redirecting is what kept the feed-link harvester coming back. |
| 6 | + |
| 7 | +const state = vi.hoisted(() => ({ |
| 8 | + validity: { valid: true } as { valid: boolean; reason?: string }, |
| 9 | + inserts: [] as Record<string, unknown>[], |
| 10 | + rpc: vi.fn(), |
| 11 | +})); |
| 12 | +vi.mock("@/lib/supabase/service", () => ({ serviceClient: () => ({ |
| 13 | + rpc: state.rpc, |
| 14 | + from(table: string) { |
| 15 | + const q = { |
| 16 | + select: () => q, eq: () => q, |
| 17 | + insert(row: Record<string, unknown>) { state.inserts.push(row); return q; }, |
| 18 | + maybeSingle: async () => ({ data: table === "ad_campaigns" |
| 19 | + ? { id: "campaign", destination_url: "https://example.com/offer", ref_slug: "ad-test", bid_credits: 4 } |
| 20 | + : { visitor_id: "visitor" } }), |
| 21 | + }; |
| 22 | + return q; |
| 23 | + }, |
| 24 | +}) })); |
| 25 | +vi.mock("@/lib/ads/fraud", async (original) => ({ |
| 26 | + ...await original<object>(), assessClickValidity: async () => state.validity, |
| 27 | +})); |
| 28 | +vi.mock("@/lib/ads/click-cooldown", () => ({ claimClickCooldown: async () => ({ allowed: true }) })); |
| 29 | +vi.mock("@/lib/ads/promos", () => ({ promoForCampaign: async () => null })); |
| 30 | +vi.mock("@/lib/ads/bids", () => ({ paperCharge: vi.fn() })); |
| 31 | +import { BLOCKED_CLICK, resolveClick } from "@/lib/ads/serve"; |
| 32 | +import { blockedClickResponse } from "@/lib/ads/blocked-click"; |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + state.validity = { valid: true }; |
| 36 | + state.inserts = []; |
| 37 | + state.rpc.mockReset(); |
| 38 | + state.rpc.mockResolvedValue({ data: [{ click_id: "click", valid: true, charged_cents: 20 }] }); |
| 39 | +}); |
| 40 | + |
| 41 | +const click = () => resolveClick({ |
| 42 | + campaignId: "campaign", slotId: "slot", impressionId: "imp", |
| 43 | + ctx: { ip: "8.8.8.8", visitorId: "visitor", device: "desktop" }, |
| 44 | +}); |
| 45 | + |
| 46 | +describe("stale ad clicks", () => { |
| 47 | + it("are refused: no row, no charge, no destination", async () => { |
| 48 | + state.validity = { valid: false, reason: "stale_impression" }; |
| 49 | + expect(await click()).toBe(BLOCKED_CLICK); |
| 50 | + expect(state.inserts).toHaveLength(0); |
| 51 | + expect(state.rpc).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("leave every other invalid click recorded and redirected", async () => { |
| 55 | + state.validity = { valid: false, reason: "duplicate" }; |
| 56 | + expect(await click()).toBe("https://example.com/offer?ref=ad-test"); |
| 57 | + expect(state.inserts).toHaveLength(1); |
| 58 | + }); |
| 59 | + |
| 60 | + it("get a 410 that links nowhere and is kept by nothing", async () => { |
| 61 | + const res = blockedClickResponse(); |
| 62 | + expect(res.status).toBe(410); |
| 63 | + expect(res.headers.get("location")).toBeNull(); |
| 64 | + expect(res.headers.get("cache-control")).toBe("no-store"); |
| 65 | + expect(res.headers.get("x-robots-tag")).toContain("nofollow"); |
| 66 | + expect(await res.text()).not.toContain("example.com"); |
| 67 | + }); |
| 68 | +}); |
0 commit comments