|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { fitAdFormat, PUBLISHER_FORMAT_IDS } from "@/lib/ads/formats"; |
| 3 | + |
| 4 | +const ALL = PUBLISHER_FORMAT_IDS; |
| 5 | + |
| 6 | +describe("fitAdFormat", () => { |
| 7 | + it("keeps a format that fits", () => { |
| 8 | + expect(fitAdFormat("banner_728x90", 800, ALL)).toBe("banner_728x90"); |
| 9 | + expect(fitAdFormat("banner_300x250", 300, ALL)).toBe("banner_300x250"); |
| 10 | + }); |
| 11 | + |
| 12 | + it("downgrades a leaderboard that cannot fit a phone column", () => { |
| 13 | + // The failure this exists for: 6,836 mobile leaderboard impressions, one |
| 14 | + // click, because the CTA sat past the right edge of a clipped iframe. |
| 15 | + expect(fitAdFormat("banner_728x90", 390, ALL)).toBe("banner_300x250"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("prefers the rectangle over the mobile strip when both fit", () => { |
| 19 | + // Both fit at 390px (300 and 320 wide). The rectangle converts ~7x better, |
| 20 | + // so the choice is by area, not by width. |
| 21 | + expect(fitAdFormat("banner_728x90", 390, ALL)).toBe("banner_300x250"); |
| 22 | + expect(fitAdFormat("banner_728x90", 325, ALL)).toBe("banner_300x250"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("falls back to the narrowest offered unit when nothing fits", () => { |
| 26 | + // A clipped ad still beats a blank slot. |
| 27 | + expect(fitAdFormat("banner_728x90", 200, ALL)).toBe("banner_300x250"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("never leaves the slot's own format list", () => { |
| 31 | + // Downgrading to a format the slot does not offer would be refused later |
| 32 | + // and return no fill at all. |
| 33 | + const only = ["banner_728x90", "banner_320x50"]; |
| 34 | + expect(fitAdFormat("banner_728x90", 390, only)).toBe("banner_320x50"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("refuses a format the slot does not offer, as before", () => { |
| 38 | + expect(fitAdFormat("banner_728x90", 800, ["banner_300x250"])).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("leaves full-width and boxless formats alone", () => { |
| 42 | + // A text link fills whatever it is given; the terminal and feed units have |
| 43 | + // no pixel box at all. |
| 44 | + expect(fitAdFormat("text_link", 320, null)).toBe("text_link"); |
| 45 | + expect(fitAdFormat("terminal_ascii", 100, null)).toBe("terminal_ascii"); |
| 46 | + expect(fitAdFormat("feed_item", 100, null)).toBe("feed_item"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("honours the request when no width was measured", () => { |
| 50 | + // Older tags, and the MOTD/feed endpoints, send no width — there is no |
| 51 | + // basis to second-guess the publisher. |
| 52 | + for (const w of [null, undefined, 0, NaN]) { |
| 53 | + expect(fitAdFormat("banner_728x90", w, ALL)).toBe("banner_728x90"); |
| 54 | + } |
| 55 | + }); |
| 56 | +}); |
0 commit comments