|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + getMegaReferralUrl, |
| 4 | + shouldOfferMegaUpgrade, |
| 5 | + MEGA_UPGRADE_OFFER_THRESHOLD |
| 6 | +} from './referral'; |
| 7 | + |
| 8 | +describe('getMegaReferralUrl', () => { |
| 9 | + it('returns null when the env var is unset or empty', () => { |
| 10 | + expect(getMegaReferralUrl({})).toBeNull(); |
| 11 | + expect(getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: '' })).toBeNull(); |
| 12 | + expect(getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: undefined })).toBeNull(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('returns the URL for https MEGA domains', () => { |
| 16 | + expect(getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: 'https://mega.nz/aff=AbCd1234' })).toBe( |
| 17 | + 'https://mega.nz/aff=AbCd1234' |
| 18 | + ); |
| 19 | + expect(getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: 'https://mega.io/pro?aff=x' })).toBe( |
| 20 | + 'https://mega.io/pro?aff=x' |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it('rejects non-MEGA hosts and non-https URLs (misconfiguration guard)', () => { |
| 25 | + expect(getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: 'https://example.com/aff=x' })).toBeNull(); |
| 26 | + expect( |
| 27 | + getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: 'https://mega.nz.evil.com/aff=x' }) |
| 28 | + ).toBeNull(); |
| 29 | + expect(getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: 'http://mega.nz/aff=x' })).toBeNull(); |
| 30 | + expect(getMegaReferralUrl({ VITE_MEGA_REFERRAL_URL: 'not a url' })).toBeNull(); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('shouldOfferMegaUpgrade', () => { |
| 35 | + const url = 'https://mega.nz/aff=x'; |
| 36 | + |
| 37 | + it('is false without a referral URL, without quota data, or with an unknown total', () => { |
| 38 | + expect(shouldOfferMegaUpgrade({ used: 100, total: 100, available: 0 }, null)).toBe(false); |
| 39 | + expect(shouldOfferMegaUpgrade(null, url)).toBe(false); |
| 40 | + expect(shouldOfferMegaUpgrade({ used: 100, total: null, available: null }, url)).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('is true at or above the offer threshold and false below it', () => { |
| 44 | + const total = 1000; |
| 45 | + const atThreshold = Math.ceil(total * MEGA_UPGRADE_OFFER_THRESHOLD); |
| 46 | + expect( |
| 47 | + shouldOfferMegaUpgrade({ used: atThreshold, total, available: total - atThreshold }, url) |
| 48 | + ).toBe(true); |
| 49 | + expect(shouldOfferMegaUpgrade({ used: total, total, available: 0 }, url)).toBe(true); |
| 50 | + expect(shouldOfferMegaUpgrade({ used: 500, total, available: 500 }, url)).toBe(false); |
| 51 | + }); |
| 52 | +}); |
0 commit comments