fingerprintjs
69 строк · 2.7 Кб
1import { getBrowserMajorVersion, isChromium, isGecko, isMobile, isSafari, isWebKit } from '../../tests/utils'2import getAudioFingerprint, { getMiddle, SpecialFingerprint, stabilize } from './audio'3
4describe('Sources', () => {5describe('audio', () => {6it('returns expected value', async () => {7const result = (await getAudioFingerprint())()8
9if (doesBrowserSuspendAudioContext()) {10expect(result).toBe(SpecialFingerprint.KnownForSuspending)11} else if (isGecko()) {12expect(result).toBeGreaterThan(0.000169)13expect(result).toBeLessThan(0.00017)14} else if (isWebKit()) {15if ('OfflineAudioContext' in window) {16expect(result).toBeGreaterThan(0.000058)17expect(result).toBeLessThan(0.000066)18} else {19expect(result).toBeGreaterThan(0.000113)20expect(result).toBeLessThan(0.000114)21}22} else if (isChromium()) {23expect(result).toBeGreaterThan(0.00006)24expect(result).toBeLessThan(0.00009)25} else {26throw new Error('Unexpected browser')27}28})29
30it('returns a stable value', async () => {31const finishFirst = await getAudioFingerprint()32const first = finishFirst()33const finishSecond = await getAudioFingerprint()34const second = finishSecond()35expect(first).toBe(second)36expect(finishFirst()).toBe(first)37})38})39
40describe('getMiddle helper', () => {41it('calculates properly and ignores zeros', () => {42const signal = [430, -5.92384345, -3.39578492, -4.39564854, 0, -3.89384529, -8.92384234, -5.01429423, -4.12045834, -3.93298453, 0,44]45expect(getMiddle(signal)).toEqual(-6.15981363)46})47})48
49describe('stabilize helper', () => {50it('calculates properly', () => {51expect(stabilize(0.000123456789, 2)).toBe(0.00012)52expect(stabilize(0.000000012345, 2)).toBe(0.000000012)53expect(stabilize(-0.000123456789, 2)).toBe(-0.00012)54expect(stabilize(0.000123456789, 7)).toBe(0.0001234568)55expect(stabilize(0.000123456789, 2.2)).toBe(0.000125)56expect(stabilize(0.000123456789, 2.5)).toBe(0.000124)57expect(stabilize(0.000123456789, 6.2)).toBe(0.000123457)58expect(stabilize(0.000123456789, 15.5)).toBe(0.000123456789)59expect(stabilize(0.000123456789, 5.2)).toBe(0.000123455)60expect(stabilize(0.000123456789, 5.5)).toBe(0.000123456)61})62})63})64
65function doesBrowserSuspendAudioContext() {66// WebKit has stopped telling its real version in the user-agent string since version 605.1.15,67// therefore the browser version has to be checked instead of the engine version.68return isSafari() && isMobile() && (getBrowserMajorVersion() ?? 0) < 1269}
70