|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { |
| 3 | + isIdentityRefusal, |
| 4 | + REFUSAL_WINDOW_MS, |
| 5 | + ROTATE_AFTER_PLAYER_CALLS, |
| 6 | + YoutubeIdentityBudget, |
| 7 | +} from "../src/youtube-identity-budget.ts"; |
| 8 | + |
| 9 | +describe("YoutubeIdentityBudget", () => { |
| 10 | + test("asks for a background renewal on every call from the limit on", () => { |
| 11 | + const budget = new YoutubeIdentityBudget(); |
| 12 | + const signals: number[] = []; |
| 13 | + for (let call = 1; call <= ROTATE_AFTER_PLAYER_CALLS + 2; call += 1) { |
| 14 | + if (budget.notePlayerCall("visitor-a")) signals.push(call); |
| 15 | + } |
| 16 | + expect(signals).toEqual([ |
| 17 | + ROTATE_AFTER_PLAYER_CALLS, |
| 18 | + ROTATE_AFTER_PLAYER_CALLS + 1, |
| 19 | + ROTATE_AFTER_PLAYER_CALLS + 2, |
| 20 | + ]); |
| 21 | + expect(budget.notePlayerCall("visitor-b")).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + test("renews after three different refused videos in a minute", () => { |
| 25 | + let now = 0; |
| 26 | + const budget = new YoutubeIdentityBudget(() => now); |
| 27 | + expect(budget.noteRefusal("visitor", "one")).toBe(false); |
| 28 | + now += 1_000; |
| 29 | + expect(budget.noteRefusal("visitor", "one")).toBe(false); |
| 30 | + expect(budget.noteRefusal("visitor", "two")).toBe(false); |
| 31 | + expect(budget.noteRefusal("visitor", "three")).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + test("a success between refusals means the videos were really unavailable", () => { |
| 35 | + const budget = new YoutubeIdentityBudget(() => 0); |
| 36 | + budget.noteRefusal("visitor", "one"); |
| 37 | + budget.noteRefusal("visitor", "two"); |
| 38 | + budget.noteSuccess("visitor"); |
| 39 | + expect(budget.noteRefusal("visitor", "three")).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + test("old refusals fall out of the window", () => { |
| 43 | + let now = 0; |
| 44 | + const budget = new YoutubeIdentityBudget(() => now); |
| 45 | + budget.noteRefusal("visitor", "one"); |
| 46 | + budget.noteRefusal("visitor", "two"); |
| 47 | + now += REFUSAL_WINDOW_MS + 1; |
| 48 | + expect(budget.noteRefusal("visitor", "three")).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + test("refusals are counted per identity", () => { |
| 52 | + const budget = new YoutubeIdentityBudget(() => 0); |
| 53 | + budget.noteRefusal("old", "one"); |
| 54 | + budget.noteRefusal("old", "two"); |
| 55 | + expect(budget.noteRefusal("new", "three")).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + test("keeps counters for the last identities only", () => { |
| 59 | + const budget = new YoutubeIdentityBudget(); |
| 60 | + for (let call = 1; call < ROTATE_AFTER_PLAYER_CALLS; call += 1) budget.notePlayerCall("first"); |
| 61 | + for (let identity = 0; identity < 8; identity += 1) budget.notePlayerCall(`other-${identity}`); |
| 62 | + expect(budget.notePlayerCall("first")).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + test("only an unavailable answer counts as a refusal", () => { |
| 66 | + expect(isIdentityRefusal("UNPLAYABLE", "Video unavailable")).toBe(true); |
| 67 | + expect(isIdentityRefusal("UNPLAYABLE", "This video is unavailable")).toBe(true); |
| 68 | + expect(isIdentityRefusal("LOGIN_REQUIRED", "Sign in to confirm your age")).toBe(false); |
| 69 | + expect(isIdentityRefusal("UNPLAYABLE", "This live event will begin in a few moments.")).toBe( |
| 70 | + false, |
| 71 | + ); |
| 72 | + expect(isIdentityRefusal("ERROR", "Video unavailable")).toBe(false); |
| 73 | + expect(isIdentityRefusal("OK")).toBe(false); |
| 74 | + }); |
| 75 | +}); |
0 commit comments