import { AdsAntiCheating, IAdsAntiCheatingConfig } from './anti-cheating'; describe('createLeadingNotifiableDebounce', () => { jest.setTimeout(3000); // increase timeout for tes const track = jest.fn() as any; const http = { get: jest.fn(), post: jest.fn() }; const config: IAdsAntiCheatingConfig = { initialThreshold: 50, highFreqThreshold: 50, maxAllowedAdsForTime: 2, banForTimeThreshold: 500, // 500ms bannedReleaseTime: 500, // 500ms banForSessionThreshold: 3000, // 4s maxAllowedAdsForSession: 7 // 7 ads }; it('should ban initial call', async () => { (window as any).__joliboxLocalStorage__ = window.localStorage; (window as any).__joliboxLocalStorage__.clear(); const adsAntiCheating = new AdsAntiCheating(track, http, () => true, config); await new Promise((resolve) => setTimeout(resolve, 10)); const a = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(a.reason).toBe('BLOCK_INITIAL'); await new Promise((resolve) => setTimeout(resolve, 100)); const b = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(b.reason).toBe('ALLOWED'); }); it('should ban continous calling', async () => { (window as any).__joliboxLocalStorage__.clear(); const adsAntiCheating = new AdsAntiCheating(track, http, () => true, config); await new Promise((resolve) => setTimeout(resolve, 100)); const a = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(a.reason).toBe('ALLOWED'); // wait 10 ms await new Promise((resolve) => setTimeout(resolve, 10)); const b = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(b.reason).toBe('HOLDING_HIGH_FREQ'); expect(b.info?.count).toBe(1); // wait another 10 ms await new Promise((resolve) => setTimeout(resolve, 10)); const d = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(d.reason).toBe('HOLDING_HIGH_FREQ'); expect(d.info?.count).toBe(2); // wait another 100ms to reset the high freq timer, but trigger banned for time await new Promise((resolve) => setTimeout(resolve, 60)); // wait 100ms const c = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(c.reason).toBe('BANNED_FOR_TIME'); // wait another 100ms await new Promise((resolve) => setTimeout(resolve, 100)); // wait 100ms const e = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(e.reason).toBe('WAITING_BANNED_RELEASE'); const isBannedForTime1 = window.localStorage.getItem('jolibox-sdk-ads-ban-for-time'); expect(isBannedForTime1).toBe('true'); // wait another 400ms to reset the banned for time timer and allow display await new Promise((resolve) => setTimeout(resolve, 400)); // wait 400ms const f = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(f.reason).toBe('ALLOWED'); // 500ms await new Promise((resolve) => setTimeout(resolve, 500)); // wait 500ms const g = adsAntiCheating.checkAdsDisplayPermission('reward'); expect(g.reason).toBe('BANNED_FOR_SESSION'); const isBannedForTime = window.localStorage.getItem('jolibox-sdk-ads-ban-for-time'); expect(isBannedForTime).toBe('false'); // should be false after the timer resets }); });